test(browser-transfer): pin idempotency key reuse across retries and resumes (01-04)

Mutations that changed the create, part or complete key per call or per
attempt passed every test. Four tests now fail on M1, M6, M7; SHA-256 gains
the two-block FIPS vector and a node:crypto comparison.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:06:32 +09:00
co-authored by Claude Opus 5.5
parent 648fe4e9f7
commit 5cc1d002d2
2 changed files with 195 additions and 1 deletions
+36 -1
View File
@@ -1,3 +1,5 @@
import { createHash } from "node:crypto";
import { describe, expect, it, vi } from "vitest";
import {
@@ -5,7 +7,10 @@ import {
type PresignedDownloadCapability,
} from "../../src/application/ports/browser-transfer/presigned-transfer.ts";
import { createPresignedCapabilityVault } from "../../src/adapters/browser-transfer/presigned/presigned-capability-vault.ts";
import { sha256Hex } from "../../src/adapters/browser-transfer/presigned/incremental-sha256.ts";
import {
createStreamingSha256Verifier,
sha256Hex,
} from "../../src/adapters/browser-transfer/presigned/incremental-sha256.ts";
import {
CHECKSUM_HEADER,
CONTROL_ENDPOINT,
@@ -32,6 +37,36 @@ describe("presigned transfer capability issuance and admission", () => {
expect(sha256Hex(new TextEncoder().encode("abc"))).toBe(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
);
// FIPS 180-2 two-block vector: the padding spills into a second block.
expect(
sha256Hex(
new TextEncoder().encode(
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
),
),
).toBe(
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
);
});
it("agrees with node:crypto across block boundaries and chunk splits", () => {
for (const size of [
...Array.from({ length: 130 }, (_, index) => index),
1023,
1024,
4097,
]) {
const bytes = Uint8Array.from({ length: size }, (_, index) =>
(index * 31 + size) % 256,
);
const expected = createHash("sha256").update(bytes).digest("hex");
expect(sha256Hex(bytes)).toBe(expected);
const verifier = createStreamingSha256Verifier(expected);
for (let offset = 0, step = 1; offset < size; offset += step, step += 7) {
verifier.update(bytes.subarray(offset, offset + step));
}
expect(verifier.verify()).toBe(true);
}
});
it("answers a refused capability envelope with a re-issuable recovery", async () => {
@@ -25,6 +25,7 @@ import {
import {
activeSignal,
createControlHarness,
executorFor,
MemoryCheckpointStore,
noContentionLock,
rangeSource,
@@ -239,3 +240,161 @@ describe("resumable upload capability reissue (01-01)", () => {
expect(puts).toBe(2);
});
});
describe("resumable upload idempotency keys (01-04)", () => {
const unavailable = (
operation: "UPLOAD_SESSION" | "UPLOAD_PART" | "UPLOAD_COMPLETE",
) =>
browserDataFailure("UNAVAILABLE", operation, {
retryable: true,
recovery: "RESUME",
});
it("retries createSession with the same idempotency key", async () => {
const harness = createControlHarness();
const original = harness.control.createSession.bind(harness.control);
const createSession = vi.fn(
async (
input: Parameters<typeof original>[0],
): ReturnType<typeof original> =>
createSession.mock.calls.length === 1
? unavailable("UPLOAD_SESSION")
: await original(input),
);
harness.control.createSession = createSession;
const runtime = createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor: executorFor(harness),
checkpoints: new MemoryCheckpointStore(),
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries: 1 }),
now: () => 1_000,
sleep: async () => {},
});
const result = await runtime.upload(request("upload_key_create_retry"));
expect(result.ok).toBe(true);
expect(createSession).toHaveBeenCalledTimes(2);
expect(createSession.mock.calls[1]?.[0].idempotencyKey).toBe(
createSession.mock.calls[0]?.[0].idempotencyKey,
);
});
it("reuses the part key across issuance retries and a resumed upload()", async () => {
const harness = createControlHarness({
issueCapability: (input, callIndex) =>
callIndex === 1
? unavailable("UPLOAD_PART")
: browserDataSuccess({
capability: Object.freeze({ id: `cap-${callIndex}` }),
uploadBindingSha256: input.uploadBindingSha256,
expiresAtEpochMs: 5_000,
}),
});
const partKeys: string[] = [];
let puts = 0;
const partExecutor: UploadPartExecutor<TestCapability> = {
async uploadPart(input) {
puts += 1;
partKeys.push(input.idempotencyKey);
if (puts === 1) return unavailable("UPLOAD_PART");
return await executorFor(harness).uploadPart(input);
},
};
const checkpoints = new MemoryCheckpointStore();
const runtimeWith = (maxRetries: number) =>
createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor,
checkpoints,
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries }),
now: () => 1_000,
sleep: async () => {},
});
const key = "upload_key_part_retry";
// Issuance fails once and is retried; the PUT then fails with no budget.
const first = await runtimeWith(1).upload(request(key));
expect(first.ok).toBe(false);
const resumed = await runtimeWith(1).upload(request(key));
expect(resumed.ok).toBe(true);
const issuedKeys = harness.issued.mock.calls.map(
([input]) => (input as Readonly<{ idempotencyKey: string }>).idempotencyKey,
);
expect(issuedKeys).toHaveLength(3);
expect(new Set([...issuedKeys, ...partKeys]).size).toBe(1);
});
it("retries complete with the same idempotency key", async () => {
const harness = createControlHarness();
const original = harness.control.complete.bind(harness.control);
const complete = vi.fn(
async (input: Parameters<typeof original>[0]): ReturnType<typeof original> =>
complete.mock.calls.length === 1
? unavailable("UPLOAD_COMPLETE")
: await original(input),
);
harness.control.complete = complete;
const runtime = createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor: executorFor(harness),
checkpoints: new MemoryCheckpointStore(),
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries: 1 }),
now: () => 1_000,
sleep: async () => {},
});
const result = await runtime.upload(request("upload_key_complete_retry"));
expect(result.ok).toBe(true);
expect(complete).toHaveBeenCalledTimes(2);
expect(complete.mock.calls[1]?.[0].idempotencyKey).toBe(
complete.mock.calls[0]?.[0].idempotencyKey,
);
});
it("resends a lost complete from a new upload() with the same key", async () => {
const harness = createControlHarness();
const original = harness.control.complete.bind(harness.control);
const complete = vi.fn(
async (input: Parameters<typeof original>[0]): ReturnType<typeof original> =>
complete.mock.calls.length === 1
? unavailable("UPLOAD_COMPLETE")
: await original(input),
);
harness.control.complete = complete;
const checkpoints = new MemoryCheckpointStore();
const runtime = () =>
createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor: executorFor(harness),
checkpoints,
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries: 0 }),
now: () => 1_000,
sleep: async () => {},
});
const key = "upload_key_complete_lost";
const lost = await runtime().upload(request(key));
expect(lost).toMatchObject({
ok: false,
error: { operation: "UPLOAD_COMPLETE" },
});
const resent = await runtime().upload(request(key));
expect(resent.ok).toBe(true);
expect(complete).toHaveBeenCalledTimes(2);
expect(complete.mock.calls[1]?.[0].idempotencyKey).toBe(
complete.mock.calls[0]?.[0].idempotencyKey,
);
});
});