Files
clean-architecture-frontend…/tests/release/mixed-replica.test.ts
T

105 lines
3.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { createReleaseMismatchRecovery } from "../../src/platform/release/release-mismatch-recovery.ts";
import {
bootAgainstReplicaSequence,
createReleaseFixture,
memoryStorage,
response,
runBootSession,
type ReleaseName,
type Surface,
} from "./release-fixture.ts";
/** A deterministic load balancer that alternates replicas request by request. */
function alternatingLoadBalancer(replicas: readonly ReleaseName[], offset = 0) {
let request = offset;
return (): readonly Readonly<{ kind: Surface; release: ReleaseName }>[] =>
(["html", "config", "manifest"] as const).map((kind) =>
response(kind, replicas[request++ % replicas.length] as ReleaseName),
);
}
describe("mixed-replica routing during a rolling release", () => {
it("boots when every response comes from R10", async () => {
const result = await bootAgainstReplicaSequence([
response("html", "R10"),
response("config", "R10"),
response("manifest", "R10"),
]);
expect(result).toMatchObject({ state: "booted", applicationMounted: true, release: "release-R10" });
});
it("boots when every response comes from R11", async () => {
const result = await bootAgainstReplicaSequence([
response("html", "R11"),
response("config", "R11"),
response("manifest", "R11"),
]);
expect(result).toMatchObject({ state: "booted", applicationMounted: true, release: "release-R11" });
});
it("fails closed when HTML and runtime config come from different releases", async () => {
const result = await bootAgainstReplicaSequence([
response("html", "R10"),
response("config", "R11"),
response("manifest", "R10"),
]);
expect(result.state).toBe("release-mismatch");
expect(result.applicationMounted).toBe(false);
});
it("fails closed when the manifest comes from a different release", async () => {
const result = await bootAgainstReplicaSequence([
response("html", "R10"),
response("config", "R10"),
response("manifest", "R11"),
]);
expect(result).toMatchObject({ state: "release-mismatch", applicationMounted: false });
});
it("never produces a silently mixed boot under an alternating load balancer", async () => {
for (let offset = 0; offset < 6; offset += 1) {
const next = alternatingLoadBalancer(["R10", "R11"], offset);
for (let boot = 0; boot < 10; boot += 1) {
const sequence = next();
const releases = new Set(sequence.map((entry) => entry.release));
const result = await bootAgainstReplicaSequence(sequence);
if (releases.size > 1) {
expect(result.applicationMounted).toBe(false);
} else {
expect(result.state).toBe("booted");
}
}
}
});
it("recovers a stale HTML document with exactly one reload once the rollout converges", async () => {
const deployment = createReleaseFixture(["R10", "R11"]);
const sequences = [
[response("html", "R10"), response("config", "R11"), response("manifest", "R11")],
[response("html", "R11"), response("config", "R11"), response("manifest", "R11")],
];
let boot = 0;
const session = await runBootSession(
() => sequences[Math.min(boot++, sequences.length - 1)] ?? [],
createReleaseMismatchRecovery(memoryStorage()),
deployment,
);
expect(session.reloads).toBe(1);
expect(session.final).toMatchObject({ state: "booted", release: "release-R11" });
});
it("cannot reload-loop when replicas stay permanently mixed", async () => {
const storage = memoryStorage();
const session = await runBootSession(
() => [response("html", "R10"), response("config", "R11"), response("manifest", "R10")],
createReleaseMismatchRecovery(storage),
);
expect(session.reloads).toBe(1);
expect(session.final).toMatchObject({ state: "release-mismatch", applicationMounted: false });
expect(storage.size()).toBe(1);
});
});