|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import { OrchestrationV2AppThreadJson } from "@t3tools/contracts"; |
| 3 | +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Schema from "effect/Schema"; |
| 6 | +import * as SqlClient from "effect/sql/SqlClient"; |
| 7 | + |
| 8 | +import { runMigrations } from "../Migrations.ts"; |
| 9 | + |
| 10 | +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); |
| 11 | +const createdAt = "2026-09-01T00:00:00.000Z"; |
| 12 | +const decodeThreadPayload = Schema.decodeUnknownEffect( |
| 13 | + Schema.fromJsonString(OrchestrationV2AppThreadJson), |
| 14 | +); |
| 15 | + |
| 16 | +layer("077_RepairBooleanSettledOverride", (it) => { |
| 17 | + it.effect("canonicalizes boolean settled overrides so recovery can decode them", () => |
| 18 | + Effect.gen(function* () { |
| 19 | + const sql = yield* SqlClient.SqlClient; |
| 20 | + yield* runMigrations({ toMigrationInclusive: 76 }); |
| 21 | + const overrides: ReadonlyArray<readonly [string, unknown]> = [ |
| 22 | + ["true", true], |
| 23 | + ["false", false], |
| 24 | + ["settled", "settled"], |
| 25 | + ["active", "active"], |
| 26 | + ["null", null], |
| 27 | + ["absent", undefined], |
| 28 | + ]; |
| 29 | + for (const [id, settledOverride] of overrides) { |
| 30 | + const payload = { |
| 31 | + id, |
| 32 | + projectId: "project-1", |
| 33 | + title: id, |
| 34 | + providerInstanceId: "codex", |
| 35 | + modelSelection: { instanceId: "codex", model: "gpt-5.4" }, |
| 36 | + runtimeMode: "full-access", |
| 37 | + interactionMode: "default", |
| 38 | + createdBy: "user", |
| 39 | + creationSource: "web", |
| 40 | + branch: null, |
| 41 | + worktreePath: null, |
| 42 | + activeProviderThreadId: null, |
| 43 | + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id }, |
| 44 | + forkedFrom: null, |
| 45 | + createdAt, |
| 46 | + updatedAt: createdAt, |
| 47 | + archivedAt: null, |
| 48 | + deletedAt: null, |
| 49 | + settledOverride, |
| 50 | + settledAt: settledOverride === true ? createdAt : null, |
| 51 | + }; |
| 52 | + yield* sql` |
| 53 | + INSERT INTO orchestration_v2_projection_threads ( |
| 54 | + thread_id, project_id, title, default_provider, runtime_mode, interaction_mode, |
| 55 | + created_at, updated_at, payload_json |
| 56 | + ) VALUES ( |
| 57 | + ${id}, 'project-1', ${id}, 'codex', 'full-access', 'default', |
| 58 | + ${createdAt}, ${createdAt}, ${JSON.stringify(payload)} |
| 59 | + ) |
| 60 | + `; |
| 61 | + } |
| 62 | + const rows = sql<{ readonly thread_id: string; readonly payload_json: string }>` |
| 63 | + SELECT thread_id, payload_json FROM orchestration_v2_projection_threads ORDER BY thread_id |
| 64 | + `; |
| 65 | + const before = new Map((yield* rows).map((row) => [row.thread_id, row.payload_json])); |
| 66 | + |
| 67 | + yield* runMigrations({ toMigrationInclusive: 77 }); |
| 68 | + const after = new Map((yield* rows).map((row) => [row.thread_id, row.payload_json])); |
| 69 | + for (const id of ["settled", "active", "null", "absent"]) { |
| 70 | + assert.strictEqual(after.get(id), before.get(id)); |
| 71 | + } |
| 72 | + const repaired = yield* sql` |
| 73 | + SELECT thread_id, |
| 74 | + json_type(payload_json, '$.settledOverride') AS type, |
| 75 | + json_extract(payload_json, '$.settledOverride') AS settled_override, |
| 76 | + json_extract(payload_json, '$.settledAt') AS settled_at |
| 77 | + FROM orchestration_v2_projection_threads WHERE thread_id IN ('true', 'false') |
| 78 | + ORDER BY thread_id |
| 79 | + `; |
| 80 | + assert.deepStrictEqual(repaired, [ |
| 81 | + { thread_id: "false", type: "null", settled_override: null, settled_at: null }, |
| 82 | + { thread_id: "true", type: "text", settled_override: "settled", settled_at: createdAt }, |
| 83 | + ]); |
| 84 | + const thread = yield* decodeThreadPayload(after.get("true")); |
| 85 | + assert.strictEqual(thread.settledOverride, "settled"); |
| 86 | + }), |
| 87 | + ); |
| 88 | +}); |
0 commit comments