Files
tech-log-frontend/tests/unit/realtime/result.test.ts
DongHyeonka c80320dfc7 chore: sync the frontend template from ebea300 to af3d8e6
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 at 8210d48.

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 at
8210d48 first 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)
2026-11-12 08:28:54 +09:00

246 lines
6.8 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
isRealtimeResult,
raceRealtimeTask,
realtimeFailure,
realtimeSuccess,
snapshotRealtimeResult,
timerDeadline,
} from "../../../src/adapters/realtime/result.ts";
function isString(value: unknown): value is string {
return typeof value === "string";
}
describe("realtime result boundary", () => {
it("uses an immutable one-shot snapshot instead of rereading a mutable result", () => {
const source = { ok: true, value: "captured" };
const captured = snapshotRealtimeResult(source, (value): value is string => {
source.value = "changed-during-validation";
return value === "captured";
});
source.value = "changed-after-validation";
expect(captured).toEqual(
realtimeSuccess("captured"),
);
expect(Object.isFrozen(captured)).toBe(true);
expect(isRealtimeResult(source, isString)).toBe(false);
});
it("canonicalizes failure fields before the source can change", () => {
const source = {
ok: false,
error: {
kind: "FORBIDDEN",
operation: "RECOVER",
retryable: false,
},
};
const captured = snapshotRealtimeResult(
source,
(_value): _value is never => false,
);
source.error.kind = "PROVIDER_UNAVAILABLE";
source.error.operation = "CONNECT";
source.error.retryable = true;
expect(captured).toEqual(
realtimeFailure("FORBIDDEN", "RECOVER", false),
);
expect(
captured && !captured.ok
? Object.isFrozen(captured.error)
: false,
).toBe(true);
});
it("rejects accessors without invoking them", () => {
let outerReads = 0;
const outerAccessor = Object.defineProperties(
{},
{
ok: {
enumerable: true,
get() {
outerReads += 1;
return true;
},
},
value: {
enumerable: true,
value: "safe",
},
},
);
let nestedReads = 0;
const nestedAccessor = Object.defineProperties(
{},
{
kind: {
enumerable: true,
get() {
nestedReads += 1;
return "FORBIDDEN";
},
},
operation: {
enumerable: true,
value: "RECOVER",
},
retryable: {
enumerable: true,
value: false,
},
},
);
expect(
snapshotRealtimeResult(outerAccessor, isString),
).toBeNull();
expect(
snapshotRealtimeResult(
{ ok: false, error: nestedAccessor },
(_value): _value is never => false,
),
).toBeNull();
expect(outerReads).toBe(0);
expect(nestedReads).toBe(0);
});
it("consumes proxy fields only from one descriptor snapshot", () => {
const propertyReads: PropertyKey[] = [];
const descriptorReads = new Map<PropertyKey, number>();
const source = new Proxy(
{ ok: true, value: "descriptor-value" },
{
get(_target, key) {
propertyReads.push(key);
return key === "value" ? "get-trap-value" : false;
},
getOwnPropertyDescriptor(target, key) {
descriptorReads.set(
key,
(descriptorReads.get(key) ?? 0) + 1,
);
return Reflect.getOwnPropertyDescriptor(target, key);
},
},
);
expect(snapshotRealtimeResult(source, isString)).toEqual(
realtimeSuccess("descriptor-value"),
);
expect(propertyReads).toEqual([]);
expect(descriptorReads).toEqual(
new Map<PropertyKey, number>([
["ok", 1],
["value", 1],
]),
);
});
it("fails closed when a proxy is revoked", () => {
const revocable = Proxy.revocable(
{ ok: true, value: "safe" },
{},
);
revocable.revoke();
expect(
snapshotRealtimeResult(revocable.proxy, isString),
).toBeNull();
expect(isRealtimeResult(revocable.proxy, isString)).toBe(false);
});
it.each([
{ ok: true, value: "safe", extra: true },
Object.assign(
Object.create({ inherited: true }) as Record<string, unknown>,
{ ok: true, value: "safe" },
),
Object.assign(
{ ok: true, value: "safe" },
{ [Symbol("hidden")]: true },
),
])("rejects extra, inherited and symbol-key shapes", (source) => {
expect(snapshotRealtimeResult(source, isString)).toBeNull();
});
});
describe("realtime abort/deadline race", () => {
const never = () => new Promise<never>(() => undefined);
it("settles with the value, a rejection or a synchronous throw", async () => {
await expect(raceRealtimeTask(() => 7)).resolves.toEqual({
kind: "VALUE",
value: 7,
});
await expect(
raceRealtimeTask(() => Promise.reject(new Error("no"))),
).resolves.toEqual({ kind: "REJECTED" });
await expect(
raceRealtimeTask(() => {
throw new Error("sync");
}),
).resolves.toEqual({ kind: "REJECTED" });
});
it("aborts without starting, or mid-flight with cleanup", async () => {
const task = vi.fn(never);
await expect(
raceRealtimeTask(task, AbortSignal.abort()),
).resolves.toEqual({ kind: "ABORTED" });
expect(task).not.toHaveBeenCalled();
const caller = new AbortController();
const remove = vi.spyOn(caller.signal, "removeEventListener");
let lease: AbortSignal | undefined;
const running = raceRealtimeTask((signal) => {
lease = signal;
return never();
}, caller.signal);
caller.abort();
await expect(running).resolves.toEqual({ kind: "ABORTED" });
expect(lease?.aborted).toBe(true);
expect(remove).toHaveBeenCalledWith("abort", expect.any(Function));
});
it("times out on the deadline and reports a broken deadline", async () => {
let deadlineSignal: AbortSignal | undefined;
await expect(
raceRealtimeTask(never, undefined, (signal) => {
deadlineSignal = signal;
return Promise.resolve();
}),
).resolves.toEqual({ kind: "TIMEOUT" });
expect(deadlineSignal?.aborted).toBe(true);
await expect(
raceRealtimeTask(never, undefined, () => Promise.reject(new Error())),
).resolves.toEqual({ kind: "CLOCK_FAILED" });
// A deadline that throws still lets the task start (e.g. a cancel).
const task = vi.fn(never);
await expect(
raceRealtimeTask(task, undefined, () => {
throw new Error("clock");
}),
).resolves.toEqual({ kind: "CLOCK_FAILED" });
expect(task).toHaveBeenCalledOnce();
});
it("clears a host-timer deadline when the task wins", async () => {
vi.useFakeTimers();
try {
await expect(
raceRealtimeTask(() => "done", undefined, timerDeadline(1_000)),
).resolves.toEqual({ kind: "VALUE", value: "done" });
expect(vi.getTimerCount()).toBe(0);
} finally {
vi.useRealTimers();
}
});
});