문제 V2에서 옮긴 두 조립 검사가 한 사례씩만 테스트된다. 쿠키 쓰기 CSRF 검사는 POST와 x-xsrf-token 한 쌍만 본다. 그래서 `!isRead`를 `method === "POST"`로 좁히거나, CSRF 헤더 이름 대신 "필수 헤더가 하나라도 있으면"으로 바꾸거나, x-csrf-token 허용을 지워도 테스트가 통과한다. realtime snapshot 바인딩(GET + SAFE·IDEMPOTENT)은 거절 사례가 없어 method 조건이나 retrySemantics 조건을 지워도 통과한다. 고른 방법 - rest-profile-contract: POST·PUT·PATCH·DELETE마다 CSRF 없는 쿠키 프로필과 x-tenant-context만 요구하는 쿠키 프로필을 거절하고, x-csrf-token과 x-xsrf-token을 각각 받는지 본다. GET·HEAD 쿠키 읽기는 CSRF 없이 된다. - realtime-stream-registry: 6 method × 4 retrySemantics 표를 CURSOR와 SNAPSHOT_ONLY 두 복구 모드로 돈다. 받는 조합은 GET×SAFE, GET×IDEMPOTENT 뿐이다. 행은 조립한 descriptor를 projectApiOperationRows로 뽑은 것에서 method와 retrySemantics만 바꾼다. 투영 칸 이름이 바뀌어도 이 표가 같이 깨진다. FE-REG-API 행이 없는 id도 거절한다. 버린 대안 - 손으로 쓴 행(fixture.ts)을 그대로 쓰기: 투영과 realtime 검사가 따로 바뀌는 것을 못 잡는다. 위험·한계 - 검사 코드는 바꾸지 않았다. 테스트만 더했다. - resumable upload 제어 요청의 쿠키 POST는 descriptor 밖이라 이 검사 범위가 아니다(영역 01). 검증 - 변이 7종을 하나씩 넣고 두 테스트 파일을 돌렸다. 모두 실패한다: M2(POST만), M3(아무 필수 헤더), H19(x-csrf-token 허용 삭제), M9(method 조건 삭제), M9b(method·retry 조건 삭제), S2(retry 조건 삭제), H21-PUT(GET 또는 PUT 허용). - 변이 없이 두 파일 47건 통과. check:types, eslint 통과. Refs: docs/reviews/2026-11-03-maturity-review/07-contracts.md 07-02 Refs: docs/reviews/2026-11-03-maturity-review/10-tests-fast-layers.md 10-03, 10-04 Refs: docs/reviews/2026-11-03-maturity-review/04-adapters-realtime.md 04-03
329 lines
9.4 KiB
TypeScript
329 lines
9.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { projectApiOperationRows } from "../../../src/contracts/api-operations.ts";
|
|
import {
|
|
composeContractContributions,
|
|
type HttpMethod,
|
|
type RetrySemantics,
|
|
} from "../../../src/contracts/external-contract-runtime.ts";
|
|
import {
|
|
createRealtimePolicyRegistry,
|
|
defineEventTypeId,
|
|
defineStreamRegistrationId,
|
|
REALTIME_HARD_LIMITS,
|
|
type RealtimeLimits,
|
|
type RealtimeStreamRegistration,
|
|
} from "../../../src/contracts/realtime-streams.ts";
|
|
import {
|
|
EFFECT_PROFILE_ID,
|
|
ENDPOINT_ID,
|
|
EVENT_TYPE,
|
|
KILL_SWITCH_ID,
|
|
STREAM_ID,
|
|
TEST_API_OPERATIONS,
|
|
TEST_LIMITS,
|
|
TEST_MAPPERS,
|
|
TEST_SCHEMA_CODECS,
|
|
createTestRealtimeRegistry,
|
|
} from "./fixture.ts";
|
|
import { TEST_CONTRACT_CONTRIBUTION } from "../../helpers/external-contract-fixture.ts";
|
|
|
|
describe("realtime policy registry", () => {
|
|
it("deep-snapshots registrations and resolves only stream-owned event types", () => {
|
|
const baseline = createTestRealtimeRegistry();
|
|
const sourceEventIds = [EVENT_TYPE];
|
|
const sourceLimits = { ...TEST_LIMITS };
|
|
const sourceStream: RealtimeStreamRegistration = {
|
|
...baseline.listStreams()[0]!,
|
|
eventTypeIds: sourceEventIds,
|
|
limits: sourceLimits,
|
|
};
|
|
const sourceEventType = {
|
|
...baseline.listEventTypes()[0]!,
|
|
};
|
|
const registry = createRealtimePolicyRegistry({
|
|
streams: [sourceStream],
|
|
eventTypes: [sourceEventType],
|
|
bindings: bindings(),
|
|
});
|
|
|
|
sourceEventIds[0] = defineEventTypeId("MUTATED_EVENT");
|
|
sourceLimits.maxQueueEvents = 1;
|
|
sourceEventType.owner = "mutated-owner";
|
|
|
|
const installed = registry.findStream(STREAM_ID);
|
|
expect(installed).toMatchObject({
|
|
id: STREAM_ID,
|
|
eventTypeIds: [EVENT_TYPE],
|
|
limits: { maxQueueEvents: TEST_LIMITS.maxQueueEvents },
|
|
});
|
|
expect(registry.findEventType(EVENT_TYPE)?.owner).toBe(
|
|
"sample-owner",
|
|
);
|
|
expect(
|
|
registry.findStreamEventType(STREAM_ID, EVENT_TYPE)?.id,
|
|
).toBe(EVENT_TYPE);
|
|
expect(
|
|
registry.findStreamEventType(STREAM_ID, "MUTATED_EVENT"),
|
|
).toBeUndefined();
|
|
expect(Object.isFrozen(installed)).toBe(true);
|
|
expect(Object.isFrozen(installed?.eventTypeIds)).toBe(true);
|
|
expect(Object.isFrozen(installed?.limits)).toBe(true);
|
|
});
|
|
|
|
it("rejects duplicates, unknown references and extra keys", () => {
|
|
const baseline = createTestRealtimeRegistry();
|
|
const stream = baseline.listStreams()[0]!;
|
|
const eventType = baseline.listEventTypes()[0]!;
|
|
|
|
expect(() =>
|
|
createRealtimePolicyRegistry({
|
|
streams: [stream, stream],
|
|
eventTypes: [eventType],
|
|
bindings: bindings(),
|
|
}),
|
|
).toThrow("stream is duplicated");
|
|
|
|
expect(() =>
|
|
createRealtimePolicyRegistry({
|
|
streams: [
|
|
{
|
|
...stream,
|
|
eventTypeIds: [defineEventTypeId("UNKNOWN_EVENT")],
|
|
},
|
|
],
|
|
eventTypes: [eventType],
|
|
bindings: bindings(),
|
|
}),
|
|
).toThrow("stream registration is invalid");
|
|
|
|
expect(() =>
|
|
createRealtimePolicyRegistry({
|
|
streams: [
|
|
{
|
|
...stream,
|
|
unregisteredOverride: true,
|
|
} as RealtimeStreamRegistration,
|
|
],
|
|
eventTypes: [eventType],
|
|
bindings: bindings(),
|
|
}),
|
|
).toThrow("stream registration is invalid");
|
|
|
|
expect(() =>
|
|
createTestRealtimeRegistry({
|
|
eventTypeMutator: (candidate) => ({
|
|
...candidate,
|
|
mapperId: "MissingMapper",
|
|
}),
|
|
}),
|
|
).toThrow("event type registration is invalid");
|
|
});
|
|
|
|
it("closes state-bearing, ephemeral and recovery contradictions", () => {
|
|
expect(() =>
|
|
createTestRealtimeRegistry({
|
|
recovery: {
|
|
mode: "SNAPSHOT_ONLY",
|
|
snapshotOperationId: "GET_REFERENCE_REALTIME_SNAPSHOT",
|
|
checkpointCodecId: "ReferenceRealtimeCheckpoint",
|
|
barrier: "NONE",
|
|
},
|
|
delivery: "INVALIDATION_HINT",
|
|
stateBearing: true,
|
|
}),
|
|
).toThrow("recovery contract is contradictory");
|
|
|
|
expect(() =>
|
|
createTestRealtimeRegistry({
|
|
recovery: {
|
|
mode: "SESSION_REBUILD",
|
|
rebuildInputId: "referenceRealtimeRebuild",
|
|
},
|
|
delivery: "EPHEMERAL",
|
|
stateBearing: true,
|
|
}),
|
|
).toThrow("recovery contract is contradictory");
|
|
|
|
expect(() =>
|
|
createTestRealtimeRegistry({
|
|
recovery: {
|
|
mode: "SESSION_REBUILD",
|
|
rebuildInputId: "referenceRealtimeRebuild",
|
|
},
|
|
delivery: "INVALIDATION_HINT",
|
|
stateBearing: false,
|
|
}),
|
|
).toThrow("recovery contract is contradictory");
|
|
|
|
const registry = createTestRealtimeRegistry({
|
|
recovery: {
|
|
mode: "SESSION_REBUILD",
|
|
rebuildInputId: "referenceRealtimeRebuild",
|
|
},
|
|
delivery: "EPHEMERAL",
|
|
stateBearing: false,
|
|
});
|
|
expect(registry.findStream(STREAM_ID)?.recovery.mode).toBe(
|
|
"SESSION_REBUILD",
|
|
);
|
|
});
|
|
|
|
it("allows only reductions of implementation ceilings", () => {
|
|
expect(() =>
|
|
createTestRealtimeRegistry({
|
|
limits: {
|
|
...TEST_LIMITS,
|
|
maxQueueEvents: REALTIME_HARD_LIMITS.maxQueueEvents + 1,
|
|
},
|
|
}),
|
|
).toThrow("exceed implementation ceilings");
|
|
|
|
expect(() =>
|
|
createTestRealtimeRegistry({
|
|
limits: {
|
|
...TEST_LIMITS,
|
|
maxQueueBytes: TEST_LIMITS.maxEventBytes - 1,
|
|
},
|
|
}),
|
|
).toThrow("cannot hold one event");
|
|
|
|
const strictLimits: RealtimeLimits = {
|
|
...TEST_LIMITS,
|
|
maxQueueEvents: 1,
|
|
maxDedupeEntries: 1,
|
|
};
|
|
expect(
|
|
createTestRealtimeRegistry({ limits: strictLimits }).findStream(
|
|
STREAM_ID,
|
|
)?.limits,
|
|
).toMatchObject({
|
|
maxQueueEvents: 1,
|
|
maxDedupeEntries: 1,
|
|
});
|
|
});
|
|
|
|
it("issues only bounded closed registry IDs", () => {
|
|
expect(defineStreamRegistrationId("VALID_STREAM")).toBe(
|
|
"VALID_STREAM",
|
|
);
|
|
expect(() => defineStreamRegistrationId("arbitrary-channel")).toThrow(
|
|
"stream ID is invalid",
|
|
);
|
|
expect(() => defineEventTypeId("X")).toThrow(
|
|
"event type ID is invalid",
|
|
);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 1103/04-03, 07-02, 10-04. Recovery sends the snapshot operation on every
|
|
* reconnect, so it must be a read that may be sent again: GET with SAFE or
|
|
* IDEMPOTENT. A HEAD carries no checkpoint body. The row is the FE-REG-API
|
|
* projection of a composed descriptor, so a renamed projected field breaks
|
|
* this table too.
|
|
*/
|
|
describe("realtime snapshot operation binding", () => {
|
|
const projected = projectApiOperationRows(
|
|
composeContractContributions([TEST_CONTRACT_CONTRIBUTION]).contributions,
|
|
);
|
|
const methods: readonly HttpMethod[] = [
|
|
"GET",
|
|
"HEAD",
|
|
"POST",
|
|
"PUT",
|
|
"PATCH",
|
|
"DELETE",
|
|
];
|
|
const retries: readonly RetrySemantics[] = [
|
|
"SAFE",
|
|
"IDEMPOTENT",
|
|
"KEYED",
|
|
"NEVER",
|
|
];
|
|
const register = (
|
|
recovery: "CURSOR" | "SNAPSHOT_ONLY",
|
|
apiOperations: Readonly<Record<string, unknown>>,
|
|
) => {
|
|
const baseline = createTestRealtimeRegistry();
|
|
const stream = baseline.listStreams()[0]!;
|
|
return createRealtimePolicyRegistry({
|
|
streams: [
|
|
{
|
|
...stream,
|
|
recovery:
|
|
recovery === "CURSOR"
|
|
? stream.recovery
|
|
: {
|
|
mode: "SNAPSHOT_ONLY",
|
|
snapshotOperationId: "GET_REFERENCE_REALTIME_SNAPSHOT",
|
|
checkpointCodecId: "ReferenceRealtimeCheckpoint",
|
|
barrier: "CONNECT_BUFFER",
|
|
},
|
|
},
|
|
],
|
|
eventTypes: [baseline.listEventTypes()[0]!],
|
|
bindings: {
|
|
...bindings(),
|
|
apiOperations: apiOperations as ReturnType<
|
|
typeof bindings
|
|
>["apiOperations"],
|
|
},
|
|
});
|
|
};
|
|
const snapshotRow = (method: HttpMethod, retrySemantics: RetrySemantics) => ({
|
|
GET_REFERENCE_REALTIME_SNAPSHOT: {
|
|
...projected.TEST_LIST_ENTITIES!,
|
|
operationId: "GET_REFERENCE_REALTIME_SNAPSHOT",
|
|
method,
|
|
retrySemantics,
|
|
},
|
|
});
|
|
const cases = methods.flatMap((method) =>
|
|
retries.map((retrySemantics) => ({
|
|
method,
|
|
retrySemantics,
|
|
accepted:
|
|
method === "GET" &&
|
|
(retrySemantics === "SAFE" || retrySemantics === "IDEMPOTENT"),
|
|
})),
|
|
);
|
|
|
|
it.each(cases)(
|
|
"$method $retrySemantics as the snapshot operation: accepted=$accepted",
|
|
({ method, retrySemantics, accepted }) => {
|
|
for (const recovery of ["CURSOR", "SNAPSHOT_ONLY"] as const) {
|
|
const install = () =>
|
|
register(recovery, snapshotRow(method, retrySemantics));
|
|
if (accepted) {
|
|
expect(install().findStream(STREAM_ID)?.recovery.mode).toBe(recovery);
|
|
} else {
|
|
expect(install, recovery).toThrow(
|
|
recovery === "CURSOR"
|
|
? "Realtime cursor recovery profile is invalid."
|
|
: "Realtime snapshot recovery profile is invalid.",
|
|
);
|
|
}
|
|
}
|
|
},
|
|
);
|
|
|
|
it("refuses a snapshot operation id with no FE-REG-API row", () => {
|
|
expect(() => register("CURSOR", {})).toThrow(
|
|
"Realtime cursor recovery profile is invalid.",
|
|
);
|
|
});
|
|
});
|
|
|
|
function bindings() {
|
|
return {
|
|
schemaCodecs: TEST_SCHEMA_CODECS,
|
|
mappers: TEST_MAPPERS,
|
|
apiOperations: TEST_API_OPERATIONS,
|
|
endpointIds: [ENDPOINT_ID],
|
|
effectProfileIds: [EFFECT_PROFILE_ID],
|
|
killSwitchIds: [KILL_SWITCH_ID],
|
|
rebuildInputIds: ["referenceRealtimeRebuild"],
|
|
} as const;
|
|
}
|