Files
clean-architecture-frontend…/tests/unit/legacy-and-optional-network-remediation.test.ts
T
DongHyeonkaandClaude Opus 5.5 4f7a3be50d fix(contracts): restrict idempotency keys to visible ASCII (R-7.1)
isValidIdempotencyKey only rejected blank values and C0/C1 control
characters. Keys with surrounding whitespace passed and fetch trimmed them
on the wire (two commands could share one key), and non-ASCII keys passed
and made Headers.set throw a TypeError that the client reported as a
retryable NETWORK_UNREACHABLE. The key is now /^[\x21-\x7e]+$/ within the
existing 256-byte bound, the same rule as the realtime HEADER_SAFE_CURSOR.
App-generated keys (randomUUID) and all test fixtures are visible ASCII.

The existing OPT-NET-02 test pinned "키-값" and "ключ" as valid keys; it
encoded the bug and is fixed here: those values plus " order-1 ",
"order-1\t", "key with-space" and a NBSP key are now in the rejected list,
and a UUID key was added to the accepted list.

RED: "OPT-NET-02 shared idempotency key validation > rejects the same values
at intent definition and at admission" failed with '" order-1 ": expected
true to be false' before the fix.
Gates: check:architecture, check:types:app/test/node, lint, test:component,
test:runtime-schema, test:contract pass; test:unit passes except
removal-fixture.test.ts, which needs generated artifacts/release files that
a fresh worktree does not have (unrelated to this change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 12:19:48 +09:00

165 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it, vi } from "vitest";
import { createCursorPaginationRuntime } from "../../src/adapters/query-cache/index.ts";
import {
defineMutationIntent,
isValidIdempotencyKey,
} from "../../src/contracts/mutation-intent.ts";
import type {
CursorPage,
CursorPaginationProfile,
} from "../../src/adapters/query-cache/index.ts";
import type { Result } from "../../src/contracts/result.ts";
const PROFILE: CursorPaginationProfile = Object.freeze({
profileId: "TEST_PAGINATION_V1",
maxPages: 3,
maxTotalItems: 30,
maxEstimatedBytes: 32_768,
maxCursorBytes: 512,
allowSparsePage: false,
});
function page(
items: readonly number[],
nextCursor: string | null,
): CursorPage<number> {
return Object.freeze({
items: Object.freeze([...items]),
nextCursor,
hasMore: nextCursor !== null,
snapshotToken: "snapshot-1",
});
}
/**
* OPT-NET-01. A loader rejection is evidence about the data source. Reporting
* it as `PAGINATION_ABORTED` because a signal merely *exists* erases a real
* network or contract failure and files it under a user decision nobody made.
*/
describe("OPT-NET-01 cursor pagination abort classification", () => {
it("preserves a loader rejection while the signal is still live", async () => {
const controller = new AbortController();
const runtime = createCursorPaginationRuntime<number>({
definitionId: "TEST_PAGINATION",
profile: PROFILE,
loadPage: async () => {
throw new TypeError("upstream exploded");
},
});
await expect(
runtime.loadAll({ signal: controller.signal }),
).rejects.toThrow("upstream exploded");
});
it("keeps the same rejection when no signal is supplied", async () => {
const runtime = createCursorPaginationRuntime<number>({
definitionId: "TEST_PAGINATION",
profile: PROFILE,
loadPage: async () => {
throw new TypeError("upstream exploded");
},
});
await expect(runtime.loadAll({})).rejects.toThrow("upstream exploded");
});
it("classifies a rejection during a real abort as PAGINATION_ABORTED", async () => {
const controller = new AbortController();
const runtime = createCursorPaginationRuntime<number>({
definitionId: "TEST_PAGINATION",
profile: PROFILE,
loadPage: async () => {
controller.abort();
throw new TypeError("cancelled upstream");
},
});
const result = await runtime.loadAll({ signal: controller.signal });
expect(result.ok).toBe(false);
expect(result.ok ? null : result.error.code).toBe("PAGINATION_ABORTED");
});
it("does not admit a page that resolves after the abort", async () => {
const controller = new AbortController();
const loadPage = vi.fn(
async (): Promise<Result<CursorPage<number>>> => {
controller.abort();
await new Promise((resolve) => setTimeout(resolve, 5));
return { ok: true, value: page([1, 2], null) };
},
);
const runtime = createCursorPaginationRuntime<number>({
definitionId: "TEST_PAGINATION",
profile: PROFILE,
loadPage,
});
const result = await runtime.loadAll({ signal: controller.signal });
expect(result.ok).toBe(false);
expect(result.ok ? null : result.error.code).toBe("PAGINATION_ABORTED");
});
});
/**
* OPT-NET-02. One idempotency-key authority. Two validators drift, and the
* looser one becomes the way a control character reaches a request header.
*/
describe("OPT-NET-02 shared idempotency key validation", () => {
const rejected = [
"",
" ",
"key\nwith-newline",
"key\u0000null",
"key\u007fdelete",
"key\u009fc1",
"a".repeat(257),
// R-7.1. fetch strips surrounding whitespace, so " order-1 " would reach
// the wire as "order-1" and collide with another command's key.
" order-1 ",
"order-1\t",
"key with-space",
// R-7.1. A non-ByteString value makes Headers.set throw a TypeError that
// the client reports as a retryable network failure.
"키-값",
"ключ",
"key nbsp",
];
it("rejects the same values at intent definition and at admission", () => {
for (const value of rejected) {
expect(isValidIdempotencyKey(value), JSON.stringify(value)).toBe(false);
expect(() =>
defineMutationIntent({
intentId: "intent-1",
operationId: "OP",
canonicalInputIdentity: "identity",
idempotencyKey: value,
createdAtMonotonicMs: 1,
}),
).toThrow(TypeError);
}
});
it("accepts the bounded visible-ASCII values both sides allow", () => {
for (const value of [
"key-1",
"a".repeat(256),
"0f8fad5b-d9cb-469f-a165-70867728950e",
"!~",
]) {
expect(isValidIdempotencyKey(value)).toBe(true);
expect(
defineMutationIntent({
intentId: "intent-1",
operationId: "OP",
canonicalInputIdentity: "identity",
idempotencyKey: value,
createdAtMonotonicMs: 1,
}).idempotencyKey,
).toBe(value);
}
});
});