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