|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodeFS from "node:fs"; |
| 3 | +import * as NodeOS from "node:os"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | +import * as NodeSqlite from "node:sqlite"; |
| 6 | + |
| 7 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 8 | +import { assert, it } from "@effect/vitest"; |
| 9 | +import { ThreadId } from "@t3tools/contracts"; |
| 10 | +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as Layer from "effect/Layer"; |
| 13 | +import * as SqlClient from "effect/unstable/sql/SqlClient"; |
| 14 | + |
| 15 | +import { deriveServerPaths, layer as configLayer, layerTest } from "../config.ts"; |
| 16 | +import { ServerConfig } from "../config.ts"; |
| 17 | +import { layerConfig } from "./Layers/Sqlite.ts"; |
| 18 | +import { runMigrations } from "./Migrations.ts"; |
| 19 | +import { initializeV2Database } from "./initializeV2Database.ts"; |
| 20 | +import { layer as eventStoreLayer } from "../orchestration-v2/EventStore.ts"; |
| 21 | +import { layer as projectionStoreLayer } from "../orchestration-v2/ProjectionStore.ts"; |
| 22 | +import { layer as eventSinkLayer } from "../orchestration-v2/EventSink.ts"; |
| 23 | +import { |
| 24 | + LegacyV1ThreadImporter, |
| 25 | + layer as importerLayer, |
| 26 | +} from "../orchestration-v2/LegacyV1ThreadImporter.ts"; |
| 27 | + |
| 28 | +it.effect( |
| 29 | + "snapshots V1, imports transcripts lazily, and preserves both databases across switches", |
| 30 | + () => { |
| 31 | + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-v1-v2-")); |
| 32 | + const sourcePath = NodePath.join(directory, "state.sqlite"); |
| 33 | + const destinationPath = NodePath.join(directory, "statev2.sqlite"); |
| 34 | + const threadId = ThreadId.make("legacy-thread"); |
| 35 | + const seed = Effect.gen(function* () { |
| 36 | + const sql = yield* SqlClient.SqlClient; |
| 37 | + yield* runMigrations({ toMigrationInclusive: 52 }); |
| 38 | + yield* sql`INSERT INTO projection_projects (project_id, title, workspace_root, scripts_json, created_at, updated_at) |
| 39 | + VALUES ('project', 'Project', '/tmp/project', '[]', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z')`; |
| 40 | + yield* sql`INSERT INTO projection_threads (thread_id, project_id, title, model_selection_json, runtime_mode, interaction_mode, created_at, updated_at) |
| 41 | + VALUES (${threadId}, 'project', 'V1 thread', '{"instanceId":"codex","model":"gpt-5.4"}', 'full-access', 'default', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z')`; |
| 42 | + for (let index = 0; index < 6; index++) { |
| 43 | + yield* sql`INSERT INTO projection_thread_messages (message_id, thread_id, role, text, is_streaming, created_at, updated_at) |
| 44 | + VALUES (${`message-${index}`}, ${threadId}, ${index % 2 ? "assistant" : "user"}, ${`Text ${index}`}, 0, ${`2026-01-0${index + 1}T00:00:00.000Z`}, ${`2026-01-0${index + 1}T00:00:00.000Z`})`; |
| 45 | + } |
| 46 | + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: sourcePath }))); |
| 47 | + |
| 48 | + return Effect.gen(function* () { |
| 49 | + yield* seed; |
| 50 | + const original = NodeFS.readFileSync(sourcePath); |
| 51 | + const config = yield* ServerConfig; |
| 52 | + const databaseLayer = layerConfig.pipe( |
| 53 | + Layer.provide(configLayer({ ...config, dbPath: destinationPath })), |
| 54 | + ); |
| 55 | + const stores = Layer.mergeAll(eventStoreLayer, projectionStoreLayer).pipe( |
| 56 | + Layer.provideMerge(databaseLayer), |
| 57 | + ); |
| 58 | + const sink = eventSinkLayer.pipe(Layer.provide(stores)); |
| 59 | + const importer = importerLayer.pipe(Layer.provideMerge(Layer.mergeAll(stores, sink))); |
| 60 | + yield* Effect.gen(function* () { |
| 61 | + const sql = yield* SqlClient.SqlClient; |
| 62 | + const legacy = yield* LegacyV1ThreadImporter; |
| 63 | + yield* legacy.reconcileShells; |
| 64 | + const pending = |
| 65 | + yield* sql`SELECT transcript_imported_at FROM orchestration_v2_legacy_imports`; |
| 66 | + assert.equal(pending[0]?.transcript_imported_at, null); |
| 67 | + yield* legacy.ensureTranscript(threadId); |
| 68 | + const imported = |
| 69 | + yield* sql`SELECT imported_message_count, transcript_imported_at FROM orchestration_v2_legacy_imports`; |
| 70 | + assert.equal(imported[0]?.imported_message_count, 6); |
| 71 | + assert.isNotNull(imported[0]?.transcript_imported_at); |
| 72 | + yield* sql`CREATE TABLE v2_work (text TEXT)`; |
| 73 | + yield* sql`INSERT INTO v2_work VALUES ('Keep V2 work')`; |
| 74 | + }).pipe(Effect.provide(importer)); |
| 75 | + assert.deepEqual(NodeFS.readFileSync(sourcePath), original); |
| 76 | + const v1 = new NodeSqlite.DatabaseSync(sourcePath); |
| 77 | + try { |
| 78 | + assert.equal( |
| 79 | + v1.prepare("SELECT MAX(migration_id) AS id FROM effect_sql_migrations").get()?.id, |
| 80 | + 52, |
| 81 | + ); |
| 82 | + assert.equal( |
| 83 | + v1 |
| 84 | + .prepare( |
| 85 | + "SELECT count(*) AS count FROM sqlite_master WHERE name = 'orchestration_v2_legacy_imports'", |
| 86 | + ) |
| 87 | + .get()?.count, |
| 88 | + 0, |
| 89 | + ); |
| 90 | + v1.exec("UPDATE projection_threads SET title = 'Continued in V1'"); |
| 91 | + } finally { |
| 92 | + v1.close(); |
| 93 | + } |
| 94 | + yield* Effect.gen(function* () { |
| 95 | + const sql = yield* SqlClient.SqlClient; |
| 96 | + assert.equal((yield* sql`SELECT text FROM v2_work`)[0]?.text, "Keep V2 work"); |
| 97 | + assert.equal((yield* sql`SELECT title FROM projection_threads`)[0]?.title, "V1 thread"); |
| 98 | + }).pipe(Effect.provide(databaseLayer)); |
| 99 | + }).pipe( |
| 100 | + Effect.provide(layerTest(directory, directory).pipe(Layer.provideMerge(NodeServices.layer))), |
| 101 | + Effect.ensuring( |
| 102 | + Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true })), |
| 103 | + ), |
| 104 | + ); |
| 105 | + }, |
| 106 | +); |
| 107 | + |
| 108 | +it.effect("includes committed WAL data and does not publish a failed snapshot", () => { |
| 109 | + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-v2-snapshot-")); |
| 110 | + const sourcePath = NodePath.join(directory, "state.sqlite"); |
| 111 | + const destinationPath = NodePath.join(directory, "statev2.sqlite"); |
| 112 | + return Effect.gen(function* () { |
| 113 | + NodeFS.writeFileSync(sourcePath, "invalid SQLite"); |
| 114 | + assert.isTrue((yield* Effect.result(initializeV2Database(destinationPath)))._tag === "Failure"); |
| 115 | + assert.isFalse(NodeFS.existsSync(destinationPath)); |
| 116 | + NodeFS.unlinkSync(sourcePath); |
| 117 | + const source = new NodeSqlite.DatabaseSync(sourcePath); |
| 118 | + try { |
| 119 | + source.exec( |
| 120 | + "PRAGMA journal_mode=WAL; CREATE TABLE messages(text TEXT); INSERT INTO messages VALUES ('committed'); BEGIN; INSERT INTO messages VALUES ('uncommitted');", |
| 121 | + ); |
| 122 | + yield* initializeV2Database(destinationPath); |
| 123 | + const copy = new NodeSqlite.DatabaseSync(destinationPath, { readOnly: true }); |
| 124 | + try { |
| 125 | + assert.deepEqual( |
| 126 | + copy |
| 127 | + .prepare("SELECT text FROM messages") |
| 128 | + .all() |
| 129 | + .map((row) => row.text), |
| 130 | + ["committed"], |
| 131 | + ); |
| 132 | + } finally { |
| 133 | + copy.close(); |
| 134 | + } |
| 135 | + source.exec("ROLLBACK"); |
| 136 | + } finally { |
| 137 | + source.close(); |
| 138 | + } |
| 139 | + }).pipe( |
| 140 | + Effect.provide(NodeServices.layer), |
| 141 | + Effect.ensuring(Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true }))), |
| 142 | + ); |
| 143 | +}); |
| 144 | + |
| 145 | +it.effect("uses statev2.sqlite for default and explicit development paths", () => |
| 146 | + Effect.gen(function* () { |
| 147 | + for (const devUrl of [undefined, new URL("http://localhost:5173")]) { |
| 148 | + for (const baseDirIsExplicit of [false, true]) { |
| 149 | + const paths = yield* deriveServerPaths("/tmp/t3", devUrl, { baseDirIsExplicit }); |
| 150 | + assert.equal(NodePath.basename(paths.dbPath), "statev2.sqlite"); |
| 151 | + assert.equal(paths.settingsPath, NodePath.join(paths.stateDir, "settings.json")); |
| 152 | + } |
| 153 | + } |
| 154 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 155 | +); |
| 156 | + |
| 157 | +it.effect("starts fresh without V1 and never imports over existing V2 state", () => { |
| 158 | + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-v2-fresh-")); |
| 159 | + const destinationPath = NodePath.join(directory, "userdata", "statev2.sqlite"); |
| 160 | + return Effect.gen(function* () { |
| 161 | + const config = yield* ServerConfig; |
| 162 | + const database = layerConfig.pipe( |
| 163 | + Layer.provide(configLayer({ ...config, dbPath: destinationPath })), |
| 164 | + ); |
| 165 | + yield* Effect.gen(function* () { |
| 166 | + const sql = yield* SqlClient.SqlClient; |
| 167 | + yield* sql`CREATE TABLE v2_work (text TEXT)`; |
| 168 | + yield* sql`INSERT INTO v2_work VALUES ('fresh V2 work')`; |
| 169 | + }).pipe(Effect.provide(database)); |
| 170 | + const sourcePath = NodePath.join(NodePath.dirname(destinationPath), "state.sqlite"); |
| 171 | + assert.isFalse(NodeFS.existsSync(sourcePath)); |
| 172 | + NodeFS.writeFileSync(sourcePath, "This source must never be opened once V2 exists"); |
| 173 | + yield* initializeV2Database(destinationPath); |
| 174 | + yield* Effect.gen(function* () { |
| 175 | + const sql = yield* SqlClient.SqlClient; |
| 176 | + assert.equal((yield* sql`SELECT text FROM v2_work`)[0]?.text, "fresh V2 work"); |
| 177 | + }).pipe(Effect.provide(database)); |
| 178 | + }).pipe( |
| 179 | + Effect.provide(layerTest(directory, directory).pipe(Layer.provideMerge(NodeServices.layer))), |
| 180 | + Effect.ensuring(Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true }))), |
| 181 | + ); |
| 182 | +}); |
0 commit comments