repository-cache.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 { branch, 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("keeps branch checkouts isolated from branchless refreshes", () =>
  60. withRemote((fixture) =>
  61. Effect.gen(function* () {
  62. yield* Effect.promise(() => branch(fixture.source, "feature", "two\n"))
  63. const cache = yield* RepositoryCache.Service
  64. const featured = yield* cache.ensure({ reference: fixture.reference, branch: "feature" })
  65. expect(featured.branch).toBe("feature")
  66. expect(featured.localPath.endsWith("repo@feature")).toBe(true)
  67. expect(yield* read(path.join(featured.localPath, "README.md"))).toBe("two\n")
  68. const refreshed = yield* cache.ensure({ reference: fixture.reference, refresh: true })
  69. expect(refreshed.localPath).not.toBe(featured.localPath)
  70. expect(yield* read(path.join(refreshed.localPath, "README.md"))).toBe("one\n")
  71. const cached = yield* cache.ensure({ reference: fixture.reference, branch: "feature" })
  72. expect(cached.status).toBe("cached")
  73. expect(yield* read(path.join(cached.localPath, "README.md"))).toBe("two\n")
  74. }).pipe(Effect.provide(cacheLayer(fixture.root))),
  75. ),
  76. )
  77. it.live("does not mistake an enclosing repository for the cache checkout", () =>
  78. withRemote((fixture) =>
  79. Effect.gen(function* () {
  80. yield* Effect.promise(() => git(fixture.root, "clone", fixture.remote, path.join(fixture.root, "repos")))
  81. const result = yield* (yield* RepositoryCache.Service).ensure({ reference: fixture.reference })
  82. expect(result.status).toBe("cloned")
  83. expect(yield* read(path.join(result.localPath, "README.md"))).toBe("one\n")
  84. }).pipe(Effect.provide(cacheLayer(fixture.root))),
  85. ),
  86. )
  87. it.live("returns typed validation and clone failures", () =>
  88. withRemote((fixture) =>
  89. Effect.gen(function* () {
  90. const cache = yield* RepositoryCache.Service
  91. const invalidRepository = yield* Effect.flip(RepositoryCache.parseRemote("not-a-repo"))
  92. expect(invalidRepository).toBeInstanceOf(RepositoryCache.InvalidRepositoryError)
  93. const invalidBranch = yield* Effect.flip(cache.ensure({ reference: fixture.reference, branch: "../unsafe" }))
  94. expect(invalidBranch).toBeInstanceOf(RepositoryCache.InvalidBranchError)
  95. const cloneFailure = yield* Effect.flip(
  96. cache.ensure({
  97. reference: { ...fixture.reference, remote: pathToFileURL(path.join(fixture.root, "missing.git")).href },
  98. }),
  99. )
  100. expect(cloneFailure).toBeInstanceOf(RepositoryCache.CloneFailedError)
  101. }).pipe(Effect.provide(cacheLayer(fixture.root))),
  102. ),
  103. )
  104. })
  105. function cacheLayer(root: string) {
  106. return AppNodeBuilder.build(RepositoryCache.node, [
  107. [Global.node, Global.layerWith({ state: path.join(root, "state"), repos: path.join(root, "repos") })],
  108. ])
  109. }
  110. function withRemote<A, E, R>(body: (fixture: Awaited<ReturnType<typeof gitRemote>>) => Effect.Effect<A, E, R>) {
  111. return Effect.acquireUseRelease(
  112. Effect.promise(async () => {
  113. const root = await tmpdir()
  114. return { root, fixture: await gitRemote(root.path) }
  115. }),
  116. (input) => body(input.fixture),
  117. (input) => Effect.promise(() => input.root[Symbol.asyncDispose]()),
  118. )
  119. }
  120. function read(file: string) {
  121. return Effect.promise(() => fs.readFile(file, "utf8")).pipe(Effect.map((content) => content.replace(/\r\n/g, "\n")))
  122. }
  123. function exists(file: string) {
  124. return Effect.promise(() =>
  125. fs.stat(file).then(
  126. () => true,
  127. () => false,
  128. ),
  129. )
  130. }