Files
tech-log-frontend/tests/browser-capabilities/indexeddb-runtime.spec.ts
T
DongHyeonka c80320dfc7 chore: sync the frontend template from ebea300 to af3d8e6
Carries 359 template commits, tag v0.2.0 plus one documentation commit: the
generator-built feature index with its app, bounded-context and feature
declarations, the src/platform kernel and scale tooling, the 2026-09-24 src
review fixes, and the 2026-09-25 maturity review round with its CHANGELOG
[보안]/[계약] rows. 691 of the 824 template paths are applied, 85 of them
merged with product changes; 127 are not carried.

Product identity is unchanged: name, version 0.1.0, routes, design and the
TechLog domain.

Three decisions worth naming. TechLog stays composed directly, as the last
two syncs recorded; its directory carries a NOT_INSTALLED marker that says so,
and every installed-* composer takes the template's projection with TechLog
added on top. The reference sample is no longer installed: the template now
installs a feature only through feature-module.ts and context-routes.ts,
which load the screens this product deleted, so its three operations, six
schemas and one topic leave the registries, recorded as registry change
evidence. And AppRouter mounts the platform and TechLog routes only; the
template's bounded-context route gateway waits for a generator-installed
feature.

Required fixes: readSubject on the BFF session owner (without it every HTTP
Studio session would close as integration-failed), zod through the facade in
nine TechLog files, noUnusedLocals, and public/config.json written out to
match the local profile. Derived baselines were recomputed after reproducing
both sides' values: CI contract 101 commands / 106 references / 141
artifacts, gate shape c9adcf85; registry approval 2dc6c3a3; the runtime
config schema baseline equals the schema at 8210d48.

Verified here: types, lint, architecture, adapter inventory, CI contract,
i18n, design system, diagnostics, browser security, dev release manifest,
TechLog contract and build pass; 220 component, 155 integration, 15
contract, 418 TechLog, 43 reference-feature, 18 recipe and 46
release-coherence tests pass, and 2381 of 2383 unit tests. Reproduced at
8210d48 first and unchanged: check:registries (byte-identical),
test:runtime-schema, verify:documentation. Brought by the template and
escalated rather than suppressed: check:request-fanout on TechLog's cursor
pagination, check:remediation-ledger (fails upstream too), the shared-chunk
hash test, and the registry-dependent sample removal and second-feature
tests. Playwright suites were not run.

Full record, the five open questions and the follow-ups in
docs/operations/template-sync-2026-09-29.md.

(cherry picked from commit b2170cf265dea45dc9cc05d3c61e627e97f31cdb)
2026-11-12 08:28:54 +09:00

1232 lines
38 KiB
TypeScript

import type {
IndexedDbMaintenanceDependencies,
IndexedDbRuntimeDependencies,
} from "../../src/adapters/storage/indexeddb/indexeddb-types.ts";
import {
expect,
test,
type Page,
} from "../support/browser/strict-browser-test.ts";
type BrowserItem = Readonly<{
label: string;
rank: number;
}>;
function databaseName(_prefix: string): string {
const token = crypto.randomUUID().replaceAll("-", "");
return `ca-idb-v1:a${token.slice(0, 31)}.n${token.slice(1)}.p${token.split("").reverse().join("").slice(0, 31)}`;
}
async function startLifecycleRuntime(
page: Page,
name: string,
targetVersion: 1 | 2,
blockedTimeoutMs = 1_000,
partitioned = false,
): Promise<void> {
await page.evaluate(
async ({
currentDatabaseName,
currentTargetVersion,
currentBlockedTimeoutMs,
currentPartitioned,
}) => {
const modulePath =
"/src/adapters/storage/indexeddb/indexeddb-runtime.ts";
const { createIndexedDbRuntime } = (await import(
/* @vite-ignore */ modulePath
)) as typeof import("../../src/adapters/storage/indexeddb/indexeddb-runtime.ts");
type LifecycleValue = Readonly<{ value: string }>;
const [authorityToken, namespaceToken, partitionToken] =
currentDatabaseName.slice("ca-idb-v1:".length).split(".");
const accountScope = currentPartitioned
? ("OPAQUE_PARTITION" as const)
: ("ORIGIN_SHARED" as const);
const scope = {
authorityToken,
namespaceToken,
partitionToken,
accountScope,
};
const storagePolicy = {
owner: "platform-storage",
namespace: "indexeddb-browser-lifecycle",
classification: "INTERNAL" as const,
authority: "SERVER" as const,
accountScope,
retention: { kind: "EXPLICIT_DELETE" as const },
softBudgetBytes: 1_000_000,
hardBudgetBytes: 2_000_000,
evictionPriority: "SYNCED_COPY" as const,
logoutAction: currentPartitioned
? ("PURGE_PARTITION" as const)
: ("KEEP_ORIGIN_SHARED" as const),
accountDeletionAction: currentPartitioned
? ("PURGE_PARTITION" as const)
: ("KEEP_ORIGIN_SHARED" as const),
pressureAction: "RETAIN" as const,
unavailableFallback: "ONLINE_ONLY" as const,
};
const migrations: IndexedDbRuntimeDependencies<
LifecycleValue,
LifecycleValue,
null
>["migrations"] = [
{
id: "schema-v1",
fromVersion: 0,
toVersion: 1,
operations: [
{
kind: "CREATE_STORE",
name: "governance",
keyPath: "bindingKey",
},
{
kind: "CREATE_STORE",
name: "retention",
keyPath: "recordKey",
indexes: [
{
name: "by-eligibility",
keyPath: "eligibleAtEpochMs",
},
],
},
{
kind: "CREATE_STORE",
name: "records",
keyPath: "key",
},
{
kind: "CREATE_STORE",
name: "receipts",
keyPath: "idempotencyKey",
indexes: [
{
name: "by-expiry",
keyPath: "expiresAtEpochMs",
},
],
},
],
},
...(currentTargetVersion === 2
? [
{
id: "schema-v2",
fromVersion: 1,
toVersion: 2,
operations: [
{
kind: "CREATE_INDEX" as const,
store: "records",
index: {
name: "by-key",
keyPath: "key",
},
},
],
},
]
: []),
];
const config: IndexedDbRuntimeDependencies<
LifecycleValue,
LifecycleValue,
null
> = {
scope,
storagePolicy,
schemaVersion: currentTargetVersion,
recordStore: "records",
governanceStore: "governance",
retentionStore: "retention",
retentionEligibilityIndex: "by-eligibility",
lifecycleMetadataStores: [],
idempotencyStore: "receipts",
idempotencyExpiryIndex: "by-expiry",
receiptRetentionMs: 60_000,
maxIdempotencyReceipts: 1_000,
migrations,
codec: {
currentVersion: 1,
encode: (value) => ({ ok: true, value }),
measureStoredBytes: (value) =>
new TextEncoder().encode(JSON.stringify(value)).byteLength,
decode: (version, value) =>
version === 1 &&
value !== null &&
typeof value === "object" &&
typeof (
value as Readonly<{ value?: unknown }>
).value === "string"
? {
ok: true,
value: value as LifecycleValue,
}
: { ok: false },
fingerprint: () => "0".repeat(64),
},
queryPolicy: { plan: () => ({ limit: 10 }) },
blockedTimeoutMs: currentBlockedTimeoutMs,
authorizeLifecycle: () => ({
authorized: true,
proofToken: "browserauthorityproof_001",
}),
};
const runtime = createIndexedDbRuntime(config);
(
globalThis as unknown as {
__indexedDbLifecycleRuntime?: unknown;
}
).__indexedDbLifecycleRuntime = runtime;
const opening = runtime.open();
(
globalThis as unknown as {
__indexedDbLifecycleOpening?: Promise<unknown>;
}
).__indexedDbLifecycleOpening = opening;
},
{
currentDatabaseName: name,
currentTargetVersion: targetVersion,
currentBlockedTimeoutMs: blockedTimeoutMs,
currentPartitioned: partitioned,
},
);
}
async function settleLifecycleRuntime(page: Page): Promise<unknown> {
return await page.evaluate(async () => {
const state = globalThis as unknown as {
__indexedDbLifecycleRuntime?: {
getStatus(): unknown;
};
__indexedDbLifecycleOpening?: Promise<unknown>;
};
if (!state.__indexedDbLifecycleRuntime || !state.__indexedDbLifecycleOpening) {
throw new Error("IndexedDB lifecycle runtime is not installed.");
}
const opened = await state.__indexedDbLifecycleOpening;
return {
opened,
status: state.__indexedDbLifecycleRuntime.getStatus(),
};
});
}
async function installLifecycleRuntime(
page: Page,
name: string,
targetVersion: 1 | 2,
blockedTimeoutMs = 1_000,
): Promise<unknown> {
await startLifecycleRuntime(page, name, targetVersion, blockedTimeoutMs);
return await settleLifecycleRuntime(page);
}
async function lifecycleStatus(page: Page): Promise<unknown> {
return await page.evaluate(() => {
const runtime = (
globalThis as unknown as {
__indexedDbLifecycleRuntime?: {
getStatus(): unknown;
};
}
).__indexedDbLifecycleRuntime;
return runtime?.getStatus() ?? null;
});
}
async function closeLifecycleRuntime(page: Page): Promise<void> {
await page.evaluate(() => {
const runtime = (
globalThis as unknown as {
__indexedDbLifecycleRuntime?: {
close(): void;
};
}
).__indexedDbLifecycleRuntime;
runtime?.close();
});
}
test("runs open, CAS, read, cursor query, close, and deletion against native IndexedDB", async ({
page,
}) => {
await page.goto("/");
const name = databaseName("runtime-browser");
const result = await page.evaluate(async (currentDatabaseName) => {
const modulePath =
"/src/adapters/storage/indexeddb/indexeddb-runtime.ts";
const { createIndexedDbRuntime } = (await import(
/* @vite-ignore */ modulePath
)) as typeof import("../../src/adapters/storage/indexeddb/indexeddb-runtime.ts");
const observations: unknown[] = [];
let epochMs = 1_000;
let lifecycleAuthorizations = 0;
const [authorityToken, namespaceToken, partitionToken] =
currentDatabaseName.slice("ca-idb-v1:".length).split(".");
const scope = {
authorityToken,
namespaceToken,
partitionToken,
accountScope: "ORIGIN_SHARED" as const,
};
const storagePolicy = {
owner: "platform-storage",
namespace: "indexeddb-browser-runtime",
classification: "INTERNAL" as const,
authority: "SERVER" as const,
accountScope: "ORIGIN_SHARED" as const,
retention: { kind: "TTL" as const, maxAgeMs: 10 },
softBudgetBytes: 1_000_000,
hardBudgetBytes: 2_000_000,
evictionPriority: "SYNCED_COPY" as const,
logoutAction: "KEEP_ORIGIN_SHARED" as const,
accountDeletionAction: "KEEP_ORIGIN_SHARED" as const,
pressureAction: "RETAIN" as const,
unavailableFallback: "ONLINE_ONLY" as const,
};
const config: IndexedDbRuntimeDependencies<
BrowserItem,
BrowserItem,
Readonly<{ limit: number }>
> = {
scope,
storagePolicy,
schemaVersion: 1,
recordStore: "records",
governanceStore: "governance",
retentionStore: "retention",
retentionEligibilityIndex: "by-eligibility",
lifecycleMetadataStores: [],
idempotencyStore: "receipts",
idempotencyExpiryIndex: "by-expiry",
receiptRetentionMs: 60_000,
maxIdempotencyReceipts: 1_000,
migrations: [
{
id: "schema-v1",
fromVersion: 0,
toVersion: 1,
operations: [
{
kind: "CREATE_STORE",
name: "governance",
keyPath: "bindingKey",
},
{
kind: "CREATE_STORE",
name: "retention",
keyPath: "recordKey",
indexes: [
{
name: "by-eligibility",
keyPath: "eligibleAtEpochMs",
},
],
},
{
kind: "CREATE_STORE",
name: "records",
keyPath: "key",
indexes: [
{
name: "by-rank",
keyPath: "payload.rank",
},
],
},
{
kind: "CREATE_STORE",
name: "receipts",
keyPath: "idempotencyKey",
indexes: [
{
name: "by-expiry",
keyPath: "expiresAtEpochMs",
},
],
},
],
},
],
codec: {
currentVersion: 1,
encode: (value) => ({ ok: true, value }),
measureStoredBytes: (value) =>
new TextEncoder().encode(JSON.stringify(value)).byteLength,
decode: (version, value) =>
version === 1 &&
value !== null &&
typeof value === "object" &&
typeof (value as Partial<BrowserItem>).label === "string" &&
typeof (value as Partial<BrowserItem>).rank === "number"
? { ok: true, value: value as BrowserItem }
: { ok: false },
fingerprint: async (value) => {
const bytes = new TextEncoder().encode(
JSON.stringify([value.rank, value.label]),
);
const digest = await crypto.subtle.digest("SHA-256", bytes);
return Array.from(
new Uint8Array(digest),
(byte) => byte.toString(16).padStart(2, "0"),
).join("");
},
},
queryPolicy: {
plan: (query) => ({
index: "by-rank",
direction: "next",
limit: query.limit,
}),
},
observe: (event) => observations.push(event),
nowEpochMilliseconds: () => epochMs,
nowMonotonicMilliseconds: () => 0,
authorizeLifecycle: () => {
lifecycleAuthorizations += 1;
return {
authorized: true,
proofToken: "browserauthorityproof_002",
};
},
};
const runtime = createIndexedDbRuntime(config);
const opened = await runtime.open();
const alpha = {
key: "alpha-key",
value: { label: "alpha", rank: 2 },
expectedRevision: null,
idempotencyKey: "alpha-create",
} as const;
const created = await runtime.compareAndSwap(alpha);
const replayed = await runtime.compareAndSwap(alpha);
const conflict = await runtime.compareAndSwap({
...alpha,
value: { label: "changed", rank: 2 },
});
const uncloneable = await runtime.compareAndSwap({
key: "uncloneable-key",
value: {
label: "uncloneable",
rank: 3,
callback: () => undefined,
} as unknown as BrowserItem,
expectedRevision: null,
idempotencyKey: "uncloneable-create",
});
const uncloneableRead = await runtime.read(
"uncloneable-key",
);
await runtime.compareAndSwap({
key: "beta-key",
value: { label: "beta", rank: 1 },
expectedRevision: null,
idempotencyKey: "beta-create",
});
const read = await runtime.read("alpha-key");
const firstPage = await runtime.query({ limit: 1 });
const secondPage = await runtime.query(
{ limit: 1 },
firstPage.ok ? firstPage.value.nextCursor : null,
);
epochMs = 1_011;
const expiredRead = await runtime.read("alpha-key");
const expiredPage = await runtime.query({ limit: 10 });
const lifecycle = await runtime.enforceLifecycleBatch({
action: "RETENTION_SWEEP",
maxRows: 10,
maxDurationMs: 1_000,
});
const auditConnection = await new Promise<IDBDatabase>(
(resolve, reject) => {
const request = indexedDB.open(currentDatabaseName, 1);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
},
);
const persistedAudit = await new Promise<string>(
(resolve, reject) => {
const transaction = auditConnection.transaction(
["governance", "retention", "receipts"],
"readonly",
);
const governance = transaction
.objectStore("governance")
.getAll();
const retention = transaction
.objectStore("retention")
.getAll();
const receipts = transaction
.objectStore("receipts")
.getAll();
transaction.oncomplete = () =>
resolve(
JSON.stringify([
governance.result,
retention.result,
receipts.result,
]),
);
transaction.onerror = () => reject(transaction.error);
transaction.onabort = () => reject(transaction.error);
},
);
auditConnection.close();
const beforeClose = runtime.getStatus();
runtime.close();
const afterClose = runtime.getStatus();
const deletion = await new Promise<string>((resolve) => {
const request = indexedDB.deleteDatabase(currentDatabaseName);
let blockedTimer: ReturnType<typeof setTimeout> | undefined;
const finish = (value: string) => {
if (blockedTimer) clearTimeout(blockedTimer);
resolve(value);
};
request.onsuccess = () => finish("DELETED");
request.onerror = () =>
finish(request.error?.name ?? "UNKNOWN_ERROR");
// `blocked` is a notification, not a terminal outcome: a connection
// that has already called close() may finish its last transaction and
// allow this same request to succeed.
request.onblocked = () => {
blockedTimer ??= setTimeout(() => finish("BLOCKED"), 2_000);
};
});
return {
opened,
created,
replayed,
conflict,
uncloneable,
uncloneableRead,
read,
firstPage,
secondPage,
expiredRead,
expiredPage,
lifecycle,
lifecycleAuthorizations,
persistedAudit,
beforeClose,
afterClose,
deletion,
observations,
};
}, name);
expect(result.opened).toMatchObject({ ok: true });
expect(result.created).toMatchObject({
ok: true,
value: { revision: 1, replayed: false },
});
expect(result.replayed).toMatchObject({
ok: true,
value: { revision: 1, replayed: true },
});
expect(result.conflict).toMatchObject({
ok: false,
error: { code: "CONFLICT" },
});
expect(result.uncloneable).toMatchObject({
ok: false,
error: { code: "CORRUPT_DATA" },
});
expect(result.uncloneableRead).toEqual({
ok: true,
value: null,
});
expect(result.read).toEqual({
ok: true,
value: {
value: { label: "alpha", rank: 2 },
revision: 1,
},
});
expect(result.firstPage).toMatchObject({
ok: true,
value: { items: [{ label: "beta", rank: 1 }] },
});
expect(result.secondPage).toEqual({
ok: true,
value: {
items: [{ label: "alpha", rank: 2 }],
nextCursor: null,
},
});
expect(result.expiredRead).toMatchObject({
ok: false,
error: { code: "EXPIRED_RESOURCE" },
});
expect(result.expiredPage).toEqual({
ok: true,
value: { items: [], nextCursor: null },
});
expect(result.lifecycle).toMatchObject({
ok: true,
value: { state: "COMPLETE", deletedRows: 2 },
});
expect(result.lifecycleAuthorizations).toBe(1);
expect(result.persistedAudit).not.toContain(
"browserauthorityproof_002",
);
expect(result.beforeClose).toEqual({
kind: "READY",
schemaVersion: 1,
});
expect(result.afterClose).toEqual({ kind: "DISPOSED" });
expect(result.deletion).toBe("DELETED");
expect(JSON.stringify(result.observations)).not.toMatch(
/alpha-key|beta-key|alpha-create/,
);
});
test("runs resumable codec maintenance against native IndexedDB", async ({
page,
}) => {
await page.goto("/");
const name = databaseName("maintenance-browser");
const result = await page.evaluate(async (currentDatabaseName) => {
const runtimeModulePath =
"/src/adapters/storage/indexeddb/indexeddb-runtime.ts";
const maintenanceModulePath =
"/src/adapters/storage/indexeddb/indexeddb-maintenance.ts";
const { createIndexedDbRuntime } = (await import(
/* @vite-ignore */ runtimeModulePath
)) as typeof import("../../src/adapters/storage/indexeddb/indexeddb-runtime.ts");
const { createIndexedDbMaintenance } = (await import(
/* @vite-ignore */ maintenanceModulePath
)) as typeof import("../../src/adapters/storage/indexeddb/indexeddb-maintenance.ts");
const [authorityToken, namespaceToken, partitionToken] =
currentDatabaseName.slice("ca-idb-v1:".length).split(".");
const scope = {
authorityToken,
namespaceToken,
partitionToken,
accountScope: "ORIGIN_SHARED" as const,
};
const storagePolicy = {
owner: "platform-storage",
namespace: "indexeddb-browser-maintenance",
classification: "INTERNAL" as const,
authority: "SERVER" as const,
accountScope: "ORIGIN_SHARED" as const,
retention: { kind: "EXPLICIT_DELETE" as const },
softBudgetBytes: 1_000_000,
hardBudgetBytes: 2_000_000,
evictionPriority: "SYNCED_COPY" as const,
logoutAction: "KEEP_ORIGIN_SHARED" as const,
accountDeletionAction: "KEEP_ORIGIN_SHARED" as const,
pressureAction: "RETAIN" as const,
unavailableFallback: "ONLINE_ONLY" as const,
};
const runtimeConfig: IndexedDbRuntimeDependencies<
Readonly<{ label: string }>,
Readonly<{ label: string }>,
null
> = {
scope,
storagePolicy,
schemaVersion: 1,
recordStore: "records",
governanceStore: "governance",
retentionStore: "retention",
retentionEligibilityIndex: "by-eligibility",
lifecycleMetadataStores: ["maintenance"],
idempotencyStore: "receipts",
idempotencyExpiryIndex: "by-expiry",
receiptRetentionMs: 60_000,
maxIdempotencyReceipts: 1_000,
migrations: [
{
id: "schema-v1",
fromVersion: 0,
toVersion: 1,
operations: [
{
kind: "CREATE_STORE",
name: "governance",
keyPath: "bindingKey",
},
{
kind: "CREATE_STORE",
name: "retention",
keyPath: "recordKey",
indexes: [
{
name: "by-eligibility",
keyPath: "eligibleAtEpochMs",
},
],
},
{
kind: "CREATE_STORE",
name: "records",
keyPath: "key",
},
{
kind: "CREATE_STORE",
name: "receipts",
keyPath: "idempotencyKey",
indexes: [
{
name: "by-expiry",
keyPath: "expiresAtEpochMs",
},
],
},
{
kind: "CREATE_STORE",
name: "maintenance",
keyPath: "checkpointKey",
},
],
},
],
codec: {
currentVersion: 2,
encode: (value) => ({ ok: true, value }),
measureStoredBytes: (value) =>
new TextEncoder().encode(JSON.stringify(value)).byteLength,
decode: (version, value) =>
version === 2 &&
value !== null &&
typeof value === "object" &&
typeof (
value as Readonly<{ label?: unknown }>
).label === "string"
? {
ok: true,
value: value as Readonly<{ label: string }>,
}
: { ok: false },
fingerprint: () => "0".repeat(64),
},
queryPolicy: { plan: () => ({ limit: 10 }) },
authorizeLifecycle: () => ({
authorized: true,
proofToken: "browserauthorityproof_003",
}),
};
const runtime = createIndexedDbRuntime(runtimeConfig);
const opened = await runtime.open();
runtime.close();
const seedConnection = await new Promise<IDBDatabase>(
(resolve, reject) => {
const request = indexedDB.open(currentDatabaseName, 1);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
},
);
await new Promise<void>((resolve, reject) => {
const transaction = seedConnection.transaction(
["records", "governance", "retention", "receipts"],
"readwrite",
);
const measuredA =
new TextEncoder().encode(
JSON.stringify({ legacyLabel: "alpha" }),
).byteLength +
512 +
"legacy-a".length * 2;
const measuredB =
new TextEncoder().encode(
JSON.stringify({ legacyLabel: "beta" }),
).byteLength +
512 +
"legacy-b".length * 2;
transaction.objectStore("records").put({
key: "legacy-a",
codecVersion: 1,
revision: 4,
payload: { legacyLabel: "alpha" },
});
transaction.objectStore("records").put({
key: "legacy-b",
codecVersion: 1,
revision: 8,
payload: { legacyLabel: "beta" },
});
transaction.objectStore("retention").put({
recordKey: "legacy-a",
writtenAtEpochMs: 1,
synchronization: "NONE",
measuredBytes: measuredA,
});
transaction.objectStore("retention").put({
recordKey: "legacy-b",
writtenAtEpochMs: 1,
synchronization: "NONE",
measuredBytes: measuredB,
});
transaction.objectStore("receipts").put({
idempotencyKey: "expired-browser-receipt",
operation: "PUT",
recordKey: "already-removed",
expectedRevision: null,
fingerprint: "a".repeat(64),
synchronization: "NONE",
revision: 1,
expiresAtEpochMs: 0,
});
transaction.objectStore("governance").put({
bindingKey: "dataset-budget",
budgetVersion: 1,
usedBytes: measuredA + measuredB,
receiptCount: 1,
});
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
transaction.onabort = () => reject(transaction.error);
});
seedConnection.close();
const maintenanceConfig: IndexedDbMaintenanceDependencies<
Readonly<{ label: string }>
> = {
scope,
storagePolicy,
schemaVersion: 1,
recordStore: "records",
governanceStore: "governance",
retentionStore: "retention",
checkpointStore: "maintenance",
checkpointKey: "records-codec",
idempotencyStore: "receipts",
idempotencyExpiryIndex: "by-expiry",
migrationPolicy: {
migrationId: "records-to-v2",
targetCodecVersion: 2,
measureStoredBytes: (value) =>
new TextEncoder().encode(JSON.stringify(value)).byteLength,
isOldWriterDrainConfirmed: () => true,
migrate: async ({ payload }) => {
const legacyLabel =
payload &&
typeof payload === "object" &&
typeof (
payload as Readonly<{ legacyLabel?: unknown }>
).legacyLabel === "string"
? (
payload as Readonly<{ legacyLabel: string }>
).legacyLabel
: null;
return legacyLabel === null
? { ok: false }
: { ok: true, value: { label: legacyLabel } };
},
},
};
const maintenance = createIndexedDbMaintenance(
maintenanceConfig,
);
const firstBatch = await maintenance.migrateCodecBatch({
maxRows: 1,
maxDurationMs: 10_000,
});
const secondBatch = await maintenance.migrateCodecBatch({
maxRows: 1,
maxDurationMs: 10_000,
});
const pruned = await maintenance.pruneExpiredReceipts({
maxRows: 1,
maxDurationMs: 10_000,
});
const inspectConnection = await new Promise<IDBDatabase>(
(resolve, reject) => {
const request = indexedDB.open(currentDatabaseName, 1);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
},
);
const rawRecords = await new Promise<unknown[]>(
(resolve, reject) => {
const transaction = inspectConnection.transaction(
"records",
"readonly",
);
const request = transaction.objectStore("records").getAll();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
},
);
const receiptCount = await new Promise<number>(
(resolve, reject) => {
const transaction = inspectConnection.transaction(
"receipts",
"readonly",
);
const request = transaction.objectStore("receipts").count();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
},
);
inspectConnection.close();
const deletion = await new Promise<string>((resolve) => {
const request = indexedDB.deleteDatabase(currentDatabaseName);
let blockedTimer: ReturnType<typeof setTimeout> | undefined;
const finish = (value: string) => {
if (blockedTimer) clearTimeout(blockedTimer);
resolve(value);
};
request.onsuccess = () => finish("DELETED");
request.onerror = () =>
finish(request.error?.name ?? "UNKNOWN_ERROR");
request.onblocked = () => {
blockedTimer ??= setTimeout(() => finish("BLOCKED"), 2_000);
};
});
return {
opened,
firstBatch,
secondBatch,
pruned,
rawRecords,
receiptCount,
deletion,
};
}, name);
expect(result.opened).toMatchObject({ ok: true });
expect(result.firstBatch).toMatchObject({
ok: true,
value: { state: "MORE", migratedRows: 1 },
});
expect(result.secondBatch).toMatchObject({
ok: true,
value: { state: "COMPLETE", migratedRows: 1 },
});
expect(result.pruned).toMatchObject({
ok: true,
value: { state: "COMPLETE", deletedRows: 1 },
});
expect(result.receiptCount).toBe(0);
expect(result.rawRecords).toEqual([
{
key: "legacy-a",
codecVersion: 2,
revision: 4,
payload: { label: "alpha" },
},
{
key: "legacy-b",
codecVersion: 2,
revision: 8,
payload: { label: "beta" },
},
]);
expect(result.deletion).toBe("DELETED");
});
test("closes a v1 runtime on versionchange so another context can upgrade to v2", async ({
page,
}) => {
const secondPage = await page.context().newPage();
const name = databaseName("versionchange");
try {
await Promise.all([page.goto("/"), secondPage.goto("/")]);
expect(await installLifecycleRuntime(page, name, 1)).toEqual({
opened: { ok: true },
status: { kind: "READY", schemaVersion: 1 },
});
const upgrade = await secondPage.evaluate(
async (currentDatabaseName) =>
await new Promise<string>((resolve) => {
const request = indexedDB.open(currentDatabaseName, 2);
request.onsuccess = () => {
request.result.close();
resolve("UPGRADED");
};
request.onerror = () =>
resolve(request.error?.name ?? "UNKNOWN_ERROR");
request.onblocked = () => resolve("BLOCKED");
}),
name,
);
expect(upgrade).toBe("UPGRADED");
expect(await lifecycleStatus(page)).toEqual({
kind: "CLOSED",
reason: "VERSION_CHANGE",
});
await closeLifecycleRuntime(page);
const deletion = await secondPage.evaluate(
async (currentDatabaseName) =>
await new Promise<string>((resolve) => {
const request = indexedDB.deleteDatabase(
currentDatabaseName,
);
request.onsuccess = () => resolve("DELETED");
request.onerror = () =>
resolve(request.error?.name ?? "UNKNOWN_ERROR");
request.onblocked = () => resolve("BLOCKED");
}),
name,
);
expect(deletion).toBe("DELETED");
} finally {
await secondPage.close();
}
});
test("fails a blocked v2 upgrade closed, then closes its late connection after the blocker exits", async ({
page,
}) => {
const blockerPage = await page.context().newPage();
const name = databaseName("blocked");
try {
await Promise.all([page.goto("/"), blockerPage.goto("/")]);
const blocker = await blockerPage.evaluate(
async (currentDatabaseName) =>
await new Promise<string>((resolve) => {
const request = indexedDB.open(currentDatabaseName, 1);
request.onupgradeneeded = () => {
const records = request.result.createObjectStore(
"records",
{ keyPath: "key" },
);
void records;
const receipts = request.result.createObjectStore(
"receipts",
{ keyPath: "idempotencyKey" },
);
receipts.createIndex(
"by-expiry",
"expiresAtEpochMs",
);
};
request.onsuccess = () => {
(
globalThis as unknown as {
__indexedDbNativeBlocker?: IDBDatabase;
}
).__indexedDbNativeBlocker = request.result;
resolve("OPEN");
};
request.onerror = () =>
resolve(request.error?.name ?? "UNKNOWN_ERROR");
}),
name,
);
expect(blocker).toBe("OPEN");
await startLifecycleRuntime(page, name, 2, 500);
await expect
.poll(async () => await lifecycleStatus(page))
.toEqual({
kind: "BLOCKED",
currentVersion: 1,
targetVersion: 2,
});
const blocked = await settleLifecycleRuntime(page);
expect(blocked).toMatchObject({
opened: {
ok: false,
error: {
code: "BLOCKED",
recovery: "RELOAD_OTHER_CONTEXTS",
},
},
status: {
kind: "CLOSED",
reason: "NOT_OPENED",
},
});
await blockerPage.evaluate(() => {
const scope = globalThis as unknown as {
__indexedDbNativeBlocker?: IDBDatabase;
};
scope.__indexedDbNativeBlocker?.close();
scope.__indexedDbNativeBlocker = undefined;
});
await expect
.poll(async () => await lifecycleStatus(page))
.toEqual({
kind: "CLOSED",
reason: "NOT_OPENED",
});
await closeLifecycleRuntime(page);
const deletion = await blockerPage.evaluate(
async (currentDatabaseName) =>
await new Promise<string>((resolve) => {
const request = indexedDB.deleteDatabase(
currentDatabaseName,
);
request.onsuccess = () => resolve("DELETED");
request.onerror = () =>
resolve(request.error?.name ?? "UNKNOWN_ERROR");
request.onblocked = () => resolve("BLOCKED");
}),
name,
);
expect(deletion).toBe("DELETED");
} finally {
await blockerPage.close();
}
});
test("05-03 deletes a partition's dataset for an authorized ACCOUNT_DELETION when its budget row is corrupt", async ({
page,
}) => {
const name = databaseName("corrupt-governance");
await page.goto("/");
await startLifecycleRuntime(page, name, 1, 1_000, true);
expect(await settleLifecycleRuntime(page)).toMatchObject({
opened: { ok: true },
});
expect(
await page.evaluate(async () => {
const runtime = (
globalThis as unknown as {
__indexedDbLifecycleRuntime: {
compareAndSwap(input: unknown): Promise<unknown>;
close(): void;
};
}
).__indexedDbLifecycleRuntime;
const written = await runtime.compareAndSwap({
key: "personal-1",
value: { value: "mine" },
expectedRevision: null,
idempotencyKey: "personal-create",
});
runtime.close();
return written;
}),
).toMatchObject({ ok: true });
expect(
await page.evaluate(
async (currentDatabaseName) =>
await new Promise<string>((resolve) => {
const request = indexedDB.open(currentDatabaseName, 1);
request.onerror = () =>
resolve(request.error?.name ?? "UNKNOWN_ERROR");
request.onsuccess = () => {
const database = request.result;
const transaction = database.transaction(
"governance",
"readwrite",
);
transaction.objectStore("governance").put({
bindingKey: "dataset-budget",
budgetVersion: 1,
usedBytes: -5,
receiptCount: 1,
});
transaction.oncomplete = () => {
database.close();
resolve("CORRUPTED");
};
transaction.onabort = () => {
database.close();
resolve("ABORTED");
};
};
}),
name,
),
).toBe("CORRUPTED");
await startLifecycleRuntime(page, name, 1, 1_000, true);
expect(await settleLifecycleRuntime(page)).toMatchObject({
opened: { ok: false, error: { code: "POLICY_REJECTED" } },
});
const purged = await page.evaluate(async () => {
const runtime = (
globalThis as unknown as {
__indexedDbLifecycleRuntime: {
enforceLifecycleBatch(input: unknown): Promise<unknown>;
};
}
).__indexedDbLifecycleRuntime;
return await runtime.enforceLifecycleBatch({
action: "ACCOUNT_DELETION",
maxRows: 100,
maxDurationMs: 5_000,
});
});
expect(purged).toMatchObject({
ok: true,
value: { state: "COMPLETE", datasetDeleted: true },
});
const remaining = await page.evaluate(async (currentDatabaseName) => {
(
globalThis as unknown as {
__indexedDbLifecycleRuntime?: { close(): void };
}
).__indexedDbLifecycleRuntime?.close();
// A fresh open of a deleted database runs its upgrade from version 0.
return await new Promise<number>((resolve, reject) => {
const request = indexedDB.open(currentDatabaseName);
let oldVersion = -1;
request.onupgradeneeded = (event) => {
oldVersion = event.oldVersion;
};
request.onsuccess = () => {
request.result.close();
const deletion = indexedDB.deleteDatabase(currentDatabaseName);
deletion.onsuccess = () => resolve(oldVersion);
deletion.onerror = () => reject(deletion.error);
};
request.onerror = () => reject(request.error);
});
}, name);
expect(remaining).toBe(0);
});
test("05-06 an older tab meets a schema a newer tab upgraded and goes online-only", async ({
page,
}) => {
const olderPage = await page.context().newPage();
const name = databaseName("future-schema");
try {
await Promise.all([page.goto("/"), olderPage.goto("/")]);
expect(await installLifecycleRuntime(page, name, 2)).toMatchObject({
opened: { ok: true },
status: { kind: "READY", schemaVersion: 2 },
});
await closeLifecycleRuntime(page);
expect(await installLifecycleRuntime(olderPage, name, 1)).toEqual({
opened: {
ok: false,
error: {
code: "UNSUPPORTED",
operation: "INDEXEDDB_OPEN",
retryable: false,
recovery: "ONLINE_ONLY",
},
},
status: { kind: "INCOMPATIBLE", schemaVersion: 1 },
});
await closeLifecycleRuntime(olderPage);
const deletion = await page.evaluate(
async (currentDatabaseName) =>
await new Promise<string>((resolve) => {
const request = indexedDB.deleteDatabase(currentDatabaseName);
request.onsuccess = () => resolve("DELETED");
request.onerror = () =>
resolve(request.error?.name ?? "UNKNOWN_ERROR");
request.onblocked = () => resolve("BLOCKED");
}),
name,
);
expect(deletion).toBe("DELETED");
} finally {
await olderPage.close();
}
});