Carries 359 template commits, tag v0.2.0 plus one documentation commit: the generator-built feature index with its app, bounded-context and feature declarations, the src/platform kernel and scale tooling, the 2026-09-24 src review fixes, and the 2026-09-25 maturity review round with its CHANGELOG [보안]/[계약] rows. 691 of the 824 template paths are applied, 85 of them merged with product changes; 127 are not carried. Product identity is unchanged: name, version 0.1.0, routes, design and the TechLog domain. Three decisions worth naming. TechLog stays composed directly, as the last two syncs recorded; its directory carries a NOT_INSTALLED marker that says so, and every installed-* composer takes the template's projection with TechLog added on top. The reference sample is no longer installed: the template now installs a feature only through feature-module.ts and context-routes.ts, which load the screens this product deleted, so its three operations, six schemas and one topic leave the registries, recorded as registry change evidence. And AppRouter mounts the platform and TechLog routes only; the template's bounded-context route gateway waits for a generator-installed feature. Required fixes: readSubject on the BFF session owner (without it every HTTP Studio session would close as integration-failed), zod through the facade in nine TechLog files, noUnusedLocals, and public/config.json written out to match the local profile. Derived baselines were recomputed after reproducing both sides' values: CI contract 101 commands / 106 references / 141 artifacts, gate shape c9adcf85; registry approval 2dc6c3a3; the runtime config schema baseline equals the schema at8210d48. Verified here: types, lint, architecture, adapter inventory, CI contract, i18n, design system, diagnostics, browser security, dev release manifest, TechLog contract and build pass; 220 component, 155 integration, 15 contract, 418 TechLog, 43 reference-feature, 18 recipe and 46 release-coherence tests pass, and 2381 of 2383 unit tests. Reproduced at8210d48first and unchanged: check:registries (byte-identical), test:runtime-schema, verify:documentation. Brought by the template and escalated rather than suppressed: check:request-fanout on TechLog's cursor pagination, check:remediation-ledger (fails upstream too), the shared-chunk hash test, and the registry-dependent sample removal and second-feature tests. Playwright suites were not run. Full record, the five open questions and the follow-ups in docs/operations/template-sync-2026-09-29.md. (cherry picked from commit b2170cf265dea45dc9cc05d3c61e627e97f31cdb)
459 lines
16 KiB
TypeScript
459 lines
16 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
createDemoSessionAdapter,
|
|
createExternalAuthSessionAdapter,
|
|
} from "../../src/adapters/auth/external-session-adapter.ts";
|
|
import {
|
|
createServerStateScopeRuntime,
|
|
type ServerStateScopeDependencies,
|
|
} from "../../src/adapters/query-cache/server-state-scope-runtime.ts";
|
|
import type { SessionState } from "../../src/application/ports/auth-session-port.ts";
|
|
import type { DiagnosticRecordInput } from "../../src/contracts/diagnostics.ts";
|
|
import type { ClientScopeLifecycleEvent } from "../../src/contracts/server-state-scope.ts";
|
|
|
|
describe("server-state session generation runtime", () => {
|
|
it("fences the old generation synchronously and publishes READY after the reset order", async () => {
|
|
let sessionListener: () => void = () => {};
|
|
let completeReset: () => void = () => {};
|
|
const reset = new Promise<void>((resolve) => {
|
|
completeReset = resolve;
|
|
});
|
|
const resetLocal = vi.fn(() => reset);
|
|
const participantOrder: string[] = [];
|
|
let tokenSequence = 0;
|
|
const runtime = createServerStateScopeRuntime({
|
|
session: {
|
|
subscribe(listener) {
|
|
sessionListener = listener;
|
|
return () => {};
|
|
},
|
|
},
|
|
queryInvalidation: {
|
|
resetLocal,
|
|
},
|
|
participants: [
|
|
{
|
|
order: 9,
|
|
label: "realtime",
|
|
close: () => void participantOrder.push("realtime"),
|
|
},
|
|
{
|
|
order: 4,
|
|
label: "admission",
|
|
close: () => void participantOrder.push("admission"),
|
|
},
|
|
],
|
|
tokenFactory: () =>
|
|
`scope-token-${String(tokenSequence++).padStart(8, "0")}`,
|
|
});
|
|
const changed = vi.fn();
|
|
const lifecycle: ClientScopeLifecycleEvent[] = [];
|
|
runtime.subscribe(changed);
|
|
runtime.subscribeLifecycle((event) => lifecycle.push(event));
|
|
const before = runtime.getSnapshot();
|
|
const identity = before.identities.intern({ id: "private" });
|
|
identity.acquire();
|
|
|
|
sessionListener();
|
|
|
|
// §10.6 steps 1-3 are synchronous: the old snapshot is immediately stale,
|
|
// FENCED is published, and subscribers are notified before any await.
|
|
expect(before.isCurrent()).toBe(false);
|
|
expect(before.signal.aborted).toBe(true);
|
|
expect(runtime.getPhase()).toBe("FENCED");
|
|
expect(lifecycle[0]).toEqual({ kind: "FENCED", previousGeneration: 1 });
|
|
expect(changed).toHaveBeenCalledOnce();
|
|
// Nothing may read as current while the scope is fenced.
|
|
expect(runtime.getSnapshot().isCurrent()).toBe(false);
|
|
|
|
await vi.waitFor(() => expect(resetLocal).toHaveBeenCalledOnce());
|
|
// Participants close in §10.6 step order, before the local cache reset.
|
|
expect(participantOrder).toEqual(["admission", "realtime"]);
|
|
|
|
completeReset();
|
|
await vi.waitFor(() => expect(runtime.getPhase()).toBe("READY"));
|
|
const after = runtime.getSnapshot();
|
|
expect(after.generation).toBe(before.generation + 1);
|
|
expect(after.fingerprint).not.toBe(before.fingerprint);
|
|
expect(after.isCurrent()).toBe(true);
|
|
expect(after.signal.aborted).toBe(false);
|
|
expect(after.signal).not.toBe(before.signal);
|
|
expect(before.identities.inspect().closed).toBe(true);
|
|
expect(lifecycle.at(-1)).toMatchObject({ kind: "READY" });
|
|
expect(changed).toHaveBeenCalledTimes(2);
|
|
|
|
runtime.dispose();
|
|
expect(runtime.getPhase()).toBe("DISPOSED");
|
|
expect(after.identities.inspect().closed).toBe(true);
|
|
expect(after.isCurrent()).toBe(false);
|
|
});
|
|
|
|
it("does not let a throwing snapshot subscriber prevent reset", async () => {
|
|
let sessionListener: () => void = () => {};
|
|
const resetLocal = vi.fn(async () => {});
|
|
const runtime = createServerStateScopeRuntime({
|
|
session: {
|
|
subscribe(listener) {
|
|
sessionListener = listener;
|
|
return () => {};
|
|
},
|
|
},
|
|
queryInvalidation: {
|
|
resetLocal,
|
|
},
|
|
tokenFactory: () => "scope-listener-token-0001",
|
|
});
|
|
runtime.subscribe(() => {
|
|
throw new Error("subscriber defect");
|
|
});
|
|
|
|
expect(() => sessionListener()).not.toThrow();
|
|
await vi.waitFor(() => expect(resetLocal).toHaveBeenCalledOnce());
|
|
await vi.waitFor(() => expect(runtime.getPhase()).toBe("READY"));
|
|
});
|
|
|
|
it("remains failed when a mandatory participant cannot close", async () => {
|
|
let sessionListener: () => void = () => {};
|
|
const resetLocal = vi.fn(async () => {});
|
|
const runtime = createServerStateScopeRuntime({
|
|
session: {
|
|
subscribe(listener) {
|
|
sessionListener = listener;
|
|
return () => {};
|
|
},
|
|
},
|
|
queryInvalidation: {
|
|
resetLocal,
|
|
},
|
|
participants: [
|
|
{
|
|
order: 4,
|
|
label: "mandatory-admission",
|
|
close: async () => {
|
|
throw new Error("close failed");
|
|
},
|
|
},
|
|
],
|
|
tokenFactory: () => "scope-participant-token-0001",
|
|
});
|
|
|
|
sessionListener();
|
|
|
|
await vi.waitFor(() => expect(resetLocal).toHaveBeenCalledOnce());
|
|
await vi.waitFor(() =>
|
|
expect(runtime.getPhase() as string).toBe("FAILED"),
|
|
);
|
|
expect(runtime.getSnapshot().isCurrent()).toBe(false);
|
|
});
|
|
|
|
it("remains failed when local cache reset rejects", async () => {
|
|
let sessionListener: () => void = () => {};
|
|
const runtime = createServerStateScopeRuntime({
|
|
session: {
|
|
subscribe(listener) {
|
|
sessionListener = listener;
|
|
return () => {};
|
|
},
|
|
},
|
|
queryInvalidation: {
|
|
resetLocal: async () => {
|
|
throw new Error("reset failed");
|
|
},
|
|
},
|
|
tokenFactory: () => "scope-reset-token-0000001",
|
|
});
|
|
|
|
sessionListener();
|
|
|
|
await vi.waitFor(() =>
|
|
expect(runtime.getPhase() as string).toBe("FAILED"),
|
|
);
|
|
expect(runtime.getSnapshot().isCurrent()).toBe(false);
|
|
});
|
|
|
|
it("activates the next generation after reset and fails closed on activation error", async () => {
|
|
let sessionListener: () => void = () => {};
|
|
const steps: string[] = [];
|
|
const runtime = createServerStateScopeRuntime({
|
|
session: {
|
|
subscribe(listener) {
|
|
sessionListener = listener;
|
|
return () => {};
|
|
},
|
|
},
|
|
queryInvalidation: {
|
|
resetLocal: async () => {
|
|
steps.push("reset");
|
|
},
|
|
},
|
|
activateNextGeneration: async () => {
|
|
steps.push("activate");
|
|
throw new Error("activation failed");
|
|
},
|
|
tokenFactory: () => "scope-activation-token-001",
|
|
} as Parameters<typeof createServerStateScopeRuntime>[0] & {
|
|
activateNextGeneration(): Promise<void>;
|
|
});
|
|
|
|
sessionListener();
|
|
|
|
await vi.waitFor(() => expect(steps).toEqual(["reset", "activate"]));
|
|
await vi.waitFor(() =>
|
|
expect(runtime.getPhase() as string).toBe("FAILED"),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("identity-affecting session changes only", () => {
|
|
function fakeSession(
|
|
initial: Readonly<{ state: SessionState; subject?: string | null }>,
|
|
) {
|
|
let current = initial;
|
|
let listener: () => void = () => {};
|
|
return {
|
|
session: {
|
|
getState: () => current.state,
|
|
getSubject: () => current.subject ?? null,
|
|
subscribe(next: () => void) {
|
|
listener = next;
|
|
return () => {};
|
|
},
|
|
},
|
|
notify(next: Readonly<{ state: SessionState; subject?: string | null }>) {
|
|
current = next;
|
|
listener();
|
|
},
|
|
};
|
|
}
|
|
|
|
function scopeFor(session: ServerStateScopeDependencies["session"]) {
|
|
const resetLocal = vi.fn(async () => {});
|
|
const record = vi.fn<(input: DiagnosticRecordInput) => void>();
|
|
const lifecycle: ClientScopeLifecycleEvent[] = [];
|
|
let tokenSequence = 0;
|
|
const runtime = createServerStateScopeRuntime({
|
|
session,
|
|
queryInvalidation: { resetLocal },
|
|
diagnostics: { record },
|
|
tokenFactory: () =>
|
|
`scope-identity-token-${String(tokenSequence++).padStart(6, "0")}`,
|
|
});
|
|
runtime.subscribeLifecycle((event) => lifecycle.push(event));
|
|
return { runtime, resetLocal, record, lifecycle };
|
|
}
|
|
|
|
const SUBJECT_MISSING_RECORD = {
|
|
level: "warn",
|
|
eventId: "cache.operation.failed",
|
|
context: { operation: "SCOPE_FENCE", reason: "SUBJECT_MISSING" },
|
|
};
|
|
|
|
it.each([
|
|
["a repeated sign-out", { state: "unauthenticated" as const }],
|
|
[
|
|
"a token refresh for the same subject",
|
|
{ state: "authenticated" as const, subject: "user-a" },
|
|
],
|
|
])("does not fence, abort or clear on %s", async (_label, identity) => {
|
|
const fake = fakeSession(identity);
|
|
const { runtime, resetLocal, record, lifecycle } = scopeFor(fake.session);
|
|
const before = runtime.getSnapshot();
|
|
|
|
fake.notify(identity);
|
|
fake.notify({ ...identity });
|
|
await Promise.resolve();
|
|
|
|
expect(lifecycle).toEqual([]);
|
|
expect(before.isCurrent()).toBe(true);
|
|
expect(before.signal.aborted).toBe(false);
|
|
expect(resetLocal).not.toHaveBeenCalled();
|
|
expect(record).not.toHaveBeenCalled();
|
|
expect(runtime.getPhase()).toBe("READY");
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"the state changes",
|
|
{ state: "unauthenticated" as const },
|
|
{ state: "authenticated" as const, subject: "user-a" },
|
|
],
|
|
[
|
|
"the subject changes",
|
|
{ state: "authenticated" as const, subject: "user-a" },
|
|
{ state: "authenticated" as const, subject: "user-b" },
|
|
],
|
|
[
|
|
"the user signs out",
|
|
{ state: "authenticated" as const, subject: "user-a" },
|
|
{ state: "unauthenticated" as const },
|
|
],
|
|
])("still fences when %s", async (_label, initial, next) => {
|
|
const fake = fakeSession(initial);
|
|
const { runtime, resetLocal, record, lifecycle } = scopeFor(fake.session);
|
|
const before = runtime.getSnapshot();
|
|
|
|
fake.notify(next);
|
|
|
|
expect(before.isCurrent()).toBe(false);
|
|
expect(before.signal.aborted).toBe(true);
|
|
expect(lifecycle[0]).toEqual({ kind: "FENCED", previousGeneration: 1 });
|
|
await vi.waitFor(() => expect(runtime.getPhase()).toBe("READY"));
|
|
expect(resetLocal).toHaveBeenCalledOnce();
|
|
expect(record).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([
|
|
["cannot name its subject", null],
|
|
["names an empty subject", ""],
|
|
])(
|
|
"fences and reports SUBJECT_MISSING on every notification when an authenticated session %s",
|
|
async (_label, subject) => {
|
|
const fake = fakeSession({ state: "authenticated", subject });
|
|
const { runtime, resetLocal, record, lifecycle } = scopeFor(fake.session);
|
|
const before = runtime.getSnapshot();
|
|
|
|
fake.notify({ state: "authenticated", subject });
|
|
|
|
expect(before.isCurrent()).toBe(false);
|
|
expect(before.signal.aborted).toBe(true);
|
|
expect(lifecycle[0]).toEqual({ kind: "FENCED", previousGeneration: 1 });
|
|
await vi.waitFor(() => expect(runtime.getPhase()).toBe("READY"));
|
|
expect(record.mock.calls).toEqual([[SUBJECT_MISSING_RECORD]]);
|
|
|
|
fake.notify({ state: "authenticated", subject });
|
|
await vi.waitFor(() => expect(resetLocal).toHaveBeenCalledTimes(2));
|
|
expect(record.mock.calls).toEqual([
|
|
[SUBJECT_MISSING_RECORD],
|
|
[SUBJECT_MISSING_RECORD],
|
|
]);
|
|
|
|
// Signing out is an ordinary fence, not a misbehaving owner.
|
|
fake.notify({ state: "unauthenticated" });
|
|
await vi.waitFor(() => expect(resetLocal).toHaveBeenCalledTimes(3));
|
|
expect(record).toHaveBeenCalledTimes(2);
|
|
},
|
|
);
|
|
|
|
it("keeps fencing if diagnostics throws while reporting SUBJECT_MISSING", async () => {
|
|
const fake = fakeSession({ state: "authenticated", subject: null });
|
|
const runtime = createServerStateScopeRuntime({
|
|
session: fake.session,
|
|
queryInvalidation: { resetLocal: async () => {} },
|
|
diagnostics: {
|
|
record() {
|
|
throw new Error("diagnostics down");
|
|
},
|
|
},
|
|
tokenFactory: () => "scope-diagnostics-token-001",
|
|
});
|
|
const before = runtime.getSnapshot();
|
|
|
|
fake.notify({ state: "authenticated", subject: null });
|
|
|
|
expect(before.isCurrent()).toBe(false);
|
|
expect(runtime.getPhase()).toBe("FENCED");
|
|
});
|
|
|
|
it("does not fence when the demo session re-announces the same user", async () => {
|
|
const demo = createDemoSessionAdapter("authenticated");
|
|
const listeners: Array<() => void> = [];
|
|
const { runtime, resetLocal, lifecycle } = scopeFor({
|
|
...demo,
|
|
subscribe(listener) {
|
|
listeners.push(listener);
|
|
return demo.subscribe(listener);
|
|
},
|
|
});
|
|
const before = runtime.getSnapshot();
|
|
|
|
// A token refresh: the owner notifies while the user stays signed in.
|
|
for (const listener of listeners) listener();
|
|
await Promise.resolve();
|
|
|
|
expect(lifecycle).toEqual([]);
|
|
expect(before.isCurrent()).toBe(true);
|
|
expect(resetLocal).not.toHaveBeenCalled();
|
|
|
|
await demo.signOut();
|
|
expect(before.isCurrent()).toBe(false);
|
|
expect(lifecycle[0]).toEqual({ kind: "FENCED", previousGeneration: 1 });
|
|
await vi.waitFor(() => expect(runtime.getPhase()).toBe("READY"));
|
|
});
|
|
|
|
it("fences and reports every authenticated notification from an external owner whose readSubject returns null", async () => {
|
|
let notify: () => void = () => {};
|
|
const session = createExternalAuthSessionAdapter({
|
|
readState: () => "authenticated",
|
|
readSubject: () => null,
|
|
subscribe(listener) {
|
|
notify = listener;
|
|
return () => {};
|
|
},
|
|
beginSignIn: async () => {},
|
|
signOut: async () => {},
|
|
attachCredential: async () => ({ headers: {} }),
|
|
recoverSession: async () => "no-session",
|
|
notifyUnauthenticated: () => {},
|
|
});
|
|
const { runtime, resetLocal, record, lifecycle } = scopeFor(session);
|
|
const before = runtime.getSnapshot();
|
|
|
|
notify();
|
|
|
|
expect(before.isCurrent()).toBe(false);
|
|
expect(lifecycle[0]).toEqual({ kind: "FENCED", previousGeneration: 1 });
|
|
await vi.waitFor(() => expect(runtime.getPhase()).toBe("READY"));
|
|
expect(resetLocal).toHaveBeenCalledOnce();
|
|
expect(record.mock.calls).toEqual([[SUBJECT_MISSING_RECORD]]);
|
|
});
|
|
|
|
it("keeps the cache when an external owner refreshes a token for the same subject", async () => {
|
|
let notify: () => void = () => {};
|
|
const session = createExternalAuthSessionAdapter({
|
|
readState: () => "authenticated",
|
|
readSubject: () => "user-a",
|
|
subscribe(listener) {
|
|
notify = listener;
|
|
return () => {};
|
|
},
|
|
beginSignIn: async () => {},
|
|
signOut: async () => {},
|
|
attachCredential: async () => ({ headers: {} }),
|
|
recoverSession: async () => "no-session",
|
|
notifyUnauthenticated: () => {},
|
|
});
|
|
const { runtime, resetLocal, record, lifecycle } = scopeFor(session);
|
|
const before = runtime.getSnapshot();
|
|
|
|
notify();
|
|
await Promise.resolve();
|
|
|
|
expect(lifecycle).toEqual([]);
|
|
expect(before.isCurrent()).toBe(true);
|
|
expect(resetLocal).not.toHaveBeenCalled();
|
|
expect(record).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("retries a failed reset on a notification with the same identity", async () => {
|
|
const fake = fakeSession({ state: "unauthenticated" });
|
|
const resetLocal = vi
|
|
.fn<() => Promise<void>>()
|
|
.mockRejectedValueOnce(new Error("reset failed"))
|
|
.mockResolvedValue(undefined);
|
|
const runtime = createServerStateScopeRuntime({
|
|
session: fake.session,
|
|
queryInvalidation: { resetLocal },
|
|
tokenFactory: () => "scope-retry-token-0000001",
|
|
});
|
|
|
|
fake.notify({ state: "authenticated", subject: "user-a" });
|
|
await vi.waitFor(() =>
|
|
expect(runtime.getPhase() as string).toBe("FAILED"),
|
|
);
|
|
fake.notify({ state: "authenticated", subject: "user-a" });
|
|
await vi.waitFor(() => expect(runtime.getPhase()).toBe("READY"));
|
|
expect(resetLocal).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|