diff --git a/tests/unit/browser-rpc/browser-rpc-remediation.test.ts b/tests/unit/browser-rpc/browser-rpc-remediation.test.ts index fa71da42..7167788f 100644 --- a/tests/unit/browser-rpc/browser-rpc-remediation.test.ts +++ b/tests/unit/browser-rpc/browser-rpc-remediation.test.ts @@ -8,6 +8,8 @@ import { type BrowserRpcTransport, } from "../../../src/adapters/browser-rpc/index.ts"; import { decodeServerStreamLease } from "../../../src/adapters/browser-rpc/transport.ts"; +import { systemClock } from "../../../src/adapters/platform/system-clock.ts"; +import type { ClockPort } from "../../../src/application/ports/clock-port.ts"; import { installBrowserRpcContractBindings } from "../../../src/contracts/browser-rpc.ts"; import { MAPPERS, @@ -539,6 +541,18 @@ describe("RPC-01 only a positive receipt confirms physical closure", () => { { label: "never settles", waitClosed: () => new Promise(() => {}) }, ]; + /** + * 1101/10-07. The system clock with every wait capped at 30 ms. Without a + * positive receipt the stream cleanup waits out its 1 s bound; capped, the + * test passes that bound instead of sleeping through it. The operation's own + * 25 ms deadlines are below the cap and unchanged. + */ + const cappedClock: ClockPort = Object.freeze({ + now: () => systemClock.now(), + sleep: (milliseconds: number, signal?: AbortSignal) => + systemClock.sleep(Math.min(milliseconds, 30), signal), + }); + for (const { label, waitClosed } of negativeReceipts) { it(`keeps the operation DRAINING when waitClosed ${label}`, async () => { const probe = leaseProbeWith(waitClosed); @@ -549,6 +563,7 @@ describe("RPC-01 only a positive receipt confirms physical closure", () => { mappers: MAPPERS, requestEncoders: { RpcResourceStreamRequestEncoder: STREAM_ENCODER }, transports: { CONNECT_REFERENCE_STREAM: probe.transport }, + clock: cappedClock, }); const stream = runtime.bindServerStream( "WATCH_RPC_RESOURCES", diff --git a/tests/unit/service-worker-runtime.test.ts b/tests/unit/service-worker-runtime.test.ts index bd57ca01..930496cc 100644 --- a/tests/unit/service-worker-runtime.test.ts +++ b/tests/unit/service-worker-runtime.test.ts @@ -2004,22 +2004,40 @@ describe("SW-01 the activation marker read is bounded in bytes", () => { }); it("bounds a stream that never produces a chunk", async () => { - let cancels = 0; - const stalled = new ReadableStream({ - pull() { - return new Promise(() => {}); - }, - cancel() { - cancels += 1; - }, - }); - const fixture = scopeWithMarkerBody(stalled); + // 1101/10-07. The read deadline is the runtime's own 5 s constant. The + // clock is faked, so the test passes the deadline instead of waiting it + // out; without a deadline the activation never settles and this fails. + vi.useFakeTimers(); + try { + let cancels = 0; + const stalled = new ReadableStream({ + pull() { + return new Promise(() => {}); + }, + cancel() { + cancels += 1; + }, + }); + const fixture = scopeWithMarkerBody(stalled); + let settled = false; - await expect( - runtimeFor(fixture.scope).onActivate(), - ).resolves.toBeTypeOf("number"); - expect(cancels).toBeGreaterThanOrEqual(1); - }, 10_000); + const activation = runtimeFor(fixture.scope) + .onActivate() + .finally(() => { + settled = true; + }); + // One read deadline per marker probe. + for (let probe = 0; probe < 4 && !settled; probe += 1) { + await vi.advanceTimersByTimeAsync(5_000); + } + + expect(settled).toBe(true); + await expect(activation).resolves.toBeTypeOf("number"); + expect(cancels).toBeGreaterThanOrEqual(1); + } finally { + vi.useRealTimers(); + } + }); it("cancels the body it refuses for a declared oversize", async () => { let cancels = 0;