Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { OrchestrationV2AppThreadJson, ThreadId } from "@t3tools/contracts";
3import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4import * as Effect from "effect/Effect";
5import * as DateTime from "effect/DateTime";
6import * as Layer from "effect/Layer";
7import * as Schema from "effect/Schema";
8import * as SqlClient from "effect/sql/SqlClient";
9
10import * as EventStore from "../../orchestration-v2/EventStore.ts";
11import * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts";
12import * as ProjectionReplay from "../../orchestration-v2/testkit/ProjectionReplay.ts";
13import { listLinkedPullRequestThreads } from "../../pullRequest/linkedThreads.ts";
14import { runMigrations } from "../Migrations.ts";
15
16const decodeThread = Schema.decodeSync(Schema.fromJsonString(OrchestrationV2AppThreadJson));
17const updatedAt = "2026-09-01T00:00:00.000Z";
18const linked = {
19 projectId: "project-1",
20 repository: "Acme/Web",
21 number: 42,
22 url: "https://github.com/acme/web/pull/42",
23};
24const 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};
34const 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
55const 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
65layer("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 assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), [
127 [64, "CanonicalizeThreadPullRequestLinks"],
128 ]);
129 assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), []);
130 const rows = yield* sql<{ readonly thread_id: string; readonly payload_json: string }>`
131 SELECT thread_id, payload_json FROM orchestration_v2_projection_threads
132 `;
133 const migrated = new Map(rows.map((row) => [row.thread_id, decodeThread(row.payload_json)]));
134 assert.deepEqual(migrated.get("github")?.pullRequests, [
135 { ...currentLink, number: 42, url: linked.url, source: "manual" },
136 ]);
137 assert.equal(migrated.get("forgejo")?.pullRequests?.[0]?.host, "forge.example:3000");
138 assert.equal(migrated.get("azure")?.pullRequests?.[0]?.host, "dev.azure.com");
139 assert.equal(migrated.get("azure")?.pullRequests?.[0]?.repository, "org/project/_git/web");
140 assert.deepEqual(migrated.get("empty")?.pullRequests, []);
141 assert.deepEqual(migrated.get("current")?.pullRequests, [currentLink]);
142 assert.isUndefined(migrated.get("unlinked")?.pullRequests);
143 assert.equal(migrated.get("page-500")?.pullRequests?.[0]?.number, 9000);
144 assert.isTrue(
145 [...migrated.values()].every((value) => DateTime.formatIso(value.updatedAt) === updatedAt),
146 );
147 const events = yield* sql<{ readonly stream_id: string; readonly payload_json: string }>`
148 SELECT stream_id, payload_json FROM orchestration_events WHERE application_event_version = 2
149 `;
150 assert.deepEqual(
151 new Map(events.map((event) => [event.stream_id, decodeThread(event.payload_json)])),
152 migrated,
153 );
154 const [v1] = yield* sql<{ readonly payload_json: string }>`
155 SELECT payload_json FROM orchestration_events WHERE event_id = 'event:v1'
156 `;
157 assert.equal(v1?.payload_json, v1Payload);
158
159 const maintenance = yield* ProjectionReplay.ProjectionReplay;
160 assert.isTrue((yield* maintenance.rebuild).valid);
161 for (const [host, repository, id] of [
162 ["github.com", "acme/web", "github"],
163 ["forge.example:3000", "acme/web", "forgejo"],
164 ["dev.azure.com", "org/project/_git/web", "azure"],
165 ] as const) {
166 assert.deepEqual(
167 (yield* listLinkedPullRequestThreads({ host, repository, number: 42 })).threads.map(
168 (value) => value.id,
169 ),
170 [ThreadId.make(id)],
171 );
172 }
173 }),
174 );
175});