repository-cache.test.ts 4.8 KB

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