vcs-hg.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { $ } from "bun"
  2. import { describe, expect } from "bun:test"
  3. import fs from "fs/promises"
  4. import path from "path"
  5. import { Effect, Fiber, Layer, Stream } from "effect"
  6. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  7. import { Bus } from "@opencode-ai/core/bus"
  8. import { Location } from "@opencode-ai/core/location"
  9. import { AbsolutePath } from "@opencode-ai/core/schema"
  10. import { Vcs } from "@opencode-ai/core/vcs"
  11. import { FileSystem } from "@opencode-ai/schema/filesystem"
  12. import { VcsEvent } from "@opencode-ai/schema/vcs-event"
  13. import { location } from "./fixture/location"
  14. import { tmpdir } from "./fixture/tmpdir"
  15. import { it } from "./lib/effect"
  16. const describeHg = Bun.which("hg") ? describe : describe.skip
  17. const provide = (directory: string) =>
  18. Effect.provide(
  19. LayerNode.compile(LayerNode.group([Vcs.node, Bus.node]), [
  20. [
  21. Location.node,
  22. Layer.succeed(
  23. Location.Service,
  24. Location.Service.of(
  25. location(
  26. { directory: AbsolutePath.make(directory) },
  27. { vcs: { type: "hg", store: AbsolutePath.make(path.join(directory, ".hg")) } },
  28. ),
  29. ),
  30. ),
  31. ],
  32. ]),
  33. )
  34. const withTmp = <A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) =>
  35. Effect.acquireRelease(
  36. Effect.promise(() => tmpdir()),
  37. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  38. ).pipe(Effect.flatMap((tmp) => f(tmp.path)))
  39. const withHg = <A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) =>
  40. withTmp((directory) =>
  41. Effect.promise(() => hg(directory, "init")).pipe(Effect.andThen(f(directory).pipe(provide(directory)))),
  42. )
  43. async function hg(directory: string, ...args: string[]) {
  44. await $`hg ${args}`.cwd(directory).env({ ...process.env, HGPLAIN: "1" }).quiet()
  45. }
  46. async function commitAll(directory: string, message: string) {
  47. await hg(directory, "addremove", "-q")
  48. await hg(directory, "commit", "-q", "-m", message, "-u", "test")
  49. }
  50. describeHg("Vcs mercurial", () => {
  51. it.live("reports modified, missing, and untracked files", () =>
  52. withHg((directory) =>
  53. Effect.gen(function* () {
  54. yield* Effect.promise(async () => {
  55. await fs.writeFile(path.join(directory, "keep.txt"), "one\ntwo\n")
  56. await fs.writeFile(path.join(directory, "gone.txt"), "bye\n")
  57. await commitAll(directory, "initial")
  58. await fs.writeFile(path.join(directory, "keep.txt"), "one\nthree\n")
  59. await fs.rm(path.join(directory, "gone.txt"))
  60. await fs.writeFile(path.join(directory, "new.txt"), "hello\nworld\n")
  61. })
  62. const vcs = yield* Vcs.Service
  63. const status = yield* vcs.status()
  64. expect(status).toEqual([
  65. { file: "gone.txt", additions: 0, deletions: 1, status: "deleted" },
  66. { file: "keep.txt", additions: 1, deletions: 1, status: "modified" },
  67. { file: "new.txt", additions: 2, deletions: 0, status: "added" },
  68. ])
  69. }),
  70. ),
  71. )
  72. it.live("diffs the working copy with synthesized untracked and missing patches", () =>
  73. withHg((directory) =>
  74. Effect.gen(function* () {
  75. yield* Effect.promise(async () => {
  76. await fs.writeFile(path.join(directory, "keep.txt"), "one\ntwo\n")
  77. await fs.writeFile(path.join(directory, "gone.txt"), "bye\n")
  78. await commitAll(directory, "initial")
  79. await fs.writeFile(path.join(directory, "keep.txt"), "one\nthree\n")
  80. await fs.rm(path.join(directory, "gone.txt"))
  81. await fs.writeFile(path.join(directory, "spaced name.txt"), "hello\n")
  82. })
  83. const vcs = yield* Vcs.Service
  84. const diff = yield* vcs.diff("working")
  85. expect(diff.map((item) => ({ file: item.file, status: item.status }))).toEqual([
  86. { file: "gone.txt", status: "deleted" },
  87. { file: "keep.txt", status: "modified" },
  88. { file: "spaced name.txt", status: "added" },
  89. ])
  90. expect(diff[0].patch).toContain("-bye")
  91. expect(diff[1].patch).toContain("-two")
  92. expect(diff[1].patch).toContain("+three")
  93. expect(diff[1].additions).toBe(1)
  94. expect(diff[1].deletions).toBe(1)
  95. expect(diff[2].patch).toContain("+hello")
  96. expect(diff[2].additions).toBe(1)
  97. }),
  98. ),
  99. )
  100. it.live("caches branch info and publishes branch metadata changes", () =>
  101. withHg((directory) =>
  102. Effect.gen(function* () {
  103. yield* Effect.promise(async () => {
  104. await fs.writeFile(path.join(directory, "file.txt"), "one\n")
  105. await commitAll(directory, "initial")
  106. })
  107. const vcs = yield* Vcs.Service
  108. const bus = yield* Bus.Service
  109. expect(yield* vcs.info()).toEqual({ branch: { current: "default", default: "default" } })
  110. const updated = yield* bus.subscribe(VcsEvent.BranchUpdated).pipe(
  111. Stream.take(1),
  112. Stream.runHead,
  113. Effect.forkScoped({ startImmediately: true }),
  114. )
  115. yield* Effect.promise(() => hg(directory, "branch", "-q", "feature"))
  116. expect(yield* vcs.info()).toEqual({ branch: { current: "default", default: "default" } })
  117. yield* bus.publish(FileSystem.Event.Changed, {
  118. file: path.join(directory, ".hg", "branch"),
  119. event: "change",
  120. })
  121. expect(yield* Fiber.join(updated)).toMatchObject({
  122. _tag: "Some",
  123. value: { location: { directory }, data: { branch: "feature" } },
  124. })
  125. expect(yield* vcs.info()).toEqual({ branch: { current: "feature", default: "default" } })
  126. }),
  127. ),
  128. )
  129. it.live("respects the context option", () =>
  130. withHg((directory) =>
  131. Effect.gen(function* () {
  132. const body = Array.from({ length: 20 }, (_, index) => `line-${index}`).join("\n") + "\n"
  133. yield* Effect.promise(async () => {
  134. await fs.writeFile(path.join(directory, "file.txt"), body)
  135. await commitAll(directory, "initial")
  136. await fs.writeFile(path.join(directory, "file.txt"), body.replace("line-10", "changed"))
  137. })
  138. const vcs = yield* Vcs.Service
  139. const full = yield* vcs.diff("working")
  140. expect(full[0].patch).toContain("line-0")
  141. expect(full[0].patch).toContain("line-19")
  142. const tight = yield* vcs.diff("working", { context: 1 })
  143. expect(tight[0].patch).toContain("line-9")
  144. expect(tight[0].patch).not.toContain("line-0")
  145. }),
  146. ),
  147. )
  148. it.live("diffs before the first commit", () =>
  149. withHg((directory) =>
  150. Effect.gen(function* () {
  151. yield* Effect.promise(async () => {
  152. await fs.writeFile(path.join(directory, "tracked.txt"), "a\nb\n")
  153. await hg(directory, "add", "-q", "tracked.txt")
  154. await fs.writeFile(path.join(directory, "loose.txt"), "hello\n")
  155. })
  156. const vcs = yield* Vcs.Service
  157. expect(yield* vcs.status()).toEqual([
  158. { file: "loose.txt", additions: 1, deletions: 0, status: "added" },
  159. { file: "tracked.txt", additions: 2, deletions: 0, status: "added" },
  160. ])
  161. const diff = yield* vcs.diff("working")
  162. expect(diff).toHaveLength(2)
  163. expect(diff[0].patch).toContain("+hello")
  164. expect(diff[1].patch).toContain("+a")
  165. }),
  166. ),
  167. )
  168. it.live("diffs a named branch against the default branch", () =>
  169. withHg((directory) =>
  170. Effect.gen(function* () {
  171. yield* Effect.promise(async () => {
  172. await fs.writeFile(path.join(directory, "file.txt"), "one\n")
  173. await commitAll(directory, "initial")
  174. })
  175. const vcs = yield* Vcs.Service
  176. expect(yield* vcs.diff("branch")).toEqual([])
  177. yield* Effect.promise(async () => {
  178. await hg(directory, "branch", "-q", "feature")
  179. await fs.writeFile(path.join(directory, "file.txt"), "one\ntwo\n")
  180. await commitAll(directory, "feature change")
  181. })
  182. const diff = yield* vcs.diff("branch")
  183. expect(diff.map((item) => ({ file: item.file, status: item.status }))).toEqual([
  184. { file: "file.txt", status: "modified" },
  185. ])
  186. expect(diff[0].patch).toContain("+two")
  187. }),
  188. ),
  189. )
  190. })