location-filesystem.test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { FileSystem } from "@opencode-ai/core/filesystem"
  7. import { FSUtil } from "@opencode-ai/core/fs-util"
  8. import { Location } from "@opencode-ai/core/location"
  9. import { Ripgrep } from "@opencode-ai/core/ripgrep"
  10. import { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
  11. import { location } from "./fixture/location"
  12. import { tmpdir } from "./fixture/tmpdir"
  13. import { it } from "./lib/effect"
  14. const provide = (directory: string) =>
  15. Effect.provide(
  16. FileSystem.layer.pipe(
  17. Layer.provide(
  18. Layer.mergeAll(
  19. FSUtil.defaultLayer,
  20. Ripgrep.defaultLayer,
  21. Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))),
  22. ),
  23. ),
  24. ),
  25. )
  26. const withTmp = <A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) =>
  27. Effect.acquireRelease(
  28. Effect.promise(() => tmpdir()),
  29. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  30. ).pipe(Effect.flatMap((tmp) => f(tmp.path)))
  31. describe("FileSystem", () => {
  32. it.live("reads text and binary files", () =>
  33. withTmp((directory) =>
  34. Effect.gen(function* () {
  35. yield* Effect.promise(() => fs.writeFile(path.join(directory, "text.txt"), "hello"))
  36. yield* Effect.promise(() => fs.writeFile(path.join(directory, "data.bin"), Buffer.from([0, 1, 2])))
  37. const service = yield* FileSystem.Service
  38. const text = yield* service.read({ path: RelativePath.make("text.txt") })
  39. const binary = yield* service.read({ path: RelativePath.make("data.bin") })
  40. expect(text).toMatchObject({ name: "text.txt", content: "hello", encoding: "utf8", mime: "text/plain" })
  41. expect(fileURLToPath(text.uri)).toBe(path.join(directory, "text.txt"))
  42. expect(binary).toMatchObject({ name: "data.bin", content: "AAEC", encoding: "base64" })
  43. }).pipe(provide(directory)),
  44. ),
  45. )
  46. it.live("lists direct children", () =>
  47. withTmp((directory) =>
  48. Effect.gen(function* () {
  49. yield* Effect.promise(() => fs.mkdir(path.join(directory, "src")))
  50. yield* Effect.promise(() => fs.writeFile(path.join(directory, "README.md"), "# Test"))
  51. const entries = yield* (yield* FileSystem.Service).list()
  52. expect(entries.map((entry) => ({ path: entry.path, type: entry.type }))).toEqual([
  53. { path: RelativePath.make("src" + path.sep), type: "directory" },
  54. { path: RelativePath.make("README.md"), type: "file" },
  55. ])
  56. }).pipe(provide(directory)),
  57. ),
  58. )
  59. it.live("rejects lexical escapes", () =>
  60. withTmp((directory) =>
  61. Effect.gen(function* () {
  62. const result = yield* (yield* FileSystem.Service)
  63. .read({ path: RelativePath.make("../outside.txt") })
  64. .pipe(Effect.exit)
  65. expect(Exit.isFailure(result)).toBe(true)
  66. }).pipe(provide(directory)),
  67. ),
  68. )
  69. })