worktree.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { describe, expect, test } from "bun:test"
  2. import { Worktree } from "./worktree"
  3. import { ServerScope } from "./server-scope"
  4. const dir = (name: string) => `/tmp/opencode-worktree-${name}-${crypto.randomUUID()}`
  5. describe("Worktree", () => {
  6. const scope = ServerScope.local
  7. test("normalizes trailing slashes", () => {
  8. const key = dir("normalize")
  9. Worktree.ready(scope, `${key}/`)
  10. expect(Worktree.get(scope, key)).toEqual({ status: "ready" })
  11. })
  12. test("pending does not overwrite a terminal state", () => {
  13. const key = dir("pending")
  14. Worktree.failed(scope, key, "boom")
  15. Worktree.pending(scope, key)
  16. expect(Worktree.get(scope, key)).toEqual({ status: "failed", message: "boom" })
  17. })
  18. test("wait resolves shared pending waiter when ready", async () => {
  19. const key = dir("wait-ready")
  20. Worktree.pending(scope, key)
  21. const a = Worktree.wait(scope, key)
  22. const b = Worktree.wait(scope, `${key}/`)
  23. expect(a).toBe(b)
  24. Worktree.ready(scope, key)
  25. expect(await a).toEqual({ status: "ready" })
  26. expect(await b).toEqual({ status: "ready" })
  27. })
  28. test("wait resolves with failure message", async () => {
  29. const key = dir("wait-failed")
  30. const waiting = Worktree.wait(scope, key)
  31. Worktree.failed(scope, key, "permission denied")
  32. expect(await waiting).toEqual({ status: "failed", message: "permission denied" })
  33. expect(await Worktree.wait(scope, key)).toEqual({ status: "failed", message: "permission denied" })
  34. })
  35. test("isolates identical directories by server scope", () => {
  36. const key = dir("scope")
  37. const remote = "https://debian.example" as ServerScope
  38. Worktree.ready(scope, key)
  39. Worktree.failed(remote, key, "remote failed")
  40. expect(Worktree.get(scope, key)).toEqual({ status: "ready" })
  41. expect(Worktree.get(remote, key)).toEqual({ status: "failed", message: "remote failed" })
  42. })
  43. })