81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
import fs from "fs";
|
|
import os from "os";
|
|
import path from "path";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
function makeTempDir(prefix: string): string {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
|
}
|
|
|
|
function writeFile(filePath: string, content: string): void {
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
fs.writeFileSync(filePath, content);
|
|
}
|
|
|
|
async function importLoadCustomInstructions(mockedHome?: string) {
|
|
vi.resetModules();
|
|
vi.doUnmock("os");
|
|
|
|
if (mockedHome) {
|
|
vi.doMock("os", async () => {
|
|
const actual = await vi.importActual<typeof import("os")>("os");
|
|
return {
|
|
...actual,
|
|
homedir: () => mockedHome,
|
|
};
|
|
});
|
|
}
|
|
|
|
const mod = await import("./instructions");
|
|
return mod.loadCustomInstructions;
|
|
}
|
|
|
|
describe("loadCustomInstructions", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.resetModules();
|
|
vi.doUnmock("os");
|
|
});
|
|
|
|
it("returns null when no instruction files exist", async () => {
|
|
const home = makeTempDir("grok-home-");
|
|
const cwd = makeTempDir("grok-cwd-");
|
|
const loadCustomInstructions = await importLoadCustomInstructions(home);
|
|
|
|
expect(loadCustomInstructions(cwd)).toBeNull();
|
|
});
|
|
|
|
it("loads global plus repo-chain AGENTS files in order", async () => {
|
|
const home = makeTempDir("grok-home-");
|
|
const repoRoot = makeTempDir("grok-repo-");
|
|
const cwd = path.join(repoRoot, "pkg", "feature");
|
|
fs.mkdirSync(path.join(repoRoot, ".git"));
|
|
fs.mkdirSync(cwd, { recursive: true });
|
|
|
|
writeFile(path.join(home, ".grok", "AGENTS.md"), "global instructions");
|
|
writeFile(path.join(repoRoot, "AGENTS.md"), "root instructions");
|
|
writeFile(path.join(repoRoot, "pkg", "AGENTS.md"), "pkg instructions");
|
|
writeFile(path.join(repoRoot, "pkg", "feature", "AGENTS.md"), "feature instructions");
|
|
const loadCustomInstructions = await importLoadCustomInstructions(home);
|
|
|
|
expect(loadCustomInstructions(cwd)).toBe(
|
|
["global instructions", "root instructions", "pkg instructions", "feature instructions"].join("\n\n"),
|
|
);
|
|
});
|
|
|
|
it("prefers AGENTS.override.md over AGENTS.md in the same directory", async () => {
|
|
const home = makeTempDir("grok-home-");
|
|
const repoRoot = makeTempDir("grok-repo-");
|
|
const cwd = path.join(repoRoot, "nested");
|
|
fs.mkdirSync(path.join(repoRoot, ".git"));
|
|
fs.mkdirSync(cwd, { recursive: true });
|
|
|
|
writeFile(path.join(repoRoot, "AGENTS.md"), "root instructions");
|
|
writeFile(path.join(repoRoot, "nested", "AGENTS.md"), "nested base instructions");
|
|
writeFile(path.join(repoRoot, "nested", "AGENTS.override.md"), "nested override instructions");
|
|
const loadCustomInstructions = await importLoadCustomInstructions(home);
|
|
|
|
expect(loadCustomInstructions(cwd)).toBe(["root instructions", "nested override instructions"].join("\n\n"));
|
|
});
|
|
});
|