Chunk recovery kept its own guard: marker keyed by the release pair, no TTL, trusted write().ok. Round 1 fixed the boot guard (key-independent marker within a TTL, read-back check) but the chunk copy still reloaded on every failure when replicas alternated A->B, B->A. platform/release gains createReloadOnceGuard (the round-1 boot rule, now with a refusal reason); createReleaseMismatchRecovery wraps it with unchanged boot semantics (marker within TTL blocks regardless of key, markCoherentBoot clears), and decideChunkRecovery uses it over its own CHUNK_RELOAD_GUARD key, keeping its reason vocabulary. Application -> platform is allowed by .dependency-cruiser.json; platform may not import application, so the guard lives in platform. Behavior change (chunk only): a second reload within the TTL is refused even for a different release pair, and a marker that does not read back is guard-write-failed. tests/component/context-route-gateway.test.tsx used a preferences stub that dropped writes; it now persists them. Verified: tests/unit/chunk-recovery.test.ts characterization (read/write failure, same pair, invariants) green before the change; alternating pair test RED before, green after. Boot tests unchanged and green; FE-RB-002 drill PASS; test:integration PASS; all gates PASS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
3.5 KiB
TypeScript
117 lines
3.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { decideChunkRecovery } from "../../src/application/use-cases/decide-chunk-recovery.ts";
|
|
import type { StoragePort } from "../../src/application/ports/storage-port.ts";
|
|
|
|
function memoryStorage(): StoragePort {
|
|
let value: unknown;
|
|
return {
|
|
read: () => ({ ok: true, value }),
|
|
write: (_key: string, next: unknown) => {
|
|
value = next;
|
|
return { ok: true };
|
|
},
|
|
remove: () => ({ ok: true }),
|
|
};
|
|
}
|
|
|
|
describe("controlled chunk recovery", () => {
|
|
it("records the release pair before allowing one reload", () => {
|
|
const storage = memoryStorage();
|
|
const input: Parameters<typeof decideChunkRecovery>[0] = {
|
|
failureKind: "CHUNK_LOAD_FAILURE",
|
|
manifestLoaded: true,
|
|
currentBuildId: "build-a",
|
|
currentReleaseId: "release-a",
|
|
activeBuildId: "build-b",
|
|
activeReleaseId: "release-b",
|
|
storage,
|
|
};
|
|
expect(decideChunkRecovery(input)).toEqual({
|
|
action: "reload-once",
|
|
releasePair: "build-a/release-a->build-b/release-b",
|
|
});
|
|
expect(decideChunkRecovery(input)).toEqual({
|
|
action: "support",
|
|
reason: "reload-already-attempted",
|
|
});
|
|
});
|
|
|
|
const storageError = {
|
|
kind: "STORAGE_UNAVAILABLE" as const,
|
|
code: "STORAGE_UNAVAILABLE",
|
|
retryable: false,
|
|
operationId: "STORAGE",
|
|
attemptCount: 1,
|
|
userMessageKey: "error.storage_unavailable",
|
|
action: "none" as const,
|
|
};
|
|
const mismatch = {
|
|
failureKind: "CHUNK_LOAD_FAILURE",
|
|
manifestLoaded: true,
|
|
currentBuildId: "build-a",
|
|
currentReleaseId: "release-a",
|
|
activeBuildId: "build-b",
|
|
activeReleaseId: "release-b",
|
|
} as const;
|
|
|
|
it("fails closed when the guard cannot be read", () => {
|
|
const storage: StoragePort = {
|
|
...memoryStorage(),
|
|
read: () => ({ ok: false, error: storageError }),
|
|
};
|
|
expect(decideChunkRecovery({ ...mismatch, storage })).toEqual({
|
|
action: "support",
|
|
reason: "guard-read-failed",
|
|
});
|
|
});
|
|
|
|
it("fails closed when the guard cannot be written", () => {
|
|
const storage: StoragePort = {
|
|
...memoryStorage(),
|
|
write: () => ({ ok: false, error: storageError }),
|
|
};
|
|
expect(decideChunkRecovery({ ...mismatch, storage })).toEqual({
|
|
action: "support",
|
|
reason: "guard-write-failed",
|
|
});
|
|
});
|
|
|
|
it("allows no second reload when replicas alternate the release pair (Q-6.9-9)", () => {
|
|
const storage = memoryStorage();
|
|
expect(decideChunkRecovery({ ...mismatch, storage }).action).toBe("reload-once");
|
|
// After the reload the page is on build-b while a replica still serves A.
|
|
expect(
|
|
decideChunkRecovery({
|
|
...mismatch,
|
|
currentBuildId: "build-b",
|
|
currentReleaseId: "release-b",
|
|
activeBuildId: "build-a",
|
|
activeReleaseId: "release-a",
|
|
storage,
|
|
}),
|
|
).toEqual({ action: "support", reason: "reload-already-attempted" });
|
|
});
|
|
|
|
it.each([
|
|
[{ failureKind: "SERVER_FAILURE" }, "not-recoverable"],
|
|
[{ manifestLoaded: false }, "manifest-unavailable"],
|
|
[
|
|
{ activeBuildId: "build-a", activeReleaseId: "release-a" },
|
|
"same-release",
|
|
],
|
|
])("stops when a recovery invariant fails: %#", (override, reason) => {
|
|
const result = decideChunkRecovery({
|
|
failureKind: "DEPLOY_MISMATCH",
|
|
manifestLoaded: true,
|
|
currentBuildId: "build-a",
|
|
currentReleaseId: "release-a",
|
|
activeBuildId: "build-b",
|
|
activeReleaseId: "release-b",
|
|
storage: memoryStorage(),
|
|
...override,
|
|
});
|
|
expect(result).toEqual({ action: "support", reason });
|
|
});
|
|
});
|