|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; |
| 5 | +import * as Path from "effect/Path"; |
| 6 | +import * as SqlClient from "effect/unstable/sql/SqlClient"; |
| 7 | + |
| 8 | +import { runMigrations } from "../src/persistence/Migrations.ts"; |
| 9 | +import * as NodeSqliteClient from "../src/persistence/NodeSqliteClient.ts"; |
| 10 | +import { runMigrateDevDb } from "./migrate-dev-db.ts"; |
| 11 | + |
| 12 | +const withDatabase = <A, E>( |
| 13 | + databasePath: string, |
| 14 | + effect: Effect.Effect<A, E, SqlClient.SqlClient>, |
| 15 | +) => effect.pipe(Effect.provide(NodeSqliteClient.layer({ filename: databasePath }))); |
| 16 | + |
| 17 | +/** A migrated source db with one thread per lifecycle state. Only |
| 18 | + * `stopped-thread` qualifies for the clone. */ |
| 19 | +const createFixtureSource = Effect.fn("createMigrateDevDbFixtureSource")(function* ( |
| 20 | + baseDir: string, |
| 21 | +) { |
| 22 | + const fs = yield* FileSystem.FileSystem; |
| 23 | + const path = yield* Path.Path; |
| 24 | + const stateDir = path.join(baseDir, "userdata"); |
| 25 | + const databasePath = path.join(stateDir, "state.sqlite"); |
| 26 | + yield* fs.makeDirectory(stateDir, { recursive: true }); |
| 27 | + yield* withDatabase( |
| 28 | + databasePath, |
| 29 | + Effect.gen(function* () { |
| 30 | + const sql = yield* SqlClient.SqlClient; |
| 31 | + yield* runMigrations(); |
| 32 | + // The real shared db carries this column from a branch build without a |
| 33 | + // matching migration; reproduce that drift so the filter is exercised. |
| 34 | + yield* sql`ALTER TABLE projection_threads ADD COLUMN monitor_json TEXT`; |
| 35 | + |
| 36 | + yield* sql`INSERT INTO projection_projects |
| 37 | + (project_id, title, workspace_root, scripts_json, created_at, updated_at, deleted_at) |
| 38 | + VALUES |
| 39 | + ('project-kept', 'Kept', '/tmp/kept', '[]', '2026-08-01', '2026-08-01', NULL), |
| 40 | + ('project-deleted', 'Deleted', '/tmp/deleted', '[]', '2026-08-01', '2026-08-02', '2026-08-02')`; |
| 41 | + |
| 42 | + const threads = [ |
| 43 | + ["stopped-thread", "project-kept", "stopped", null, null], |
| 44 | + ["running-thread", "project-kept", "running", null, null], |
| 45 | + ["settled-thread", "project-kept", "stopped", "2026-08-01", null], |
| 46 | + ["monitored-thread", "project-kept", "stopped", null, '{"kind":"pr"}'], |
| 47 | + ["deleted-project-thread", "project-deleted", "stopped", null, null], |
| 48 | + ] as const; |
| 49 | + for (const [threadId, projectId, status, settledAt, monitorJson] of threads) { |
| 50 | + yield* sql`INSERT INTO projection_threads |
| 51 | + (thread_id, project_id, title, created_at, updated_at, settled_at, monitor_json) |
| 52 | + VALUES (${threadId}, ${projectId}, ${threadId}, '2026-08-01', '2026-08-01', ${settledAt}, ${monitorJson})`; |
| 53 | + yield* sql`INSERT INTO projection_thread_sessions (thread_id, status, updated_at) |
| 54 | + VALUES (${threadId}, ${status}, '2026-08-01')`; |
| 55 | + yield* sql`INSERT INTO orchestration_events |
| 56 | + (event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at, actor_kind, payload_json, metadata_json) |
| 57 | + VALUES (${`event-${threadId}`}, 'thread', ${threadId}, 0, 'thread.created', '2026-08-01', 'user', '{}', '{}')`; |
| 58 | + } |
| 59 | + yield* sql`INSERT INTO auth_sessions (session_id, subject, scopes, method, issued_at, expires_at) |
| 60 | + VALUES ('session-1', 'user', '[]', 'pairing', '2026-08-01', '2027-08-01')`; |
| 61 | + }), |
| 62 | + ); |
| 63 | + return databasePath; |
| 64 | +}); |
| 65 | + |
| 66 | +it.layer(NodeServices.layer)("migrate-dev-db", (it) => { |
| 67 | + it.effect("keeps only stopped threads from live projects and clears auth state", () => |
| 68 | + Effect.gen(function* () { |
| 69 | + const fs = yield* FileSystem.FileSystem; |
| 70 | + const path = yield* Path.Path; |
| 71 | + const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-src-" }); |
| 72 | + const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-dest-" }); |
| 73 | + const source = yield* createFixtureSource(sourceDir); |
| 74 | + |
| 75 | + const result = yield* runMigrateDevDb( |
| 76 | + { baseDir: destDir, source, projects: 5, threadsPerProject: 10 }, |
| 77 | + { sharedHome: sourceDir }, |
| 78 | + ); |
| 79 | + |
| 80 | + assert.equal(result.databasePath, path.join(destDir, "userdata", "state.sqlite")); |
| 81 | + const kept = yield* withDatabase( |
| 82 | + result.databasePath, |
| 83 | + Effect.gen(function* () { |
| 84 | + const sql = yield* SqlClient.SqlClient; |
| 85 | + const threads = yield* sql<{ thread_id: string }>` |
| 86 | + SELECT thread_id FROM projection_threads ORDER BY thread_id`; |
| 87 | + const events = yield* sql<{ stream_id: string }>` |
| 88 | + SELECT stream_id FROM orchestration_events`; |
| 89 | + const [auth] = yield* sql<{ count: number }>` |
| 90 | + SELECT COUNT(*) AS count FROM auth_sessions`; |
| 91 | + return { threads, events, authCount: auth?.count ?? 0 }; |
| 92 | + }), |
| 93 | + ); |
| 94 | + assert.deepStrictEqual( |
| 95 | + kept.threads.map((row) => row.thread_id), |
| 96 | + ["stopped-thread"], |
| 97 | + ); |
| 98 | + assert.deepStrictEqual( |
| 99 | + kept.events.map((row) => row.stream_id), |
| 100 | + ["stopped-thread"], |
| 101 | + ); |
| 102 | + assert.equal(kept.authCount, 0); |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + it.effect("fails loudly on a migration slot collision", () => |
| 107 | + Effect.gen(function* () { |
| 108 | + const fs = yield* FileSystem.FileSystem; |
| 109 | + const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-slot-" }); |
| 110 | + const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-slot-dest-" }); |
| 111 | + const source = yield* createFixtureSource(sourceDir); |
| 112 | + // Simulate another branch having claimed slot 1 first: the id is |
| 113 | + // recorded, so this checkout's migration 1 silently never runs. |
| 114 | + yield* withDatabase( |
| 115 | + source, |
| 116 | + Effect.gen(function* () { |
| 117 | + const sql = yield* SqlClient.SqlClient; |
| 118 | + yield* sql`UPDATE effect_sql_migrations |
| 119 | + SET name = 'SomebodyElsesMigration' WHERE migration_id = 1`; |
| 120 | + }), |
| 121 | + ); |
| 122 | + |
| 123 | + const error = yield* runMigrateDevDb( |
| 124 | + { baseDir: destDir, source, projects: 5, threadsPerProject: 10 }, |
| 125 | + { sharedHome: sourceDir }, |
| 126 | + ).pipe(Effect.flip); |
| 127 | + assert.equal(error._tag, "MigrateDevDbSlotCollisionError"); |
| 128 | + if (error._tag === "MigrateDevDbSlotCollisionError") { |
| 129 | + assert.equal(error.slot, 1); |
| 130 | + assert.equal(error.appliedName, "SomebodyElsesMigration"); |
| 131 | + } |
| 132 | + }), |
| 133 | + ); |
| 134 | + |
| 135 | + it.effect("refuses while a dev server holds the destination", () => |
| 136 | + Effect.gen(function* () { |
| 137 | + const fs = yield* FileSystem.FileSystem; |
| 138 | + const path = yield* Path.Path; |
| 139 | + const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-busy-" }); |
| 140 | + const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-busy-dest-" }); |
| 141 | + const source = yield* createFixtureSource(sourceDir); |
| 142 | + // This test process stands in for a live dev server. |
| 143 | + const stateDir = path.join(destDir, "userdata"); |
| 144 | + yield* fs.makeDirectory(stateDir, { recursive: true }); |
| 145 | + yield* fs.writeFileString( |
| 146 | + path.join(stateDir, "server-runtime.json"), |
| 147 | + `{"version":1,"pid":${process.pid}}`, |
| 148 | + ); |
| 149 | + |
| 150 | + const error = yield* runMigrateDevDb( |
| 151 | + { baseDir: destDir, source, projects: 5, threadsPerProject: 10 }, |
| 152 | + { sharedHome: sourceDir }, |
| 153 | + ).pipe(Effect.flip); |
| 154 | + assert.equal(error._tag, "MigrateDevDbServerRunningError"); |
| 155 | + if (error._tag === "MigrateDevDbServerRunningError") { |
| 156 | + assert.equal(error.pid, process.pid); |
| 157 | + } |
| 158 | + }), |
| 159 | + ); |
| 160 | + |
| 161 | + it.effect("refuses a source that resolves to a destination path", () => |
| 162 | + Effect.gen(function* () { |
| 163 | + const fs = yield* FileSystem.FileSystem; |
| 164 | + const path = yield* Path.Path; |
| 165 | + const sharedDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-overlap-" }); |
| 166 | + const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-overlap-dest-" }); |
| 167 | + // A leftover snapshot from a prior failed run, passed as --source: it |
| 168 | + // must not be deleted before it is read. |
| 169 | + const leftoverSnapshot = path.join(destDir, "userdata", "state.sqlite.migrate-dev-db-tmp"); |
| 170 | + yield* fs.makeDirectory(path.dirname(leftoverSnapshot), { recursive: true }); |
| 171 | + yield* fs.writeFileString(leftoverSnapshot, "not a real db"); |
| 172 | + |
| 173 | + const error = yield* runMigrateDevDb( |
| 174 | + { baseDir: destDir, source: leftoverSnapshot, projects: 5, threadsPerProject: 10 }, |
| 175 | + { sharedHome: sharedDir }, |
| 176 | + ).pipe(Effect.flip); |
| 177 | + assert.equal(error._tag, "MigrateDevDbSourceIsDestinationError"); |
| 178 | + assert.equal(yield* fs.exists(leftoverSnapshot), true); |
| 179 | + }), |
| 180 | + ); |
| 181 | + |
| 182 | + it.effect("refuses to rebuild the shared home", () => |
| 183 | + Effect.gen(function* () { |
| 184 | + const fs = yield* FileSystem.FileSystem; |
| 185 | + const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-shared-" }); |
| 186 | + const source = yield* createFixtureSource(sourceDir); |
| 187 | + |
| 188 | + const error = yield* runMigrateDevDb( |
| 189 | + { baseDir: sourceDir, source, projects: 5, threadsPerProject: 10 }, |
| 190 | + { sharedHome: sourceDir }, |
| 191 | + ).pipe(Effect.flip); |
| 192 | + assert.equal(error._tag, "MigrateDevDbSharedHomeError"); |
| 193 | + }), |
| 194 | + ); |
| 195 | +}); |
0 commit comments