instruction.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { afterEach, beforeEach, 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 { Global } from "../../src/global"
  6. import { tmpdir } from "../fixture/fixture"
  7. describe("InstructionPrompt.resolve", () => {
  8. test("returns empty when AGENTS.md is at project root (already in systemPaths)", async () => {
  9. await using tmp = await tmpdir({
  10. init: async (dir) => {
  11. await Bun.write(path.join(dir, "AGENTS.md"), "# Root Instructions")
  12. await Bun.write(path.join(dir, "src", "file.ts"), "const x = 1")
  13. },
  14. })
  15. await Instance.provide({
  16. directory: tmp.path,
  17. fn: async () => {
  18. const system = await InstructionPrompt.systemPaths()
  19. expect(system.has(path.join(tmp.path, "AGENTS.md"))).toBe(true)
  20. const results = await InstructionPrompt.resolve([], path.join(tmp.path, "src", "file.ts"), "test-message-1")
  21. expect(results).toEqual([])
  22. },
  23. })
  24. })
  25. test("returns AGENTS.md from subdirectory (not in systemPaths)", async () => {
  26. await using tmp = await tmpdir({
  27. init: async (dir) => {
  28. await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Subdir Instructions")
  29. await Bun.write(path.join(dir, "subdir", "nested", "file.ts"), "const x = 1")
  30. },
  31. })
  32. await Instance.provide({
  33. directory: tmp.path,
  34. fn: async () => {
  35. const system = await InstructionPrompt.systemPaths()
  36. expect(system.has(path.join(tmp.path, "subdir", "AGENTS.md"))).toBe(false)
  37. const results = await InstructionPrompt.resolve(
  38. [],
  39. path.join(tmp.path, "subdir", "nested", "file.ts"),
  40. "test-message-2",
  41. )
  42. expect(results.length).toBe(1)
  43. expect(results[0].filepath).toBe(path.join(tmp.path, "subdir", "AGENTS.md"))
  44. },
  45. })
  46. })
  47. test("doesn't reload AGENTS.md when reading it directly", async () => {
  48. await using tmp = await tmpdir({
  49. init: async (dir) => {
  50. await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Subdir Instructions")
  51. await Bun.write(path.join(dir, "subdir", "nested", "file.ts"), "const x = 1")
  52. },
  53. })
  54. await Instance.provide({
  55. directory: tmp.path,
  56. fn: async () => {
  57. const filepath = path.join(tmp.path, "subdir", "AGENTS.md")
  58. const system = await InstructionPrompt.systemPaths()
  59. expect(system.has(filepath)).toBe(false)
  60. const results = await InstructionPrompt.resolve([], filepath, "test-message-2")
  61. expect(results).toEqual([])
  62. },
  63. })
  64. })
  65. })
  66. describe("InstructionPrompt.systemPaths OPENCODE_CONFIG_DIR", () => {
  67. let originalConfigDir: string | undefined
  68. beforeEach(() => {
  69. originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
  70. })
  71. afterEach(() => {
  72. if (originalConfigDir === undefined) {
  73. delete process.env["OPENCODE_CONFIG_DIR"]
  74. } else {
  75. process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
  76. }
  77. })
  78. test("prefers OPENCODE_CONFIG_DIR AGENTS.md over global when both exist", async () => {
  79. await using profileTmp = await tmpdir({
  80. init: async (dir) => {
  81. await Bun.write(path.join(dir, "AGENTS.md"), "# Profile Instructions")
  82. },
  83. })
  84. await using globalTmp = await tmpdir({
  85. init: async (dir) => {
  86. await Bun.write(path.join(dir, "AGENTS.md"), "# Global Instructions")
  87. },
  88. })
  89. await using projectTmp = await tmpdir()
  90. process.env["OPENCODE_CONFIG_DIR"] = profileTmp.path
  91. const originalGlobalConfig = Global.Path.config
  92. ;(Global.Path as { config: string }).config = globalTmp.path
  93. try {
  94. await Instance.provide({
  95. directory: projectTmp.path,
  96. fn: async () => {
  97. const paths = await InstructionPrompt.systemPaths()
  98. expect(paths.has(path.join(profileTmp.path, "AGENTS.md"))).toBe(true)
  99. expect(paths.has(path.join(globalTmp.path, "AGENTS.md"))).toBe(false)
  100. },
  101. })
  102. } finally {
  103. ;(Global.Path as { config: string }).config = originalGlobalConfig
  104. }
  105. })
  106. test("falls back to global AGENTS.md when OPENCODE_CONFIG_DIR has no AGENTS.md", async () => {
  107. await using profileTmp = await tmpdir()
  108. await using globalTmp = await tmpdir({
  109. init: async (dir) => {
  110. await Bun.write(path.join(dir, "AGENTS.md"), "# Global Instructions")
  111. },
  112. })
  113. await using projectTmp = await tmpdir()
  114. process.env["OPENCODE_CONFIG_DIR"] = profileTmp.path
  115. const originalGlobalConfig = Global.Path.config
  116. ;(Global.Path as { config: string }).config = globalTmp.path
  117. try {
  118. await Instance.provide({
  119. directory: projectTmp.path,
  120. fn: async () => {
  121. const paths = await InstructionPrompt.systemPaths()
  122. expect(paths.has(path.join(profileTmp.path, "AGENTS.md"))).toBe(false)
  123. expect(paths.has(path.join(globalTmp.path, "AGENTS.md"))).toBe(true)
  124. },
  125. })
  126. } finally {
  127. ;(Global.Path as { config: string }).config = originalGlobalConfig
  128. }
  129. })
  130. test("uses global AGENTS.md when OPENCODE_CONFIG_DIR is not set", async () => {
  131. await using globalTmp = await tmpdir({
  132. init: async (dir) => {
  133. await Bun.write(path.join(dir, "AGENTS.md"), "# Global Instructions")
  134. },
  135. })
  136. await using projectTmp = await tmpdir()
  137. delete process.env["OPENCODE_CONFIG_DIR"]
  138. const originalGlobalConfig = Global.Path.config
  139. ;(Global.Path as { config: string }).config = globalTmp.path
  140. try {
  141. await Instance.provide({
  142. directory: projectTmp.path,
  143. fn: async () => {
  144. const paths = await InstructionPrompt.systemPaths()
  145. expect(paths.has(path.join(globalTmp.path, "AGENTS.md"))).toBe(true)
  146. },
  147. })
  148. } finally {
  149. ;(Global.Path as { config: string }).config = originalGlobalConfig
  150. }
  151. })
  152. })