diff --git a/tests/unit/browser-file-content.test.ts b/tests/unit/browser-file-content.test.ts index 9afef765..b44ef84a 100644 --- a/tests/unit/browser-file-content.test.ts +++ b/tests/unit/browser-file-content.test.ts @@ -646,6 +646,75 @@ describe("browser file content policy", () => { expect(vault.retainedByteLength).toBe(0); }); + // 2026-11-03 02-03. Logout disposes the runtime while a call is in flight. + // The lease registry still hands out leases after dispose, so each + // consumer's own re-check is the only thing that keeps a URL from outliving + // the session. + it("creates no object URL for a preview whose runtime is disposed while it resolves", async () => { + let urlSequence = 0; + const leases = new ObjectUrlLeaseRegistry({ + createObjectURL: () => `blob:disposed-${urlSequence++}`, + revokeObjectURL() {}, + }); + const { preview, request } = await verifiedPngPreview(leases); + + const pending = preview.create(request(new AbortController().signal)); + preview.dispose(); + + expect(await pending).toMatchObject({ + ok: false, + error: { code: "UNAVAILABLE", operation: "PREVIEW" }, + }); + expect(urlSequence).toBe(0); + expect(leases.activeLeaseCount).toBe(0); + }); + + it("opens no byte source for a file whose vault is disposed while it loads", async () => { + const vault = new BrowserFileVault({ policies: pngPolicies() }); + const captured = vault.captureFiles( + [ + new File([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], "a.png", { + type: "image/png", + lastModified: 1, + }), + ], + pngSelectionPolicy, + ); + if (!captured.ok || !captured.value[0]) throw new Error("capture"); + + const pending = vault.openSource({ + ref: captured.value[0].ref, + signal: new AbortController().signal, + }); + vault.dispose(); + + expect(await pending).toMatchObject({ + ok: false, + error: { code: "UNAVAILABLE", operation: "FILE_READ" }, + }); + }); + + it("lets go of every selected File on dispose", () => { + const vault = new BrowserFileVault({ policies: pngPolicies() }); + expect( + vault.captureFiles( + [ + new File([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], "a.png", { + type: "image/png", + lastModified: 1, + }), + ], + pngSelectionPolicy, + ), + ).toMatchObject({ ok: true }); + expect(vault.activeReferenceCount).toBe(1); + + vault.dispose(); + + expect(vault.activeReferenceCount).toBe(0); + expect(vault.retainedByteLength).toBe(0); + }); + it("returns ABORTED without a lease when the caller aborts right after create() (02-01)", async () => { const revoked: string[] = []; let urlSequence = 0; diff --git a/tests/unit/public-response-cache-repair.test.ts b/tests/unit/public-response-cache-repair.test.ts index 9f67c020..692cc95b 100644 --- a/tests/unit/public-response-cache-repair.test.ts +++ b/tests/unit/public-response-cache-repair.test.ts @@ -4,6 +4,7 @@ import type { PublicCacheAsset } from "../../src/application/ports/browser-file- import { activePointerRequest, controlCacheName, + markerRequest, } from "../../src/adapters/cache-storage/public-cache-codec.ts"; import { createDefaultPublicCachePolicy, @@ -526,6 +527,63 @@ describe("public cache pointer after a policy change or damage (0929/02-01)", () control.match = realMatch; }); + it("does not clean up on a damaged pointer, which may name the release being served (2026-11-03 02-03)", async () => { + const cacheStorage = new MemoryCacheStorage(); + const policy = policyRetaining(1); + const adapter = adapterFor(cacheStorage, policy); + expect(await deploy(adapter, "r1", policy)).toMatchObject({ ok: true }); + expect(await deploy(adapter, "r2", policy)).toMatchObject({ ok: true }); + const control = await cacheStorage.open(controlCacheName(policy)); + await control.put( + activePointerRequest(policy), + new Response('{"schemaVersion":1,"releaseRegis', { + headers: { "content-type": "application/json" }, + }), + ); + const before = (await cacheStorage.keys()).sort(); + + expect(await adapter.admin.cleanupOwned()).toMatchObject({ + ok: false, + error: { code: "CORRUPT_DATA", operation: "CACHE_DELETE" }, + }); + expect((await cacheStorage.keys()).sort()).toEqual(before); + }); + + it("repairs a damaged release marker by staging the same manifest again (2026-11-03 02-03)", async () => { + const cacheStorage = new MemoryCacheStorage(); + const policy = policyRetaining(1); + const adapter = adapterFor(cacheStorage, policy); + expect(await deploy(adapter, "r1", policy)).toMatchObject({ ok: true }); + const releaseName = (await cacheStorage.keys()).find((name) => + name.includes("release:r1:"), + ); + if (!releaseName) throw new Error("release cache missing"); + const release = await cacheStorage.open(releaseName); + await release.put( + markerRequest(policy), + new Response('{"schemaVersion":1,"verif', { + headers: { "content-type": "application/json" }, + }), + ); + // REHYDRATE: the damaged candidate cannot be activated as it is ... + const manifest = await manifestOf("r1", policy); + expect( + await adapter.admin.activateRelease( + manifest.releaseRegistryId, + manifest.manifestDigestHex, + ), + ).toMatchObject({ + ok: false, + error: { code: "CORRUPT_DATA", recovery: "REHYDRATE" }, + }); + + // ... and staging it again is the recovery that hint promises. + expect(await deploy(adapter, "r1", policy)).toMatchObject({ ok: true }); + expect( + await adapter.responses.matchActiveExact({ absoluteUrl: assetUrl("r1") }), + ).toMatchObject({ ok: true, value: expect.anything() }); + }); + it("still stops activation when the pointer read fails transiently", async () => { const cacheStorage = new MemoryCacheStorage(); const policy = policyRetaining(1);