instruction.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import { InstructionPrompt } from "../../src/session/instruction"
  4. import { Instance } from "../../src/project/instance"
  5. import { tmpdir } from "../fixture/fixture"
  6. describe("InstructionPrompt.resolve", () => {
  7. test("returns empty when AGENTS.md is at project root (already in systemPaths)", async () => {
  8. await using tmp = await tmpdir({
  9. init: async (dir) => {
  10. await Bun.write(path.join(dir, "AGENTS.md"), "# Root Instructions")
  11. await Bun.write(path.join(dir, "src", "file.ts"), "const x = 1")
  12. },
  13. })
  14. await Instance.provide({
  15. directory: tmp.path,
  16. fn: async () => {
  17. const system = await InstructionPrompt.systemPaths()
  18. expect(system.has(path.join(tmp.path, "AGENTS.md"))).toBe(true)
  19. const results = await InstructionPrompt.resolve([], path.join(tmp.path, "src", "file.ts"), "test-message-1")
  20. expect(results).toEqual([])
  21. },
  22. })
  23. })
  24. test("returns AGENTS.md from subdirectory (not in systemPaths)", async () => {
  25. await using tmp = await tmpdir({
  26. init: async (dir) => {
  27. await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Subdir Instructions")
  28. await Bun.write(path.join(dir, "subdir", "nested", "file.ts"), "const x = 1")
  29. },
  30. })
  31. await Instance.provide({
  32. directory: tmp.path,
  33. fn: async () => {
  34. const system = await InstructionPrompt.systemPaths()
  35. expect(system.has(path.join(tmp.path, "subdir", "AGENTS.md"))).toBe(false)
  36. const results = await InstructionPrompt.resolve(
  37. [],
  38. path.join(tmp.path, "subdir", "nested", "file.ts"),
  39. "test-message-2",
  40. )
  41. expect(results.length).toBe(1)
  42. expect(results[0].filepath).toBe(path.join(tmp.path, "subdir", "AGENTS.md"))
  43. },
  44. })
  45. })
  46. test("doesn't reload AGENTS.md when reading it directly", async () => {
  47. await using tmp = await tmpdir({
  48. init: async (dir) => {
  49. await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Subdir Instructions")
  50. await Bun.write(path.join(dir, "subdir", "nested", "file.ts"), "const x = 1")
  51. },
  52. })
  53. await Instance.provide({
  54. directory: tmp.path,
  55. fn: async () => {
  56. const filepath = path.join(tmp.path, "subdir", "AGENTS.md")
  57. const system = await InstructionPrompt.systemPaths()
  58. expect(system.has(filepath)).toBe(false)
  59. const results = await InstructionPrompt.resolve([], filepath, "test-message-2")
  60. expect(results).toEqual([])
  61. },
  62. })
  63. })
  64. })