1import { assert, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3import { ContextHandoffId, OrchestrationV2Command, ThreadId } from "@t3tools/contracts";
4
5import {
6 appendContextHandoffId,
7 canReplayCommandReceipt,
8 shouldPrepareLegacyImportHandoff,
9} from "./Orchestrator.ts";
10
11it("reissues imported context until a V2 run completes", () => {
12 assert.isTrue(
13 shouldPrepareLegacyImportHandoff({
14 historyOrigin: "v1_import",
15 hasCompletedRun: false,
16 legacyImportItemCount: 2,
17 }),
18 );
19 assert.isFalse(
20 shouldPrepareLegacyImportHandoff({
21 historyOrigin: "v1_import",
22 hasCompletedRun: true,
23 legacyImportItemCount: 2,
24 }),
25 );
26 assert.isFalse(
27 shouldPrepareLegacyImportHandoff({
28 historyOrigin: undefined,
29 hasCompletedRun: false,
30 legacyImportItemCount: 2,
31 }),
32 );
33 assert.isFalse(
34 shouldPrepareLegacyImportHandoff({
35 historyOrigin: "v1_import",
36 hasCompletedRun: false,
37 legacyImportItemCount: 0,
38 }),
39 );
40});
41
42it("records a reissued legacy handoff on an existing provider thread", () => {
43 const existingHandoffId = ContextHandoffId.make("handoff:legacy-import:existing");
44 const retryHandoffId = ContextHandoffId.make("handoff:legacy-import:retry");
45
46 assert.deepEqual(appendContextHandoffId([existingHandoffId], retryHandoffId), [
47 existingHandoffId,
48 retryHandoffId,
49 ]);
50 assert.deepEqual(appendContextHandoffId([existingHandoffId], existingHandoffId), [
51 existingHandoffId,
52 ]);
53 assert.deepEqual(appendContextHandoffId([existingHandoffId], null), [existingHandoffId]);
54});
55
56it("only replays a command receipt for the thread it was recorded against", () => {
57 const threadA = ThreadId.make("thread-a");
58 const threadB = ThreadId.make("thread-b");
59
60 assert.strictEqual(canReplayCommandReceipt(threadA, threadA), true);
61 // A reused command id aimed at another thread must not report the first
62 // thread's success as this thread's (v1 #5246).
63 assert.strictEqual(canReplayCommandReceipt(threadA, threadB), false);
64});
65
66it("links and unlinks a pull request through thread.metadata.update (#8160)", () => {
67 // The fold is exercised through the schema: a command carrying the link
68 // must round-trip, and one without it must leave the field untouched.
69 const decode = Schema.decodeUnknownSync(OrchestrationV2Command);
70 const linked = decode({
71 type: "thread.metadata.update",
72 commandId: "command-link",
73 threadId: "thread-1",
74 linkedPullRequest: {
75 projectId: "project-1",
76 repository: "pingdotgg/t3code",
77 number: 8160,
78 url: "https://github.com/pingdotgg/t3code/pull/8160",
79 },
80 });
81 assert.deepStrictEqual(
82 (linked as Extract<typeof linked, { type: "thread.metadata.update" }>).linkedPullRequest,
83 {
84 projectId: "project-1",
85 repository: "pingdotgg/t3code",
86 number: 8160,
87 url: "https://github.com/pingdotgg/t3code/pull/8160",
88 },
89 );
90 const unlinked = decode({
91 type: "thread.metadata.update",
92 commandId: "command-unlink",
93 threadId: "thread-1",
94 linkedPullRequest: null,
95 });
96 assert.strictEqual(
97 (unlinked as Extract<typeof unlinked, { type: "thread.metadata.update" }>).linkedPullRequest,
98 null,
99 );
100});