| 1 | // Run: tsx src/__tests__/hydrate-replace-surface-failure.test.ts |
| 2 | |
| 3 | import { initialState, reducer, type Item } from "../lib/useController"; |
| 4 | |
| 5 | let passed = 0; |
| 6 | let failed = 0; |
| 7 | function ok(value: boolean, label: string) { |
| 8 | process.stdout.write(` ${value ? "PASS" : "FAIL"} ${label}\n`); |
| 9 | if (value) passed += 1; |
| 10 | else failed += 1; |
| 11 | } |
| 12 | |
| 13 | console.log("\nreplace-surface hydrate failure"); |
| 14 | const sourceItems: Item[] = [{ kind: "user", id: "source-only", text: "SOURCE SESSION SENTINEL" }]; |
| 15 | const source = { ...initialState, items: sourceItems }; |
| 16 | const targetLoading = reducer( |
| 17 | reducer(reducer(source, { type: "hydrate_start", reason: "open-topic" }), { type: "reset" }), |
| 18 | { type: "hydrate_start", reason: "open-topic" }, |
| 19 | ); |
| 20 | ok(targetLoading.items.length === 0, "replace-surface clears source rows before target history settles"); |
| 21 | const targetFailed = reducer(targetLoading, { type: "hydrate_error", reason: "open-topic", error: "history failed" }); |
| 22 | ok(targetFailed.hydrating === false, "target history failure exits the loading state"); |
| 23 | ok(targetFailed.hydrateError === "history failed", "target history failure exposes a retryable error"); |
| 24 | ok(!targetFailed.items.some((item) => item.kind === "user" && item.text === "SOURCE SESSION SENTINEL"), "target history failure never restores source rows"); |
| 25 | |
| 26 | console.log(`\n${passed} passed, ${failed} failed`); |
| 27 | if (failed > 0) process.exit(1); |
| 28 |