|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import test from "node:test"; |
| 6 | +import type { ExtensionAPI, ExtensionContext, ToolResultEvent } from "@earendil-works/pi-coding-agent"; |
| 7 | +import localAgentsMdExtension, { findLocalAgentsFiles, findSubdirectoryAgentsFiles } from "../agents-md.ts"; |
| 8 | + |
| 9 | +test("does not discover project-local instructions in an untrusted project", () => { |
| 10 | + const root = mkdtempSync(join(tmpdir(), "local-agents-")); |
| 11 | + try { |
| 12 | + const nested = join(root, "project", "nested"); |
| 13 | + mkdirSync(nested, { recursive: true }); |
| 14 | + const parentFile = join(root, "project", "AGENTS.local.md"); |
| 15 | + const childFile = join(nested, "AGENTS.local.md"); |
| 16 | + writeFileSync(parentFile, "parent"); |
| 17 | + writeFileSync(childFile, "child"); |
| 18 | + |
| 19 | + assert.deepEqual(findLocalAgentsFiles(nested, true), [parentFile, childFile]); |
| 20 | + assert.deepEqual(findLocalAgentsFiles(nested, false), []); |
| 21 | + } finally { |
| 22 | + rmSync(root, { recursive: true, force: true }); |
| 23 | + } |
| 24 | +}); |
| 25 | + |
| 26 | +test("discovers AGENTS.md files between the project root and a read file", () => { |
| 27 | + const root = mkdtempSync(join(tmpdir(), "subdirectory-agents-")); |
| 28 | + try { |
| 29 | + const sourceDir = join(root, "nested", "child"); |
| 30 | + mkdirSync(sourceDir, { recursive: true }); |
| 31 | + const parentFile = join(root, "nested", "AGENTS.md"); |
| 32 | + const childFile = join(sourceDir, "AGENTS.md"); |
| 33 | + writeFileSync(parentFile, "parent"); |
| 34 | + writeFileSync(childFile, "child"); |
| 35 | + |
| 36 | + assert.deepEqual(findSubdirectoryAgentsFiles(root, join(sourceDir, "source.ts")), [parentFile, childFile]); |
| 37 | + assert.deepEqual(findSubdirectoryAgentsFiles(root, join(root, "outside.ts")), []); |
| 38 | + } finally { |
| 39 | + rmSync(root, { recursive: true, force: true }); |
| 40 | + } |
| 41 | +}); |
| 42 | + |
| 43 | +test("adds newly discovered instructions to a read result once", () => { |
| 44 | + const root = mkdtempSync(join(tmpdir(), "subdirectory-agents-")); |
| 45 | + try { |
| 46 | + const sourceDir = join(root, "nested"); |
| 47 | + mkdirSync(sourceDir); |
| 48 | + writeFileSync(join(sourceDir, "AGENTS.md"), "Use bun test."); |
| 49 | + |
| 50 | + let handler: ((event: ToolResultEvent, ctx: ExtensionContext) => unknown) | undefined; |
| 51 | + localAgentsMdExtension({ |
| 52 | + on(event: string, callback: unknown) { |
| 53 | + if (event === "tool_result") handler = callback as typeof handler; |
| 54 | + }, |
| 55 | + } as ExtensionAPI); |
| 56 | + const event = { |
| 57 | + type: "tool_result", |
| 58 | + toolCallId: "test", |
| 59 | + toolName: "read", |
| 60 | + input: { path: join(sourceDir, "source.ts") }, |
| 61 | + content: [{ type: "text", text: "source" }], |
| 62 | + details: undefined, |
| 63 | + isError: false, |
| 64 | + } as ToolResultEvent; |
| 65 | + const ctx = { cwd: root, isProjectTrusted: () => true } as ExtensionContext; |
| 66 | + |
| 67 | + assert.ok(handler); |
| 68 | + const result = handler(event, ctx) as { content: Array<{ type: "text"; text: string }> }; |
| 69 | + assert.match(result.content.at(-1)?.text ?? "", /Use bun test/); |
| 70 | + assert.equal(handler(event, ctx), undefined); |
| 71 | + } finally { |
| 72 | + rmSync(root, { recursive: true, force: true }); |
| 73 | + } |
| 74 | +}); |
0 commit comments