The best way to control Claude Code, Codex and more from any computer or phone

main
ts 202 lines 8.34 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import { OrchestrationV2AppThreadJson, ThreadId } from "@t3tools/contracts";
3 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4 import * as Effect from "effect/Effect";
5 import * as DateTime from "effect/DateTime";
6 import * as Layer from "effect/Layer";
7 import * as Schema from "effect/Schema";
8 import * as SqlClient from "effect/sql/SqlClient";
9
10 import * as EventStore from "../../orchestration-v2/EventStore.ts";
11 import * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts";
12 import * as ProjectionReplay from "../../orchestration-v2/testkit/ProjectionReplay.ts";
13 import { listLinkedPullRequestThreads } from "../../pullRequest/linkedThreads.ts";
14 import { runMigrations } from "../Migrations.ts";
15
16 const decodeThread = Schema.decodeSync(Schema.fromJsonString(OrchestrationV2AppThreadJson));
17 const updatedAt = "2026-09-01T00:00:00.000Z";
18 const linked = {
19 projectId: "project-1",
20 repository: "Acme/Web",
21 number: 42,
22 url: "https://github.com/acme/web/pull/42",
23 };
24 const currentLink = {
25 host: "github.com",
26 repository: "acme/web",
27 number: 43,
28 url: "https://github.com/acme/web/pull/43",
29 source: "agent" as const,
30 linkedAt: updatedAt,
31 snapshot: null,
32 stack: null,
33 };
34 const thread = (id: string) => ({
35 id,
36 projectId: "project-1",
37 title: id,
38 providerInstanceId: "codex",
39 modelSelection: { instanceId: "codex", model: "gpt-5.4" },
40 runtimeMode: "full-access",
41 interactionMode: "default",
42 createdBy: "user",
43 creationSource: "web",
44 branch: null,
45 worktreePath: null,
46 activeProviderThreadId: null,
47 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id },
48 forkedFrom: null,
49 createdAt: updatedAt,
50 updatedAt,
51 archivedAt: null,
52 deletedAt: null,
53 });
54
55 const layer = it.layer(
56 ProjectionReplay.layer.pipe(
57 Layer.provideMerge(
58 Layer.mergeAll(EventStore.layer, ProjectionStore.layer).pipe(
59 Layer.provideMerge(NodeSqliteClient.layer({ filename: ":memory:" })),
60 ),
61 ),
62 ),
63 );
64
65 layer("064_CanonicalizeThreadPullRequestLinks", (it) => {
66 it.effect("migrates single links once, preserves arrays, and keeps links after a rebuild", () =>
67 Effect.gen(function* () {
68 const sql = yield* SqlClient.SqlClient;
69 yield* runMigrations({ toMigrationInclusive: 63 });
70 const fixtures = [
71 { ...thread("github"), linkedPullRequest: linked },
72 {
73 ...thread("forgejo"),
74 linkedPullRequest: { ...linked, url: "http://forge.example:3000/acme/web/pulls/42" },
75 },
76 {
77 ...thread("azure"),
78 linkedPullRequest: {
79 ...linked,
80 repository: "project/_git/web",
81 url: "https://dev.azure.com/org/project/_git/web/pullrequest/42",
82 },
83 },
84 { ...thread("empty"), linkedPullRequest: linked, pullRequests: [] },
85 { ...thread("current"), linkedPullRequest: linked, pullRequests: [currentLink] },
86 { ...thread("unlinked"), linkedPullRequest: null },
87 ...Array.from({ length: 501 }, (_, index) => ({
88 ...thread(`page-${String(index).padStart(3, "0")}`),
89 linkedPullRequest: {
90 ...linked,
91 number: 9000,
92 url: "https://github.com/acme/web/pull/9000",
93 },
94 })),
95 ];
96 for (const fixture of fixtures) {
97 const payload = JSON.stringify(fixture);
98 yield* sql`
99 INSERT INTO orchestration_v2_projection_threads (
100 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
101 created_at, updated_at, payload_json
102 ) VALUES (
103 ${fixture.id}, 'project-1', ${fixture.title}, 'codex', 'full-access', 'default',
104 ${updatedAt}, ${updatedAt}, ${payload}
105 )
106 `;
107 yield* sql`
108 INSERT INTO orchestration_events (
109 event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
110 actor_kind, payload_json, metadata_json, application_event_version
111 ) VALUES (
112 ${`event:${fixture.id}`}, 'thread', ${fixture.id}, 0, 'thread.created', ${updatedAt},
113 'user', ${payload}, '{}', 2
114 )
115 `;
116 }
117 const v1Payload = JSON.stringify({ linkedPullRequest: linked });
118 yield* sql`
119 INSERT INTO orchestration_events (
120 event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
121 actor_kind, payload_json, metadata_json, application_event_version
122 ) VALUES ('event:v1', 'thread', 'v1', 0, 'thread.created', ${updatedAt},
123 'user', ${v1Payload}, '{}', 1)
124 `;
125
126 yield* sql`
127 INSERT INTO orchestration_v2_projection_threads (
128 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
129 created_at, updated_at, payload_json
130 ) VALUES (
131 'damaged', 'project-1', 'damaged', 'codex', 'full-access', 'default',
132 ${updatedAt}, ${updatedAt}, ''
133 )
134 `;
135 yield* sql`
136 INSERT INTO orchestration_events (
137 event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
138 actor_kind, payload_json, metadata_json, application_event_version
139 ) VALUES ('event:damaged', 'thread', 'damaged', 0, 'thread.created', ${updatedAt},
140 'user', '', '{}', 2)
141 `;
142
143 assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), [
144 [64, "CanonicalizeThreadPullRequestLinks"],
145 ]);
146 const [damagedThread] = yield* sql<{ readonly payload_json: string }>`
147 SELECT payload_json FROM orchestration_v2_projection_threads WHERE thread_id = 'damaged'
148 `;
149 const [damagedEvent] = yield* sql<{ readonly payload_json: string }>`
150 SELECT payload_json FROM orchestration_events WHERE event_id = 'event:damaged'
151 `;
152 assert.equal(damagedThread?.payload_json, "");
153 assert.equal(damagedEvent?.payload_json, "");
154 yield* sql`DELETE FROM orchestration_v2_projection_threads WHERE thread_id = 'damaged'`;
155 yield* sql`DELETE FROM orchestration_events WHERE event_id = 'event:damaged'`;
156 assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), []);
157 const rows = yield* sql<{ readonly thread_id: string; readonly payload_json: string }>`
158 SELECT thread_id, payload_json FROM orchestration_v2_projection_threads
159 `;
160 const migrated = new Map(rows.map((row) => [row.thread_id, decodeThread(row.payload_json)]));
161 assert.deepEqual(migrated.get("github")?.pullRequests, [
162 { ...currentLink, number: 42, url: linked.url, source: "manual" },
163 ]);
164 assert.equal(migrated.get("forgejo")?.pullRequests?.[0]?.host, "forge.example:3000");
165 assert.equal(migrated.get("azure")?.pullRequests?.[0]?.host, "dev.azure.com");
166 assert.equal(migrated.get("azure")?.pullRequests?.[0]?.repository, "org/project/_git/web");
167 assert.deepEqual(migrated.get("empty")?.pullRequests, []);
168 assert.deepEqual(migrated.get("current")?.pullRequests, [currentLink]);
169 assert.isUndefined(migrated.get("unlinked")?.pullRequests);
170 assert.equal(migrated.get("page-500")?.pullRequests?.[0]?.number, 9000);
171 assert.isTrue(
172 [...migrated.values()].every((value) => DateTime.formatIso(value.updatedAt) === updatedAt),
173 );
174 const events = yield* sql<{ readonly stream_id: string; readonly payload_json: string }>`
175 SELECT stream_id, payload_json FROM orchestration_events WHERE application_event_version = 2
176 `;
177 assert.deepEqual(
178 new Map(events.map((event) => [event.stream_id, decodeThread(event.payload_json)])),
179 migrated,
180 );
181 const [v1] = yield* sql<{ readonly payload_json: string }>`
182 SELECT payload_json FROM orchestration_events WHERE event_id = 'event:v1'
183 `;
184 assert.equal(v1?.payload_json, v1Payload);
185
186 const maintenance = yield* ProjectionReplay.ProjectionReplay;
187 assert.isTrue((yield* maintenance.rebuild).valid);
188 for (const [host, repository, id] of [
189 ["github.com", "acme/web", "github"],
190 ["forge.example:3000", "acme/web", "forgejo"],
191 ["dev.azure.com", "org/project/_git/web", "azure"],
192 ] as const) {
193 assert.deepEqual(
194 (yield* listLinkedPullRequestThreads({ host, repository, number: 42 })).threads.map(
195 (value) => value.id,
196 ),
197 [ThreadId.make(id)],
198 );
199 }
200 }),
201 );
202 });