|
1 | 1 | import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + createMigrationDivergenceConsentToken, |
| 4 | + serializeMigrationDivergenceConsentChallenge, |
| 5 | + serializeMigrationSchemaTooNewStartupBlock, |
| 6 | + type MigrationDivergenceConsentChallenge, |
| 7 | + type MigrationSchemaTooNewStartupBlock, |
| 8 | +} from "@forkara/shared/migrationRecovery"; |
2 | 9 |
|
3 | 10 | import { BackendStartupBlockDetector } from "./backendStartupBlock"; |
4 | 11 |
|
| 12 | +const divergenceChallengeWithoutToken: Omit<MigrationDivergenceConsentChallenge, "consentToken"> = { |
| 13 | + version: 1, |
| 14 | + databasePath: "/data/state.sqlite", |
| 15 | + backupDirectory: "/data/state.sqlite.backups", |
| 16 | + sourceVersion: "imported-v90-from90", |
| 17 | + targetVersion: 96, |
| 18 | + firstDivergedId: 90, |
| 19 | + expectedName: "ProjectionThreadMessageTextSegments", |
| 20 | + recordedName: "AuthSessionRenewalPolicy", |
| 21 | + highWaterMark: 90, |
| 22 | + lineageFingerprint: "a".repeat(64), |
| 23 | +}; |
| 24 | +const divergenceChallenge: MigrationDivergenceConsentChallenge = { |
| 25 | + ...divergenceChallengeWithoutToken, |
| 26 | + consentToken: createMigrationDivergenceConsentToken(divergenceChallengeWithoutToken), |
| 27 | +}; |
| 28 | + |
| 29 | +const schemaTooNewBlock: MigrationSchemaTooNewStartupBlock = { |
| 30 | + version: 1, |
| 31 | + databasePath: "/data/state.sqlite", |
| 32 | + databaseMigrationId: 97, |
| 33 | + latestSupportedMigrationId: 96, |
| 34 | + recovery: { |
| 35 | + kind: "restore-available", |
| 36 | + backupPath: "/data/state.sqlite.backups/exact.sqlite", |
| 37 | + provenancePath: "/data/state.sqlite.migration-backup.json", |
| 38 | + backupMigrationId: 90, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
5 | 42 | describe("BackendStartupBlockDetector", () => { |
6 | 43 | it("recognizes a live database owner across output chunks", () => { |
7 | 44 | const detector = new BackendStartupBlockDetector(); |
@@ -30,6 +67,133 @@ describe("BackendStartupBlockDetector", () => { |
30 | 67 | expect(detector.read()).toEqual({ kind: "migration-recovery-required" }); |
31 | 68 | }); |
32 | 69 |
|
| 70 | + it("extracts a divergence consent challenge across output chunks", () => { |
| 71 | + const detector = new BackendStartupBlockDetector(); |
| 72 | + const serialized = serializeMigrationDivergenceConsentChallenge(divergenceChallenge); |
| 73 | + |
| 74 | + detector.push(`MigrationDivergenceConsentRequiredError: blocked\n${serialized.slice(0, 80)}`); |
| 75 | + detector.push(`${serialized.slice(80)}\n at migrate`); |
| 76 | + |
| 77 | + expect(detector.read()).toEqual({ |
| 78 | + kind: "migration-divergence-consent-required", |
| 79 | + challenge: divergenceChallenge, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it("selects the final authoritative challenge from drained output", () => { |
| 84 | + const detector = new BackendStartupBlockDetector(); |
| 85 | + const injectedWithoutToken = { |
| 86 | + ...divergenceChallengeWithoutToken, |
| 87 | + databasePath: "/injected/state.sqlite", |
| 88 | + backupDirectory: "/injected/state.sqlite.backups", |
| 89 | + recordedName: "InjectedMigration", |
| 90 | + }; |
| 91 | + const injected = { |
| 92 | + ...injectedWithoutToken, |
| 93 | + consentToken: createMigrationDivergenceConsentToken(injectedWithoutToken), |
| 94 | + }; |
| 95 | + |
| 96 | + detector.push(`${serializeMigrationDivergenceConsentChallenge(injected)}\n`); |
| 97 | + detector.push(`${serializeMigrationDivergenceConsentChallenge(divergenceChallenge)}\n`); |
| 98 | + |
| 99 | + expect(detector.read()).toEqual({ |
| 100 | + kind: "migration-divergence-consent-required", |
| 101 | + challenge: divergenceChallenge, |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("decodes split UTF-8 independently for stdout and stderr", () => { |
| 106 | + const detector = new BackendStartupBlockDetector(); |
| 107 | + const challengeWithoutToken = { |
| 108 | + ...divergenceChallengeWithoutToken, |
| 109 | + recordedName: "Migrazione cafè", |
| 110 | + }; |
| 111 | + const challenge = { |
| 112 | + ...challengeWithoutToken, |
| 113 | + consentToken: createMigrationDivergenceConsentToken(challengeWithoutToken), |
| 114 | + }; |
| 115 | + const bytes = Buffer.from(`${serializeMigrationDivergenceConsentChallenge(challenge)}\n`); |
| 116 | + const splitAt = bytes.indexOf(Buffer.from("è")) + 1; |
| 117 | + |
| 118 | + detector.push(Buffer.from("🙂").subarray(0, 1), "stdout"); |
| 119 | + detector.push(bytes.subarray(0, splitAt), "stderr"); |
| 120 | + detector.push(bytes.subarray(splitAt), "stderr"); |
| 121 | + detector.end("stderr"); |
| 122 | + |
| 123 | + expect(detector.read()).toEqual({ |
| 124 | + kind: "migration-divergence-consent-required", |
| 125 | + challenge, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it("preserves a consent challenge larger than the general output buffer", () => { |
| 130 | + const detector = new BackendStartupBlockDetector(); |
| 131 | + const challengeWithoutToken = { |
| 132 | + ...divergenceChallengeWithoutToken, |
| 133 | + recordedName: |
| 134 | + `prefix-${serializeMigrationDivergenceConsentChallenge(divergenceChallenge)}-` + |
| 135 | + "x".repeat(20_000), |
| 136 | + }; |
| 137 | + const challenge = { |
| 138 | + ...challengeWithoutToken, |
| 139 | + consentToken: createMigrationDivergenceConsentToken(challengeWithoutToken), |
| 140 | + }; |
| 141 | + const serialized = serializeMigrationDivergenceConsentChallenge(challenge); |
| 142 | + |
| 143 | + detector.push(`MigrationDivergenceConsentRequiredError: blocked\n${serialized.slice(0, 100)}`); |
| 144 | + detector.push(serialized.slice(100)); |
| 145 | + |
| 146 | + expect(detector.read()).toEqual({ |
| 147 | + kind: "migration-divergence-consent-required", |
| 148 | + challenge, |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it("recognizes a migration bundle identity mismatch", () => { |
| 153 | + const detector = new BackendStartupBlockDetector(); |
| 154 | + |
| 155 | + detector.push( |
| 156 | + "MigrationRuntimeIdentityMismatchError: desktop and server bundles were built from different migration sources\n", |
| 157 | + ); |
| 158 | + |
| 159 | + expect(detector.read()).toEqual({ kind: "migration-runtime-identity-mismatch" }); |
| 160 | + }); |
| 161 | + |
| 162 | + it("extracts schema-too-new recovery before crash supervision retries", () => { |
| 163 | + const detector = new BackendStartupBlockDetector(); |
| 164 | + const serialized = serializeMigrationSchemaTooNewStartupBlock(schemaTooNewBlock); |
| 165 | + |
| 166 | + detector.push(`MigrationSchemaTooNewError: blocked\n${serialized.slice(0, 70)}`); |
| 167 | + detector.push(`${serialized.slice(70)}\n at migrate`); |
| 168 | + |
| 169 | + expect(detector.read()).toEqual({ kind: "migration-schema-too-new", block: schemaTooNewBlock }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("fails closed instead of retrying a malformed structured block", () => { |
| 173 | + const detector = new BackendStartupBlockDetector(); |
| 174 | + |
| 175 | + detector.push("FORKARA_MIGRATION_SCHEMA_TOO_NEW={not-json}\n"); |
| 176 | + |
| 177 | + expect(detector.read()).toEqual({ kind: "migration-startup-block-invalid" }); |
| 178 | + }); |
| 179 | + |
| 180 | + it("rejects a valid structured block before parsing when it exceeds the safety cap", () => { |
| 181 | + const detector = new BackendStartupBlockDetector(); |
| 182 | + const oversizedBlock: MigrationSchemaTooNewStartupBlock = { |
| 183 | + ...schemaTooNewBlock, |
| 184 | + recovery: { |
| 185 | + kind: "restore-available", |
| 186 | + backupPath: `/data/${"x".repeat(1_050_000)}.sqlite`, |
| 187 | + provenancePath: "/data/state.sqlite.migration-backup.json", |
| 188 | + backupMigrationId: 90, |
| 189 | + }, |
| 190 | + }; |
| 191 | + |
| 192 | + detector.push(serializeMigrationSchemaTooNewStartupBlock(oversizedBlock)); |
| 193 | + |
| 194 | + expect(detector.read()).toEqual({ kind: "migration-startup-block-invalid" }); |
| 195 | + }); |
| 196 | + |
33 | 197 | it("ignores unrelated startup failures", () => { |
34 | 198 | const detector = new BackendStartupBlockDetector(); |
35 | 199 |
|
|
0 commit comments