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

main
ts 227 lines 9.71 KB
Raw
1 import { assert, describe, it } from "@effect/vitest";
2 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Effect from "effect/Effect";
4 import * as Exit from "effect/Exit";
5 import * as Migrator from "effect/sql/Migrator";
6 import * as SqlClient from "effect/sql/SqlClient";
7
8 import { migrationManifest, runMigrations } from "./Migrations.ts";
9 import PullRequestFilesViewed from "./Migrations/053_PullRequestFilesViewed.ts";
10 import RemoveRedundantProjectionIndexes from "./Migrations/056_RemoveRedundantProjectionIndexes.ts";
11 import OrchestrationV2 from "./Migrations/055_OrchestrationV2.ts";
12
13 const seedPreview = Effect.gen(function* () {
14 const sql = yield* SqlClient.SqlClient;
15 yield* runMigrations({ toMigrationInclusive: 52 });
16 yield* Migrator.make({})({
17 loader: Migrator.fromRecord({ "53_OrchestrationV2": OrchestrationV2 }),
18 });
19 yield* sql`
20 INSERT INTO orchestration_v2_legacy_imports
21 (thread_id, source_updated_at, shell_imported_at, transcript_imported_at, imported_message_count)
22 VALUES ('preview-thread', '2026-09-15', '2026-09-15', '2026-09-16', 42)
23 `;
24 yield* sql`
25 UPDATE effect_sql_migrations SET created_at = '2026-09-15 00:00:00' WHERE migration_id = 53
26 `;
27 });
28
29 describe("V2 preview upgrade", () => {
30 it.effect("upgrades a published preview without replaying V2 or losing import progress", () =>
31 Effect.gen(function* () {
32 const sql = yield* SqlClient.SqlClient;
33 yield* seedPreview;
34 const imports = yield* sql`SELECT * FROM orchestration_v2_legacy_imports`;
35 assert.deepStrictEqual(yield* runMigrations(), [
36 [53, "PullRequestFilesViewed"],
37 [54, "ProjectionThreadsAutoSettleDisabledAt"],
38 [56, "RemoveRedundantProjectionIndexes"],
39 [57, "ScheduledTaskWebhooks"],
40 [58, "WebhookRelayDeliveries"],
41 [59, "McpAppModelContext"],
42 [60, "ThreadSnapshotWindowIndexes"],
43 [61, "PullRequestReviewGuides"],
44 [62, "ThreadFailureLookupIndex"],
45 [63, "DropUnusedV1Projections"],
46 [64, "CanonicalizeThreadPullRequestLinks"],
47 [65, "CodexUsageNativeTurnIndex"],
48 [66, "RepairClientSettlementTimestamps"],
49 [67, "WebhookDedupe"],
50 [68, "ShellStopRequestIndex"],
51 [69, "ThreadMessageSearch"],
52 [70, "PeerLinks"],
53 [71, "PeerLinkFence"],
54 [72, "WaitingLinkRequestIndex"],
55 [73, "ActiveTurnItemIndexes"],
56 [74, "ExcludeAttachmentPlaceholdersFromSearch"],
57 [75, "SubagentDeliveryState"],
58 [76, "ProjectionProjectsPinArchive"],
59 [77, "RepairBooleanSettledOverride"],
60 [78, "DropOrchestrationEventCorrelationIndex"],
61 [79, "DelegatedRecoveryIndexes"],
62 [80, "ScheduledTaskThreadMode"],
63 [81, "ProjectionMessageSearchText"],
64 [82, "TurnItemConversationIndex"],
65 [83, "CommandReadIndexes"],
66 [84, "LastExecutedRunIndex"],
67 [85, "TurnItemCounts"],
68 [86, "QueuedTurnInputIndex"],
69 [87, "ProjectionProjectsCustomTitle"],
70 [88, "TurnItemTypeStatusIndex"],
71 [89, "ShellThreadReadIndexes"],
72 [90, "AuthConnectDevices"],
73 [91, "AuthConnectDeniedRequests"],
74 [92, "QueuedTurnInputIndexJsonValid"],
75 [93, "ProviderTurnAttemptIndex"],
76 [94, "TurnItemMessageOrdinalIndex"],
77 [95, "TurnItemInlineWorkOutput"],
78 [96, "RepairStaleTitleRegeneration"],
79 [97, "IssueTrackerBindings"],
80 [98, "ActiveShellIndexIndexes"],
81 [99, "BackgroundRecoveryIndexes"],
82 [100, "TurnStartReadIndexes"],
83 [101, "DelegatedTaskReadIndexes"],
84 [102, "ThreadItemCounts"],
85 ]);
86 assert.deepStrictEqual(yield* runMigrations(), []);
87 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
88 const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
89 SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id
90 `;
91 assert.deepStrictEqual(
92 history.map((row) => [row.migration_id, row.name] as const),
93 migrationManifest,
94 );
95 assert.deepStrictEqual(
96 yield* sql`SELECT created_at FROM effect_sql_migrations WHERE migration_id = 55`,
97 [{ created_at: "2026-09-15 00:00:00" }],
98 );
99 yield* sql`
100 INSERT INTO pull_request_files_viewed
101 (provider, host, repository, number, viewer, path, revision, viewed_at)
102 VALUES ('github', 'github.com', 'owner/repo', 1, 'viewer', 'file.ts', 'revision', '2026-09-17')
103 `;
104 assert.strictEqual((yield* sql`SELECT * FROM pull_request_files_viewed`).length, 1);
105 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
106 );
107
108 it.effect.each([false, true])(
109 "upgrades preview migration 54 with index cleanup %s",
110 (withIndexes) =>
111 Effect.gen(function* () {
112 const sql = yield* SqlClient.SqlClient;
113 yield* runMigrations({ toMigrationInclusive: 52 });
114 yield* Migrator.make({})({
115 loader: Migrator.fromRecord({
116 "53_PullRequestFilesViewed": PullRequestFilesViewed,
117 "54_OrchestrationV2": OrchestrationV2,
118 ...(withIndexes
119 ? { "55_RemoveRedundantProjectionIndexes": RemoveRedundantProjectionIndexes }
120 : {}),
121 }),
122 });
123 yield* runMigrations();
124 assert.deepStrictEqual(yield* runMigrations(), []);
125 const history = yield* sql<{
126 readonly migration_id: number;
127 readonly name: string;
128 }>`SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id`;
129 assert.deepStrictEqual(
130 history.map((row) => [row.migration_id, row.name] as const),
131 migrationManifest,
132 );
133 const columns = yield* sql<{
134 readonly name: string;
135 }>`PRAGMA table_info(projection_threads)`;
136 assert.ok(columns.some((column) => column.name === "auto_settle_disabled_at"));
137 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
138 );
139
140 it.effect("rolls back schema and ledger together on failure and can retry", () =>
141 Effect.gen(function* () {
142 const sql = yield* SqlClient.SqlClient;
143 yield* seedPreview;
144 yield* sql`
145 CREATE TRIGGER fail_preview_upgrade BEFORE INSERT ON effect_sql_migrations
146 WHEN NEW.name = 'PullRequestFilesViewed'
147 BEGIN SELECT RAISE(ABORT, 'injected failure'); END
148 `;
149 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
150 assert.deepStrictEqual(
151 yield* sql`SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 53`,
152 [{ migration_id: 53, name: "OrchestrationV2" }],
153 );
154 assert.deepStrictEqual(
155 yield* sql`SELECT name FROM sqlite_master WHERE name = 'pull_request_files_viewed'`,
156 [],
157 );
158 assert.strictEqual((yield* sql`SELECT * FROM orchestration_v2_legacy_imports`).length, 1);
159 yield* sql`DROP TRIGGER fail_preview_upgrade`;
160 assert.deepStrictEqual(yield* runMigrations(), [
161 [53, "PullRequestFilesViewed"],
162 [54, "ProjectionThreadsAutoSettleDisabledAt"],
163 [56, "RemoveRedundantProjectionIndexes"],
164 [57, "ScheduledTaskWebhooks"],
165 [58, "WebhookRelayDeliveries"],
166 [59, "McpAppModelContext"],
167 [60, "ThreadSnapshotWindowIndexes"],
168 [61, "PullRequestReviewGuides"],
169 [62, "ThreadFailureLookupIndex"],
170 [63, "DropUnusedV1Projections"],
171 [64, "CanonicalizeThreadPullRequestLinks"],
172 [65, "CodexUsageNativeTurnIndex"],
173 [66, "RepairClientSettlementTimestamps"],
174 [67, "WebhookDedupe"],
175 [68, "ShellStopRequestIndex"],
176 [69, "ThreadMessageSearch"],
177 [70, "PeerLinks"],
178 [71, "PeerLinkFence"],
179 [72, "WaitingLinkRequestIndex"],
180 [73, "ActiveTurnItemIndexes"],
181 [74, "ExcludeAttachmentPlaceholdersFromSearch"],
182 [75, "SubagentDeliveryState"],
183 [76, "ProjectionProjectsPinArchive"],
184 [77, "RepairBooleanSettledOverride"],
185 [78, "DropOrchestrationEventCorrelationIndex"],
186 [79, "DelegatedRecoveryIndexes"],
187 [80, "ScheduledTaskThreadMode"],
188 [81, "ProjectionMessageSearchText"],
189 [82, "TurnItemConversationIndex"],
190 [83, "CommandReadIndexes"],
191 [84, "LastExecutedRunIndex"],
192 [85, "TurnItemCounts"],
193 [86, "QueuedTurnInputIndex"],
194 [87, "ProjectionProjectsCustomTitle"],
195 [88, "TurnItemTypeStatusIndex"],
196 [89, "ShellThreadReadIndexes"],
197 [90, "AuthConnectDevices"],
198 [91, "AuthConnectDeniedRequests"],
199 [92, "QueuedTurnInputIndexJsonValid"],
200 [93, "ProviderTurnAttemptIndex"],
201 [94, "TurnItemMessageOrdinalIndex"],
202 [95, "TurnItemInlineWorkOutput"],
203 [96, "RepairStaleTitleRegeneration"],
204 [97, "IssueTrackerBindings"],
205 [98, "ActiveShellIndexIndexes"],
206 [99, "BackgroundRecoveryIndexes"],
207 [100, "TurnStartReadIndexes"],
208 [101, "DelegatedTaskReadIndexes"],
209 [102, "ThreadItemCounts"],
210 ]);
211 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
212 );
213
214 it.effect("refuses unexpected later migrations without modifying their history", () =>
215 Effect.gen(function* () {
216 const sql = yield* SqlClient.SqlClient;
217 yield* seedPreview;
218 yield* sql`INSERT INTO effect_sql_migrations (migration_id, name) VALUES (54, 'UnknownFork')`;
219 const history = yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`;
220 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
221 assert.deepStrictEqual(
222 yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`,
223 history,
224 );
225 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
226 );
227 });