location-filesystem.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { fileURLToPath } from "url"
  4. import { describe, expect } from "bun:test"
  5. import { Effect, Exit, Layer } from "effect"
  6. import { AppFileSystem } from "@opencode-ai/core/filesystem"
  7. import { Location } from "@opencode-ai/core/location"
  8. import { LocationFileSystem } from "@opencode-ai/core/location-filesystem"
  9. import { ProjectReference } from "@opencode-ai/core/project-reference"
  10. import { Repository } from "@opencode-ai/core/repository"
  11. import { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
  12. import { tmpdir } from "./fixture/tmpdir"
  13. import { location } from "./fixture/location"
  14. import { it } from "./lib/effect"
  15. const inertReferences = ProjectReference.Service.of({
  16. list: () => Effect.succeed([]),
  17. get: () => Effect.succeed(undefined),
  18. resolveMention: () => Effect.succeed(undefined),
  19. ensurePath: () => Effect.void,
  20. containsManagedPath: () => Effect.succeed(false),
  21. })
  22. function provide(directory: string, references = inertReferences) {
  23. return Effect.provide(
  24. LocationFileSystem.layer.pipe(
  25. Layer.provide(
  26. Layer.mergeAll(
  27. AppFileSystem.defaultLayer,
  28. Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))),
  29. Layer.succeed(ProjectReference.Service, references),
  30. ),
  31. ),
  32. ),
  33. )
  34. }
  35. function withTmp<A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) {
  36. return Effect.acquireRelease(
  37. Effect.promise(() => tmpdir()),
  38. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  39. ).pipe(Effect.flatMap((tmp) => f(tmp.path)))
  40. }
  41. describe("LocationFileSystem", () => {
  42. it.live("reads text and binary files", () =>
  43. withTmp((directory) =>
  44. Effect.gen(function* () {
  45. yield* Effect.promise(() => fs.writeFile(path.join(directory, "hello.txt"), "hello"))
  46. yield* Effect.promise(() => fs.writeFile(path.join(directory, "data.bin"), Buffer.from([0, 1, 2])))
  47. const service = yield* LocationFileSystem.Service
  48. expect(yield* service.read({ path: RelativePath.make("hello.txt") })).toEqual({
  49. type: "text",
  50. content: "hello",
  51. mime: "text/plain",
  52. })
  53. expect(yield* service.read({ path: RelativePath.make("data.bin") })).toEqual({
  54. type: "binary",
  55. content: "AAEC",
  56. encoding: "base64",
  57. mime: "application/octet-stream",
  58. })
  59. }).pipe(provide(directory)),
  60. ),
  61. )
  62. it.live("lists direct children with relative paths and resolved URIs", () =>
  63. withTmp((directory) =>
  64. Effect.gen(function* () {
  65. yield* Effect.promise(() => fs.mkdir(path.join(directory, "src")))
  66. yield* Effect.promise(() => fs.writeFile(path.join(directory, "README.md"), "# Test"))
  67. const service = yield* LocationFileSystem.Service
  68. const entries = yield* service.list()
  69. expect(entries.map(({ uri: _uri, ...entry }) => entry)).toEqual([
  70. {
  71. path: RelativePath.make("src"),
  72. type: "directory",
  73. mime: "application/x-directory",
  74. },
  75. {
  76. path: RelativePath.make("README.md"),
  77. type: "file",
  78. mime: "text/markdown",
  79. },
  80. ])
  81. expect(
  82. yield* Effect.promise(() => Promise.all(entries.map((entry) => fs.realpath(fileURLToPath(entry.uri))))),
  83. ).toEqual(
  84. yield* Effect.promise(() =>
  85. Promise.all([fs.realpath(path.join(directory, "src")), fs.realpath(path.join(directory, "README.md"))]),
  86. ),
  87. )
  88. }).pipe(provide(directory)),
  89. ),
  90. )
  91. it.live("rejects paths outside the location", () =>
  92. withTmp((directory) =>
  93. Effect.gen(function* () {
  94. const service = yield* LocationFileSystem.Service
  95. expect(
  96. Exit.isFailure(yield* service.read({ path: RelativePath.make("../outside.txt") }).pipe(Effect.exit)),
  97. ).toBe(true)
  98. }).pipe(provide(directory)),
  99. ),
  100. )
  101. it.live("reads and lists paths relative to a local project reference", () =>
  102. withTmp((directory) => {
  103. const docs = path.join(directory, "docs")
  104. return Effect.gen(function* () {
  105. yield* Effect.promise(async () => {
  106. await fs.mkdir(docs)
  107. await fs.writeFile(path.join(docs, "README.md"), "docs")
  108. })
  109. const service = yield* LocationFileSystem.Service
  110. expect(yield* service.read({ reference: "docs", path: RelativePath.make("README.md") })).toMatchObject({
  111. type: "text",
  112. content: "docs",
  113. })
  114. expect(yield* service.list({ reference: "docs" })).toMatchObject([{ path: "README.md", type: "file" }])
  115. }).pipe(provide(directory, references({ docs: { name: "docs", kind: "local", path: docs } })))
  116. }),
  117. )
  118. it.live("materializes Git references before filesystem access", () =>
  119. withTmp((directory) => {
  120. const docs = path.join(directory, "docs")
  121. const ensured: string[] = []
  122. return Effect.gen(function* () {
  123. yield* Effect.promise(async () => {
  124. await fs.mkdir(docs)
  125. await fs.writeFile(path.join(docs, "README.md"), "docs")
  126. })
  127. expect(
  128. yield* (yield* LocationFileSystem.Service).read({ reference: "sdk", path: RelativePath.make("README.md") }),
  129. ).toMatchObject({ content: "docs" })
  130. expect(ensured).toEqual([docs])
  131. }).pipe(
  132. provide(
  133. directory,
  134. references(
  135. {
  136. sdk: {
  137. name: "sdk",
  138. kind: "git",
  139. repository: "owner/repo",
  140. reference: Repository.parseRemote("owner/repo"),
  141. path: docs,
  142. },
  143. },
  144. (target) => Effect.sync(() => ensured.push(target ?? "")),
  145. ),
  146. ),
  147. )
  148. }),
  149. )
  150. it.live("rejects unknown, invalid, and escaping project reference paths", () =>
  151. withTmp((directory) => {
  152. const docs = path.join(directory, "docs")
  153. return Effect.gen(function* () {
  154. yield* Effect.promise(() => fs.mkdir(docs))
  155. const service = yield* LocationFileSystem.Service
  156. expect(Exit.isFailure(yield* service.list({ reference: "unknown" }).pipe(Effect.exit))).toBe(true)
  157. expect(Exit.isFailure(yield* service.list({ reference: "invalid" }).pipe(Effect.exit))).toBe(true)
  158. expect(
  159. Exit.isFailure(
  160. yield* service.read({ reference: "docs", path: RelativePath.make("../outside") }).pipe(Effect.exit),
  161. ),
  162. ).toBe(true)
  163. }).pipe(
  164. provide(
  165. directory,
  166. references({
  167. docs: { name: "docs", kind: "local", path: docs },
  168. invalid: { name: "invalid", kind: "invalid", message: "invalid reference" },
  169. }),
  170. ),
  171. )
  172. }),
  173. )
  174. it.live("rejects aliases when project references are disabled", () =>
  175. withTmp((directory) =>
  176. Effect.gen(function* () {
  177. expect(
  178. Exit.isFailure(yield* (yield* LocationFileSystem.Service).list({ reference: "docs" }).pipe(Effect.exit)),
  179. ).toBe(true)
  180. }).pipe(provide(directory)),
  181. ),
  182. )
  183. it.live("rejects symlink escapes from project references", () =>
  184. withTmp((directory) => {
  185. const docs = path.join(directory, "docs")
  186. const outside = path.join(directory, "outside.txt")
  187. return Effect.gen(function* () {
  188. if (process.platform === "win32") return
  189. yield* Effect.promise(async () => {
  190. await fs.mkdir(docs)
  191. await fs.writeFile(outside, "outside")
  192. await fs.symlink(outside, path.join(docs, "link.txt"))
  193. })
  194. expect(
  195. Exit.isFailure(
  196. yield* (yield* LocationFileSystem.Service)
  197. .read({ reference: "docs", path: RelativePath.make("link.txt") })
  198. .pipe(Effect.exit),
  199. ),
  200. ).toBe(true)
  201. }).pipe(provide(directory, references({ docs: { name: "docs", kind: "local", path: docs } })))
  202. }),
  203. )
  204. })
  205. function references(
  206. entries: Record<string, ProjectReference.Resolved>,
  207. ensurePath: ProjectReference.Interface["ensurePath"] = () => Effect.void,
  208. ) {
  209. return ProjectReference.Service.of({
  210. list: () => Effect.succeed(Object.values(entries)),
  211. get: (name) => Effect.succeed(entries[name]),
  212. resolveMention: () => Effect.succeed(undefined),
  213. ensurePath,
  214. containsManagedPath: () => Effect.succeed(false),
  215. })
  216. }