location-filesystem.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { pathToFileURL } 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 { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
  10. import { tmpdir } from "./fixture/tmpdir"
  11. import { location } from "./fixture/location"
  12. import { it } from "./lib/effect"
  13. function provide(directory: string) {
  14. return Effect.provide(
  15. LocationFileSystem.locationLayer.pipe(
  16. Layer.provide(
  17. Layer.mergeAll(
  18. AppFileSystem.defaultLayer,
  19. Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))),
  20. ),
  21. ),
  22. ),
  23. )
  24. }
  25. function withTmp<A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) {
  26. return Effect.acquireRelease(
  27. Effect.promise(() => tmpdir()),
  28. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  29. ).pipe(Effect.flatMap((tmp) => f(tmp.path).pipe(provide(tmp.path))))
  30. }
  31. describe("LocationFileSystem", () => {
  32. it.live("reads text and binary files", () =>
  33. withTmp((directory) =>
  34. Effect.gen(function* () {
  35. yield* Effect.promise(() => fs.writeFile(path.join(directory, "hello.txt"), "hello"))
  36. yield* Effect.promise(() => fs.writeFile(path.join(directory, "data.bin"), Buffer.from([0, 1, 2])))
  37. const service = yield* LocationFileSystem.Service
  38. expect(yield* service.read({ path: RelativePath.make("hello.txt") })).toEqual({
  39. type: "text",
  40. content: "hello",
  41. mime: "text/plain",
  42. })
  43. expect(yield* service.read({ path: RelativePath.make("data.bin") })).toEqual({
  44. type: "binary",
  45. content: "AAEC",
  46. encoding: "base64",
  47. mime: "application/octet-stream",
  48. })
  49. }),
  50. ),
  51. )
  52. it.live("lists direct children with relative paths and resolved URIs", () =>
  53. withTmp((directory) =>
  54. Effect.gen(function* () {
  55. yield* Effect.promise(() => fs.mkdir(path.join(directory, "src")))
  56. yield* Effect.promise(() => fs.writeFile(path.join(directory, "README.md"), "# Test"))
  57. const service = yield* LocationFileSystem.Service
  58. expect(yield* service.list()).toEqual([
  59. {
  60. path: RelativePath.make("src"),
  61. uri: pathToFileURL(path.join(directory, "src")).href,
  62. type: "directory",
  63. mime: "application/x-directory",
  64. },
  65. {
  66. path: RelativePath.make("README.md"),
  67. uri: pathToFileURL(path.join(directory, "README.md")).href,
  68. type: "file",
  69. mime: "text/markdown",
  70. },
  71. ])
  72. }),
  73. ),
  74. )
  75. it.live("rejects paths outside the location", () =>
  76. withTmp((directory) =>
  77. Effect.gen(function* () {
  78. const service = yield* LocationFileSystem.Service
  79. expect(
  80. Exit.isFailure(yield* service.read({ path: RelativePath.make("../outside.txt") }).pipe(Effect.exit)),
  81. ).toBe(true)
  82. }),
  83. ),
  84. )
  85. })