@markjaquith/agency 3.2.14 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +20 -3
  2. package/cli-main.ts +1 -0
  3. package/package.json +7 -2
  4. package/src/cli-parser.ts +13 -0
  5. package/src/commands/work.ts +70 -2
  6. package/src/services/ReadinessService.ts +20 -0
  7. package/src/workbase/execution-contract.ts +10 -1
  8. package/src/workbase/schemas.ts +12 -0
  9. package/fixtures/protocol/orchestration-recipes.json +0 -53
  10. package/pi-extensions/agency.test.ts +0 -117
  11. package/src/check-commits.test.ts +0 -58
  12. package/src/cli-parser.test.ts +0 -945
  13. package/src/cli.test.ts +0 -1889
  14. package/src/commands/act.test.ts +0 -418
  15. package/src/commands/archive.test.ts +0 -236
  16. package/src/commands/context.test.ts +0 -711
  17. package/src/commands/description.test.ts +0 -103
  18. package/src/commands/doctor.test.ts +0 -185
  19. package/src/commands/epic.test.ts +0 -196
  20. package/src/commands/init.test.ts +0 -117
  21. package/src/commands/integration.test.ts +0 -165
  22. package/src/commands/pr.test.ts +0 -248
  23. package/src/commands/push.test.ts +0 -56
  24. package/src/commands/read-only.test.ts +0 -161
  25. package/src/commands/repo.test.ts +0 -199
  26. package/src/commands/restore.test.ts +0 -106
  27. package/src/commands/status.test.ts +0 -113
  28. package/src/commands/sync.test.ts +0 -200
  29. package/src/commands/task-phase.test.ts +0 -410
  30. package/src/commands/task.test.ts +0 -281
  31. package/src/commands/validate.test.ts +0 -186
  32. package/src/commands/work.test.ts +0 -1375
  33. package/src/commands/workbase.test.ts +0 -170
  34. package/src/graph-schema.test.ts +0 -124
  35. package/src/protocol.test.ts +0 -163
  36. package/src/readiness.test.ts +0 -105
  37. package/src/services/ArchiveBulkService.test.ts +0 -384
  38. package/src/services/ArchiveService.test.ts +0 -1092
  39. package/src/services/EpicService.test.ts +0 -74
  40. package/src/services/FileSystemService.test.ts +0 -81
  41. package/src/services/GraphMutationService.test.ts +0 -486
  42. package/src/services/GraphService.test.ts +0 -494
  43. package/src/services/IntegrationService.test.ts +0 -1091
  44. package/src/services/LifecycleTransaction.test.ts +0 -145
  45. package/src/services/PullRequestService.test.ts +0 -716
  46. package/src/services/PushService.test.ts +0 -408
  47. package/src/services/ReadinessService.test.ts +0 -290
  48. package/src/services/RepositoryService.test.ts +0 -789
  49. package/src/services/ReviewService.test.ts +0 -408
  50. package/src/services/SyncService.test.ts +0 -1377
  51. package/src/services/TaskPhaseService.test.ts +0 -852
  52. package/src/services/VersionControlService.test.ts +0 -62
  53. package/src/services/WorkbaseService.test.ts +0 -910
  54. package/src/services/WorktreeLock.test.ts +0 -205
  55. package/src/services/WorktreePerformance.test.ts +0 -71
  56. package/src/services/WorktreeService.test.ts +0 -2292
  57. package/src/test-utils.ts +0 -110
  58. package/src/usage-log.test.ts +0 -188
  59. package/src/utils/chooser.test.ts +0 -192
  60. package/src/utils/effect.test.ts +0 -30
  61. package/src/utils/interactive.pty.test.ts +0 -128
  62. package/src/utils/interactive.test.tsx +0 -860
  63. package/src/utils/process.test.ts +0 -132
  64. package/src/utils/progress.test.ts +0 -37
  65. package/src/work-view.test.ts +0 -151
  66. package/src/workbase/agent-command.test.ts +0 -128
  67. package/src/workbase/checkout-command.test.ts +0 -62
  68. package/src/workbase/delivery-command.test.ts +0 -180
  69. package/src/workbase/dependency-graph.test.ts +0 -50
  70. package/src/workbase/execution-contract.test.ts +0 -161
  71. package/src/workbase/frontmatter.test.ts +0 -67
  72. package/src/workbase/repository-reference.test.ts +0 -25
  73. package/src/workbase/schemas.test.ts +0 -543
  74. package/src/workbase/work-target.test.ts +0 -108
  75. package/src/workbase/worktree-command.test.ts +0 -61
@@ -1,789 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
2
- import { Effect } from "effect"
3
- import { mkdir, realpath, stat } from "node:fs/promises"
4
- import { dirname, join } from "node:path"
5
- import { cleanupTempDir, createTempDir, runTestEffect } from "../test-utils"
6
- import { RepositoryService } from "./RepositoryService"
7
-
8
- const runGit = async (args: string[]) => {
9
- const process = Bun.spawn(["git", ...args], {
10
- stdout: "pipe",
11
- stderr: "pipe",
12
- })
13
- await process.exited
14
- if (process.exitCode !== 0) {
15
- throw new Error(await new Response(process.stderr).text())
16
- }
17
- }
18
-
19
- const gitOutput = async (args: string[]) => {
20
- const process = Bun.spawn(["git", ...args], {
21
- stdout: "pipe",
22
- stderr: "pipe",
23
- })
24
- await process.exited
25
- if (process.exitCode !== 0) {
26
- throw new Error(await new Response(process.stderr).text())
27
- }
28
- return (await new Response(process.stdout).text()).trim()
29
- }
30
-
31
- const portableRemote = (name: string) =>
32
- `https://example.com/agency-tests/${name}.git`
33
-
34
- const setPortableOrigin = (path: string, name: string) =>
35
- runGit(["-C", path, "remote", "add", "origin", portableRemote(name)])
36
-
37
- const startGitDaemon = async (basePath: string) => {
38
- const port = 20000 + Math.floor(Math.random() * 20000)
39
- const process = Bun.spawn(
40
- [
41
- "git",
42
- "daemon",
43
- "--reuseaddr",
44
- "--export-all",
45
- `--base-path=${basePath}`,
46
- "--listen=127.0.0.1",
47
- `--port=${port}`,
48
- basePath,
49
- ],
50
- { stdout: "pipe", stderr: "pipe" },
51
- )
52
- const remote = `git://127.0.0.1:${port}/source.git`
53
- for (let attempt = 0; attempt < 40; attempt++) {
54
- const probe = Bun.spawn(["git", "ls-remote", remote], {
55
- stdout: "ignore",
56
- stderr: "ignore",
57
- })
58
- if ((await probe.exited) === 0) return { process, remote }
59
- await Bun.sleep(25)
60
- }
61
- process.kill()
62
- throw new Error("Git daemon did not start")
63
- }
64
-
65
- const write = async (root: string, path: string, content: string) => {
66
- const fullPath = join(root, path)
67
- await mkdir(dirname(fullPath), { recursive: true })
68
- await Bun.write(fullPath, content)
69
- }
70
-
71
- describe("RepositoryService", () => {
72
- let root: string
73
-
74
- beforeEach(async () => {
75
- root = await createTempDir()
76
- await Bun.write(join(root, "agency.json"), '{"version":2}\n')
77
- })
78
-
79
- afterEach(async () => {
80
- await cleanupTempDir(root)
81
- })
82
-
83
- test("adds a bare repository from a remote", async () => {
84
- const source = join(root, "source.git")
85
- await runGit(["init", "--bare", "--initial-branch=main", source])
86
- await setPortableOrigin(source, "agency")
87
-
88
- const destination = await runTestEffect(
89
- RepositoryService.pipe(
90
- Effect.flatMap((service) => service.add("agency", "source.git", root)),
91
- ),
92
- )
93
-
94
- expect(destination).toBe(join(root, "repos/agency"))
95
- expect(await Bun.file(join(destination, "HEAD")).exists()).toBe(true)
96
-
97
- const repositories = await runTestEffect(
98
- RepositoryService.pipe(Effect.flatMap((service) => service.list(root))),
99
- )
100
- expect(repositories).toEqual([
101
- {
102
- alias: "agency",
103
- path: destination,
104
- kind: "bare",
105
- remote: portableRemote("agency"),
106
- declaredRemote: portableRemote("agency"),
107
- target: null,
108
- states: ["declared", "materialized"],
109
- },
110
- ])
111
- })
112
-
113
- test("links an existing repository", async () => {
114
- const target = join(root, "linked-repository")
115
- await mkdir(target, { recursive: true })
116
- await runGit(["init", "--initial-branch=main", target])
117
- await setPortableOrigin(target, "effect")
118
-
119
- const destination = await runTestEffect(
120
- RepositoryService.pipe(
121
- Effect.flatMap((service) => service.link("effect", target, root)),
122
- ),
123
- )
124
-
125
- const repositories = await runTestEffect(
126
- RepositoryService.pipe(Effect.flatMap((service) => service.list(root))),
127
- )
128
- expect(repositories[0]).toEqual({
129
- alias: "effect",
130
- path: destination,
131
- kind: "symlink",
132
- remote: portableRemote("effect"),
133
- declaredRemote: portableRemote("effect"),
134
- target,
135
- states: ["declared", "linked"],
136
- })
137
- })
138
-
139
- test("inspects only the requested repository alias", async () => {
140
- const requested = join(root, "repos/requested")
141
- const unrelated = join(root, "repos/unrelated")
142
- await mkdir(requested, { recursive: true })
143
- await mkdir(unrelated, { recursive: true })
144
- await runGit(["init", "--initial-branch=main", requested])
145
- await runGit(["init", "--initial-branch=main", unrelated])
146
-
147
- const spawn = spyOn(Bun, "spawn")
148
- try {
149
- const repository = await runTestEffect(
150
- RepositoryService.pipe(
151
- Effect.flatMap((service) => service.show("requested", root)),
152
- ),
153
- )
154
- expect(repository.alias).toBe("requested")
155
- expect(spawn.mock.calls).toHaveLength(1)
156
- expect(spawn.mock.calls[0]?.[0]).toContain(requested)
157
- expect(spawn.mock.calls[0]?.[0]).not.toContain(unrelated)
158
- } finally {
159
- spawn.mockRestore()
160
- }
161
- })
162
-
163
- test("materializes a linked alias without invalidating active worktrees", async () => {
164
- const target = join(root, "linked-repository")
165
- const checkout = join(root, "tasks/active/code/linked")
166
- await mkdir(target, { recursive: true })
167
- await runGit(["init", "--initial-branch=main", target])
168
- await runGit(["-C", target, "config", "user.email", "test@example.com"])
169
- await runGit(["-C", target, "config", "user.name", "Test"])
170
- await Bun.write(join(target, "README.md"), "linked\n")
171
- await runGit(["-C", target, "add", "README.md"])
172
- await runGit(["-C", target, "commit", "-m", "initial"])
173
- const commit = await gitOutput(["-C", target, "rev-parse", "HEAD"])
174
- const sourceObject = join(
175
- target,
176
- ".git/objects",
177
- commit.slice(0, 2),
178
- commit.slice(2),
179
- )
180
- await setPortableOrigin(target, "materialized")
181
- await runTestEffect(
182
- RepositoryService.pipe(
183
- Effect.flatMap((service) => service.link("linked", target, root)),
184
- ),
185
- )
186
- await write(
187
- root,
188
- "tasks/active/TASK.md",
189
- `---
190
- ticketUrl: null
191
- repo: linked
192
- branch: task/active
193
- base: main
194
- pr: null
195
- status: working
196
- ---
197
- `,
198
- )
199
- await mkdir(dirname(checkout), { recursive: true })
200
- await runGit([
201
- "-C",
202
- target,
203
- "worktree",
204
- "add",
205
- "-b",
206
- "task/active",
207
- checkout,
208
- "main",
209
- ])
210
- await Bun.write(join(checkout, "dirty.txt"), "preserved\n")
211
- await runGit([
212
- "-C",
213
- target,
214
- "update-ref",
215
- "refs/agency/reviews/active",
216
- "main",
217
- ])
218
-
219
- const result = await runTestEffect(
220
- RepositoryService.pipe(
221
- Effect.flatMap((service) => service.materialize("linked", root)),
222
- ),
223
- )
224
-
225
- expect(result.kind).toBe("bare")
226
- expect(result.target).toBeNull()
227
- expect(result.states).toEqual(["declared", "materialized"])
228
- expect(await Bun.file(join(checkout, "dirty.txt")).text()).toBe(
229
- "preserved\n",
230
- )
231
- expect(await gitOutput(["-C", checkout, "branch", "--show-current"])).toBe(
232
- "task/active",
233
- )
234
- expect(
235
- await gitOutput(["-C", checkout, "rev-parse", "--git-common-dir"]),
236
- ).toBe(await realpath(join(root, "repos/linked")))
237
- expect(
238
- await gitOutput(["-C", target, "worktree", "list", "--porcelain"]),
239
- ).not.toContain(checkout)
240
- expect(await Bun.file(join(target, ".git/HEAD")).exists()).toBe(true)
241
- expect(await Bun.file(join(root, "tasks/active/TASK.md")).text()).toContain(
242
- "repo: linked",
243
- )
244
- expect(
245
- await gitOutput([
246
- "--git-dir",
247
- join(root, "repos/linked"),
248
- "rev-parse",
249
- "refs/agency/reviews/active",
250
- ]),
251
- ).toBe(await gitOutput(["-C", target, "rev-parse", "main"]))
252
- const materializedObject = join(
253
- root,
254
- "repos/linked/objects",
255
- commit.slice(0, 2),
256
- commit.slice(2),
257
- )
258
- expect((await stat(materializedObject)).ino).not.toBe(
259
- (await stat(sourceObject)).ino,
260
- )
261
- })
262
-
263
- test("preserves detached registered worktree commits from a shallow linked repository", async () => {
264
- const upstream = join(root, "upstream")
265
- const target = join(root, "shallow-linked-repository")
266
- const checkout = join(root, "tasks/detached/code/linked")
267
- await runGit(["init", "--bare", "--initial-branch=main", upstream])
268
- const seed = join(root, "seed")
269
- await runGit(["clone", upstream, seed])
270
- await runGit(["-C", seed, "config", "user.email", "test@example.com"])
271
- await runGit(["-C", seed, "config", "user.name", "Test"])
272
- await Bun.write(join(seed, "README.md"), "shallow\n")
273
- await runGit(["-C", seed, "add", "README.md"])
274
- await runGit(["-C", seed, "commit", "-m", "initial"])
275
- await runGit(["-C", seed, "push", "origin", "main"])
276
- await runGit(["clone", "--depth=1", `file://${upstream}`, target])
277
- await runGit(["-C", target, "config", "user.email", "test@example.com"])
278
- await runGit(["-C", target, "config", "user.name", "Test"])
279
- await runGit([
280
- "-C",
281
- target,
282
- "remote",
283
- "set-url",
284
- "origin",
285
- portableRemote("shallow-materialized"),
286
- ])
287
- await mkdir(dirname(checkout), { recursive: true })
288
- await runGit([
289
- "-C",
290
- target,
291
- "worktree",
292
- "add",
293
- "--detach",
294
- checkout,
295
- "main",
296
- ])
297
- await Bun.write(join(checkout, "detached.txt"), "preserved\n")
298
- await runGit(["-C", checkout, "add", "detached.txt"])
299
- await runGit(["-C", checkout, "commit", "-m", "detached work"])
300
- const detachedCommit = await gitOutput([
301
- "-C",
302
- checkout,
303
- "rev-parse",
304
- "HEAD",
305
- ])
306
- await runTestEffect(
307
- RepositoryService.pipe(
308
- Effect.flatMap((service) => service.link("linked", target, root)),
309
- ),
310
- )
311
-
312
- await runTestEffect(
313
- RepositoryService.pipe(
314
- Effect.flatMap((service) => service.materialize("linked", root)),
315
- ),
316
- )
317
-
318
- expect(await gitOutput(["-C", checkout, "rev-parse", "HEAD"])).toBe(
319
- detachedCommit,
320
- )
321
- expect(await Bun.file(join(checkout, "detached.txt")).text()).toBe(
322
- "preserved\n",
323
- )
324
- expect(
325
- await gitOutput(["-C", checkout, "rev-parse", "--git-common-dir"]),
326
- ).toBe(await realpath(join(root, "repos/linked")))
327
- await runGit([
328
- "--git-dir",
329
- join(root, "repos/linked"),
330
- "cat-file",
331
- "-e",
332
- `${detachedCommit}^{commit}`,
333
- ])
334
- expect(
335
- await gitOutput([
336
- "--git-dir",
337
- join(root, "repos/linked"),
338
- "for-each-ref",
339
- "--format=%(refname)",
340
- "refs/agency/materialize/",
341
- ]),
342
- ).toBe("")
343
- })
344
-
345
- test("materializes an alias linked through an existing worktree", async () => {
346
- const primary = join(root, "primary-repository")
347
- const linkedWorktree = join(root, "linked-worktree")
348
- await mkdir(primary, { recursive: true })
349
- await runGit(["init", "--initial-branch=main", primary])
350
- await runGit(["-C", primary, "config", "user.email", "test@example.com"])
351
- await runGit(["-C", primary, "config", "user.name", "Test"])
352
- await Bun.write(join(primary, "README.md"), "linked worktree\n")
353
- await runGit(["-C", primary, "add", "README.md"])
354
- await runGit(["-C", primary, "commit", "-m", "initial"])
355
- await setPortableOrigin(primary, "linked-worktree")
356
- await runGit([
357
- "-C",
358
- primary,
359
- "worktree",
360
- "add",
361
- "-b",
362
- "linked-branch",
363
- linkedWorktree,
364
- "main",
365
- ])
366
- await runTestEffect(
367
- RepositoryService.pipe(
368
- Effect.flatMap((service) =>
369
- service.link("linked", linkedWorktree, root),
370
- ),
371
- ),
372
- )
373
-
374
- await runTestEffect(
375
- RepositoryService.pipe(
376
- Effect.flatMap((service) => service.materialize("linked", root)),
377
- ),
378
- )
379
-
380
- expect(
381
- await gitOutput(["-C", linkedWorktree, "rev-parse", "--git-common-dir"]),
382
- ).toBe(await realpath(join(root, "repos/linked")))
383
- expect(
384
- await gitOutput(["-C", primary, "worktree", "list", "--porcelain"]),
385
- ).not.toContain(linkedWorktree)
386
- })
387
-
388
- test("rejects invalid and duplicate aliases", async () => {
389
- const source = join(root, "source.git")
390
- await runGit(["init", "--bare", "--initial-branch=main", source])
391
- await setPortableOrigin(source, "duplicate")
392
-
393
- await expect(
394
- runTestEffect(
395
- RepositoryService.pipe(
396
- Effect.flatMap((service) => service.add("bad/alias", source, root)),
397
- ),
398
- ),
399
- ).rejects.toThrow("Invalid repository alias")
400
-
401
- await runTestEffect(
402
- RepositoryService.pipe(
403
- Effect.flatMap((service) => service.add("agency", source, root)),
404
- ),
405
- )
406
- await expect(
407
- runTestEffect(
408
- RepositoryService.pipe(
409
- Effect.flatMap((service) => service.add("agency", source, root)),
410
- ),
411
- ),
412
- ).rejects.toThrow("already exists")
413
- })
414
-
415
- test("leaves no declaration or materialization when cloning fails", async () => {
416
- await expect(
417
- runTestEffect(
418
- RepositoryService.pipe(
419
- Effect.flatMap((service) =>
420
- service.add("failed", "git://127.0.0.1:1/missing.git", root),
421
- ),
422
- ),
423
- ),
424
- ).rejects.toThrow("Failed to clone repository")
425
- expect(await Bun.file(join(root, "agency.json")).json()).toEqual({
426
- version: 2,
427
- })
428
- expect(await Bun.file(join(root, "repos/failed")).exists()).toBe(false)
429
- })
430
-
431
- test("rolls back a repository move when config installation fails", async () => {
432
- const source = join(root, "source.git")
433
- await runGit(["init", "--bare", "--initial-branch=main", source])
434
- await setPortableOrigin(source, "rollback")
435
- await runTestEffect(
436
- RepositoryService.pipe(
437
- Effect.flatMap((service) => service.add("old", source, root)),
438
- ),
439
- )
440
- const configBefore = await Bun.file(join(root, "agency.json")).text()
441
- const originalWrite = Bun.write
442
- ;(Bun as { write: typeof Bun.write }).write = ((
443
- destination: unknown,
444
- data: unknown,
445
- ) =>
446
- String(destination).includes(".agency-transaction-")
447
- ? Promise.reject(new Error("injected config write failure"))
448
- : originalWrite(
449
- destination as never,
450
- data as never,
451
- )) as typeof Bun.write
452
-
453
- try {
454
- await expect(
455
- runTestEffect(
456
- RepositoryService.pipe(
457
- Effect.flatMap((service) => service.rename("old", "new", root)),
458
- ),
459
- ),
460
- ).rejects.toThrow("completed changes were rolled back")
461
- } finally {
462
- ;(Bun as { write: typeof Bun.write }).write = originalWrite
463
- }
464
-
465
- expect(await Bun.file(join(root, "repos/old/HEAD")).exists()).toBe(true)
466
- expect(await Bun.file(join(root, "repos/new/HEAD")).exists()).toBe(false)
467
- expect(await Bun.file(join(root, "agency.json")).text()).toBe(configBefore)
468
- expect(
469
- (await Array.fromAsync(new Bun.Glob(".agency-*").scan(root))).length,
470
- ).toBe(0)
471
- })
472
-
473
- test("reports a file collision as invalid setup state", async () => {
474
- await Bun.write(
475
- join(root, "agency.json"),
476
- JSON.stringify({
477
- version: 2,
478
- repositories: {
479
- blocked: { remote: portableRemote("blocked") },
480
- },
481
- }),
482
- )
483
- await mkdir(join(root, "repos"), { recursive: true })
484
- await Bun.write(join(root, "repos/blocked"), "not a repository")
485
-
486
- const setup = await runTestEffect(
487
- RepositoryService.pipe(
488
- Effect.flatMap((service) => service.setup({ cwd: root })),
489
- ),
490
- )
491
-
492
- expect(setup.actions).toEqual([])
493
- expect(setup.repositories[0]?.states).toEqual(["declared", "invalid"])
494
- expect(setup.unresolved[0]).toMatchObject({
495
- alias: "blocked",
496
- state: "invalid",
497
- })
498
- })
499
-
500
- test("shows, fetches, updates, and verifies a repository", async () => {
501
- const source = join(root, "source")
502
- const replacement = portableRemote("replacement")
503
- await runGit(["init", "--initial-branch=main", source])
504
- await setPortableOrigin(source, "source")
505
- await Bun.write(join(source, "README.md"), "# Source\n")
506
- await runGit(["-C", source, "add", "README.md"])
507
- await runGit([
508
- "-C",
509
- source,
510
- "-c",
511
- "user.name=Agency Tests",
512
- "-c",
513
- "user.email=agency@example.com",
514
- "commit",
515
- "-m",
516
- "Initial commit",
517
- ])
518
- await runTestEffect(
519
- RepositoryService.pipe(
520
- Effect.flatMap((service) => service.add("agency", source, root)),
521
- ),
522
- )
523
- await runGit([
524
- "-C",
525
- join(root, "repos/agency"),
526
- "config",
527
- `url.${source}.insteadOf`,
528
- portableRemote("source"),
529
- ])
530
-
531
- const result = await runTestEffect(
532
- RepositoryService.pipe(
533
- Effect.flatMap((service) =>
534
- Effect.gen(function* () {
535
- const shown = yield* service.show("agency", root)
536
- yield* service.fetch("agency", root)
537
- const updated = yield* service.remote("agency", replacement, root)
538
- const verified = yield* service.verify("agency", root)
539
- return { shown, updated, verified }
540
- }),
541
- ),
542
- ),
543
- )
544
-
545
- expect(result.shown.remote).toBe(source)
546
- expect(result.shown.declaredRemote).toBe(portableRemote("source"))
547
- expect(result.updated.remote).toBe(replacement)
548
- expect(result.verified.valid).toBe(true)
549
- expect(result.verified.issues).toEqual([])
550
- })
551
-
552
- test("renames and removes an unused repository", async () => {
553
- const source = join(root, "source.git")
554
- await runGit(["init", "--bare", "--initial-branch=main", source])
555
- await setPortableOrigin(source, "rename")
556
- await runTestEffect(
557
- RepositoryService.pipe(
558
- Effect.flatMap((service) => service.add("old", source, root)),
559
- ),
560
- )
561
-
562
- const removed = await runTestEffect(
563
- RepositoryService.pipe(
564
- Effect.flatMap((service) =>
565
- Effect.gen(function* () {
566
- const renamed = yield* service.rename("old", "new", root)
567
- expect(renamed.alias).toBe("new")
568
- return yield* service.remove("new", root)
569
- }),
570
- ),
571
- ),
572
- )
573
-
574
- expect(removed.alias).toBe("new")
575
- expect(await Bun.file(join(root, "repos/new/HEAD")).exists()).toBe(false)
576
- })
577
-
578
- test("unlinks a symlink without deleting its target", async () => {
579
- const target = join(root, "linked-repository")
580
- await mkdir(target, { recursive: true })
581
- await runGit(["init", "--initial-branch=main", target])
582
- await setPortableOrigin(target, "linked")
583
- await runTestEffect(
584
- RepositoryService.pipe(
585
- Effect.flatMap((service) => service.link("linked", target, root)),
586
- ),
587
- )
588
-
589
- await runTestEffect(
590
- RepositoryService.pipe(
591
- Effect.flatMap((service) => service.unlink("linked", root)),
592
- ),
593
- )
594
-
595
- expect(await Bun.file(join(target, ".git/HEAD")).exists()).toBe(true)
596
- expect(await Bun.file(join(root, "repos/linked/.git/HEAD")).exists()).toBe(
597
- false,
598
- )
599
- const declared = await runTestEffect(
600
- RepositoryService.pipe(
601
- Effect.flatMap((service) => service.show("linked", root)),
602
- ),
603
- )
604
- expect(declared.states).toEqual(["declared", "missing"])
605
- })
606
-
607
- test("refuses to unlink an alias with active references", async () => {
608
- const target = join(root, "referenced-link")
609
- await mkdir(target, { recursive: true })
610
- await runGit(["init", "--initial-branch=main", target])
611
- await setPortableOrigin(target, "referenced-link")
612
- await runTestEffect(
613
- RepositoryService.pipe(
614
- Effect.flatMap((service) => service.link("linked", target, root)),
615
- ),
616
- )
617
- await write(
618
- root,
619
- "tasks/active/TASK.md",
620
- `---
621
- ticketUrl: null
622
- repo: linked
623
- branch: task/active-link
624
- base: main
625
- pr: null
626
- ---
627
- `,
628
- )
629
-
630
- await expect(
631
- runTestEffect(
632
- RepositoryService.pipe(
633
- Effect.flatMap((service) => service.unlink("linked", root)),
634
- ),
635
- ),
636
- ).rejects.toThrow("active reference execution-unit:task/active")
637
- expect(await Bun.file(join(root, "repos/linked/.git/HEAD")).exists()).toBe(
638
- true,
639
- )
640
- })
641
-
642
- test("replaces an unused managed clone with a linked checkout", async () => {
643
- const source = join(root, "source.git")
644
- const target = join(root, "local-checkout")
645
- await runGit(["init", "--bare", "--initial-branch=main", source])
646
- await setPortableOrigin(source, "replace")
647
- await mkdir(target, { recursive: true })
648
- await runGit(["init", "--initial-branch=main", target])
649
-
650
- const linked = await runTestEffect(
651
- RepositoryService.pipe(
652
- Effect.flatMap((service) =>
653
- Effect.gen(function* () {
654
- yield* service.add("agency", source, root)
655
- yield* service.link("agency", target, root)
656
- return yield* service.show("agency", root)
657
- }),
658
- ),
659
- ),
660
- )
661
-
662
- expect(linked.kind).toBe("symlink")
663
- expect(linked.target).toBe(target)
664
- expect(linked.declaredRemote).toBe(portableRemote("replace"))
665
- expect(linked.states).toEqual(["declared", "linked", "remote-drifted"])
666
- })
667
-
668
- test("reports active references and refuses unsafe removal", async () => {
669
- const source = join(root, "source.git")
670
- await runGit(["init", "--bare", "--initial-branch=main", source])
671
- await setPortableOrigin(source, "referenced")
672
- await runTestEffect(
673
- RepositoryService.pipe(
674
- Effect.flatMap((service) => service.add("agency", source, root)),
675
- ),
676
- )
677
- await write(
678
- root,
679
- "tasks/active/TASK.md",
680
- `---
681
- ticketUrl: null
682
- repo: agency
683
- branch: task/active
684
- base: main
685
- pr: null
686
- ---
687
- `,
688
- )
689
-
690
- await expect(
691
- runTestEffect(
692
- RepositoryService.pipe(
693
- Effect.flatMap((service) => service.remove("agency", root)),
694
- ),
695
- ),
696
- ).rejects.toThrow("active reference execution-unit:task/active")
697
- expect(await Bun.file(join(root, "repos/agency/HEAD")).exists()).toBe(true)
698
- })
699
-
700
- test("plans and applies missing portable repository setup", async () => {
701
- const source = join(root, "source.git")
702
- await runGit(["init", "--bare", "--initial-branch=main", source])
703
- const daemon = await startGitDaemon(root)
704
- try {
705
- await Bun.write(
706
- join(root, "agency.json"),
707
- JSON.stringify({
708
- version: 2,
709
- repositories: { agency: { remote: daemon.remote } },
710
- }),
711
- )
712
-
713
- const result = await runTestEffect(
714
- RepositoryService.pipe(
715
- Effect.flatMap((service) =>
716
- Effect.gen(function* () {
717
- const planned = yield* service.setup({ cwd: root })
718
- const absent = !(yield* Effect.promise(() =>
719
- Bun.file(join(root, "repos/agency/HEAD")).exists(),
720
- ))
721
- const applied = yield* service.setup({ cwd: root, apply: true })
722
- return { planned, absent, applied }
723
- }),
724
- ),
725
- ),
726
- )
727
-
728
- expect(result.planned.mode).toBe("dry-run")
729
- expect(result.planned.actions[0]).toMatchObject({
730
- kind: "materialize",
731
- status: "planned",
732
- })
733
- expect(result.absent).toBe(true)
734
- expect(result.applied.actions[0]?.status).toBe("applied")
735
- expect(result.applied.repositories[0]?.states).toEqual([
736
- "declared",
737
- "materialized",
738
- ])
739
- expect(await Bun.file(join(root, "repos/agency/HEAD")).exists()).toBe(
740
- true,
741
- )
742
- } finally {
743
- daemon.process.kill()
744
- await daemon.process.exited
745
- }
746
- })
747
-
748
- test("adopts legacy materializations and reports remote drift", async () => {
749
- const legacy = join(root, "repos/legacy")
750
- await mkdir(legacy, { recursive: true })
751
- await runGit(["init", "--initial-branch=main", legacy])
752
- await setPortableOrigin(legacy, "legacy")
753
-
754
- const result = await runTestEffect(
755
- RepositoryService.pipe(
756
- Effect.flatMap((service) =>
757
- Effect.gen(function* () {
758
- const planned = yield* service.setup({ cwd: root })
759
- const applied = yield* service.setup({ cwd: root, apply: true })
760
- yield* Effect.promise(() =>
761
- runGit([
762
- "-C",
763
- legacy,
764
- "remote",
765
- "set-url",
766
- "origin",
767
- "https://example.com/agency-tests/changed.git",
768
- ]),
769
- )
770
- const drifted = yield* service.show("legacy", root)
771
- const setup = yield* service.setup({ cwd: root })
772
- return { planned, applied, drifted, setup }
773
- }),
774
- ),
775
- ),
776
- )
777
-
778
- expect(result.planned.actions[0]?.kind).toBe("adopt")
779
- expect(result.applied.repositories[0]?.states).toEqual([
780
- "declared",
781
- "materialized",
782
- ])
783
- expect(result.drifted.states).toContain("remote-drifted")
784
- expect(result.setup.unresolved[0]).toMatchObject({
785
- alias: "legacy",
786
- state: "remote-drifted",
787
- })
788
- })
789
- })