repository-cache.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { describe, expect } from "bun:test"
  2. import fs from "fs/promises"
  3. import path from "path"
  4. import { pathToFileURL } from "url"
  5. import { Effect, Layer } from "effect"
  6. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  7. import { LayerNode } from "@opencode-ai/core/effect/layer-node"
  8. import { Global } from "@opencode-ai/core/global"
  9. import { Repository } from "@opencode-ai/core/repository"
  10. import { RepositoryCache } from "@opencode-ai/core/repository-cache"
  11. import { git, gitRemote } from "./fixture/git"
  12. import { tmpdir } from "./fixture/tmpdir"
  13. import { testEffect } from "./lib/effect"
  14. const it = testEffect(Layer.empty)
  15. describe("RepositoryCache", () => {
  16. it.live("replaces a stale cache directory before cloning", () =>
  17. withRemote((fixture) =>
  18. Effect.gen(function* () {
  19. const localPath = Repository.cachePath(path.join(fixture.root, "repos"), fixture.reference)
  20. yield* Effect.promise(async () => {
  21. await fs.mkdir(localPath, { recursive: true })
  22. await fs.writeFile(path.join(localPath, "stale.txt"), "stale")
  23. })
  24. const result = yield* (yield* RepositoryCache.Service).ensure({ reference: fixture.reference })
  25. expect(result.status).toBe("cloned")
  26. expect(yield* exists(path.join(localPath, "stale.txt"))).toBe(false)
  27. expect(yield* read(path.join(localPath, "README.md"))).toBe("one\n")
  28. }).pipe(Effect.provide(cacheLayer(fixture.root))),
  29. ),
  30. )
  31. it.live("serializes concurrent materialization for the same checkout", () =>
  32. withRemote((fixture) =>
  33. Effect.gen(function* () {
  34. const cache = yield* RepositoryCache.Service
  35. const results = yield* Effect.all(
  36. [cache.ensure({ reference: fixture.reference }), cache.ensure({ reference: fixture.reference })],
  37. { concurrency: "unbounded" },
  38. )
  39. expect(results.map((result) => result.status).toSorted()).toEqual(["cached", "cloned"])
  40. expect(results[0].localPath).toBe(results[1].localPath)
  41. }).pipe(Effect.provide(cacheLayer(fixture.root))),
  42. ),
  43. )
  44. it.live("replaces an existing checkout whose origin does not match", () =>
  45. withRemote((fixture) =>
  46. Effect.gen(function* () {
  47. const cache = yield* RepositoryCache.Service
  48. const initial = yield* cache.ensure({ reference: fixture.reference })
  49. yield* Effect.promise(async () => {
  50. await git(initial.localPath, "config", "remote.origin.url", "https://github.com/other/repo.git")
  51. await fs.writeFile(path.join(initial.localPath, "stale.txt"), "stale")
  52. })
  53. const replaced = yield* cache.ensure({ reference: fixture.reference })
  54. expect(replaced.status).toBe("cloned")
  55. expect(yield* exists(path.join(replaced.localPath, "stale.txt"))).toBe(false)
  56. }).pipe(Effect.provide(cacheLayer(fixture.root))),
  57. ),
  58. )
  59. it.live("returns typed validation and clone failures", () =>
  60. withRemote((fixture) =>
  61. Effect.gen(function* () {
  62. const cache = yield* RepositoryCache.Service
  63. const invalidRepository = yield* Effect.flip(RepositoryCache.parseRemote("not-a-repo"))
  64. expect(invalidRepository).toBeInstanceOf(RepositoryCache.InvalidRepositoryError)
  65. const invalidBranch = yield* Effect.flip(cache.ensure({ reference: fixture.reference, branch: "../unsafe" }))
  66. expect(invalidBranch).toBeInstanceOf(RepositoryCache.InvalidBranchError)
  67. const cloneFailure = yield* Effect.flip(
  68. cache.ensure({
  69. reference: { ...fixture.reference, remote: pathToFileURL(path.join(fixture.root, "missing.git")).href },
  70. }),
  71. )
  72. expect(cloneFailure).toBeInstanceOf(RepositoryCache.CloneFailedError)
  73. }).pipe(Effect.provide(cacheLayer(fixture.root))),
  74. ),
  75. )
  76. })
  77. function cacheLayer(root: string) {
  78. return AppNodeBuilder.build(RepositoryCache.node, [
  79. [Global.node, Global.layerWith({ state: path.join(root, "state"), repos: path.join(root, "repos") })],
  80. ])
  81. }
  82. function withRemote<A, E, R>(body: (fixture: Awaited<ReturnType<typeof gitRemote>>) => Effect.Effect<A, E, R>) {
  83. return Effect.acquireUseRelease(
  84. Effect.promise(async () => {
  85. const root = await tmpdir()
  86. return { root, fixture: await gitRemote(root.path) }
  87. }),
  88. (input) => body(input.fixture),
  89. (input) => Effect.promise(() => input.root[Symbol.asyncDispose]()),
  90. )
  91. }
  92. function read(file: string) {
  93. return Effect.promise(() => fs.readFile(file, "utf8")).pipe(Effect.map((content) => content.replace(/\r\n/g, "\n")))
  94. }
  95. function exists(file: string) {
  96. return Effect.promise(() =>
  97. fs.stat(file).then(
  98. () => true,
  99. () => false,
  100. ),
  101. )
  102. }