Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): cache public static resources across Main and Blog (#1002)

Gener33149e1dbbc6
diff --git a/scripts/ci/e2e-shards.mjs b/scripts/ci/e2e-shards.mjs
index b2645541e..07c19f532 100644
--- a/scripts/ci/e2e-shards.mjs
+++ b/scripts/ci/e2e-shards.mjs
@@ -85,6 +85,7 @@ export const SPEC_SECONDS = {
"63-mobile-notification-chat-route.spec.ts": DEFAULT_SPEC_SECONDS,
"64-billing-contact.spec.ts": 6,
"65-analytics-document-boundary.spec.ts": DEFAULT_SPEC_SECONDS,
+ "66-public-static-resource-cache.spec.ts": DEFAULT_SPEC_SECONDS,
}
function walk(directory) {
diff --git a/scripts/ci/e2e-shards.test.ts b/scripts/ci/e2e-shards.test.ts
index e7c5ea497..925646b05 100644
--- a/scripts/ci/e2e-shards.test.ts
+++ b/scripts/ci/e2e-shards.test.ts
@@ -75,17 +75,20 @@ describe("planE2eShards", () => {
expect(first).toEqual(second)
expect(Object.keys(SPEC_SECONDS).sort()).toEqual(specs)
- expect(first).toHaveLength(15)
+ expect(first).toHaveLength(16)
expect([...assigned].sort()).toEqual(specs)
expect(new Set(assigned).size).toBe(specs.length)
expect(SPEC_SECONDS["18-new-divider-scroll.spec.ts"]).toBe(DEFAULT_SPEC_SECONDS)
expect(SPEC_SECONDS["55-message-scroll-characterization.spec.ts"]).toBe(DEFAULT_SPEC_SECONDS)
+ expect(SPEC_SECONDS["66-public-static-resource-cache.spec.ts"]).toBe(DEFAULT_SPEC_SECONDS)
+ expect(first.find((shard) => shard.files.includes("66-public-static-resource-cache.spec.ts"))?.files)
+ .toEqual(["66-public-static-resource-cache.spec.ts"])
expect(specSeconds).toEqual([
- 165, 165, 165, 165, 165, 155.638, 154.234, 155.283,
+ 165, 165, 165, 165, 165, 165, 155.638, 154.234, 155.283,
156.095, 155.905, 156.323, 155.711, 153.963, 154.488, 156.138,
])
expect(predictedSeconds).toEqual([
- 240, 240, 240, 240, 240, 230.638, 229.234, 230.283,
+ 240, 240, 240, 240, 240, 240, 230.638, 229.234, 230.283,
231.095, 230.905, 231.323, 230.711, 228.963, 229.488, 231.138,
])
expect(first.every((shard) => (
diff --git a/src/web/blog/public/_headers b/src/web/blog/public/_headers
index f970d0d60..0536e07d9 100644
--- a/src/web/blog/public/_headers
+++ b/src/web/blog/public/_headers
@@ -18,3 +18,6 @@
/favicon.ico
Cache-Control: public, max-age=300, must-revalidate
+
+/manifest.json
+ Cache-Control: public, max-age=300, must-revalidate
diff --git a/src/web/blog/shared-build-inputs.json b/src/web/blog/shared-build-inputs.json
index 7571a2a76..85ae997d5 100644
--- a/src/web/blog/shared-build-inputs.json
+++ b/src/web/blog/shared-build-inputs.json
@@ -14,6 +14,7 @@
"src/components/error-content.tsx",
"src/components/not-found-content.tsx",
"src/components/public-layout.tsx",
+ "src/components/service-worker-registration.tsx",
"src/components/theme-color-sync.tsx",
"src/components/theme-provider.tsx",
"src/components/theme-toggle.tsx",
diff --git a/src/web/blog/src/app/layout.test.ts b/src/web/blog/src/app/layout.test.ts
index 16a515b90..852c31774 100644
--- a/src/web/blog/src/app/layout.test.ts
+++ b/src/web/blog/src/app/layout.test.ts
@@ -7,6 +7,10 @@ vi.mock("@/components/analytics-consent", () => ({
AnalyticsConsent: "analytics-consent",
}));
+vi.mock("@/components/service-worker-registration", () => ({
+ ServiceWorkerRegistration: "public-service-worker-registration",
+}));
+
vi.mock("@/components/theme-color-sync", () => ({
ThemeColorSync: "theme-color-sync",
}));
@@ -39,6 +43,13 @@ vi.mock("@/lib/seo/site-metadata", () => ({
import BlogRootLayout, { metadata, viewport } from "./layout";
describe("BlogRootLayout", () => {
+ it("mounts the shared root service worker registration on a direct Blog entry", () => {
+ const layout = BlogRootLayout({ children: createElement("main", null, "Post") });
+ const body = layout.props.children[1];
+ expect(body.props.children.filter((child: { type: unknown }) => child.type === "public-service-worker-registration"))
+ .toHaveLength(1);
+ });
+
it("renders the shared analytics consent control after blog content", () => {
const child = createElement("main", null, "Post");
const layout = BlogRootLayout({ children: child });
diff --git a/src/web/blog/src/app/layout.tsx b/src/web/blog/src/app/layout.tsx
index b9b372e77..358c2c78f 100644
--- a/src/web/blog/src/app/layout.tsx
+++ b/src/web/blog/src/app/layout.tsx
@@ -1,6 +1,7 @@
import { ObservedRouteCommit } from "@/lib/observability/regions";
import type { Metadata, Viewport } from "next";
import { AnalyticsConsent } from "@/components/analytics-consent";
+import { ServiceWorkerRegistration } from "@/components/service-worker-registration";
import { caveat, dmMono, dmSans, instrumentSerif, literata, vt323 } from "@/app/fonts";
import { ThemeColorSync } from "@/components/theme-color-sync";
import { ThemeProvider } from "@/components/theme-provider";
@@ -37,6 +38,7 @@ export default function BlogRootLayout({
<ObservedRouteCommit />
<AnalyticsConsent />
</ThemeProvider>
+ <ServiceWorkerRegistration />
</body>
</html>
);
diff --git a/src/web/playwright.public-static-qa.config.ts b/src/web/playwright.public-static-qa.config.ts
new file mode 100644
index 000000000..bfb7061ac
--- /dev/null
+++ b/src/web/playwright.public-static-qa.config.ts
@@ -0,0 +1,16 @@
+import { defineConfig } from "@playwright/test"
+import ordinary from "./playwright.config"
+
+process.env.ALOOK_PUBLIC_STATIC_QA = "1"
+
+export default defineConfig({
+ ...ordinary,
+ testDir: "./src/test/e2e-ui/_qa/public-static",
+ testMatch: "**/*.qa.ts",
+ maxFailures: 1,
+ projects: ordinary.projects!.flatMap(project => [{
+ ...project, name: `${project.name}-phase-capability`, grep: /@phase-capability/,
+ }, {
+ ...project, grepInvert: /@phase-capability/, dependencies: [`${project.name}-phase-capability`],
+ }]),
+})
diff --git a/src/web/public/_headers b/src/web/public/_headers
index aeadefd9f..78762d34a 100644
--- a/src/web/public/_headers
+++ b/src/web/public/_headers
@@ -24,3 +24,18 @@
/icon-512.png
Cache-Control: public, max-age=300, must-revalidate
+
+/favicon.ico
+ Cache-Control: public, max-age=300, must-revalidate
+
+/manifest.json
+ Cache-Control: public, max-age=300, must-revalidate
+
+/contact/gus-photo.jpeg
+ Cache-Control: public, max-age=300, must-revalidate
+
+/fonts/jgs5/jgs5.ttf
+ Cache-Control: public, max-age=300, must-revalidate
+
+/social/readme-card-58c75311e5ba.png
+ Cache-Control: public, max-age=300, must-revalidate
diff --git a/src/web/scripts/dev-zone-ingress.mjs b/src/web/scripts/dev-zone-ingress.mjs
index bdaf6292b..9f6f9e8c7 100644
--- a/src/web/scripts/dev-zone-ingress.mjs
+++ b/src/web/scripts/dev-zone-ingress.mjs
@@ -51,8 +51,21 @@ export function createZoneIngress({
hostname = "127.0.0.1",
port = 3000,
backends = ZONE_BACKENDS,
+ phaseGate,
+ wsBackend,
} = {}) {
- const server = http.createServer((request, response) => {
+ const upgrades = createUpgradeSocketOwner()
+ let closing = false
+ let pendingListen
+ const server = http.createServer(async (request, response) => {
+ if (phaseGate) {
+ try { await phaseGate.ready() } catch {
+ response.writeHead(503, { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" })
+ response.end("Managed release transition failed")
+ return
+ }
+ }
+ if (request.destroyed || response.destroyed) return
const target = targetForRequest(request.url, backends)
const proxy = http.request({
hostname: target.hostname,
@@ -80,7 +93,11 @@ export function createZoneIngress({
})
server.on("upgrade", (request, socket, head) => {
- const target = targetForRequest(request.url, backends)
+ if (closing) { socket.destroy(); return }
+ upgrades.own(socket)
+ const pathname = new URL(request.url ?? "/", "http://dev.alook.local").pathname
+ const target = wsBackend && (pathname === "/api/ws" || pathname.startsWith("/api/ws/"))
+ ? wsBackend : targetForRequest(request.url, backends)
const proxy = http.request({
hostname: target.hostname,
port: target.port,
@@ -90,6 +107,8 @@ export function createZoneIngress({
})
proxy.on("upgrade", (backendResponse, backendSocket, backendHead) => {
+ if (closing) { backendSocket.destroy(); socket.destroy(); return }
+ upgrades.own(socket, backendSocket)
writeUpgradeResponse(socket, backendResponse)
if (backendHead.length > 0) socket.write(backendHead)
if (head.length > 0) backendSocket.write(head)
@@ -102,24 +121,107 @@ export function createZoneIngress({
})
proxy.on("error", () => socket.destroy())
socket.on("error", () => proxy.destroy())
+ socket.once("close", () => proxy.destroy())
proxy.end()
})
return {
server,
listen() {
- return new Promise((resolve, reject) => {
+ if (closing || pendingListen) return Promise.reject(new Error("Development ingress is closed or already starting"))
+ pendingListen = new Promise((resolve, reject) => {
server.once("error", reject)
server.listen(port, hostname, () => {
server.off("error", reject)
+ if (closing) {
+ server.close(() => reject(new Error("Development ingress retired during startup")))
+ return
+ }
resolve(server.address())
})
})
+ return pendingListen
},
- close() {
- return new Promise((resolve, reject) => {
- server.close((error) => (error ? reject(error) : resolve()))
- })
+ async close() {
+ closing = true
+ const results = await Promise.allSettled([
+ upgrades.close(),
+ (async () => {
+ await pendingListen?.catch(() => {})
+ if (!server.listening) return
+ await new Promise((resolve, reject) => { server.close(error => error ? reject(error) : resolve()) })
+ })(),
+ ])
+ const failures = results.filter(result => result.status === "rejected").map(result => result.reason)
+ if (failures.length) throw new AggregateError(failures, "Development ingress cleanup failed")
+ },
+ }
+}
+
+export function createUpgradeSocketOwner() {
+ const pairs = new Set()
+ const pending = new Set()
+ let closing = false
+ const closed = socket => socket.closed ? Promise.resolve() : new Promise(resolve => socket.once("close", resolve))
+ return {
+ own(client, backend = client) {
+ if (closing) { client.destroy(); backend.destroy(); throw new Error("Upgrade socket owner is closed") }
+ let retired = false
+ const pair = { retire: () => {
+ if (retired) return
+ retired = true
+ pairs.delete(pair)
+ client.destroy()
+ backend.destroy()
+ } }
+ const complete = Promise.all([closed(client), closed(backend)])
+ pending.add(complete)
+ void complete.then(() => pending.delete(complete))
+ for (const socket of [client, backend]) {
+ socket.on("error", pair.retire)
+ socket.once("close", pair.retire)
+ }
+ pairs.add(pair)
+ return pair.retire
+ },
+ async close() {
+ closing = true
+ for (const pair of [...pairs]) pair.retire()
+ let timer
+ try {
+ await Promise.race([
+ Promise.all([...pending]),
+ new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("Owned upgraded sockets did not close")), 5_000) }),
+ ])
+ } finally { if (timer) clearTimeout(timer) }
+ },
+ }
+}
+
+export function createPhaseGate() {
+ let pending
+ let release
+ let reject
+ let failure
+ return {
+ hold() {
+ if (pending || failure) throw new Error("Managed release gate is already held or failed")
+ pending = new Promise((resolve, rejectPromise) => { release = resolve; reject = rejectPromise })
+ pending.catch(() => {})
+ },
+ ready() {
+ return failure ? Promise.reject(failure) : pending ?? Promise.resolve()
+ },
+ release() {
+ release?.()
+ pending = undefined
+ release = undefined
+ reject = undefined
+ },
+ fail(error) {
+ failure = error
+ reject?.(error)
+ pending = undefined
},
}
}
diff --git a/src/web/scripts/dev-zone-processes.mjs b/src/web/scripts/dev-zone-processes.mjs
index 6f9760ed9..b57c87fe6 100644
--- a/src/web/scripts/dev-zone-processes.mjs
+++ b/src/web/scripts/dev-zone-processes.mjs
@@ -1,3 +1,9 @@
+import { createHash } from "node:crypto"
+import { existsSync, lstatSync, readFileSync, realpathSync, renameSync, writeFileSync } from "node:fs"
+import { dirname, isAbsolute, relative, resolve, sep } from "node:path"
+import { createRequire } from "node:module"
+import ts from "typescript"
+
export const LOCAL_WORKER_ENDPOINTS = Object.freeze({
main: Object.freeze({
port: 3001,
@@ -36,3 +42,319 @@ export function workerDevArgs(configPaths, endpoint) {
export function exactWorkerDevArgs(wranglerEntry, configPaths, endpoint) {
return [wranglerEntry, ...wranglerDevArgs(configPaths, endpoint)]
}
+
+
+export const PUBLIC_STATIC_RELEASES = Object.freeze(["N", "Nplus1"])
+
+export function publicStaticParserIdentity() {
+ const parserRequire = createRequire(resolve(process.cwd(), "package.json"))
+ if (parserRequire("typescript") !== ts) throw new Error("Parser identity resolved a different TypeScript instance from the loaded parser")
+ const path = realpathSync(parserRequire.resolve("typescript"))
+ const bytes = readFileSync(path)
+ return { version: ts.version, path, bytes: bytes.length, sha256: createHash("sha256").update(bytes).digest("hex") }
+}
+
+function requireString(value, label, pattern) {
+ if (typeof value !== "string" || !value || (pattern && !pattern.test(value))) {
+ throw new Error(`Invalid public static QA ${label}`)
+ }
+ return value
+}
+
+export function containedPath(root, path) {
+ const target = resolve(root, path)
+ const suffix = relative(root, target)
+ if (!suffix || suffix === ".." || suffix.startsWith(`..${sep}`) || isAbsolute(suffix)) {
+ throw new Error("Public static QA path escapes its owned root")
+ }
+ return target
+}
+
+export function canonicalPhasePath(path) {
+ const absolute = resolve(path)
+ if (existsSync(absolute)) return realpathSync(absolute)
+ const parent = dirname(absolute)
+ if (parent === absolute) throw new Error("Cannot resolve public static QA owned path")
+ return resolve(canonicalPhasePath(parent), relative(parent, absolute))
+}
+
+function isWithin(root, path) {
+ const suffix = relative(root, path)
+ return suffix === "" || (suffix !== ".." && !suffix.startsWith(`..${sep}`) && !isAbsolute(suffix))
+}
+
+export function assertPublicStaticWriteSeparation(manifest, mutablePaths) {
+ const writes = [...mutablePaths, manifest.controlDirectory].map(canonicalPhasePath)
+ for (const release of Object.values(manifest.releases)) {
+ const root = canonicalPhasePath(release.root)
+ if (writes.some(write => isWithin(write, root))) throw new Error("Release root is inside a mutable/reset area")
+ for (const input of release.inputs) {
+ const file = canonicalPhasePath(containedPath(root, input.path))
+ if (writes.some(write => isWithin(write, file) || isWithin(file, write))) throw new Error("Bound release input intersects a mutable/reset area")
+ }
+ for (const output of ["src/web/.open-next", "src/web/.next", "src/web/public", "src/web/blog/.open-next", "src/web/blog/.next", "src/web/blog/public"]) {
+ const sealed = canonicalPhasePath(resolve(root, output))
+ if (writes.some(write => isWithin(sealed, write) || isWithin(write, sealed))) throw new Error("Mutable area intersects sealed release output")
+ }
+ }
+}
+
+export function validatePublicStaticManifest(value, ownedRoot) {
+ if (!value || value.schema !== 1) throw new Error("Invalid public static QA manifest schema")
+ const runId = requireString(value.runId, "run ID", /^[a-zA-Z0-9_-]{8,80}$/)
+ const owner = canonicalPhasePath(ownedRoot)
+ const controlDirectory = canonicalPhasePath(containedPath(resolve(ownedRoot), requireString(value.controlDirectory, "control directory")))
+ containedPath(owner, controlDirectory)
+ if (!PUBLIC_STATIC_RELEASES.includes(value.defaultRelease)) throw new Error("Invalid default QA release")
+ if (!value.releases || Object.keys(value.releases).sort().join(",") !== "N,Nplus1") {
+ throw new Error("Public static QA requires exactly N and Nplus1")
+ }
+ const releases = Object.fromEntries(PUBLIC_STATIC_RELEASES.map(name => {
+ const release = value.releases[name]
+ const root = requireString(release?.root, `${name} root`)
+ if (!isAbsolute(root)) throw new Error("Release root must be absolute")
+ requireString(release.head, `${name} head`, /^[0-9a-f]{40}$/)
+ requireString(release.tree, `${name} tree`, /^[0-9a-f]{40}$/)
+ requireString(release.swSha256, `${name} worker digest`, /^[0-9a-f]{64}$/)
+ for (const field of ["mainBuildId", "blogBuildId"]) requireString(release[field], `${name} ${field}`, /^[a-zA-Z0-9_-]+$/)
+ if (!Array.isArray(release.inputs) || release.inputs.length === 0) throw new Error("Release inputs are required")
+ const paths = new Set()
+ for (const input of release.inputs) {
+ requireString(input.path, "input path")
+ containedPath(root, input.path)
+ if (isAbsolute(input.path) || paths.has(input.path)) throw new Error("Release input paths must be relative and unique")
+ paths.add(input.path)
+ requireString(input.sha256, "input digest", /^[0-9a-f]{64}$/)
+ if (!Number.isSafeInteger(input.bytes) || input.bytes < 0) throw new Error("Invalid release input byte count")
+ }
+ for (const path of ["src/web/wrangler.toml", "src/web/blog/wrangler.toml", "src/web/custom-worker.ts", "src/web/blog/custom-worker.ts", "src/web/.open-next/worker.js", "src/web/blog/.open-next/worker.js", "src/web/public/sw.js", "src/web/.open-next/assets/sw.js"]) {
+ if (!paths.has(path)) throw new Error(`Missing required release input: ${path}`)
+ }
+ if (release.inputs.find(input => input.path === "src/web/public/sw.js").sha256 !== release.swSha256
+ || release.inputs.find(input => input.path === "src/web/.open-next/assets/sw.js").sha256 !== release.swSha256) {
+ throw new Error("Release SW inputs do not match its declared digest")
+ }
+ if (!Array.isArray(release.assets) || release.assets.length === 0) throw new Error("Release asset catalogue is required")
+ const urls = new Set()
+ for (const asset of release.assets) {
+ if (typeof asset.url !== "string" || !asset.url.startsWith("/") || asset.url.startsWith("//") || /[?#]/.test(asset.url) || urls.has(asset.url)) throw new Error("Invalid or duplicate release asset URL")
+ urls.add(asset.url)
+ const input = release.inputs.find(input => input.path === asset.path)
+ if (!input || asset.sha256 !== input.sha256 || asset.bytes !== input.bytes || !["main", "blog"].includes(asset.zone)) throw new Error("Asset is not bound to a release input")
+ if (asset.kind !== undefined && !["code", "fonts", "images"].includes(asset.kind)) throw new Error("Invalid runtime asset kind")
+ if (asset.precache !== undefined && typeof asset.precache !== "boolean") throw new Error("Invalid precache marker")
+ if (asset.precache && asset.revision !== null && (typeof asset.revision !== "string" || !asset.revision)) throw new Error("A precache asset requires its actual emitted revision or null")
+ }
+ for (const [zone, buildId] of [["main", release.mainBuildId], ["blog", release.blogBuildId]]) {
+ for (const helper of ["_buildManifest.js", "_clientMiddlewareManifest.js", "_ssgManifest.js"]) {
+ if (!release.assets.some(asset => asset.zone === zone && asset.url === `${zone === "blog" ? "/blog-static" : ""}/_next/static/${buildId}/${helper}`)) throw new Error("Release catalogue is missing an actual build helper")
+ }
+ }
+ return [name, { ...release, root: resolve(root) }]
+ }))
+ const manifest = { schema: 1, runId, controlDirectory, defaultRelease: value.defaultRelease, releases }
+ assertPublicStaticWriteSeparation(manifest, [owner])
+ return manifest
+}
+
+export function assertPublicStaticInputs(manifest) {
+ for (const release of Object.values(manifest.releases)) {
+ for (const input of release.inputs) {
+ const path = containedPath(release.root, input.path)
+ containedPath(realpathSync(release.root), realpathSync(path))
+ const stat = lstatSync(path)
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size !== input.bytes
+ || createHash("sha256").update(readFileSync(path)).digest("hex") !== input.sha256) {
+ throw new Error(`Public static QA input changed: ${input.path}`)
+ }
+ }
+ const emitted = builtPrecacheEntries(readFileSync(resolve(release.root, "src/web/public/sw.js"), "utf8"))
+ const declared = release.assets.filter(asset => asset.precache).map(({ url, revision }) => ({ url, revision }))
+ const normalized = entries => JSON.stringify(entries.toSorted((a, b) => a.url.localeCompare(b.url)))
+ if (normalized(emitted) !== normalized(declared)) throw new Error("Release precache catalogue does not match its actual emitted worker entries")
+ }
+}
+
+export function builtPrecacheEntries(source) {
+ const fileName = "/frozen-public-worker.js"
+ const ast = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS)
+ if (ast.parseDiagnostics.length) throw new Error("Frozen worker has invalid JavaScript syntax")
+ const options = { allowJs: true, noLib: true, noResolve: true, noEmit: true, target: ts.ScriptTarget.Latest }
+ const host = { ...ts.createCompilerHost(options), getSourceFile: file => file === fileName ? ast : undefined, fileExists: file => file === fileName, readFile: file => file === fileName ? source : undefined }
+ const checker = ts.createProgram([fileName], options, host).getTypeChecker()
+ const nodes = []
+ const visit = node => { nodes.push(node); ts.forEachChild(node, visit) }
+ visit(ast)
+ const nameOf = node => ts.isIdentifier(node) || ts.isStringLiteral(node) ? node.text : undefined
+ const candidates = nodes.filter(node => ts.isPropertyAssignment(node) && nameOf(node.name) === "precacheEntries" && ts.isNewExpression(node.parent.parent))
+ if (candidates.length !== 1) throw new Error("Frozen worker must expose exactly one unambiguous precache constructor input")
+ const fail = () => { throw new Error("Frozen precache input is not an unambiguous static array binding") }
+ const statement = node => {
+ let current = node
+ while (current && !ts.isStatement(current)) current = current.parent
+ return current
+ }
+ const directAssignment = node => {
+ let current = node
+ while (current.parent && !ts.isExpressionStatement(current.parent)) {
+ const parent = current.parent
+ if (!ts.isParenthesizedExpression(parent) && !(ts.isBinaryExpression(parent) && parent.operatorToken.kind === ts.SyntaxKind.CommaToken)) return false
+ current = parent
+ }
+ return ts.isExpressionStatement(current.parent)
+ }
+ const linearScope = scope => {
+ if (!scope) return false
+ if (ts.isSourceFile(scope)) return true
+ if (!ts.isBlock(scope) || (!ts.isArrowFunction(scope.parent) && !ts.isFunctionExpression(scope.parent))) return false
+ if (scope.parent.asteriskToken || scope.parent.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.AsyncKeyword)) return false
+ let expression = scope.parent
+ while (ts.isParenthesizedExpression(expression.parent)) expression = expression.parent
+ const call = expression.parent
+ return ts.isCallExpression(call) && call.expression === expression && ts.isExpressionStatement(call.parent) && linearScope(call.parent.parent)
+ }
+ const candidate = candidates[0]
+ const optionsObject = candidate.parent
+ const optionNames = new Set()
+ for (const property of optionsObject.properties) {
+ if (!ts.isPropertyAssignment(property)) return fail()
+ const name = nameOf(property.name)
+ if (!name || optionNames.has(name)) return fail()
+ optionNames.add(name)
+ }
+ const constructor = optionsObject.parent
+ let consumer = constructor
+ while (ts.isParenthesizedExpression(consumer.parent)) consumer = consumer.parent
+ const parent = consumer.parent
+ if (!(ts.isVariableDeclaration(parent) && parent.initializer === consumer) && !(ts.isExpressionStatement(parent) && parent.expression === consumer)) return fail()
+ if (!linearScope(statement(consumer)?.parent)) return fail()
+ const linearBefore = node => {
+ const current = statement(node), scope = current?.parent
+ if (!scope || (!ts.isSourceFile(scope) && !ts.isBlock(scope))) return false
+ const index = scope.statements.indexOf(current)
+ return index >= 0 && scope.statements.slice(0, index).every(value => ts.isVariableStatement(value) || ts.isExpressionStatement(value) || ts.isClassDeclaration(value) || ts.isFunctionDeclaration(value) || ts.isEmptyStatement(value))
+ }
+ if (!linearBefore(consumer)) return fail()
+ if (nodes.some(node => ts.isWithStatement(node) || (ts.isIdentifier(node) && node.text === "eval"))) return fail()
+ const symbolFor = node => ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node
+ ? checker.getShorthandAssignmentValueSymbol(node.parent) : checker.getSymbolAtLocation(node)
+ const references = symbol => nodes.filter(node => ts.isIdentifier(node) && symbolFor(node) === symbol)
+ const shape = root => {
+ const names = new Map()
+ const encode = node => {
+ let value
+ if (ts.isIdentifier(node)) { if (!names.has(node.text)) names.set(node.text, names.size); value = names.get(node.text) }
+ else if (ts.isStringLiteral(node) || ts.isNumericLiteral(node)) value = node.text
+ const children = []
+ ts.forEachChild(node, child => { children.push(encode(child)) })
+ return [node.kind, value, children]
+ }
+ return JSON.stringify(encode(root))
+ }
+ const template = ts.createSourceFile("initializer.js", "var init=(e,t,s)=>()=>{if(s)throw s[0];try{return e&&(t=e(e=0)),t}catch(r){throw s=[r],r}}", ts.ScriptTarget.Latest, true, ts.ScriptKind.JS)
+ const initializerShape = shape(template.statements[0].declarationList.declarations[0].initializer)
+ const emittedInitialization = (assignment, scope, use) => {
+ const body = statement(assignment)?.parent, callback = body?.parent
+ if (!body || !ts.isBlock(body) || body.statements.length !== 1 || !callback || !ts.isArrowFunction(callback) || callback.modifiers?.length || callback.parameters.length) return false
+ const onlyStatement = body.statements[0]
+ if (!ts.isExpressionStatement(onlyStatement)) return false
+ let expression = onlyStatement.expression
+ while (ts.isParenthesizedExpression(expression)) expression = expression.expression
+ if (expression !== assignment) return false
+ const factoryCall = callback.parent
+ if (!ts.isCallExpression(factoryCall) || factoryCall.arguments.length !== 1 || factoryCall.arguments[0] !== callback || !ts.isIdentifier(factoryCall.expression)) return false
+ const initialized = factoryCall.parent
+ if (!ts.isVariableDeclaration(initialized) || initialized.initializer !== factoryCall || !ts.isIdentifier(initialized.name) || statement(initialized)?.parent !== scope) return false
+ const factorySymbol = symbolFor(factoryCall.expression), initSymbol = symbolFor(initialized.name)
+ const factory = factorySymbol?.declarations?.length === 1 ? factorySymbol.declarations[0] : undefined
+ if (!factory || !ts.isVariableDeclaration(factory) || !factory.initializer || factory.initializer.end >= factoryCall.pos || shape(factory.initializer) !== initializerShape || statement(factory)?.parent !== scope) return false
+ if (!references(factorySymbol).every(node => node === factory.name || (ts.isCallExpression(node.parent) && node.parent.expression === node))) return false
+ const calls = references(initSymbol).filter(node => node !== initialized.name)
+ if (!calls.length || !calls.every(node => ts.isCallExpression(node.parent) && node.parent.expression === node && !node.parent.arguments.length)) return false
+ return calls.some(node => node.pos > initialized.end && node.pos < use.pos && directAssignment(node.parent) && statement(node)?.parent === scope && linearBefore(node))
+ }
+ const arrayFor = (input, seen = new Set()) => {
+ if (ts.isParenthesizedExpression(input)) return arrayFor(input.expression, seen)
+ if (ts.isArrayLiteralExpression(input)) return input
+ if (!ts.isIdentifier(input)) return fail()
+ const symbol = symbolFor(input)
+ if (!symbol || seen.has(symbol) || symbol.declarations?.length !== 1) return fail()
+ const declaration = symbol.declarations[0]
+ if (!ts.isVariableDeclaration(declaration) || !ts.isIdentifier(declaration.name)) return fail()
+ const scope = statement(declaration)?.parent
+ if (!scope || ts.isSourceFile(scope) || scope !== statement(input)?.parent || !linearScope(scope)) return fail()
+ const writes = declaration.initializer ? [{ value: declaration.initializer, position: declaration.pos }] : []
+ for (const node of nodes) {
+ if (!ts.isIdentifier(node) || symbolFor(node) !== symbol || node === input || node === declaration.name) continue
+ const parent = node.parent
+ if (ts.isBinaryExpression(parent) && parent.left === node && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken && directAssignment(parent)
+ && (statement(parent)?.parent === scope || emittedInitialization(parent, scope, input))) writes.push({ value: parent.right, position: parent.pos })
+ else return fail()
+ }
+ if (writes.length !== 1 || writes[0].position >= input.pos) return fail()
+ return arrayFor(writes[0].value, new Set([...seen, symbol]))
+ }
+ const array = arrayFor(candidate.initializer)
+ const entries = array.elements.map(element => {
+ if (!ts.isObjectLiteralExpression(element)) return fail()
+ const fields = new Map()
+ for (const property of element.properties) {
+ if (!ts.isPropertyAssignment(property)) return fail()
+ const name = nameOf(property.name)
+ if (!name || !["url", "revision", "integrity"].includes(name) || fields.has(name)) return fail()
+ fields.set(name, property.initializer)
+ }
+ const url = fields.get("url"), revision = fields.get("revision")
+ if (!url || !ts.isStringLiteral(url) || !url.text || !revision || (revision.kind !== ts.SyntaxKind.NullKeyword && !ts.isStringLiteral(revision))) return fail()
+ if (fields.has("integrity") && !ts.isStringLiteral(fields.get("integrity"))) return fail()
+ return { url: url.text, revision: revision.kind === ts.SyntaxKind.NullKeyword ? null : revision.text }
+ })
+ if (!entries.length || new Set(entries.map(entry => entry.url)).size !== entries.length) return fail()
+ return entries
+}
+
+export function readPublicStaticManifest(path, ownedRoot, verify = true) {
+ const bytes = readFileSync(path)
+ const manifest = validatePublicStaticManifest(JSON.parse(bytes.toString("utf8")), ownedRoot)
+ if (verify) assertPublicStaticInputs(manifest)
+ return { ...manifest, digest: createHash("sha256").update(bytes).digest("hex") }
+}
+
+export function writePhaseFile(path, value) {
+ const temporary = `${path}.${process.pid}.tmp`
+ writeFileSync(temporary, `${JSON.stringify(value)}\n`, { mode: 0o600 })
+ renameSync(temporary, path)
+}
+
+export function readPhaseFile(path) {
+ return existsSync(path) ? JSON.parse(readFileSync(path, "utf8")) : null
+}
+
+export function validatePhaseCommand(command, manifest, receipt, now = Date.now()) {
+ if (!command || command.runId !== manifest.runId || command.manifestDigest !== manifest.digest
+ || !Number.isSafeInteger(command.sequence) || command.sequence !== receipt.sequence + 1
+ || command.from !== receipt.release || !PUBLIC_STATIC_RELEASES.includes(command.to)
+ || !["switch", "release"].includes(command.action) || typeof command.caseId !== "string" || !command.caseId
+ || (receipt.caseId !== undefined && command.caseId !== receipt.caseId)
+ || (command.action === "switch" && command.to === command.from)
+ || (command.action === "release" && (receipt.caseId !== command.caseId || command.to !== command.from || command.to !== manifest.defaultRelease))
+ || !Number.isSafeInteger(command.deadline) || command.deadline <= now || command.deadline - now > 60_000
+ || receipt.status !== "ready") throw new Error("Invalid, expired or foreign public static QA phase command")
+ return command
+}
+
+export function phaseCommandDigest(command) {
+ return createHash("sha256").update(JSON.stringify(command)).digest("hex")
+}
+
+export function phaseWorkerDefinitions(manifest, releaseName, wranglerEntry, runtimeRoot) {
+ if (!PUBLIC_STATIC_RELEASES.includes(releaseName)) throw new Error("Unknown public static QA release")
+ const release = manifest.releases[releaseName]
+ return ["main", "blog"].map(zone => ({
+ label: zone === "main" ? "main backend" : "Blog backend",
+ command: process.execPath,
+ args: exactWorkerDevArgs(wranglerEntry,
+ [resolve(release.root, zone === "main" ? "src/web/wrangler.toml" : "src/web/blog/wrangler.toml")],
+ { ...LOCAL_WORKER_ENDPOINTS[zone], persistTo: resolve(runtimeRoot, zone === "main" ? "src/web/.wrangler/state" : "src/web/blog/.wrangler/state") }),
+ }))
+}
diff --git a/src/web/scripts/dev-zones.mjs b/src/web/scripts/dev-zones.mjs
index 480a3c8af..f37c8420d 100644
--- a/src/web/scripts/dev-zones.mjs
+++ b/src/web/scripts/dev-zones.mjs
@@ -1,11 +1,21 @@
import { spawn } from "node:child_process"
+import { createHash } from "node:crypto"
+import { mkdirSync } from "node:fs"
import net from "node:net"
import { resolve } from "node:path"
-import { createZoneIngress } from "./dev-zone-ingress.mjs"
+import { createPhaseGate, createZoneIngress } from "./dev-zone-ingress.mjs"
import {
exactWorkerDevArgs,
LOCAL_WORKER_ENDPOINTS,
workerDevArgs,
+ assertPublicStaticInputs,
+ assertPublicStaticWriteSeparation,
+ phaseWorkerDefinitions,
+ phaseCommandDigest,
+ readPhaseFile,
+ readPublicStaticManifest,
+ validatePhaseCommand,
+ writePhaseFile,
} from "./dev-zone-processes.mjs"
const packageManagerCli = process.env.npm_execpath
@@ -21,10 +31,42 @@ if ((withWsDo || wranglerEntry) && !workerMode) {
throw new Error("--with-ws-do and --wrangler-entry require --worker")
}
+const manifestFlag = process.argv.indexOf("--public-static-manifest")
+const ownerFlag = process.argv.indexOf("--public-static-owner-root")
+const manifestPath = manifestFlag === -1 ? undefined : process.argv[manifestFlag + 1]
+const ownerRoot = ownerFlag === -1 ? undefined : process.argv[ownerFlag + 1]
+if ((manifestFlag !== -1 || ownerFlag !== -1) && (!manifestPath || !ownerRoot || !wranglerEntry || !workerMode || !withWsDo)) {
+ throw new Error("Public static phases require their manifest, owner root and exact combined worker mode")
+}
+const manifest = manifestPath ? readPublicStaticManifest(manifestPath, ownerRoot) : undefined
+const runtimeRoot = resolve(process.cwd(), "../..")
+if (manifest) assertPublicStaticWriteSeparation(manifest, [ownerRoot, resolve(runtimeRoot, "src/web/.wrangler/state"), resolve(runtimeRoot, "src/web/.wrangler/state.e2e-backup"), resolve(runtimeRoot, "src/web/blog/.wrangler/state"), resolve(runtimeRoot, "src/web/e2e-service-logs"), resolve(runtimeRoot, ".wrangler/registry")])
+const phaseGate = manifest ? createPhaseGate() : undefined
+const expectedExits = new WeakSet()
+const phaseChildren = []
+let phaseTimer
+let phaseBusy = false
+let receipt = manifest ? {
+ schema: 1, runId: manifest.runId, manifestDigest: manifest.digest,
+ sequence: 0, release: manifest.defaultRelease, status: "ready",
+} : undefined
+const receiptPath = manifest ? resolve(manifest.controlDirectory, "receipt.json") : undefined
+if (manifest) {
+ mkdirSync(manifest.controlDirectory, { recursive: true, mode: 0o700 })
+ if (readPhaseFile(receiptPath)) throw new Error("Public static QA control directory has an unresolved previous owner")
+ writePhaseFile(receiptPath, receipt)
+}
+
const registryPath = resolve(process.cwd(), "../../.wrangler/registry")
const childEnv = { ...process.env, WRANGLER_REGISTRY_PATH: registryPath }
const processes = []
let shuttingDown = false
+const lifetime = new AbortController()
+
+function assertLive(deadline = Infinity) {
+ if (shuttingDown || lifetime.signal.aborted || receipt?.status === "failed") throw new Error("Managed development owner is retired")
+ if (Date.now() >= deadline) throw new Error("Managed phase exceeded its original deadline")
+}
function spawnBackend(label, command, args) {
const child = spawn(command, args, {
@@ -39,9 +81,10 @@ function spawnBackend(label, command, args) {
shutdown(1)
})
child.once("exit", (code, signal) => {
- if (!shuttingDown) {
+ if (!shuttingDown && !expectedExits.has(child)) {
console.error(`${label} exited (${signal ?? code ?? "unknown"})`)
- shutdown(code ?? 1)
+ failPhase(new Error(`${label} exited unexpectedly (${signal ?? code ?? "unknown"})`))
+ shutdown(1)
}
})
@@ -49,37 +92,50 @@ function spawnBackend(label, command, args) {
}
function waitForPort(port, timeoutMs = 60_000) {
- const startedAt = Date.now()
-
+ const deadline = Date.now() + timeoutMs
return new Promise((resolveReady, rejectReady) => {
+ let socket
+ let timer
+ let finished = false
+ const finish = (error) => {
+ if (finished) return
+ finished = true
+ if (timer) clearTimeout(timer)
+ lifetime.signal.removeEventListener("abort", abort)
+ socket?.destroy()
+ if (error) rejectReady(error)
+ else resolveReady()
+ }
+ const abort = () => finish(new Error("Managed port probe cancelled"))
function probe() {
- const socket = net.createConnection({ host: "127.0.0.1", port })
+ try { assertLive(deadline) } catch (error) { finish(error); return }
+ socket = net.createConnection({ host: "127.0.0.1", port })
socket.once("connect", () => {
- socket.destroy()
- resolveReady()
+ finish()
})
+ socket.setTimeout(Math.max(1, Math.min(1_000, deadline - Date.now())), () => socket.destroy(new Error("Port probe timed out")))
socket.once("error", () => {
socket.destroy()
- if (Date.now() - startedAt >= timeoutMs) {
- rejectReady(new Error(`Timed out waiting for port ${port}`))
- return
- }
- setTimeout(probe, 200)
+ if (finished) return
+ if (lifetime.signal.aborted || Date.now() >= deadline) finish(new Error(`Timed out or cancelled waiting for port ${port}`))
+ else timer = setTimeout(probe, 200)
})
}
-
+ lifetime.signal.addEventListener("abort", abort, { once: true })
probe()
})
}
async function waitForHttpOk(url, timeoutMs = 60_000) {
- const startedAt = Date.now()
+ const deadline = Date.now() + timeoutMs
let lastResult = "unreachable"
- while (Date.now() - startedAt < timeoutMs) {
+ while (Date.now() < deadline) {
+ assertLive(deadline)
try {
- const response = await fetch(url, { redirect: "manual" })
+ const response = await fetch(url, { redirect: "manual", signal: AbortSignal.any([lifetime.signal, AbortSignal.timeout(Math.max(1, Math.min(3_000, deadline - Date.now())))]) })
await response.body?.cancel()
+ assertLive(deadline)
if (response.ok) return
lastResult = `HTTP ${response.status}`
} catch (error) {
@@ -126,20 +182,150 @@ const backends = workerMode
},
]
-for (const backend of backends) {
- spawnBackend(backend.label, backend.command, backend.args)
+const selectedBackends = manifest ? [
+ {
+ label: "stable WS/queue",
+ command: process.execPath,
+ args: exactWorkerDevArgs(wranglerEntry, ["../ws-do/wrangler.toml", "../queue-worker/wrangler.toml"], {
+ port: 8789, inspectorPort: 9230, persistTo: resolve(runtimeRoot, "src/web/.wrangler/state"),
+ }),
+ },
+ ...phaseWorkerDefinitions(manifest, manifest.defaultRelease, wranglerEntry, runtimeRoot),
+] : backends
+
+for (const backend of selectedBackends) {
+ const child = spawnBackend(backend.label, backend.command, backend.args)
+ if (manifest && backend.label !== "stable WS/queue") phaseChildren.push({ label: backend.label, child })
+}
+
+const ingress = createZoneIngress({ phaseGate, wsBackend: manifest ? { hostname: "127.0.0.1", port: 8789 } : undefined })
+
+function failPhase(error) {
+ if (!manifest || receipt?.status === "failed") return
+ phaseGate.fail(error)
+ receipt = { ...receipt, status: "failed", failure: error instanceof Error ? error.message : String(error) }
+ writePhaseFile(receiptPath, receipt)
}
-const ingress = createZoneIngress()
+async function phaseResponse(port, path, deadline) {
+ assertLive(deadline)
+ const remaining = deadline - Date.now()
+ if (remaining <= 0) throw new Error("Public static phase readiness exceeded its case deadline")
+ return fetch(`http://127.0.0.1:${port}${path}`, {
+ redirect: "manual", signal: AbortSignal.any([lifetime.signal, AbortSignal.timeout(Math.min(remaining, 3_000))]),
+ headers: { host: "localhost:3000", "x-forwarded-host": "localhost:3000", "x-forwarded-port": "3000", "x-forwarded-proto": "https" },
+ })
+}
+
+async function verifyRelease(name, deadline) {
+ const release = manifest.releases[name]
+ let lastError
+ while (Date.now() < deadline) {
+ try {
+ const health = await phaseResponse(3001, "/api/health", deadline)
+ const healthBody = await health.text()
+ assertLive(deadline)
+ if (health.status !== 200 || healthBody !== '{"status":"ok"}') throw new Error("Main phase exact health did not match")
+ const blog = await phaseResponse(3002, "/blog", deadline)
+ if (blog.status !== 200 || !(blog.headers.get("content-type") ?? "").includes("text/html")) throw new Error("Blog phase document readiness did not match")
+ await blog.body?.cancel()
+ assertLive(deadline)
+ const sw = await phaseResponse(3001, "/sw.js", deadline)
+ if (sw.status !== 200 || createHash("sha256").update(Buffer.from(await sw.arrayBuffer())).digest("hex") !== release.swSha256) throw new Error("Served phase SW bytes do not match the bound release")
+ assertLive(deadline)
+ for (const [zone, buildId] of [["main", release.mainBuildId], ["blog", release.blogBuildId]]) {
+ for (const helper of ["_buildManifest.js", "_clientMiddlewareManifest.js", "_ssgManifest.js"]) {
+ const path = `${zone === "blog" ? "/blog-static" : ""}/_next/static/${buildId}/${helper}`
+ const asset = release.assets.find(value => value.url === path && value.zone === zone)
+ if (!asset) throw new Error(`Phase catalogue has no actual helper: ${path}`)
+ const response = await phaseResponse(zone === "main" ? 3001 : 3002, path, deadline)
+ const body = Buffer.from(await response.arrayBuffer())
+ assertLive(deadline)
+ if (response.status !== 200 || body.length !== asset.bytes || createHash("sha256").update(body).digest("hex") !== asset.sha256) throw new Error(`Phase helper bytes do not match: ${path}`)
+ }
+ }
+ return
+ } catch (error) {
+ lastError = error
+ assertLive(deadline)
+ if (receipt.status === "failed") throw error
+ await new Promise(resolveDelay => setTimeout(resolveDelay, 100))
+ }
+ }
+ throw lastError ?? new Error("Public static phase readiness expired")
+}
+
+async function stopPhaseChild({ child }) {
+ assertLive(receipt.deadline)
+ if (!child.pid || child.exitCode !== null || child.signalCode !== null) throw new Error("Expected phase child was not alive before registered stop")
+ expectedExits.add(child)
+ await new Promise((resolveStop, rejectStop) => {
+ const done = (error) => { clearTimeout(timer); lifetime.signal.removeEventListener("abort", abort); child.removeListener("exit", closed); if (error) rejectStop(error); else resolveStop() }
+ const abort = () => done(new Error("Registered child stop cancelled"))
+ const closed = () => done()
+ const timer = setTimeout(() => done(new Error("Registered phase child did not stop")), Math.max(1, Math.min(5_000, receipt.deadline - Date.now())))
+ lifetime.signal.addEventListener("abort", abort, { once: true })
+ child.once("exit", closed)
+ child.kill("SIGTERM")
+ })
+}
+
+async function pollPhase() {
+ if (phaseBusy || shuttingDown) return
+ phaseBusy = true
+ try {
+ const command = readPhaseFile(resolve(manifest.controlDirectory, "request.json"))
+ if (!command || (command.sequence === receipt.sequence && phaseCommandDigest(command) === receipt.commandDigest)) return
+ validatePhaseCommand(command, manifest, receipt)
+ if (command.action === "release") {
+ receipt = { schema: 1, runId: manifest.runId, manifestDigest: manifest.digest, sequence: command.sequence, commandDigest: phaseCommandDigest(command), release: receipt.release, status: "ready" }
+ writePhaseFile(receiptPath, receipt)
+ return
+ }
+ assertPublicStaticInputs(manifest)
+ validatePhaseCommand(command, manifest, receipt)
+ const previous = phaseChildren.splice(0)
+ if (previous.length !== 2) throw new Error("Phase owner has no exact Main/Blog child pair")
+ receipt = { ...receipt, sequence: command.sequence, caseId: command.caseId, status: "switching", deadline: command.deadline, expectedStops: previous.map(({ label, child }) => ({ label, pid: child.pid })) }
+ writePhaseFile(receiptPath, receipt)
+ phaseGate.hold()
+ await Promise.all(previous.map(stopPhaseChild))
+ assertLive(command.deadline)
+ for (const definition of phaseWorkerDefinitions(manifest, command.to, wranglerEntry, runtimeRoot)) {
+ assertLive(command.deadline)
+ phaseChildren.push({ label: definition.label, child: spawnBackend(definition.label, definition.command, definition.args) })
+ }
+ await verifyRelease(command.to, command.deadline)
+ assertLive(command.deadline)
+ assertPublicStaticInputs(manifest)
+ if (Date.now() >= command.deadline || receipt.status === "failed") throw new Error("Phase transition failed before commit")
+ receipt = { schema: 1, runId: manifest.runId, manifestDigest: manifest.digest, commandDigest: phaseCommandDigest(command), sequence: command.sequence, caseId: command.caseId, release: command.to, status: "ready" }
+ writePhaseFile(receiptPath, receipt)
+ phaseGate.release()
+ } catch (error) {
+ failPhase(error)
+ console.error(error)
+ await shutdown(1)
+ } finally {
+ phaseBusy = false
+ }
+}
async function shutdown(exitCode = 0) {
if (shuttingDown) return
shuttingDown = true
+ lifetime.abort(new Error("Managed development owner stopped"))
+ if (phaseTimer) clearInterval(phaseTimer)
+ phaseGate?.fail(new Error("Managed public static phase owner stopped"))
+ if (manifest && receipt.status !== "failed") {
+ receipt = { ...receipt, status: "stopped" }
+ writePhaseFile(receiptPath, receipt)
+ }
for (const child of processes) {
if (child.exitCode === null && child.signalCode === null) child.kill("SIGTERM")
}
- if (ingress.server.listening) await ingress.close().catch(() => {})
+ try { await ingress.close() } catch (error) { console.error(error); exitCode = 1 }
process.exitCode = exitCode
}
@@ -149,7 +335,16 @@ for (const signal of ["SIGINT", "SIGTERM"]) {
try {
await Promise.all([waitForPort(3001), waitForPort(3002)])
+ assertLive()
+ if (manifest) {
+ await waitForHttpOk("http://127.0.0.1:8789/health")
+ assertLive()
+ await verifyRelease(manifest.defaultRelease, Date.now() + 60_000)
+ assertLive()
+ }
await ingress.listen()
+ assertLive()
+ if (manifest) phaseTimer = setInterval(() => { void pollPhase() }, 100)
if (!workerMode) await waitForHttpOk("http://127.0.0.1:3000/")
console.log(
`Alook multi-zone ${workerMode ? "Worker" : "Next"} development ready at http://127.0.0.1:3000`,
diff --git a/src/web/scripts/http-cache-policy.test.ts b/src/web/scripts/http-cache-policy.test.ts
index 0e559b344..dc9928b3a 100644
--- a/src/web/scripts/http-cache-policy.test.ts
+++ b/src/web/scripts/http-cache-policy.test.ts
@@ -37,9 +37,15 @@ describe("HTTP cache configuration", () => {
it.each([
["public/_headers", "/landing/people/lin.webp"],
["public/_headers", "/alook.svg"],
+ ["public/_headers", "/contact/gus-photo.jpeg"],
+ ["public/_headers", "/fonts/jgs5/jgs5.ttf"],
+ ["public/_headers", "/manifest.json"],
+ ["public/_headers", "/favicon.ico"],
+ ["public/_headers", "/social/readme-card-58c75311e5ba.png"],
["blog/public/_headers", "/blog/article/hero.webp"],
["blog/public/_headers", "/alook.svg"],
- ])("keeps non-versioned images on a short policy: %s %s", (filename, pathname) => {
+ ["blog/public/_headers", "/manifest.json"],
+ ])("keeps non-versioned public resources on a short policy: %s %s", (filename, pathname) => {
expect(matchingPolicies(filename, pathname)).toEqual(["public, max-age=300, must-revalidate"])
})
diff --git a/src/web/src/app/serwist/[path]/route.test.ts b/src/web/src/app/serwist/[path]/route.test.ts
index 818d54034..f521b30a6 100644
--- a/src/web/src/app/serwist/[path]/route.test.ts
+++ b/src/web/src/app/serwist/[path]/route.test.ts
@@ -33,13 +33,13 @@ describe("public service worker route", () => {
expect(options.useNativeEsbuild).toBe(true)
expect(options.globPatterns).toEqual([
".next/static/chunks/**/*.{js,css}",
- ".next/static/media/*.{woff,woff2}",
- "public/alook.svg",
- "public/official-server-badge-flat.svg",
- "public/apple-touch-icon.png",
- "public/icon-192.png",
- "public/icon-512.png",
+ ".next/static/media/**/*.{woff,woff2,ttf,png,jpg,jpeg,webp,svg,ico}",
+ ".next/static/media/pdf.worker.min.*.mjs",
+ ".next/static/*/_{buildManifest,clientMiddlewareManifest,ssgManifest}.js",
+ "public/**/*.{svg,png,jpg,jpeg,webp,ttf,json}",
+ "src/app/favicon.ico",
])
+ expect(options.modifyURLPrefix).toEqual({ "src/app/favicon.ico": "/favicon.ico" })
expect(options.maximumFileSizeToCacheInBytes).toBe(10 * 1024 * 1024)
expect(options.esbuildOptions).toEqual({
format: "iife", sourcemap: false,
@@ -53,13 +53,21 @@ describe("public service worker route", () => {
".next/static/chunks/app-a.js",
".next/static/chunks/styles-a.css",
".next/static/media/font-a.woff2",
+ ".next/static/media/pdf.worker.min.35m0adryw15hm.mjs",
+ ".next/static/PGQscKRC0WsRFC5mS4zSH/_buildManifest.js",
+ ".next/static/PGQscKRC0WsRFC5mS4zSH/_clientMiddlewareManifest.js",
+ ".next/static/PGQscKRC0WsRFC5mS4zSH/_ssgManifest.js",
+ "/favicon.ico",
+ "public/manifest.json",
+ "public/fonts/jgs5/jgs5.ttf",
+ "public/contact/gus-photo.jpeg",
"public/alook.svg",
"public/official-server-badge-flat.svg",
"public/apple-touch-icon.png",
"public/icon-192.png",
"public/icon-512.png",
]
- const publicEntries = urls.map((url, i) => ({ url, revision: `public-${i}` }))
+ const publicEntries = urls.map((url, i) => ({ url, revision: `public-${i}`, size: i + 1 }))
const excluded = [
"public/sw.js", "public/unknown.png", "public/avatar.png",
".next/server/app/c.html", ".next/server/app/c.rsc",
@@ -67,7 +75,11 @@ describe("public service worker route", () => {
"/api/community/servers/server-a/avatar?v=1", "/api/files/private-a",
"https://other.test/_next/static/chunks/app-a.js",
".next/static/chunks/app-a.js?account=a",
- ].map((url, i) => ({ url, revision: `excluded-${i}` }))
+ ".next/static/media/other-worker.mjs",
+ ".next/static/media/pdf.worker.min.mjs",
+ ".next/static/PGQscKRC0WsRFC5mS4zSH/unknown.js",
+ ".next/static/private/nested/_buildManifest.js",
+ ].map((url, i) => ({ url, revision: `excluded-${i}`, size: i + 1 }))
const transform = options.manifestTransforms![0]
const result = await transform([...publicEntries, ...excluded])
expect(result).toEqual({ manifest: publicEntries, warnings: [] })
diff --git a/src/web/src/app/serwist/[path]/route.ts b/src/web/src/app/serwist/[path]/route.ts
index f8b851e85..75c23eaea 100644
--- a/src/web/src/app/serwist/[path]/route.ts
+++ b/src/web/src/app/serwist/[path]/route.ts
@@ -6,13 +6,13 @@ const serviceWorkerRoute = createSerwistRoute({
useNativeEsbuild: true,
globPatterns: [
".next/static/chunks/**/*.{js,css}",
- ".next/static/media/*.{woff,woff2}",
- "public/alook.svg",
- "public/official-server-badge-flat.svg",
- "public/apple-touch-icon.png",
- "public/icon-192.png",
- "public/icon-512.png",
+ ".next/static/media/**/*.{woff,woff2,ttf,png,jpg,jpeg,webp,svg,ico}",
+ ".next/static/media/pdf.worker.min.*.mjs",
+ ".next/static/*/_{buildManifest,clientMiddlewareManifest,ssgManifest}.js",
+ "public/**/*.{svg,png,jpg,jpeg,webp,ttf,json}",
+ "src/app/favicon.ico",
],
+ modifyURLPrefix: { "src/app/favicon.ico": "/favicon.ico" },
maximumFileSizeToCacheInBytes: 10 * 1024 * 1024,
manifestTransforms: [async entries => ({
manifest: entries.filter(entry => isPublicAssetPath(
diff --git a/src/web/src/app/sw.test.ts b/src/web/src/app/sw.test.ts
index bcdbd5010..a5851b7f7 100644
--- a/src/web/src/app/sw.test.ts
+++ b/src/web/src/app/sw.test.ts
@@ -1,8 +1,53 @@
import { afterEach, describe, expect, it, vi } from "vitest"
+import { CacheFirst, NetworkFirst, Serwist, Strategy } from "serwist"
+import { publicWorkerRuntimeCacheNames } from "../lib/service-worker/cache-identity"
-afterEach(() => { vi.unstubAllGlobals(); vi.resetModules() })
+afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); vi.unstubAllEnvs(); vi.resetModules() })
describe("public worker activation", () => {
+ it("registers actual Blog strategies while keeping precache and sensitive request boundaries", async () => {
+ vi.stubEnv("NODE_ENV", "production")
+ const listeners = vi.spyOn(Serwist.prototype, "addEventListeners")
+ const location = new URL("https://example.test/sw.js")
+ vi.stubGlobal("location", location)
+ vi.stubGlobal("self", { location, __SW_MANIFEST: [{ url: "/icon-192.png", revision: "fixture" }, { url: "/favicon.ico", revision: "fixture-favicon" }, { url: "/_next/static/chunks/example.js", revision: "fixture-code" }], registration: { scope: `${location.origin}/` }, addEventListener: vi.fn() })
+ await import("./sw")
+ const serwist = listeners.mock.contexts[0]
+ if (!(serwist instanceof Serwist)) throw new Error("Missing actual Serwist instance")
+ const matched = (path: string, init?: RequestInit) => {
+ const url = new URL(path, location)
+ const request = new Request(url, init)
+ return serwist.findMatchingRoute({ url, request, sameOrigin: url.origin === location.origin, event: new Event("fetch") as FetchEvent })
+ }
+ const match = (path: string, init?: RequestInit) => matched(path, init).route?.handler
+ const names = publicWorkerRuntimeCacheNames()
+ for (const [path, kind, StrategyClass] of [
+ ["/blog-static/_next/static/chunks/article.js", "code", CacheFirst],
+ ["/blog-static/_next/static/chunks/article.css", "code", CacheFirst],
+ ["/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/_buildManifest.js", "code", CacheFirst],
+ ["/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/_clientMiddlewareManifest.js", "code", CacheFirst],
+ ["/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/_ssgManifest.js", "code", CacheFirst],
+ ["/blog-static/_next/static/media/font.woff2", "fonts", CacheFirst],
+ ["/blog/_generated/0123456789abcdef-480.webp", "images", NetworkFirst],
+ ["/blog/example-article/hero.webp", "images", NetworkFirst],
+ ] as const) {
+ const handler = match(path)
+ expect(handler, path).toBeInstanceOf(StrategyClass)
+ expect((handler as Strategy).cacheName, path).toBe(names[kind])
+ expect(match(path, { headers: { Authorization: "fixture" } }), path).toBeUndefined()
+ expect(match(`${path}?_rsc=fixture`), path).toBeUndefined()
+ }
+ expect(match("/icon-192.png")).toBe(serwist.precacheStrategy)
+ for (const path of ["/favicon.ico?favicon.227o87hfvs4zs.ico", "/favicon.ico?favicon.abc123.ico"]) {
+ const result = matched(path)
+ expect(result.route?.handler, path).toBe(serwist.precacheStrategy)
+ expect(result.params, path).toEqual(expect.objectContaining({ cacheKey: serwist.getPrecacheKeyForUrl("/favicon.ico") }))
+ expect(match(path, { headers: { RSC: "1" } })).toBeUndefined()
+ }
+ for (const path of ["/favicon.ico?favicon.abc123.ico=", "/favicon.ico?favicon.abc123.ico&x=1", "/favicon.ico?%66avicon.abc123.ico", "/favicon.ico?__WB_REVISION__=fixture", "/_next/static/chunks/example.js?favicon.abc123.ico"]) expect(match(path), path).toBeUndefined()
+ expect(match("/icon-192.png", { headers: { Cookie: "fixture" } })).toBeUndefined()
+ for (const path of ["/api/auth/get-session", "/blog", "/blog/example-article", "/c/me", "https://other.test/blog/example-article/hero.webp"]) expect(match(path), path).toBeUndefined()
+ })
it("runs actual Serwist activation and deletes old custom cache names without changing the current cache", async () => {
const names = new Set(["alook-public-precache-v0", "alook-public-precache-v1", "alook-public-v99-runtime", "unrelated-cache"])
const storage = { keys: vi.fn(async () => [...names]), delete: vi.fn(async (name: string) => names.delete(name)),
diff --git a/src/web/src/app/sw.ts b/src/web/src/app/sw.ts
index dd47c9f92..602471119 100644
--- a/src/web/src/app/sw.ts
+++ b/src/web/src/app/sw.ts
@@ -1,8 +1,9 @@
/// <reference lib="webworker" />
import type { PrecacheEntry, SerwistGlobalConfig } from "serwist"
import { Serwist } from "serwist"
-import { isPublicAssetResponse, restrictPublicPrecacheRoutes } from "../lib/service-worker/public-cache"
+import { isPublicAssetResponse, publicFaviconQueryParameter, restrictPublicPrecacheRoutes } from "../lib/service-worker/public-cache"
import { publicWorkerCacheNames } from "../lib/service-worker/cache-identity"
+import { createPublicRuntimeCaching } from "../lib/service-worker/public-cache-runtime"
import { clearOutdatedPublicWorkerCaches } from "../lib/service-worker/public-cache-storage"
@@ -18,11 +19,16 @@ const serwist = new Serwist({
precacheEntries: self.__SW_MANIFEST,
cacheId: publicWorkerCacheNames().cacheId,
clientsClaim: true,
+ runtimeCaching: createPublicRuntimeCaching(self.location.origin, {
+ code: { maxEntries: 80, maxAgeSeconds: 30 * 24 * 60 * 60 },
+ fonts: { maxEntries: 160, maxAgeSeconds: 30 * 24 * 60 * 60 },
+ images: { maxEntries: 128, maxAgeSeconds: 7 * 24 * 60 * 60 },
+ }),
precacheOptions: {
cacheName: publicWorkerCacheNames().precache,
cleanURLs: false,
directoryIndex: null,
- ignoreURLParametersMatching: [],
+ ignoreURLParametersMatching: [publicFaviconQueryParameter],
plugins: [{
requestWillFetch: async ({ request }) => new Request(request, { credentials: "omit", redirect: "error" }),
cacheWillUpdate: async ({ request, response }) => isPublicAssetResponse(request, response) ? response : null,
diff --git a/src/web/src/lib/community/share-image-session.dom.test.ts b/src/web/src/lib/community/share-image-session.dom.test.ts
index f6a288dfa..0b82afe0a 100644
--- a/src/web/src/lib/community/share-image-session.dom.test.ts
+++ b/src/web/src/lib/community/share-image-session.dom.test.ts
@@ -78,6 +78,30 @@ function prepare(
})
}
+function measureTableLayout(bodyWidth = 240) {
+ vi.spyOn(Element.prototype, "clientWidth", "get").mockImplementation(function (this: Element) {
+ return this.hasAttribute("data-share-body-id") ? bodyWidth : 320
+ })
+ vi.mocked(HTMLElement.prototype.getBoundingClientRect).mockImplementation(function (this: HTMLElement) {
+ const width = this instanceof HTMLTableElement
+ ? Number(this.dataset.measuredWidth)
+ : this.hasAttribute("data-share-body-id")
+ ? bodyWidth
+ : this.matches("[data-share-card-source], [data-share-card]")
+ ? Number.parseFloat(this.style.width) || 320
+ : 320
+ return new DOMRect(0, 0, width, 180)
+ })
+}
+
+function cellContentNodes(element: Element): unknown[] {
+ return [...element.childNodes].map(node => ({
+ name: node.nodeName,
+ text: node.nodeValue,
+ children: node instanceof Element ? cellContentNodes(node) : [],
+ }))
+}
+
beforeEach(() => {
vi.mocked(getFontEmbedCSS).mockResolvedValue(FONT_CSS)
vi.stubGlobal("Image", DecodableImage)
@@ -116,6 +140,94 @@ describe("prepareShareImageSession", () => {
expect(Object.isFrozen(prepared)).toBe(true)
})
+ it("freezes every rich table cell without clipping and widens only for the largest overflow", async () => {
+ measureTableLayout()
+ const longToken = "complete-unbroken-cell-content".repeat(12)
+ const source = sourceCard(`
+ <div data-share-body-id="table-body" style="max-height:48px;-webkit-line-clamp:2;display:-webkit-box;overflow:hidden">
+ <div data-table-viewport style="max-height:24px;overflow:auto">
+ <table data-measured-width="320.4">
+ <thead><tr><th>First column</th><th>Second column</th></tr></thead>
+ <tbody>
+ <tr><td>before <strong>emphasis</strong> <a href="/about">link</a> after</td><td>${longToken}</td></tr>
+ <tr><td>complete last row</td><td><code>final rich content</code></td></tr>
+ </tbody>
+ </table>
+ </div>
+ <div data-table-viewport style="max-height:24px;overflow:hidden">
+ <table data-measured-width="270.2"><tbody><tr><td>second complete table</td></tr></tbody></table>
+ </div>
+ </div>
+ `)
+ const original = source.outerHTML
+ const expectedCells = [...source.querySelectorAll("th,td")].map(cell => ({ text: cell.textContent, content: cellContentNodes(cell) }))
+ const expectedRows = source.querySelectorAll("tr").length
+
+ const prepared = await prepare(source)
+ const output = document.createElement("div")
+ output.innerHTML = prepared.markup
+ const frozen = output.firstElementChild as HTMLElement
+ const body = frozen.querySelector<HTMLElement>("[data-share-body-id]")!
+ const cells = [...frozen.querySelectorAll<HTMLElement>("th,td")]
+
+ expect(source.outerHTML).toBe(original)
+ expect(frozen.querySelectorAll("tr")).toHaveLength(expectedRows)
+ expect(cells.map(cell => cell.textContent)).toEqual(expectedCells.map(cell => cell.text))
+ expect(frozen.textContent).toContain(longToken)
+ expect(frozen.textContent).toContain("complete last row")
+ expect(body.style.maxHeight).toBe("none")
+ expect(body.style.display).toBe("block")
+ expect(body.style.overflow).toBe("visible")
+ for (const viewport of frozen.querySelectorAll<HTMLElement>("[data-table-viewport]")) {
+ expect(viewport.style.maxHeight).toBe("none")
+ expect(viewport.style.overflow).toBe("visible")
+ }
+ for (const table of frozen.querySelectorAll("table")) expect(table.style.tableLayout).toBe("auto")
+ for (const [index, cell] of cells.entries()) {
+ const content = cell.firstElementChild as HTMLElement
+ expect(cellContentNodes(content)).toEqual(expectedCells[index]!.content)
+ expect(content.style.maxWidth).toBe("240px")
+ expect(content.style.whiteSpace).toBe("normal")
+ expect(content.style.overflowWrap).toBe("anywhere")
+ expect(content.style.wordBreak).toBe("normal")
+ }
+ expect(frozen.style.width).toBe("401px")
+ expect(prepared.width).toBe(401)
+ expect(Object.isFrozen(prepared)).toBe(true)
+ })
+
+ it("uses the card width for a table without a body container and keeps a fitting card unchanged", async () => {
+ measureTableLayout()
+ const source = sourceCard(`
+ <div data-table-viewport style="max-height:24px;overflow:auto">
+ <table data-measured-width="200"><tbody><tr><td>root <em>fallback</em> content</td></tr></tbody></table>
+ </div>
+ `)
+ const original = source.outerHTML
+ const originalContent = cellContentNodes(source.querySelector("td")!)
+
+ const prepared = await prepare(source)
+ const output = document.createElement("div")
+ output.innerHTML = prepared.markup
+ const frozen = output.firstElementChild as HTMLElement
+ const viewport = frozen.querySelector<HTMLElement>("[data-table-viewport]")!
+ const cell = frozen.querySelector("td")!
+ const content = cell.firstElementChild as HTMLElement
+
+ expect(source.outerHTML).toBe(original)
+ expect(frozen.querySelector("[data-share-body-id]")).toBeNull()
+ expect(frozen.querySelector("table")!.style.tableLayout).toBe("auto")
+ expect(viewport.style.maxHeight).toBe("none")
+ expect(viewport.style.overflow).toBe("visible")
+ expect(cell.textContent).toBe("root fallback content")
+ expect(cellContentNodes(content)).toEqual(originalContent)
+ expect(content.style.maxWidth).toBe("320px")
+ expect(content.style.whiteSpace).toBe("normal")
+ expect(content.style.overflowWrap).toBe("anywhere")
+ expect(frozen.style.width).toBe("320px")
+ expect(prepared.width).toBe(320)
+ })
+
it("reuses loaded pixels once for deferred duplicate images without fetching or decoding source bytes", async () => {
const live = document.createElement("img")
live.src = "/loaded.png"
diff --git a/src/web/src/lib/service-worker/cache-identity.ts b/src/web/src/lib/service-worker/cache-identity.ts
index 3563c925b..233de679f 100644
--- a/src/web/src/lib/service-worker/cache-identity.ts
+++ b/src/web/src/lib/service-worker/cache-identity.ts
@@ -8,3 +8,12 @@ export function publicWorkerCacheNames() {
export function isPublicWorkerCacheName(name: string) {
return name.startsWith(publicCacheIdentity.namespace)
}
+
+export function publicWorkerRuntimeCacheNames() {
+ const { cacheId } = publicWorkerCacheNames()
+ return { code: `${cacheId}-blog-code`, fonts: `${cacheId}-blog-fonts`, images: `${cacheId}-blog-images` }
+}
+
+export function isPublicWorkerRuntimeCacheName(name: string) {
+ return isPublicWorkerCacheName(name) && /^v\d+-blog-(?:code|fonts|images)$/.test(name.slice(publicCacheIdentity.namespace.length))
+}
diff --git a/src/web/src/lib/service-worker/public-cache-runtime.test.ts b/src/web/src/lib/service-worker/public-cache-runtime.test.ts
new file mode 100644
index 000000000..afd7c9821
--- /dev/null
+++ b/src/web/src/lib/service-worker/public-cache-runtime.test.ts
@@ -0,0 +1,319 @@
+// @vitest-environment jsdom
+import * as fakeIndexedDB from "fake-indexeddb"
+import { CacheExpiration, CacheFirst, ExpirationPlugin, NetworkFirst, Strategy, type RuntimeCaching } from "serwist"
+import { afterEach, describe, expect, it, vi } from "vitest"
+import { publicWorkerRuntimeCacheNames } from "./cache-identity"
+import { createPublicRuntimeCaching, type PublicRuntimeLimits } from "./public-cache-runtime"
+
+const origin = "https://alook.test"
+const code = "/blog-static/_next/static/chunks/article-a.js"
+const font = "/blog-static/_next/static/media/font-a.woff2"
+const image = "/blog/example-article/hero.webp"
+const artificialLimits: PublicRuntimeLimits = {
+ code: { maxEntries: 2, maxAgeSeconds: 60 },
+ fonts: { maxEntries: 2, maxAgeSeconds: 60 },
+ images: { maxEntries: 2, maxAgeSeconds: 60 },
+}
+
+class WorkerFetchEvent extends Event {
+ readonly pending: Promise<unknown>[] = []
+ #request: Request
+ constructor(request: Request) { super("fetch"); this.#request = request }
+ get request() { return this.#request }
+ waitUntil(promise: Promise<unknown>) { this.pending.push(promise) }
+}
+
+const urlOf = (request: RequestInfo | URL) => typeof request === "string" ? new URL(request, origin).href : request instanceof URL ? request.href : request.url
+const request = (path: string, init?: RequestInit) => new Request(new URL(path, origin), init)
+const response = (body: string, type = "application/javascript", headers?: Record<string, string>, status = 200) => new Response(body, {
+ status, headers: { "Content-Type": type, "Cache-Control": "public, max-age=300", Date: new Date(Date.now()).toUTCString(), ...headers },
+})
+
+function fixture() {
+ for (const [name, value] of Object.entries(fakeIndexedDB)) if (name.startsWith("IDB")) vi.stubGlobal(name, value)
+ vi.stubGlobal("indexedDB", new fakeIndexedDB.IDBFactory())
+ vi.stubGlobal("ExtendableEvent", WorkerFetchEvent)
+ vi.stubGlobal("FetchEvent", WorkerFetchEvent)
+ vi.stubGlobal("__WB_DISABLE_DEV_LOGS", true)
+ const entries = new Map<string, Map<string, Response>>()
+ const operations: Array<{ operation: string; cache: string; url: string; date: string | null }> = []
+ vi.stubGlobal("caches", {
+ match: async (key: RequestInfo, options: MultiCacheQueryOptions) => entries.get(options.cacheName!)?.get(urlOf(key))?.clone(),
+ open: async (name: string) => {
+ if (!entries.has(name)) entries.set(name, new Map())
+ const stored = entries.get(name)!
+ return {
+ match: async (key: RequestInfo) => stored.get(urlOf(key))?.clone(),
+ put: async (key: RequestInfo, value: Response) => {
+ operations.push({ operation: "put", cache: name, url: urlOf(key), date: value.headers.get("Date") })
+ stored.set(urlOf(key), value.clone())
+ },
+ delete: async (key: RequestInfo) => {
+ operations.push({ operation: "delete", cache: name, url: urlOf(key), date: stored.get(urlOf(key))?.headers.get("Date") ?? null })
+ return stored.delete(urlOf(key))
+ },
+ keys: async () => [...stored.keys()].map(url => new Request(url)),
+ }
+ },
+ })
+ const network = vi.fn<typeof fetch>(async () => response("first"))
+ vi.stubGlobal("fetch", network)
+ const routes = createPublicRuntimeCaching(origin, artificialLimits)
+ const strategy = (kind: keyof PublicRuntimeLimits) => {
+ const handler = routes.find(route => route.handler instanceof Strategy && route.handler.cacheName === publicWorkerRuntimeCacheNames()[kind])?.handler
+ if (!(handler instanceof Strategy)) throw new Error(`Missing runtime strategy: ${kind}`)
+ return handler
+ }
+ return { entries, network, routes, strategy, operations }
+}
+
+function matches(routes: RuntimeCaching[], req: Request) {
+ const event = new WorkerFetchEvent(req) as unknown as FetchEvent
+ return routes.filter(route => typeof route.matcher === "function" && route.matcher({ request: req, url: new URL(req.url), sameOrigin: new URL(req.url).origin === origin, event }))
+}
+
+async function read(strategy: Strategy, path: string) {
+ const req = request(path)
+ const event = new WorkerFetchEvent(req)
+ const results = await Promise.allSettled(strategy.handleAll({ request: req, event: event as unknown as FetchEvent }))
+ const lifetime = await Promise.allSettled(event.pending)
+ for (const result of [...results, ...lifetime]) if (result.status === "rejected") throw result.reason
+ return (results[0] as PromiseFulfilledResult<Response>).value
+}
+
+afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); vi.unstubAllEnvs() })
+
+describe("official public runtime strategies with artificial limits", () => {
+ it.each([
+ ["code", "maxEntries", 0], ["fonts", "maxEntries", -1], ["images", "maxEntries", 1.5],
+ ["code", "maxEntries", Number.POSITIVE_INFINITY], ["fonts", "maxEntries", Number.NaN], ["images", "maxEntries", Number.MAX_SAFE_INTEGER + 1],
+ ["code", "maxAgeSeconds", 0], ["fonts", "maxAgeSeconds", -1], ["images", "maxAgeSeconds", Number.POSITIVE_INFINITY], ["code", "maxAgeSeconds", Number.NaN],
+ ] as const)("requires a bounded positive runtime policy: %s %s %s", (kind, field, value) => {
+ fixture()
+ const limits = { ...artificialLimits, [kind]: { ...artificialLimits[kind], [field]: value } }
+ expect(() => createPublicRuntimeCaching(origin, limits)).toThrow(RangeError)
+ })
+ it("routes each public Blog type once while leaving Main assets to precache", () => {
+ const { routes, strategy } = fixture()
+ expect(strategy("code")).toBeInstanceOf(CacheFirst)
+ expect(strategy("fonts")).toBeInstanceOf(CacheFirst)
+ expect(strategy("images")).toBeInstanceOf(NetworkFirst)
+ for (const path of [code, font, image]) expect(matches(routes, request(path))).toHaveLength(1)
+ for (const path of ["/_next/static/chunks/main.js", "/favicon.ico", "/blog", "/blog/example-article", "/blog/example-article/source/hero.png", "/api/auth/get-session", `${code}?_rsc=one`]) {
+ expect(matches(routes, request(path))).toHaveLength(0)
+ }
+ expect(matches(routes, request(`https://other.test${code}`))).toHaveLength(0)
+ expect(matches(routes, request(code, { method: "POST" }))).toHaveLength(0)
+ const sensitiveHeaders: HeadersInit[] = [{ Authorization: "fixture" }, { Cookie: "fixture" }, { RSC: "1" }, { "Next-Router-State-Tree": "[]" }, { Accept: "text/x-component" }]
+ for (const headers of sensitiveHeaders) {
+ expect(matches(routes, request(code, { headers }))).toHaveLength(0)
+ }
+ const navigation = request(code)
+ Object.defineProperty(navigation, "mode", { value: "navigate" })
+ expect(matches(routes, navigation)).toHaveLength(0)
+ })
+
+ it.each([["code", code, "application/javascript"], ["fonts", font, "font/woff2"]] as const)("serves a warmed fingerprinted %s URL without another network request", async (kind, path, type) => {
+ const { strategy, network } = fixture()
+ network.mockImplementation(async () => response("first", type))
+ expect(await (await read(strategy(kind), path)).text()).toBe("first")
+ network.mockImplementation(async () => response("changed", type))
+ expect(await (await read(strategy(kind), path)).text()).toBe("first")
+ expect(network).toHaveBeenCalledTimes(1)
+ const [, options] = network.mock.calls[0]
+ expect(options).toMatchObject({ credentials: "omit", redirect: "error" })
+ })
+
+ it("revalidates a non-fingerprinted image at the same URL and falls back to its last public bytes offline", async () => {
+ const { strategy, network, entries } = fixture()
+ network.mockImplementationOnce(async () => response("first image", "image/webp"))
+ expect(await (await read(strategy("images"), image)).text()).toBe("first image")
+ network.mockImplementationOnce(async () => response("new image", "image/webp"))
+ expect(await (await read(strategy("images"), image)).text()).toBe("new image")
+ expect(await entries.get(publicWorkerRuntimeCacheNames().images)?.get(new URL(image, origin).href)?.clone().text()).toBe("new image")
+ network.mockRejectedValueOnce(new TypeError("offline"))
+ expect(await (await read(strategy("images"), image)).text()).toBe("new image")
+ expect(network).toHaveBeenCalledTimes(3)
+ for (const [, options] of network.mock.calls) expect(options).toMatchObject({ credentials: "omit", redirect: "error", cache: "no-cache" })
+ })
+
+ it.each(["_buildManifest.js", "_clientMiddlewareManifest.js", "_ssgManifest.js"])("persists the actual public Blog helper %s in its CacheFirst code cache", async name => {
+ const { routes, strategy, network, entries } = fixture()
+ const path = `/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/${name}`
+ expect(matches(routes, request(path))).toHaveLength(1)
+ const handler = strategy("code")
+ expect(handler).toBeInstanceOf(CacheFirst)
+ expect(handler.cacheName).toBe(publicWorkerRuntimeCacheNames().code)
+ network.mockImplementationOnce(async () => response(`public ${name}`))
+ expect(await (await read(handler, path)).text()).toBe(`public ${name}`)
+ expect(await entries.get(handler.cacheName)?.get(new URL(path, origin).href)?.clone().text()).toBe(`public ${name}`)
+ network.mockRejectedValueOnce(new TypeError("offline"))
+ expect(await (await read(handler, path)).text()).toBe(`public ${name}`)
+ expect(network).toHaveBeenCalledTimes(1)
+ })
+
+ it.each(["_buildManifest.js", "_clientMiddlewareManifest.js", "_ssgManifest.js"])("does not retain an HTML response at the public Blog helper %s URL", async name => {
+ const { strategy, network, entries } = fixture()
+ const path = `/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/${name}`
+ network.mockImplementationOnce(async () => response("HTML", "text/html"))
+ expect(await (await read(strategy("code"), path)).text()).toBe("HTML")
+ expect(entries.get(publicWorkerRuntimeCacheNames().code)?.has(new URL(path, origin).href) ?? false).toBe(false)
+ network.mockRejectedValueOnce(new TypeError("offline"))
+ await expect(read(strategy("code"), path)).rejects.toThrow()
+ })
+
+ it.each([
+ ["private", () => response("private", "image/webp", { "Cache-Control": "private" })],
+ ["no-store", () => response("private", "image/webp", { "Cache-Control": "public, no-store" })],
+ ["html", () => response("HTML", "text/html")],
+ ["RSC", () => response("RSC", "text/x-component")],
+ ["404", () => response("missing", "image/webp", undefined, 404)],
+ ["opaque", () => {
+ const opaque = response("opaque", "image/webp")
+ Object.defineProperties(opaque, { status: { value: 0 }, type: { value: "opaque" } })
+ opaque.clone = () => opaque
+ return opaque
+ }],
+ ] as const)("does not persist a %s network response through NetworkFirst's default permissive path", async (_name, makeResponse) => {
+ const { strategy, network, entries } = fixture()
+ network.mockImplementationOnce(async () => makeResponse())
+ await read(strategy("images"), image)
+ expect(entries.get(publicWorkerRuntimeCacheNames().images)?.size ?? 0).toBe(0)
+ network.mockRejectedValueOnce(new TypeError("offline"))
+ await expect(read(strategy("images"), image)).rejects.toThrow()
+ })
+
+ it("evicts the oldest entry at an artificial entry limit without evicting another runtime class", async () => {
+ const { strategy, network, entries } = fixture()
+ let now = Date.now()
+ vi.spyOn(Date, "now").mockImplementation(() => now)
+ network.mockImplementation(async input => response("public", urlOf(input).endsWith("woff2") ? "font/woff2" : "application/javascript"))
+ await read(strategy("fonts"), font)
+ for (const suffix of ["a", "b", "c"]) {
+ now += 1000
+ await read(strategy("code"), `/blog-static/_next/static/chunks/${suffix}.js`)
+ }
+ expect([...entries.get(publicWorkerRuntimeCacheNames().code)!.keys()]).toEqual(["b", "c"].map(suffix => `${origin}/blog-static/_next/static/chunks/${suffix}.js`))
+ expect(entries.get(publicWorkerRuntimeCacheNames().fonts)?.has(new URL(font, origin).href)).toBe(true)
+ })
+
+ it("rejects an aged cached response and fetches new bytes under an artificial age limit", async () => {
+ const { strategy, network, entries, operations } = fixture()
+ let now = Date.now()
+ vi.spyOn(Date, "now").mockImplementation(() => now)
+ network.mockImplementationOnce(async () => response("old font", "font/woff2"))
+ expect(await (await read(strategy("fonts"), font)).text()).toBe("old font")
+ now += 61_000
+ network.mockImplementationOnce(async () => response("new font", "font/woff2"))
+ expect(await (await read(strategy("fonts"), font)).text()).toBe("new font")
+ expect(network).toHaveBeenCalledTimes(2)
+ expect(await entries.get(publicWorkerRuntimeCacheNames().fonts)?.get(new URL(font, origin).href)?.clone().text(), JSON.stringify(operations)).toBe("new font")
+ })
+
+ it("does not return an aged cached font when its network request also fails", async () => {
+ const { strategy, network, entries } = fixture()
+ let now = Date.now()
+ vi.spyOn(Date, "now").mockImplementation(() => now)
+ network.mockImplementationOnce(async () => response("old font", "font/woff2"))
+ await read(strategy("fonts"), font)
+ now += 61_000
+ network.mockRejectedValueOnce(new TypeError("offline"))
+ await expect(read(strategy("fonts"), font)).rejects.toThrow()
+ expect(entries.get(publicWorkerRuntimeCacheNames().fonts)?.has(new URL(font, origin).href)).toBe(false)
+ })
+
+ it("finishes the scheduled stale scan before fetching and storing replacement bytes", async () => {
+ const { strategy, network, entries, operations } = fixture()
+ let now = Date.now()
+ vi.spyOn(Date, "now").mockImplementation(() => now)
+ network.mockImplementationOnce(async () => response("old font", "font/woff2"))
+ await read(strategy("fonts"), font)
+ now += 61_000
+ const staleCleanupStarted = Promise.withResolvers<void>()
+ const releaseStaleCleanup = Promise.withResolvers<void>()
+ const originalExpire = CacheExpiration.prototype.expireEntries
+ let staleCleanupScheduled = false
+ let staleCleanupFinished = false
+ vi.spyOn(CacheExpiration.prototype, "expireEntries").mockImplementation(async function (this: CacheExpiration) {
+ if (!staleCleanupScheduled) {
+ staleCleanupScheduled = true
+ staleCleanupStarted.resolve()
+ await releaseStaleCleanup.promise
+ await originalExpire.call(this)
+ staleCleanupFinished = true
+ } else await originalExpire.call(this)
+ })
+ let networkStartedAfterCleanup = false
+ network.mockImplementationOnce(async () => {
+ networkStartedAfterCleanup = staleCleanupFinished
+ return response("new font", "font/woff2")
+ })
+ const refreshed = read(strategy("fonts"), font)
+ await staleCleanupStarted.promise
+ releaseStaleCleanup.resolve()
+ expect(await (await refreshed).text()).toBe("new font")
+ expect(networkStartedAfterCleanup).toBe(true)
+ expect(network).toHaveBeenCalledTimes(2)
+ expect(await entries.get(publicWorkerRuntimeCacheNames().fonts)?.get(new URL(font, origin).href)?.clone().text(), JSON.stringify(operations)).toBe("new font")
+ })
+
+ it("still fetches fresh bytes when stale cleanup rejects and preserves the event failure", async () => {
+ const { strategy, network, entries } = fixture()
+ let now = Date.now()
+ vi.spyOn(Date, "now").mockImplementation(() => now)
+ network.mockImplementationOnce(async () => response("old font", "font/woff2"))
+ await read(strategy("fonts"), font)
+ now += 61_000
+ const cleanupFailure = new Error("expiry storage unavailable")
+ vi.spyOn(CacheExpiration.prototype, "expireEntries").mockRejectedValueOnce(cleanupFailure)
+ network.mockImplementationOnce(async () => response("new font", "font/woff2"))
+ const req = request(font)
+ const event = new WorkerFetchEvent(req)
+ const results = await Promise.allSettled(strategy("fonts").handleAll({ request: req, event: event as unknown as FetchEvent }))
+ const lifetime = await Promise.allSettled(event.pending)
+ expect(results[0].status).toBe("fulfilled")
+ if (results[0].status !== "fulfilled") throw results[0].reason
+ expect(await results[0].value.text()).toBe("new font")
+ expect(network).toHaveBeenCalledTimes(2)
+ expect(await entries.get(publicWorkerRuntimeCacheNames().fonts)?.get(req.url)?.clone().text()).toBe("new font")
+ expect(lifetime).toContainEqual({ status: "rejected", reason: cleanupFailure })
+ })
+
+ it("preserves the native request getter when the SDK handles a rejected waitUntil registration", async () => {
+ vi.stubEnv("NODE_ENV", "development")
+ const { strategy, network, entries } = fixture()
+ let now = Date.now()
+ vi.spyOn(Date, "now").mockImplementation(() => now)
+ network.mockImplementationOnce(async () => response("old font", "font/woff2"))
+ await read(strategy("fonts"), font)
+ now += 61_000
+ network.mockImplementationOnce(async () => response("new font", "font/woff2"))
+ const req = request(font)
+ const event = new WorkerFetchEvent(req)
+ const requestGetter = vi.spyOn(event, "request", "get")
+ const originalCachedResponseWillBeUsed = ExpirationPlugin.prototype.cachedResponseWillBeUsed
+ let callbackRequestReads = 0
+ vi.spyOn(ExpirationPlugin.prototype, "cachedResponseWillBeUsed").mockImplementation(function (this: ExpirationPlugin, options) {
+ const before = requestGetter.mock.calls.length
+ const result = originalCachedResponseWillBeUsed.call(this, options)
+ callbackRequestReads += requestGetter.mock.calls.length - before
+ return result
+ })
+ const originalWaitUntil = event.waitUntil.bind(event)
+ let registrations = 0
+ vi.spyOn(event, "waitUntil").mockImplementation(promise => {
+ if (++registrations === 2) {
+ throw new DOMException("event lifetime already closed", "InvalidStateError")
+ }
+ originalWaitUntil(promise)
+ })
+ const results = await Promise.allSettled(strategy("fonts").handleAll({ request: req, event: event as unknown as FetchEvent }))
+ const lifetime = await Promise.allSettled(event.pending)
+ for (const result of [...results, ...lifetime]) if (result.status === "rejected") throw result.reason
+ expect(callbackRequestReads).toBe(1)
+ expect(registrations).toBe(2)
+ expect(await (results[0] as PromiseFulfilledResult<Response>).value.text()).toBe("new font")
+ expect(network).toHaveBeenCalledTimes(2)
+ expect(await entries.get(publicWorkerRuntimeCacheNames().fonts)?.get(req.url)?.clone().text()).toBe("new font")
+ })
+})
diff --git a/src/web/src/lib/service-worker/public-cache-runtime.ts b/src/web/src/lib/service-worker/public-cache-runtime.ts
new file mode 100644
index 000000000..de67dd658
--- /dev/null
+++ b/src/web/src/lib/service-worker/public-cache-runtime.ts
@@ -0,0 +1,46 @@
+import { CacheFirst, ExpirationPlugin, NetworkFirst, type RuntimeCaching, type SerwistPlugin } from "serwist"
+import { publicWorkerRuntimeCacheNames } from "./cache-identity"
+import { isPublicAssetRequest, isPublicAssetResponse, publicRuntimeAssetKind } from "./public-cache"
+
+export type PublicRuntimeLimits = Record<keyof ReturnType<typeof publicWorkerRuntimeCacheNames>, { maxEntries: number; maxAgeSeconds: number }>
+
+function waitForStaleExpiration(expiration: ExpirationPlugin): SerwistPlugin {
+ return {
+ cachedResponseWillBeUsed: async options => {
+ const pending: Promise<unknown>[] = []
+ const event = new Proxy(options.event, {
+ get(target, property) {
+ if (property === "waitUntil") return (promise: Promise<unknown>) => {
+ pending.push(promise)
+ target.waitUntil(promise)
+ }
+ return Reflect.get(target, property, target)
+ },
+ })
+ const response = expiration.cachedResponseWillBeUsed({ ...options, event })
+ if (!response) await Promise.allSettled(pending)
+ return response
+ },
+ cacheDidUpdate: options => expiration.cacheDidUpdate(options),
+ }
+}
+
+export function createPublicRuntimeCaching(origin: string, limits: PublicRuntimeLimits): RuntimeCaching[] {
+ const names = publicWorkerRuntimeCacheNames()
+ for (const kind of ["code", "fonts", "images"] as const) {
+ const { maxEntries, maxAgeSeconds } = limits[kind]
+ if (!Number.isSafeInteger(maxEntries) || maxEntries <= 0 || !Number.isFinite(maxAgeSeconds) || maxAgeSeconds <= 0) throw new RangeError(`Invalid public runtime limits: ${kind}`)
+ }
+ return (["code", "fonts", "images"] as const).map<RuntimeCaching>(kind => ({
+ method: "GET",
+ matcher: ({ request, url }) => isPublicAssetRequest(request, origin) && publicRuntimeAssetKind(url.pathname) === kind,
+ handler: new (kind === "images" ? NetworkFirst : CacheFirst)({
+ cacheName: names[kind],
+ fetchOptions: { credentials: "omit", redirect: "error", ...(kind === "images" ? { cache: "no-cache" as const } : {}) },
+ plugins: [
+ { cacheWillUpdate: async ({ request, response }) => isPublicAssetResponse(request, response) ? response : null },
+ waitForStaleExpiration(new ExpirationPlugin({ ...limits[kind] })),
+ ],
+ }),
+ }))
+}
diff --git a/src/web/src/lib/service-worker/public-cache-storage.test.ts b/src/web/src/lib/service-worker/public-cache-storage.test.ts
index e516f9486..7c811fe1b 100644
--- a/src/web/src/lib/service-worker/public-cache-storage.test.ts
+++ b/src/web/src/lib/service-worker/public-cache-storage.test.ts
@@ -1,8 +1,14 @@
+// @vitest-environment jsdom
+import * as fakeIndexedDB from "fake-indexeddb"
+import { openDB } from "idb"
+import { CacheExpiration } from "serwist"
import { afterEach, describe, expect, it, vi } from "vitest"
import { publicWorkerCacheNames } from "./cache-identity"
import { clearOutdatedPublicWorkerCaches, clearPublicWorkerCaches, getPublicWorkerCacheSizeBytes } from "./public-cache-storage"
function fixture() {
+ for (const [name, value] of Object.entries(fakeIndexedDB)) if (name.startsWith("IDB")) vi.stubGlobal(name, value)
+ vi.stubGlobal("indexedDB", new fakeIndexedDB.IDBFactory())
const response = new Response("你好")
const entries = new Map(["alook-public-precache-v0", "alook-public-precache-v1", "alook-public-v99-runtime", "unrelated-cache"].map((name) => [name, new Map([["https://alook.test/file", response.clone()]])]))
const storage = { keys: vi.fn(async () => [...entries.keys()]), delete: vi.fn(async (name: string) => entries.delete(name)),
@@ -12,7 +18,13 @@ function fixture() {
return { entries, storage }
}
-afterEach(() => vi.unstubAllGlobals())
+afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals() })
+
+async function timestamp(name: string) {
+ const expiration = new CacheExpiration(name, { maxAgeSeconds: 3600 })
+ await expiration.updateTimestamp("https://alook.test/blog/example/hero.webp")
+ return () => expiration.isURLExpired("https://alook.test/blog/example/hero.webp")
+}
describe("owned public worker cache family", () => {
it("keeps both existing worker names while sizing every version in the owned family", async () => {
@@ -94,4 +106,94 @@ describe("owned public worker cache family", () => {
await expect(getPublicWorkerCacheSizeBytes(second.signal)).rejects.toMatchObject({ name: "AbortError" })
expect(storage.open).not.toHaveBeenCalled()
})
+
+ it("clears current and enumerated old runtime timestamps while preserving other metadata", async () => {
+ const { entries } = fixture()
+ const owned = ["alook-public-v1-blog-code", "alook-public-v1-blog-fonts", "alook-public-v1-blog-images", "alook-public-v0-blog-images"]
+ for (const name of owned) entries.set(name, new Map())
+ const removed = await Promise.all(owned.map(timestamp))
+ const unrelated = await timestamp("another-worker-blog-images")
+ const unknown = await timestamp("alook-public-v1-another-plugin")
+ await clearPublicWorkerCaches()
+ expect(await Promise.all(removed.map(expired => expired()))).toEqual([true, true, true, true])
+ expect(await unrelated()).toBe(false)
+ expect(await unknown()).toBe(false)
+ expect([...entries.keys()]).toEqual(["unrelated-cache"])
+ })
+
+ it("clears current orphan timestamps even when their Cache Storage entries are absent", async () => {
+ const { entries } = fixture()
+ entries.clear()
+ const expired = await timestamp("alook-public-v1-blog-code")
+ await clearPublicWorkerCaches()
+ expect(await expired()).toBe(true)
+ expect([...entries.keys()]).toEqual([])
+ })
+
+ it("keeps current runtime caches and timestamps on activation while removing an old version", async () => {
+ const { entries } = fixture()
+ const currentNames = ["alook-public-v1-blog-code", "alook-public-v1-blog-fonts", "alook-public-v1-blog-images"]
+ const oldNames = ["alook-public-v0-blog-code", "alook-public-v0-blog-fonts", "alook-public-v0-blog-images"]
+ for (const name of [...currentNames, ...oldNames]) entries.set(name, new Map())
+ const current = await Promise.all(currentNames.map(timestamp))
+ const old = await Promise.all(oldNames.map(timestamp))
+ const unrelated = await timestamp("another-worker-blog-images")
+ await clearOutdatedPublicWorkerCaches()
+ expect(await Promise.all(current.map(expired => expired()))).toEqual([false, false, false])
+ expect(await Promise.all(old.map(expired => expired()))).toEqual([true, true, true])
+ expect(await unrelated()).toBe(false)
+ for (const name of currentNames) expect(entries.has(name)).toBe(true)
+ for (const name of oldNames) expect(entries.has(name)).toBe(false)
+ })
+
+ it("can repeat clearing with missing caches and missing timestamps without deleting the shared metadata DB", async () => {
+ const { entries } = fixture()
+ entries.clear()
+ const unrelated = await timestamp("another-worker-blog-images")
+ const deleteDatabase = vi.spyOn(indexedDB, "deleteDatabase")
+ await clearPublicWorkerCaches()
+ await clearPublicWorkerCaches()
+ expect(await unrelated()).toBe(false)
+ expect(deleteDatabase).not.toHaveBeenCalled()
+ expect(entries.size).toBe(0)
+ })
+
+ it("finishes cache and other metadata deletions before reporting a metadata API error", async () => {
+ const { entries } = fixture()
+ const other = await timestamp("alook-public-v1-blog-images")
+ const failed = new Error("timestamp storage unavailable")
+ vi.spyOn(CacheExpiration.prototype, "delete").mockRejectedValueOnce(failed)
+ await expect(clearPublicWorkerCaches()).rejects.toBe(failed)
+ expect(await other()).toBe(true)
+ expect([...entries.keys()]).toEqual(["unrelated-cache"])
+ })
+
+ it("still clears metadata when a Cache Storage deletion fails", async () => {
+ const { storage } = fixture()
+ const expired = await timestamp("alook-public-v1-blog-code")
+ const failed = new Error("cache deletion unavailable")
+ storage.delete.mockRejectedValueOnce(failed)
+ await expect(clearPublicWorkerCaches()).rejects.toBe(failed)
+ expect(await expired()).toBe(true)
+ })
+
+ it("propagates IndexedDB open failure after finishing the Cache Storage deletions", async () => {
+ const { entries } = fixture()
+ const failed = new Error("IndexedDB access denied")
+ vi.spyOn(indexedDB, "open").mockImplementation(() => { throw failed })
+ await expect(clearPublicWorkerCaches()).rejects.toBe(failed)
+ expect([...entries.keys()]).toEqual(["unrelated-cache"])
+ })
+
+ it("does not change business IndexedDB contents while clearing public runtime metadata", async () => {
+ fixture()
+ const business = await openDB("keyval-store", 1, { upgrade(db) { db.createObjectStore("keyval") } })
+ try {
+ await business.put("keyval", { account: "fixture-only", schema: 6 }, "alook:qc:cache")
+ const expired = await timestamp("alook-public-v1-blog-fonts")
+ await clearPublicWorkerCaches()
+ expect(await expired()).toBe(true)
+ expect(await business.get("keyval", "alook:qc:cache")).toEqual({ account: "fixture-only", schema: 6 })
+ } finally { business.close() }
+ })
})
diff --git a/src/web/src/lib/service-worker/public-cache-storage.ts b/src/web/src/lib/service-worker/public-cache-storage.ts
index f8094188b..a825d1457 100644
--- a/src/web/src/lib/service-worker/public-cache-storage.ts
+++ b/src/web/src/lib/service-worker/public-cache-storage.ts
@@ -1,4 +1,4 @@
-import { isPublicWorkerCacheName, publicWorkerCacheNames } from "./cache-identity"
+import { isPublicWorkerCacheName, isPublicWorkerRuntimeCacheName, publicWorkerCacheNames, publicWorkerRuntimeCacheNames } from "./cache-identity"
function assertRead(signal?: AbortSignal) {
if (signal?.aborted) throw new DOMException("Cancelled public cache read", "AbortError")
@@ -25,7 +25,15 @@ export async function getPublicWorkerCacheSizeBytes(signal?: AbortSignal): Promi
async function deletePublicWorkerCaches(keep: (name: string) => boolean): Promise<void> {
if (typeof caches === "undefined") return
const names = await caches.keys()
- const results = await Promise.allSettled(names.filter(name => isPublicWorkerCacheName(name) && !keep(name)).map(name => caches.delete(name)))
+ const deletedNames = names.filter(name => isPublicWorkerCacheName(name) && !keep(name))
+ const metadataNames = new Set([...deletedNames.filter(isPublicWorkerRuntimeCacheName), ...Object.values(publicWorkerRuntimeCacheNames()).filter(name => !keep(name))])
+ const results = await Promise.allSettled([
+ ...deletedNames.map(name => caches.delete(name)),
+ ...[...metadataNames].map(async name => {
+ const { CacheExpiration } = await import("serwist")
+ return new CacheExpiration(name, { maxEntries: Number.POSITIVE_INFINITY }).delete()
+ }),
+ ])
for (const result of results) if (result.status === "rejected") throw result.reason
}
diff --git a/src/web/src/lib/service-worker/public-cache.test.ts b/src/web/src/lib/service-worker/public-cache.test.ts
index 0f5ccc9fa..55ba994f2 100644
--- a/src/web/src/lib/service-worker/public-cache.test.ts
+++ b/src/web/src/lib/service-worker/public-cache.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest"
-import { isPublicAssetPath, isPublicAssetRequest, isPublicAssetResponse, restrictPublicPrecacheRoutes } from "./public-cache"
+import { isPublicAssetPath, isPublicAssetRequest, isPublicAssetResponse, publicRuntimeAssetKind, restrictPublicPrecacheRoutes } from "./public-cache"
import { Route } from "serwist"
+import imageAssets from "../../../blog/src/lib/blog/image-assets.json"
const origin = "https://alook.ai"
const script = "/_next/static/chunks/abcd1234.js"
@@ -11,18 +12,143 @@ const response = (headers?: HeadersInit, status = 200) => new Response("public b
})
describe("public service worker cache boundary", () => {
+ it.each(["227o87hfvs4zs", "abc123"])("admits only the framework favicon metadata query: %s", fingerprint => {
+ const path = `/favicon.ico?favicon.${fingerprint}.ico`
+ expect(isPublicAssetRequest(request(path), origin)).toBe(true)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": "image/x-icon" }))).toBe(true)
+ for (const headers of [{ Authorization: "fixture" }, { Cookie: "fixture" }, { RSC: "1" }, { "Next-Router-State-Tree": "[]" }] as HeadersInit[]) expect(isPublicAssetRequest(request(path, { headers }), origin)).toBe(false)
+ expect(isPublicAssetRequest(request(`https://other.test${path}`), origin)).toBe(false)
+ expect(isPublicAssetRequest(request(path, { method: "POST" }), origin)).toBe(false)
+ const navigation = request(path)
+ Object.defineProperty(navigation, "mode", { value: "navigate" })
+ expect(isPublicAssetRequest(navigation, origin)).toBe(false)
+ })
+
+ it.each([
+ "/favicon.ico?favicon.abc123.ico=", "/favicon.ico?favicon.abc123.ico=one",
+ "/favicon.ico?favicon.abc123.ico&x=one", "/favicon.ico?favicon.abc123.ico&",
+ "/favicon.ico?favicon.abc123.ico&favicon.abc123.ico", "/favicon.ico?favicon..ico",
+ "/favicon.ico?favicon.ABC123.ico", "/favicon.ico?%66avicon.abc123.ico",
+ "/favicon.ico?favicon.%61bc123.ico", "/favicon.ico?__WB_REVISION__=fixture",
+ "/favicon.ico?__SW_REVISION__=fixture", "/favicon.ico?_rsc=one",
+ "/_next/static/chunks/abcd1234.js?favicon.abc123.ico", "/blog/example-article/hero.webp?favicon.abc123.ico",
+ ])("rejects value, duplicate, encoded and unrelated query variants: %s", path => {
+ expect(isPublicAssetRequest(request(path), origin)).toBe(false)
+ })
+ it("admits the current Blog image catalog and every responsive srcSet URL", () => {
+ for (const [path, asset] of Object.entries(imageAssets)) {
+ const paths = [path, ...("srcSet" in asset && typeof asset.srcSet === "string" ? asset.srcSet.split(",").map(value => value.trim().split(/\s+/)[0]) : [])]
+ for (const url of paths) {
+ expect(publicRuntimeAssetKind(url), url).toBe("images")
+ expect(isPublicAssetRequest(request(url), origin), url).toBe(true)
+ expect(isPublicAssetResponse(request(url), response({ "Content-Type": url.endsWith(".svg") ? "image/svg+xml" : url.endsWith(".png") ? "image/png" : /\.jpe?g$/.test(url) ? "image/jpeg" : "image/webp" })), url).toBe(true)
+ }
+ }
+ })
+
+ it.each(["/blog/_generated/not-a-hash-480.webp", "/blog/_generated/0123456789abcdef-0.webp", "/blog/_generated/0123456789abcdef-480.png", "/blog/_generated/0123456789abcdef-480.webp.map"])("excludes unknown generated image forms: %s", path => {
+ expect(publicRuntimeAssetKind(path)).toBeUndefined()
+ expect(isPublicAssetPath(path)).toBe(false)
+ })
+ it.each([
+ ["/blog-static/_next/static/chunks/blog.js", "code"],
+ ["/blog-static/_next/static/chunks/blog.css", "code"],
+ ["/blog-static/_next/static/media/font.woff2", "fonts"],
+ ["/blog/example-article/hero.webp", "images"],
+ [script, undefined],
+ ["/blog/example-article", undefined],
+ ])("separates Blog runtime classes from Main precache and document paths: %s", (path, kind) => {
+ expect(publicRuntimeAssetKind(path!)).toBe(kind)
+ })
+ it.each([
+ ["/contact/gus-photo.jpeg", "image/jpeg"],
+ ["/landing/people/alex.webp", "image/webp"],
+ ["/landing/people/lin.webp", "image/webp"],
+ ["/landing/people/nina.webp", "image/webp"],
+ ["/landing/people/priya.webp", "image/webp"],
+ ["/landing/people/sam.webp", "image/webp"],
+ ["/fonts/jgs5/jgs5.ttf", "font/ttf"],
+ ["/social/readme-card-58c75311e5ba.png", "image/png"],
+ ["/manifest.json", "application/manifest+json"],
+ ["/manifest.json", "application/json"],
+ ["/favicon.ico", "image/x-icon"],
+ ["/favicon.ico", "image/vnd.microsoft.icon"],
+ ["/_next/static/media/portrait.abc123.webp", "image/webp"],
+ ["/_next/static/media/font.abc123.ttf", "font/ttf"],
+ ["/_next/static/media/pdf.worker.min.35m0adryw15hm.mjs", "application/javascript"],
+ ["/_next/static/media/pdf.worker.min.35m0adryw15hm.mjs", "text/javascript"],
+ ["/_next/static/PGQscKRC0WsRFC5mS4zSH/_buildManifest.js", "application/javascript"],
+ ["/_next/static/PGQscKRC0WsRFC5mS4zSH/_clientMiddlewareManifest.js", "application/javascript"],
+ ["/_next/static/PGQscKRC0WsRFC5mS4zSH/_ssgManifest.js", "application/javascript"],
+ ])("caches an explicitly public resource with its real MIME: %s", (path, type) => {
+ expect(isPublicAssetRequest(request(path), origin)).toBe(true)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": type }))).toBe(true)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": "text/html" }))).toBe(false)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": type, "Cache-Control": "private, max-age=300" }))).toBe(false)
+ })
+
+ it.each([
+ ["/blog-static/_next/static/chunks/blog-abc.js", "application/javascript"],
+ ["/blog-static/_next/static/chunks/blog-abc.css", "text/css"],
+ ["/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/_buildManifest.js", "application/javascript"],
+ ["/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/_clientMiddlewareManifest.js", "text/javascript"],
+ ["/blog-static/_next/static/6IhY41v62o9J7c32DmjH3/_ssgManifest.js", "application/javascript"],
+ ["/blog-static/_next/static/media/font-abc.woff2", "font/woff2"],
+ ["/blog-static/_next/static/media/font-abc.ttf", "font/ttf"],
+ ["/blog/example-article/hero.webp", "image/webp"],
+ ["/blog/example-article/timeline.svg", "image/svg+xml"],
+ ["/blog/example-article/photo.jpeg", "image/jpeg"],
+ ["/blog/example-article/hero.png", "image/png"],
+ ])("admits a public Blog resource while rejecting its query, RSC and private variants: %s", (path, type) => {
+ expect(isPublicAssetRequest(request(path), origin)).toBe(true)
+ if (path.endsWith("Manifest.js")) {
+ expect(publicRuntimeAssetKind(path)).toBe("code")
+ for (const headers of [{ Authorization: "fixture" }, { Cookie: "fixture" }, { RSC: "1" }, { "Next-Router-State-Tree": "[]" }, { "Next-Router-Prefetch": "1" }, { Accept: "text/x-component" }] as HeadersInit[]) expect(isPublicAssetRequest(request(path, { headers }), origin)).toBe(false)
+ for (const query of ["?download=one", "?favicon.abc123.ico", "?__WB_REVISION__=one"]) expect(isPublicAssetRequest(request(`${path}${query}`), origin)).toBe(false)
+ for (const method of ["POST", "HEAD"]) expect(isPublicAssetRequest(request(path, { method }), origin)).toBe(false)
+ const navigation = request(path)
+ Object.defineProperty(navigation, "mode", { value: "navigate" })
+ expect(isPublicAssetRequest(navigation, origin)).toBe(false)
+ const document = request(path)
+ Object.defineProperty(document, "destination", { value: "document" })
+ expect(isPublicAssetRequest(document, origin)).toBe(false)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": "application/json" }))).toBe(false)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": type, "Cache-Control": "public, no-store" }))).toBe(false)
+ }
+ expect(isPublicAssetRequest(request(`${path}?_rsc=one`), origin)).toBe(false)
+ expect(isPublicAssetRequest(request(path, { headers: { RSC: "1" } }), origin)).toBe(false)
+ expect(isPublicAssetRequest(request(`https://another.test${path}`), origin)).toBe(false)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": type }))).toBe(true)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": type, "Cache-Control": "private" }))).toBe(false)
+ expect(isPublicAssetResponse(request(path), response({ "Content-Type": "text/html" }))).toBe(false)
+ })
+
+ it.each([
+ "/blog", "/blog/example-article", "/blog/example-article/", "/blog/example-article/index.html",
+ "/blog/example-article/data.json", "/blog/example-article/source/hero.png", "/blog/private/attachment/file.png",
+ "/blog-static/_next/static/chunks/blog.js.map", "/blog-static/_next/static/chunks/private.json",
+ "/blog-static/_next/image", "/blog-static/fonts/server-only.ttf", "/blog-static/_next/static/media/unknown.bin",
+ "/blog-static/_next/static/_buildManifest.js", "/blog-static/_next/static/build/unknown.js",
+ "/blog-static/_next/static/build/nested/_buildManifest.js", "/blog-static/_next/static/build/_buildManifest.js.map",
+ "/blog-static/_next/static/build/_buildmanifest.js", "/blog-static/_next/static/build/_ssgManifest.json",
+ ])("excludes Blog documents, source-only images and unknown static types: %s", path => {
+ expect(isPublicAssetPath(path)).toBe(false)
+ expect(isPublicAssetRequest(request(path), origin)).toBe(false)
+ expect(isPublicAssetResponse(request(path), response())).toBe(false)
+ })
+
it.each([script, "/_next/static/chunks/styles123.css", "/_next/static/media/font123.woff2", "/alook.svg", "/official-server-badge-flat.svg", "/apple-touch-icon.png", "/icon-192.png", "/icon-512.png"])("allows a public asset: %s", path => {
expect(isPublicAssetPath(path)).toBe(true)
expect(isPublicAssetRequest(request(path), origin)).toBe(true)
})
- it.each(["/c", "/c/channels/server/channel", "/sign-in", "/api/community/servers/icon", "/api/community/files/attachment", "/api/auth/get-session", "/_next/image", "/_next/static/chunks/code.js.map", "/_next/static/chunks/data.json", "/landing/unknown.svg", "/unknown.png", "/serwist/sw.js", "/sw.js"])("excludes private and unknown paths: %s", path => {
+ it.each(["/c", "/c/channels/server/channel", "/sign-in", "/api/community/servers/icon", "/api/community/files/attachment", "/api/auth/get-session", "/_next/image", "/_next/static/chunks/code.js.map", "/_next/static/chunks/data.json", "/_next/static/media/unknown.mjs", "/_next/static/media/other.worker.min.35m0adryw15hm.mjs", "/_next/static/media/pdf.worker.min.mjs", "/_next/static/chunks/pdf.worker.min.35m0adryw15hm.mjs", "/attachments/pdf.worker.min.35m0adryw15hm.mjs", "/landing/unknown.svg", "/landing/people/unknown.webp", "/contact/private.jpeg", "/fonts/dm-sans-600.ttf", "/fonts/jgs5/LICENSE.txt", "/analytics-events.html", "/unknown.json", "/unknown.png", "/serwist/sw.js", "/sw.js"])("excludes private and unknown paths: %s", path => {
expect(isPublicAssetPath(path)).toBe(false)
expect(isPublicAssetRequest(request(path), origin)).toBe(false)
expect(isPublicAssetResponse(request(path), response())).toBe(false)
})
- it.each([
+ it.each<Record<string, string>>([
{ Authorization: "Bearer test" },
{ Cookie: "session=test" },
{ RSC: "1" },
@@ -48,7 +174,7 @@ describe("public service worker cache boundary", () => {
expect(isPublicAssetRequest(navigation, origin)).toBe(false)
})
- it.each([
+ it.each<Record<string, string>>([
{ "Cache-Control": "private, max-age=600" },
{ "Cache-Control": "public, no-store" },
{ "Cache-Control": "public, no-cache" },
@@ -68,6 +194,7 @@ describe("public service worker cache boundary", () => {
it("rejects errors and redirects and requires the correct asset content type", () => {
expect(isPublicAssetResponse(request(), response())).toBe(true)
expect(isPublicAssetResponse(request(), response({}, 404))).toBe(false)
+ expect(isPublicAssetResponse(request(), Response.error())).toBe(false)
const redirected = response()
Object.defineProperty(redirected, "redirected", { value: true })
expect(isPublicAssetResponse(request(), redirected)).toBe(false)
diff --git a/src/web/src/lib/service-worker/public-cache.ts b/src/web/src/lib/service-worker/public-cache.ts
index ff7f3a80d..e8d0a2b42 100644
--- a/src/web/src/lib/service-worker/public-cache.ts
+++ b/src/web/src/lib/service-worker/public-cache.ts
@@ -1,22 +1,47 @@
import type { Serwist } from "serwist"
-const publicIcons = new Set([
+const publicAssets = new Set([
"/alook.svg",
"/official-server-badge-flat.svg",
"/apple-touch-icon.png",
"/icon-192.png",
"/icon-512.png",
+ "/favicon.ico",
+ "/manifest.json",
+ "/contact/gus-photo.jpeg",
+ "/fonts/jgs5/jgs5.ttf",
+ "/landing/people/alex.webp",
+ "/landing/people/lin.webp",
+ "/landing/people/nina.webp",
+ "/landing/people/priya.webp",
+ "/landing/people/sam.webp",
+ "/social/readme-card-58c75311e5ba.png",
])
+export const publicFaviconQueryParameter = /^favicon\.[a-z0-9]+\.ico$/
+
export function isPublicAssetPath(pathname: string): boolean {
- return publicIcons.has(pathname) || /^\/_next\/static\/(?:chunks|media)\/[a-zA-Z0-9_./-]+\.(?:js|css|woff2?)$/.test(pathname)
+ return publicAssets.has(pathname)
+ || /^\/_next\/static\/chunks\/[a-zA-Z0-9_./-]+\.(?:js|css)$/.test(pathname)
+ || /^\/_next\/static\/[a-zA-Z0-9_-]+\/_(?:buildManifest|clientMiddlewareManifest|ssgManifest)\.js$/.test(pathname)
+ || /^\/_next\/static\/media\/pdf\.worker\.min\.[a-zA-Z0-9_-]+\.mjs$/.test(pathname)
+ || /^\/_next\/static\/media\/[a-zA-Z0-9_./-]+\.(?:woff2?|ttf|png|jpe?g|webp|svg|ico)$/.test(pathname)
+ || publicRuntimeAssetKind(pathname) !== undefined
+}
+
+export function publicRuntimeAssetKind(pathname: string): "code" | "fonts" | "images" | undefined {
+ if (/^\/blog-static\/_next\/static\/chunks\/[a-zA-Z0-9_./-]+\.(?:js|css)$/.test(pathname)
+ || /^\/blog-static\/_next\/static\/[a-zA-Z0-9_-]+\/_(?:buildManifest|clientMiddlewareManifest|ssgManifest)\.js$/.test(pathname)) return "code"
+ if (/^\/blog-static\/_next\/static\/media\/[a-zA-Z0-9_./-]+\.(?:woff2?|ttf)$/.test(pathname)) return "fonts"
+ if (/^\/blog\/_generated\/[a-f0-9]{16}-(?:480|960)\.webp$/.test(pathname)) return "images"
+ if (/^\/blog\/[a-z0-9-]+\/[a-zA-Z0-9_.-]+\.(?:png|jpe?g|webp|svg)$/.test(pathname)) return "images"
}
export function isPublicAssetRequest(request: Request, origin: string): boolean {
const url = new URL(request.url)
return request.method === "GET"
&& url.origin === origin
- && !url.search
+ && (!url.search || (url.pathname === "/favicon.ico" && publicFaviconQueryParameter.test(url.search.slice(1))))
&& isPublicAssetPath(url.pathname)
&& request.mode !== "navigate"
&& request.destination !== "document"
@@ -38,10 +63,15 @@ export function isPublicAssetResponse(request: Request, response: Response): boo
|| /(?:^|,)\s*(?:private|no-store|no-cache)\b/i.test(cacheControl)
|| response.headers.has("Set-Cookie")
|| /(?:^|,)\s*(?:\*|cookie|authorization|rsc|next-router[^,]*)\s*(?:,|$)/i.test(vary)) return false
- if (pathname.endsWith(".js")) return type === "application/javascript" || type === "text/javascript"
+ if (/\.m?js$/.test(pathname)) return type === "application/javascript" || type === "text/javascript"
if (pathname.endsWith(".css")) return type === "text/css"
if (/\.woff2?$/.test(pathname)) return type === "font/woff" || type === "font/woff2" || type === "application/font-woff" || type === "application/octet-stream"
+ if (pathname.endsWith(".ttf")) return type === "font/ttf" || type === "application/x-font-ttf" || type === "application/font-sfnt"
if (pathname.endsWith(".svg")) return type === "image/svg+xml"
+ if (/\.jpe?g$/.test(pathname)) return type === "image/jpeg"
+ if (pathname.endsWith(".webp")) return type === "image/webp"
+ if (pathname.endsWith(".ico")) return type === "image/x-icon" || type === "image/vnd.microsoft.icon"
+ if (pathname === "/manifest.json") return type === "application/manifest+json" || type === "application/json"
return type === "image/png"
}
diff --git a/src/web/src/test/e2e-public-static-qa.test.ts b/src/web/src/test/e2e-public-static-qa.test.ts
new file mode 100644
index 000000000..8c16cb490
--- /dev/null
+++ b/src/web/src/test/e2e-public-static-qa.test.ts
@@ -0,0 +1,530 @@
+import { createHash } from "node:crypto"
+import { chmodSync, mkdtempSync, mkdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"
+import { tmpdir } from "node:os"
+import { dirname, join } from "node:path"
+import { EventEmitter } from "node:events"
+import { afterEach, describe, expect, it, vi } from "vitest"
+import { assertPublicStaticInputs, assertPublicStaticWriteSeparation, builtPrecacheEntries, containedPath, phaseWorkerDefinitions, readPublicStaticManifest, validatePhaseCommand, validatePublicStaticManifest } from "../../scripts/dev-zone-processes.mjs"
+import { createPhaseGate, createUpgradeSocketOwner } from "../../scripts/dev-zone-ingress.mjs"
+import { phaseProbeCrossed, phaseReadinessFailure, type PublicStaticPhaseReceipt } from "./e2e-ui/_setup/service-lifecycle"
+import { capturePublicStaticRuntimeState, cleanupWithPublicStaticEvidence, publicStaticQaManifest, serviceDefinitions } from "./e2e-ui/_setup/services"
+import { signIn } from "./e2e-ui/_setup/auth"
+import { assertFaultBase, blogBuildIdFromScripts, createPublicStaticForensicRecord, exactSwRequest, ownPublicStaticForensics, qaApi, releaseDelta, runQaScope, withinQaDeadline, type QaAsset } from "./e2e-ui/_fixtures/public-static-qa"
+
+const directories: string[] = []
+afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }) })
+
+function fixture() {
+ const directory = mkdtempSync(join(tmpdir(), "alook-222-phase-unit-"))
+ const owner = join(directory, "auth")
+ mkdirSync(owner)
+ directories.push(directory)
+ const releases = Object.fromEntries(["N", "Nplus1"].map(name => {
+ const root = join(directory, name)
+ const revision = `revision-${name}`
+ const precacheUrl = "/_next/static/main/_buildManifest.js"
+ const sw = Buffer.from(`new Serwist({precacheEntries:${JSON.stringify([{ url: precacheUrl, revision }])}})`)
+ const paths = ["src/web/wrangler.toml", "src/web/blog/wrangler.toml", "src/web/custom-worker.ts", "src/web/blog/custom-worker.ts", "src/web/.open-next/worker.js", "src/web/blog/.open-next/worker.js", "src/web/public/sw.js", "src/web/.open-next/assets/sw.js"]
+ const helpers = ["main", "blog"].flatMap(zone => ["_buildManifest.js", "_clientMiddlewareManifest.js", "_ssgManifest.js"].map(helper => ({
+ zone, url: `${zone === "blog" ? "/blog-static" : ""}/_next/static/${zone}/${helper}`,
+ path: `src/web/${zone === "blog" ? "blog/" : ""}.open-next/assets/${zone}-${helper}`,
+ })))
+ const inputs = [...paths, ...helpers.map(value => value.path)].map(path => {
+ const bytes = path.endsWith("/sw.js") ? sw : Buffer.from(`${name}:${path}`)
+ mkdirSync(dirname(join(root, path)), { recursive: true })
+ writeFileSync(join(root, path), bytes)
+ return { path, bytes: bytes.length, sha256: createHash("sha256").update(bytes).digest("hex") }
+ })
+ return [name, { root, head: "a".repeat(40), tree: "b".repeat(40), swSha256: createHash("sha256").update(sw).digest("hex"), mainBuildId: "main", blogBuildId: "blog", inputs, assets: helpers.map(value => ({ ...value, ...inputs.find(input => input.path === value.path)!, ...(value.url === precacheUrl ? { precache: true, revision } : {}) })) }]
+ }))
+ const value = { schema: 1, runId: "unit-run-222", controlDirectory: "control", defaultRelease: "Nplus1", releases }
+ const file = join(directory, "release-inputs.json")
+ writeFileSync(file, JSON.stringify(value))
+ return { owner, value, file }
+}
+
+describe("public static QA immutable input and phase controls", () => {
+ it("loads the real supplementary config with original budgets and a mandatory phase-capability dependency", async () => {
+ vi.stubEnv("ALOOK_PUBLIC_STATIC_QA", "0")
+ const { default: ordinary } = await import("../../playwright.config")
+ const { default: supplementary } = await import("../../playwright.public-static-qa.config")
+ expect(process.env.ALOOK_PUBLIC_STATIC_QA).toBe("1")
+ expect(supplementary).toMatchObject({
+ testDir: "./src/test/e2e-ui/_qa/public-static",
+ testMatch: "**/*.qa.ts",
+ timeout: 60_000,
+ expect: { timeout: 10_000 },
+ workers: 1,
+ retries: 0,
+ maxFailures: 1,
+ })
+ expect(supplementary.globalSetup).toBe(ordinary.globalSetup)
+ expect(supplementary.globalTeardown).toBe(ordinary.globalTeardown)
+ expect(supplementary.use).toEqual(ordinary.use)
+ expect(supplementary.projects).toHaveLength(ordinary.projects!.length * 2)
+ for (const project of ordinary.projects!) {
+ const capabilityName = `${project.name}-phase-capability`
+ const capability = supplementary.projects!.find(value => value.name === capabilityName)
+ const journeys = supplementary.projects!.find(value => value.name === project.name)
+ expect(capability).toMatchObject({ ...project, name: capabilityName, grep: /@phase-capability/ })
+ expect(journeys).toMatchObject({ ...project, grepInvert: /@phase-capability/, dependencies: [capabilityName] })
+ }
+ })
+ it("submits only once for a separately owned same-device login and retains its original failure", async () => {
+ const visits: string[] = [], submits: number[] = []
+ const failure = new Error("UI response lost")
+ const page = {
+ goto: async (url: string) => { visits.push(url) },
+ getByRole: (role: string) => role === "textbox" ? { waitFor: async () => {}, fill: async () => {} } : { click: async () => { throw failure } },
+ waitForURL: async () => {},
+ } as unknown as Parameters<typeof signIn>[0]
+ await expect(signIn(page, "[email protected]", { singleAttempt: true, onSubmission: () => { submits.push(1) } })).rejects.toBe(failure)
+ expect(visits).toHaveLength(1)
+ expect(visits[0]).toContain("/sign-in?redirect=/c")
+ expect(submits).toEqual([1])
+ })
+ it("binds the complete precache catalogue to actual frozen worker entries and rejects ambiguity", () => {
+ expect(builtPrecacheEntries('new SW({precacheEntries:[{"url":"/a]b","revision":"actual"}]})')).toEqual([{ url: "/a]b", revision: "actual" }])
+ expect(() => builtPrecacheEntries("precacheEntries:self.__SW_MANIFEST")).toThrow("unambiguous")
+ expect(() => builtPrecacheEntries('x({precacheEntries:[{"url":"/a","revision":null}]});y({precacheEntries:[{"url":"/b","revision":null}]})')).toThrow("unambiguous")
+ const { owner, file } = fixture()
+ const manifest = readPublicStaticManifest(file, owner)
+ manifest.releases.N.assets.find((asset: { precache?: boolean; revision?: string | null }) => asset.precache)!.revision = "not-emitted"
+ expect(() => assertPublicStaticInputs(manifest)).toThrow("actual emitted")
+ })
+
+ it("resolves the two real emitted binding forms, aliases and lexical shadowing without executing worker text", () => {
+ const entries = [{ revision: null, url: "/one.js" }, { revision: "actual-revision", url: "/two.webp" }]
+ const literal = JSON.stringify(entries)
+ expect(builtPrecacheEntries(`(()=>{var p=${literal};new W({precacheEntries:p})})()`)).toEqual(entries)
+ expect(builtPrecacheEntries(`(()=>{var g;g=${literal};new W({precacheEntries:g})})()`)).toEqual(entries)
+ expect(builtPrecacheEntries(`(()=>{var p=${literal};var q=p;new W({precacheEntries:q})})()`)).toEqual(entries)
+ expect(builtPrecacheEntries(`var p=[{url:"/wrong",revision:null}];(()=>{var p=${literal};new W({precacheEntries:p})})()`)).toEqual(entries)
+ const sentinel = Symbol.for("qa222-no-worker-execution")
+ delete (globalThis as Record<symbol, unknown>)[sentinel]
+ expect(builtPrecacheEntries(`globalThis[Symbol.for("qa222-no-worker-execution")]=true;(()=>{var p=${literal};new W({precacheEntries:p})})()`)).toEqual(entries)
+ expect((globalThis as Record<symbol, unknown>)[sentinel]).toBeUndefined()
+ })
+
+ it("resolves the real memoized bundler initializer only after its unchanged factory is called directly", () => {
+ const factory = '(e,t,s)=>()=>{if(s)throw s[0];try{return e&&(t=e(e=0)),t}catch(r){throw s=[r],r}}'
+ const array = '[{url:"/real",revision:"revision"}]'
+ const prefix = `var b=${factory};var g,h=b(()=>{g=${array}});`
+ expect(builtPrecacheEntries(`(()=>{${prefix}h();var worker=new W({precacheEntries:g})})()`)).toEqual([{ url: "/real", revision: "revision" }])
+ const invalid = [
+ `${prefix}new W({precacheEntries:g})`,
+ `${prefix}if(false)h();new W({precacheEntries:g})`,
+ `${prefix}false&&h();new W({precacheEntries:g})`,
+ `${prefix}return;h();new W({precacheEntries:g})`,
+ `${prefix}h=()=>{};h();new W({precacheEntries:g})`,
+ `var b=()=>()=>{};var g,h=b(()=>{g=${array}});h();new W({precacheEntries:g})`,
+ `var b=${factory};var g,h=b(()=>{if(false)g=${array}});h();new W({precacheEntries:g})`,
+ `var g,h=b(()=>{g=${array}});var b=${factory};h();new W({precacheEntries:g})`,
+ `var b=${factory};var g,h=b(()=>{g=${array},sideEffect()});h();new W({precacheEntries:g})`,
+ ]
+ for (const source of invalid) expect(() => builtPrecacheEntries(`(()=>{${source}})()`), source).toThrow("unambiguous")
+ })
+
+ it("rejects reassignment, mutation, conditional or late writes, cycles and dynamic entry values", () => {
+ const array = '[{url:"/actual",revision:"revision"}]'
+ const invalid = [
+ `var p=${array};p=${array};new W({precacheEntries:p})`,
+ `var p=${array};p.push({url:"/extra",revision:null});new W({precacheEntries:p})`,
+ `var p;if(false)p=${array};new W({precacheEntries:p})`,
+ `var p;false&&(p=${array});new W({precacheEntries:p})`,
+ `var p;new W({precacheEntries:p});p=${array}`,
+ 'var p=q;var q=p;new W({precacheEntries:p})',
+ 'var p=makeEntries();new W({precacheEntries:p})',
+ 'new W({precacheEntries:[{url:readURL(),revision:null}]})',
+ 'new W({precacheEntries:[{url:"/a",revision:null,...other}]})',
+ 'new W({precacheEntries:[{url:"/a",url:"/b",revision:null}]})',
+ 'var p;function unused(){p=[{url:"/a",revision:null}]}new W({precacheEntries:p})',
+ 'if(false)new W({precacheEntries:[{url:"/a",revision:null}]})',
+ 'false&&new W({precacheEntries:[{url:"/a",revision:null}]})',
+ 'try{new W({precacheEntries:[{url:"/a",revision:null}]})}catch{}',
+ 'new W({precacheEntries:[{url:"/a",revision:null}],...{precacheEntries:[]}})',
+ 'new W({precacheEntries:[{url:"/a",revision:null}],["precacheEntries"]:[]})',
+ 'var p=[{url:"/a",revision:null}];var box={p};box.p[0].url="changed";new W({precacheEntries:p})',
+ 'var p=[{url:"/a",revision:null}];eval("p=[]");new W({precacheEntries:p})',
+ 'new W({precacheEntries:[{url:"/a",revision:null,extra:sideEffect()}]})',
+ 'return;new W({precacheEntries:[{url:"/a",revision:null}]})',
+ 'throw new Error("stop");new W({precacheEntries:[{url:"/a",revision:null}]})',
+ '(function*(){var p=[{url:"/a",revision:null}];new W({precacheEntries:p})})()',
+ '(async()=>{var p=[{url:"/a",revision:null}];new W({precacheEntries:p})})()',
+ ]
+ for (const source of invalid) expect(() => builtPrecacheEntries(`(()=>{${source}})()`), source).toThrow("unambiguous")
+ })
+ it("retires HTTP samples crossing a valid completed transition while retaining stable and foreign failures", () => {
+ const before: PublicStaticPhaseReceipt = { schema: 1, runId: "run", manifestDigest: "bound", sequence: 0, release: "N", status: "ready" }
+ expect(phaseProbeCrossed(before, { ...before }, 1000)).toBe(false)
+ expect(phaseProbeCrossed(before, { ...before, sequence: 1, release: "Nplus1" }, 1000)).toBe(true)
+ expect(phaseProbeCrossed(before, { ...before, sequence: 1, status: "switching", deadline: 1500, caseId: "case", expectedStops: [{ label: "main backend", pid: 10 }, { label: "Blog backend", pid: 11 }] }, 1000)).toBe(true)
+ for (const change of [{ status: "failed" as const, failure: "stable failed" }, { runId: "foreign" }, { sequence: -1 }, { release: "Nplus1" as const }, { status: "switching" as const, deadline: 999 }]) expect(() => phaseProbeCrossed(before, { ...before, ...change }, 1000)).toThrow()
+ })
+ it("binds helper URLs to the unique actual Flight build and rejects missing/ambiguous data", () => {
+ expect(blogBuildIdFromScripts('self.__next_f.push([1,"0:{\\"b\\":\\"real_build-1\\"}"])')).toBe("real_build-1")
+ expect(blogBuildIdFromScripts('{"b":"same"}\n{"b":"same"}')).toBe("same")
+ expect(() => blogBuildIdFromScripts("no build here")).toThrow("exactly one")
+ expect(() => blogBuildIdFromScripts('{"b":"main"}\n{"b":"blog"}')).toThrow("exactly one")
+ })
+
+ it("selects only the exact service-worker network leg and preserves the real public fault base", () => {
+ const worker = {} as Parameters<typeof exactSwRequest>[2]
+ const other = {} as Parameters<typeof exactSwRequest>[2]
+ const url = "http://localhost/real.js"
+ expect(exactSwRequest({ url: () => url, serviceWorker: () => worker }, url, worker)).toBe(true)
+ for (const [requestUrl, owner] of [[url, null], [url, other], [`${url}?query`, worker]] as const) expect(exactSwRequest({ url: () => requestUrl, serviceWorker: () => owner }, url, worker)).toBe(false)
+ const body = Buffer.from("real original helper")
+ const asset = { url: "/_next/static/build/_buildManifest.js", bytes: body.length, sha256: createHash("sha256").update(body).digest("hex") } as QaAsset
+ const headers = { "content-type": "application/javascript", "cache-control": "public, max-age=31536000" }
+ expect(() => assertFaultBase(asset, 200, headers, body)).not.toThrow()
+ for (const [status, actualHeaders, actualBody] of [[404, headers, body], [200, { ...headers, "cache-control": "private" }, body], [200, { ...headers, "content-type": "text/html" }, body], [200, { ...headers, vary: "Cookie" }, body], [200, { ...headers, "set-cookie": "qa=fake" }, body], [200, headers, Buffer.from("other")]] as const) expect(() => assertFaultBase(asset, status, actualHeaders, actualBody)).toThrow("real bound")
+ })
+
+ it("keeps primary and every independent cleanup failure and closes in reverse ownership order", async () => {
+ const order: number[] = [], primary = new Error("primary"), first = new Error("cleanup first"), second = new Error("cleanup second")
+ const result = runQaScope(async onClose => {
+ onClose(async () => { order.push(1); throw first })
+ onClose(async () => { order.push(2); throw second })
+ throw primary
+ })
+ await expect(result).rejects.toMatchObject({ errors: [primary, second, first] })
+ expect(order).toEqual([2, 1])
+ })
+
+ it("cancels pending owned operations and does not turn deadline failure into success", async () => {
+ const controller = new AbortController()
+ const pending = withinQaDeadline(new Promise<void>(() => {}), Date.now() + 1000, controller.signal)
+ controller.abort()
+ await expect(pending).rejects.toThrow("cancelled")
+ await expect(withinQaDeadline(new Promise<void>(() => {}), Date.now() - 1)).rejects.toThrow("original deadline")
+ expect(await withinQaDeadline(Promise.resolve(42), Date.now() + 1000)).toBe(42)
+ })
+
+ it("distinguishes real same-URL body changes from added, deleted and unchanged precaches", () => {
+ const asset = (url: string, sha256: string) => ({ url, sha256, precache: true }) as QaAsset
+ expect(releaseDelta([asset("/same", "old"), asset("/gone", "x"), asset("/stable", "s")], [asset("/same", "new"), asset("/new", "n"), asset("/stable", "s")])).toEqual({ added: ["/new"], deleted: ["/gone"], changed: ["/same"], unchanged: ["/stable"] })
+ })
+
+ it("checks actual bound bytes and rejects a changed input without touching another release", () => {
+ const { owner, file } = fixture()
+ const manifest = readPublicStaticManifest(file, owner)
+ expect(manifest.digest).toMatch(/^[0-9a-f]{64}$/)
+ writeFileSync(join(manifest.releases.N.root, "src/web/public/sw.js"), "changed")
+ expect(() => assertPublicStaticInputs(manifest)).toThrow("input changed")
+ })
+
+ it("rejects escaping ownership, repeated inputs and mismatched SW/helper catalogues before startup", () => {
+ const { owner, value } = fixture()
+ expect(() => containedPath(owner, "../outside")).toThrow("escapes")
+ expect(() => validatePublicStaticManifest({ ...value, controlDirectory: "/outside" }, owner)).toThrow("escapes")
+ const duplicate = structuredClone(value)
+ duplicate.releases.N.inputs.push(duplicate.releases.N.inputs[0])
+ expect(() => validatePublicStaticManifest(duplicate, owner)).toThrow("unique")
+ const wrong = structuredClone(value)
+ wrong.releases.N.swSha256 = "f".repeat(64)
+ expect(() => validatePublicStaticManifest(wrong, owner)).toThrow("SW inputs")
+ const missing = structuredClone(value)
+ missing.releases.N.assets.pop()
+ expect(() => validatePublicStaticManifest(missing, owner)).toThrow("helper")
+ })
+
+ it("rejects a symlinked input even when its bytes and length match", () => {
+ const { owner, file } = fixture()
+ const manifest = readPublicStaticManifest(file, owner)
+ const target = join(manifest.releases.N.root, "src/web/public/sw.js")
+ rmSync(target)
+ symlinkSync(join(manifest.releases.N.root, "src/web/.open-next/assets/sw.js"), target)
+ expect(() => assertPublicStaticInputs(manifest)).toThrow("input changed")
+ })
+
+ it("rejects release roots reset by setup and real control-parent escapes before any write", () => {
+ const { owner, value, file } = fixture()
+ const reset = structuredClone(value)
+ reset.releases.N.root = join(owner, "will-be-deleted")
+ expect(() => validatePublicStaticManifest(reset, owner)).toThrow("mutable/reset")
+ symlinkSync(value.releases.N.root, join(owner, "link"), "dir")
+ expect(() => validatePublicStaticManifest({ ...value, controlDirectory: "link/control" }, owner)).toThrow("escapes")
+ const manifest = readPublicStaticManifest(file, owner)
+ expect(() => assertPublicStaticWriteSeparation(manifest, [join(value.releases.N.root, "src/web/public")])).toThrow(/mutable|sealed/)
+ })
+
+ it("keeps one absolute persistence identity while selecting the real version config roots", () => {
+ const { owner, file } = fixture()
+ const manifest = readPublicStaticManifest(file, owner)
+ const old = phaseWorkerDefinitions(manifest, "N", "/installed/wrangler.js", owner)
+ const next = phaseWorkerDefinitions(manifest, "Nplus1", "/installed/wrangler.js", owner)
+ for (let index = 0; index < 2; index++) {
+ expect(old[index].args[3]).not.toBe(next[index].args[3])
+ expect(old[index].args[old[index].args.indexOf("--persist-to") + 1]).toBe(next[index].args[next[index].args.indexOf("--persist-to") + 1])
+ expect(old[index].args[0]).toBe("/installed/wrangler.js")
+ }
+ })
+
+ it("refuses stale/foreign/deadline commands and retains a case lease until restored release", () => {
+ const manifest = { runId: "run", digest: "digest", defaultRelease: "Nplus1" }
+ const receipt = { sequence: 1, release: "N", status: "ready", caseId: "case-a" }
+ const command = { action: "switch", sequence: 2, runId: "run", manifestDigest: "digest", caseId: "case-a", from: "N", to: "Nplus1", deadline: 1_100 }
+ expect(validatePhaseCommand(command, manifest, receipt, 1_000)).toBe(command)
+ for (const change of [{ caseId: "case-b" }, { sequence: 1 }, { deadline: 999 }, { deadline: 61_001 }, { from: "Nplus1" }, { action: "release" }, { manifestDigest: "foreign" }]) {
+ expect(() => validatePhaseCommand({ ...command, ...change }, manifest, receipt, 1_000)).toThrow("foreign")
+ }
+ expect(() => validatePhaseCommand({ ...command, action: "release", from: "Nplus1", to: "Nplus1" }, manifest, { ...receipt, release: "Nplus1" }, 1_000)).not.toThrow()
+ })
+
+ it("requires explicit supplementary/prebuilt mode and leaves ordinary combined argv unchanged", () => {
+ vi.stubEnv("ALOOK_E2E_RELEASE_MANIFEST", "")
+ vi.stubEnv("ALOOK_PUBLIC_STATIC_QA", "")
+ expect(publicStaticQaManifest()).toBeNull()
+ expect(serviceDefinitions()[0].args).toContain("--with-ws-do")
+ expect(serviceDefinitions()[0].args).not.toContain("--public-static-manifest")
+ vi.stubEnv("ALOOK_PUBLIC_STATIC_QA", "1")
+ expect(() => publicStaticQaManifest()).toThrow("requires ALOOK_E2E_RELEASE_MANIFEST")
+ vi.stubEnv("ALOOK_E2E_RELEASE_MANIFEST", "/does-not-exist")
+ vi.stubEnv("ALOOK_E2E_PREBUILT", "")
+ expect(() => publicStaticQaManifest()).toThrow("prebuilt")
+ })
+
+ it("refuses an old default before a complete candidate QA run can use candidate-only assertions", () => {
+ const { value, file } = fixture()
+ value.defaultRelease = "N"
+ writeFileSync(file, JSON.stringify(value))
+ vi.stubEnv("ALOOK_PUBLIC_STATIC_QA", "1")
+ vi.stubEnv("ALOOK_E2E_PREBUILT", "1")
+ vi.stubEnv("ALOOK_E2E_RELEASE_MANIFEST", file)
+ expect(() => publicStaticQaManifest(false)).toThrow("requires Nplus1")
+ })
+
+ it("holds HTTP until a phase commits and rejects all pending/future work on owner failure", async () => {
+ const gate = createPhaseGate()
+ await gate.ready()
+ gate.hold()
+ let passed = false
+ const waiting = gate.ready().then(() => { passed = true })
+ await Promise.resolve()
+ expect(passed).toBe(false)
+ gate.release()
+ await waiting
+ expect(passed).toBe(true)
+ gate.hold()
+ const held = gate.ready()
+ gate.fail(new Error("owner failed"))
+ await expect(held).rejects.toThrow("owner failed")
+ await expect(gate.ready()).rejects.toThrow("owner failed")
+ expect(() => gate.hold()).toThrow("failed")
+ })
+
+ it("retires both upgraded sockets on either error and drains owned pairs without opening a port", async () => {
+ class Socket extends EventEmitter {
+ closed = false
+ destroyCount = 0
+ destroy() { if (!this.closed) { this.closed = true; this.destroyCount++; this.emit("close") }; return this }
+ }
+ const owner = createUpgradeSocketOwner()
+ const client = new Socket(), backend = new Socket()
+ owner.own(client, backend)
+ backend.emit("error", new Error("backend failed"))
+ expect(client.closed).toBe(true)
+ expect(backend.closed).toBe(true)
+ const otherClient = new Socket(), otherBackend = new Socket()
+ owner.own(otherClient, otherBackend)
+ await owner.close()
+ expect(otherClient.closed && otherBackend.closed).toBe(true)
+ expect([client, backend, otherClient, otherBackend].map(value => value.destroyCount)).toEqual([1, 1, 1, 1])
+ })
+
+ it("permits only two specifically registered children inside a live bounded transition", () => {
+ const receipt: PublicStaticPhaseReceipt = { schema: 1, runId: "run", manifestDigest: "digest", sequence: 1, release: "N", status: "switching", caseId: "case-a", deadline: 1_100, expectedStops: [{ label: "main backend", pid: 10 }, { label: "Blog backend", pid: 20 }] }
+ expect(phaseReadinessFailure(receipt, 1_000)).toBeNull()
+ expect(phaseReadinessFailure(receipt, 1_100)).toContain("expired")
+ expect(phaseReadinessFailure({ ...receipt, expectedStops: [{ label: "stable WS/queue", pid: 10 }, { label: "Blog backend", pid: 20 }] }, 1_000)).toContain("invalid")
+ expect(phaseReadinessFailure({ ...receipt, expectedStops: [{ label: "main backend", pid: 10 }, { label: "Blog backend", pid: 10 }] }, 1_000)).toContain("invalid")
+ expect(phaseReadinessFailure({ ...receipt, status: "failed", failure: "stable socket lost" }, 1_000)).toBe("stable socket lost")
+ })
+})
+
+describe("original-path forensic evidence", () => {
+ const initial = () => ({ status: "stopping" as const, restorePending: true, services: [{ name: "owned", pid: 123, startedAt: "unit", healthUrl: "unit", expectedStatus: 200, expectedBody: { status: "ok" as const }, logPath: "unit", internalLogDirectory: "unit" }], health: {} })
+ const cleanup = (overrides: Partial<Parameters<typeof cleanupWithPublicStaticEvidence>[1]> = {}) => {
+ const order: string[] = []
+ const dependencies = { terminate: () => { order.push("terminate") }, waitForStop: async () => { order.push("stopped"); return true }, capture: () => { order.push("capture") }, restore: () => { order.push("restore") }, persist: (state: Parameters<Parameters<typeof cleanupWithPublicStaticEvidence>[1]["persist"]>[0]) => { order.push(`persist:${state.status}:${state.restorePending}`) }, ...overrides }
+ return { order, dependencies }
+ }
+ const captureFixture = () => {
+ const directory = mkdtempSync(join(tmpdir(), "alook-222-forensic-unit-"))
+ directories.push(directory)
+ const runtimeRoot = join(directory, "runtime"), ownerRoot = join(directory, "owner"), controlDirectory = join(ownerRoot, "control")
+ mkdirSync(runtimeRoot)
+ mkdirSync(controlDirectory, { recursive: true })
+ return { runtimeRoot, ownerRoot, controlDirectory, runId: "unit-forensic-222", manifestDigest: "a".repeat(64) }
+ }
+ const observedApi = () => {
+ const callbacks: Array<() => Promise<void>> = []
+ const attachments: Array<{ body: string }> = []
+ const resources = { deadline: Date.now() + 10_000, own: (value: unknown, dispose: (value: unknown) => Promise<void>) => { const close = () => dispose(value); callbacks.push(close); return { value, close } } } as unknown as Parameters<typeof ownPublicStaticForensics>[0]
+ const info = { attach: async (_name: string, value: { body: string }) => { attachments.push(value) } } as unknown as Parameters<typeof ownPublicStaticForensics>[1]
+ vi.stubEnv("ALOOK_E2E_PUBLIC_STATIC_FORENSICS", "1")
+ ownPublicStaticForensics(resources, info)
+ return { resources, info, attachments, flush: async () => { for (const callback of callbacks.splice(0)) await callback() } }
+ }
+
+ it("requires the explicit forensic flag and supplementary mode before setup mutates state", () => {
+ vi.stubEnv("ALOOK_E2E_PUBLIC_STATIC_FORENSICS", "0")
+ expect(() => publicStaticQaManifest(false)).toThrow("Invalid public static forensic opt-in")
+ vi.stubEnv("ALOOK_E2E_PUBLIC_STATIC_FORENSICS", "1")
+ vi.stubEnv("ALOOK_PUBLIC_STATIC_QA", "0")
+ expect(() => publicStaticQaManifest(false)).toThrow("requires supplementary QA")
+ })
+
+ it("separates ready submission, probe IDs, work and cleanup while excluding dynamic data", () => {
+ let clock = 0
+ const record = createPublicStaticForensicRecord({ now: () => ++clock, monotonic: () => String(clock) })
+ record.phase("before")
+ record.event("ready-send-before")
+ record.event("ready-send-returned")
+ const request = record.start("GET", "/api/community/machines")
+ record.response(request, new Response(null, { status: 500, headers: { "server-timing": 'alook_sampling;desc="selected"', "set-cookie": "unit-private-cookie" } }))
+ record.phase("cleanup")
+ record.event("cleanup-start")
+ record.failed(record.start("DELETE", "/api/community/machines/unit-private-machine"))
+ record.phase("unit-private-phase")
+ record.frame("unit-private-frame")
+ record.failed(record.start("unit-private-method", "/unit-private-query?token=unit-private-token"))
+ const snapshot = record.snapshot()
+ expect(snapshot.complete).toBe(true)
+ expect(snapshot.rows.slice(0, 4).map(value => value.event)).toEqual(["ready-send-before", "ready-send-returned", "http-start", "http-response"])
+ expect(snapshot.rows[3]).toMatchObject({ request, status: 500, phase: "before", sampling: "selected" })
+ expect(snapshot.rows[5]).toMatchObject({ method: "DELETE", phase: "cleanup", path: "/api/community/machines/:owned-machine" })
+ expect(snapshot.rows.find(value => value.path === "other")).toMatchObject({ phase: "unknown", method: "other", path: "other" })
+ expect(JSON.stringify(snapshot)).not.toContain("unit-private")
+ snapshot.rows[0]!.event = "changed-copy"
+ expect(record.snapshot().rows[0]!.event).toBe("ready-send-before")
+ })
+
+ it("keeps missing or ambiguous native trace identity unavailable and records observer incompleteness", () => {
+ const record = createPublicStaticForensicRecord({ limit: 2 })
+ record.response(1, new Response(null, { headers: { "server-timing": 'alook_sampling;desc="selected",alook_sampling;desc="not_selected",alook_server;desc="00-00000000000000000000000000000000-1111111111111111-01"' } }))
+ expect(record.snapshot().rows[0]).toMatchObject({ sampling: "unavailable" })
+ expect(record.snapshot().rows[0]).not.toHaveProperty("serverTraceContext")
+ record.event("ws-open")
+ record.event("ws-close")
+ expect(record.snapshot()).toMatchObject({ complete: false, dropped: 1, observerFailures: 0 })
+ const broken = createPublicStaticForensicRecord({ now: () => { throw new Error("observer-clock") } })
+ expect(() => broken.event("ws-open")).not.toThrow()
+ expect(broken.snapshot()).toMatchObject({ complete: false, observerFailures: 1, rows: [] })
+ const pending = createPublicStaticForensicRecord()
+ pending.start("POST", "/api/community/daemon/activate")
+ expect(pending.snapshot()).toMatchObject({ complete: false, pendingRequests: 1 })
+ })
+
+ it("rejects malformed duplicate trace metrics even alongside a valid native value", () => {
+ const parent = `00-${"1".repeat(32)}-${"2".repeat(16)}-01`
+ const record = createPublicStaticForensicRecord()
+ record.response(1, new Response(null, { headers: { "server-timing": `alook_sampling;desc="selected",alook_server;desc="${parent}"` } }))
+ expect(record.snapshot().rows[0]).toMatchObject({ sampling: "selected", serverTraceContext: parent })
+ record.response(2, new Response(null, { headers: { "server-timing": `alook_sampling;desc="selected",alook_sampling;desc="garbage",alook_server;desc="${parent}",alook_server;desc="malformed"` } }))
+ expect(record.snapshot().rows[1]).toMatchObject({ sampling: "unavailable" })
+ expect(record.snapshot().rows[1]).not.toHaveProperty("serverTraceContext")
+ record.response(3, new Response(null, { headers: { "server-timing": `other;desc="quoted,alook_sampling;desc=selected",alook_server;desc="${parent}"` } }))
+ expect(record.snapshot().rows[2]).toMatchObject({ sampling: "unavailable", serverTraceContext: parent })
+ })
+
+ it("records the original500 once and does not consume its body or change request credentials", async () => {
+ const observed = observedApi()
+ const json = vi.fn(() => { throw new Error("must not read failed response") })
+ const response = { status: 500, ok: false, headers: new Headers({ "server-timing": 'alook_sampling;desc="selected"' }), json }
+ const fetchMock = vi.fn(async (_input: string | URL | Request, _init?: RequestInit) => response)
+ vi.stubGlobal("fetch", fetchMock)
+ const actors = { sessionCookie: () => "unit-private-cookie" } as unknown as Parameters<typeof qaApi>[0]
+ await expect(qaApi(actors, observed.resources, "alice", "/api/community/machines")).rejects.toThrow("QA GET /api/community/machines returned 500")
+ expect(fetchMock).toHaveBeenCalledTimes(1)
+ expect(fetchMock.mock.calls[0]![1]).toMatchObject({ headers: { Cookie: "unit-private-cookie" } })
+ expect(json).not.toHaveBeenCalled()
+ await observed.flush()
+ const snapshot = JSON.parse(observed.attachments[0]!.body)
+ expect(snapshot.rows.map((value: { event: string }) => value.event)).toEqual(["http-start", "http-response"])
+ expect(snapshot.rows[1]).toMatchObject({ request: 1, status: 500 })
+ expect(observed.attachments[0]!.body).not.toContain("unit-private-cookie")
+ })
+
+ it("rethrows the same fetch failure and releases the case observation even on failure", async () => {
+ const observed = observedApi(), failure = new Error("original transport failure")
+ const fetchMock = vi.fn(async () => { throw failure })
+ vi.stubGlobal("fetch", fetchMock)
+ const actors = { sessionCookie: () => "unit-private-cookie" } as unknown as Parameters<typeof qaApi>[0]
+ await expect(qaApi(actors, observed.resources, "alice", "/api/community/machines")).rejects.toBe(failure)
+ expect(fetchMock).toHaveBeenCalledTimes(1)
+ await observed.flush()
+ expect(JSON.parse(observed.attachments[0]!.body).rows[1]).toMatchObject({ event: "http-fetch-error", request: 1 })
+ ownPublicStaticForensics(observed.resources, observed.info)
+ await observed.flush()
+ expect(observed.attachments).toHaveLength(2)
+ expect(JSON.parse(observed.attachments[1]!.body).rows).toHaveLength(0)
+ })
+
+ it("copies complete stopped state and WAL/SHM byte and mode exact without opening SQLite", () => {
+ const options = captureFixture(), state = join(options.runtimeRoot, "src/web/.wrangler/state/v3/observability")
+ mkdirSync(state, { recursive: true })
+ const originals = ["trace.sqlite", "trace.sqlite-wal", "trace.sqlite-shm"]
+ for (const [index, name] of originals.entries()) { writeFileSync(join(state, name), Buffer.from([0, index, 255, 7])); chmodSync(join(state, name), 0o640) }
+ const receipt = capturePublicStaticRuntimeState(options)
+ expect(receipt.files.filter(value => value.type === "file")).toHaveLength(3)
+ for (const name of originals) {
+ const source = join(state, name), copied = join(options.controlDirectory, "forensics-stopped-state/src/web/.wrangler/state/v3/observability", name)
+ expect(readFileSync(copied)).toEqual(readFileSync(source))
+ expect(statSync(copied).mode & 0o777).toBe(0o640)
+ expect(receipt.files.find(value => value.path.endsWith(`/${name}`))!.sha256).toBe(createHash("sha256").update(readFileSync(source)).digest("hex"))
+ }
+ expect(receipt.sources.find(value => value.path === "src/web/blog/.wrangler/state")).toMatchObject({ present: false })
+ expect(() => capturePublicStaticRuntimeState(options)).toThrow("already exists")
+ })
+
+ it("rejects escaped, aliased and linked capture paths without following a foreign state tree", () => {
+ const escaped = captureFixture()
+ expect(() => capturePublicStaticRuntimeState({ ...escaped, controlDirectory: escaped.runtimeRoot })).toThrow("case owner")
+ const overlap = captureFixture(), state = join(overlap.runtimeRoot, "src/web/.wrangler/state")
+ mkdirSync(join(state, "control"), { recursive: true })
+ expect(() => capturePublicStaticRuntimeState({ ...overlap, ownerRoot: state, controlDirectory: join(state, "control") })).toThrow("overlap")
+ const linked = captureFixture(), link = join(linked.runtimeRoot, "src/web/.wrangler/state"), foreign = join(dirname(linked.runtimeRoot), "foreign")
+ mkdirSync(foreign)
+ writeFileSync(join(foreign, "untouched"), "foreign-state")
+ mkdirSync(dirname(link), { recursive: true })
+ symlinkSync(foreign, link)
+ expect(() => capturePublicStaticRuntimeState(linked)).toThrow("symlinks")
+ expect(readFileSync(join(foreign, "untouched"), "utf8")).toBe("foreign-state")
+ })
+
+ it("captures only after known stop and before restoring, then persists the final lifecycle", async () => {
+ const { order, dependencies } = cleanup()
+ const state = await cleanupWithPublicStaticEvidence(initial(), dependencies)
+ expect(order).toEqual(["terminate", "stopped", "capture", "restore", "persist:stopped:false"])
+ expect(state).toMatchObject({ status: "stopped", restorePending: false })
+ const withoutBackup = cleanup()
+ await cleanupWithPublicStaticEvidence({ ...initial(), restorePending: false }, withoutBackup.dependencies)
+ expect(withoutBackup.order).toEqual(["terminate", "stopped", "capture", "persist:stopped:false"])
+ })
+
+ it("restores and persists despite capture failure, while keeping both original restoration and capture errors", async () => {
+ const failure = new Error("capture failed"), restoration = new Error("original restore failed")
+ const first = cleanup({ capture: () => { first.order.push("capture"); throw failure } })
+ await expect(cleanupWithPublicStaticEvidence(initial(), first.dependencies)).rejects.toBe(failure)
+ expect(first.order).toEqual(["terminate", "stopped", "capture", "restore", "persist:stopped:false"])
+ const second = cleanup({ capture: () => { throw failure }, restore: () => { throw restoration } })
+ try { await cleanupWithPublicStaticEvidence(initial(), second.dependencies); expect.fail("expected original restoration failure") } catch (error) {
+ expect(error).toBeInstanceOf(AggregateError)
+ expect((error as AggregateError).errors).toEqual([restoration, failure])
+ }
+ const persistence = new Error("original persistence failed")
+ const third = cleanup({ capture: () => { throw failure }, persist: () => { throw persistence } })
+ try { await cleanupWithPublicStaticEvidence(initial(), third.dependencies); expect.fail("expected original persistence failure") } catch (error) {
+ expect(error).toBeInstanceOf(AggregateError)
+ expect((error as AggregateError).errors).toEqual([persistence, failure])
+ }
+ })
+
+ it("does not capture or restore when the original shutdown remains unknown", async () => {
+ const { order, dependencies } = cleanup({ waitForStop: async () => false })
+ await expect(cleanupWithPublicStaticEvidence(initial(), dependencies)).rejects.toThrow("E2E services did not stop")
+ expect(order).toEqual(["terminate", "terminate"])
+ })
+})
diff --git a/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts b/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts
index 9bbab04de..f00a4196e 100644
--- a/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts
+++ b/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts
@@ -157,6 +157,7 @@ test("desktop landing keeps the embedded phone Back control on true mobile geome
await page.emulateMedia({ reducedMotion: "reduce" })
await page.goto("/")
+ await page.locator('[data-slot="phone-mockup-card"]').scrollIntoViewIfNeeded()
const stage = page.getByTestId(tid.landingMobileMotionStage)
await stage.scrollIntoViewIfNeeded()
const back = stage.locator('button[aria-label="Back"]')
diff --git a/src/web/src/test/e2e-ui/66-public-static-resource-cache.spec.ts b/src/web/src/test/e2e-ui/66-public-static-resource-cache.spec.ts
new file mode 100644
index 000000000..1c73b5bf1
--- /dev/null
+++ b/src/web/src/test/e2e-ui/66-public-static-resource-cache.spec.ts
@@ -0,0 +1,238 @@
+import { mkdtempSync } from "node:fs"
+import { rm } from "node:fs/promises"
+import { tmpdir } from "node:os"
+import { join } from "node:path"
+import { chromium, type BrowserContext, type Page, type TestInfo } from "@playwright/test"
+import { publicWorkerCacheNames, publicWorkerRuntimeCacheNames } from "../../lib/service-worker/cache-identity"
+import { publicRuntimeAssetKind } from "../../lib/service-worker/public-cache"
+import { expect, test } from "./_fixtures/case-fixture"
+import { blogBuildIdFromScripts } from "./_fixtures/public-static-qa"
+
+type Asset = { url: string; cacheName: string }
+type Resource = Asset & { bytes: number; sha256: string; fromServiceWorker: boolean }
+
+async function runWithCleanup(journey: () => Promise<void>, cleanup: () => Promise<void>) {
+ const errors: unknown[] = []
+ try { await journey() } catch (error) { errors.push(error) }
+ try { await cleanup() } catch (error) { errors.push(error) }
+ if (errors.length === 1) throw errors[0]
+ if (errors.length > 1) throw new AggregateError(errors, "Public static resource journey and cleanup failed")
+}
+
+async function expectRootWorker(page: Page) {
+ const root = new URL("/", page.url()).href
+ const script = new URL("/sw.js", root).href
+ await expect.poll(() => page.evaluate(async () => (await navigator.serviceWorker.getRegistrations()).map(registration => ({
+ scope: registration.scope, script: registration.active?.scriptURL, updateViaCache: registration.updateViaCache,
+ })))).toEqual([{ scope: root, script, updateViaCache: "none" }])
+ await expect.poll(() => page.evaluate(() => navigator.serviceWorker.controller?.scriptURL)).toBe(script)
+}
+
+async function publicCacheKeys(page: Page) {
+ return page.evaluate(async () => {
+ const result: Record<string, string[]> = {}
+ for (const name of (await caches.keys()).filter(name => name.startsWith("alook-public-"))) {
+ result[name] = (await (await caches.open(name)).keys()).map(request => request.url).sort()
+ }
+ return result
+ })
+}
+
+async function readResource(page: Page, asset: Asset): Promise<Resource> {
+ const received = page.waitForResponse(response => response.url() === asset.url && response.request().method() === "GET")
+ const [body, response] = await Promise.all([
+ page.evaluate(async url => {
+ const response = await fetch(url, { credentials: "omit", cache: "no-store", redirect: "error" })
+ if (response.status !== 200) throw new Error(`Static resource returned ${response.status}: ${url}`)
+ const bytes = await response.arrayBuffer()
+ const sha256 = [...new Uint8Array(await crypto.subtle.digest("SHA-256", bytes))].map(value => value.toString(16).padStart(2, "0")).join("")
+ return { bytes: bytes.byteLength, sha256 }
+ }, asset.url),
+ received,
+ ])
+ return { ...asset, ...body, fromServiceWorker: response.fromServiceWorker() }
+}
+
+async function expectCachedBody(page: Page, resource: Resource) {
+ await expect.poll(() => page.evaluate(async ({ cacheName, url }) => {
+ if (!(await caches.has(cacheName))) return null
+ const response = await (await caches.open(cacheName)).match(url)
+ if (!response) return null
+ const bytes = await response.arrayBuffer()
+ return {
+ bytes: bytes.byteLength,
+ sha256: [...new Uint8Array(await crypto.subtle.digest("SHA-256", bytes))].map(value => value.toString(16).padStart(2, "0")).join(""),
+ }
+ }, resource)).toEqual({ bytes: resource.bytes, sha256: resource.sha256 })
+}
+
+async function expectOfflineResources(page: Page, context: BrowserContext, resources: Resource[]) {
+ const image = resources.find(resource => resource.cacheName === publicWorkerRuntimeCacheNames().images)!
+ await Promise.all([
+ context.waitForEvent("requestfailed", { predicate: request => request.url() === image.url && request.serviceWorker() !== null })
+ .then(request => expect(request.failure()?.errorText).toContain("ERR_INTERNET_DISCONNECTED")),
+ (async () => {
+ for (const resource of resources) {
+ expect(await readResource(page, resource)).toEqual(resource)
+ await expectCachedBody(page, resource)
+ }
+ })(),
+ ])
+}
+
+async function warmBlogResources(page: Page): Promise<Resource[]> {
+ await page.goto("/blog")
+ await expect(page.getByRole("heading", { name: "Blog", exact: true })).toBeVisible()
+ await expectRootWorker(page)
+ await page.reload()
+ await page.evaluate(() => document.fonts.ready.then(() => undefined))
+ const observed = await page.evaluate(() => ({
+ loaded: performance.getEntriesByType("resource").map(entry => entry.name),
+ images: [...document.querySelectorAll("img")].map(image => image.currentSrc || image.src),
+ }))
+ const origin = new URL(page.url()).origin
+ const select = (urls: string[], pattern: RegExp) => urls.find(value => {
+ const url = new URL(value)
+ return url.origin === origin && !url.search && pattern.test(url.pathname)
+ })
+ const names = publicWorkerRuntimeCacheNames()
+ const selected = [
+ { url: select(observed.loaded, /^\/blog-static\/_next\/static\/chunks\/.+\.js$/), cacheName: names.code },
+ { url: select(observed.loaded, /^\/blog-static\/_next\/static\/chunks\/.+\.css$/), cacheName: names.code },
+ { url: select(observed.loaded, /^\/blog-static\/_next\/static\/media\/.+\.(?:woff2?|ttf)$/), cacheName: names.fonts },
+ { url: observed.images.find(value => new URL(value).origin === origin && !new URL(value).search && publicRuntimeAssetKind(new URL(value).pathname) === "images"), cacheName: names.images },
+ ]
+ const resources: Resource[] = []
+ for (const asset of selected) {
+ expect(asset.url, `Rendered Blog asset for ${asset.cacheName}`).toBeTruthy()
+ const resource = await readResource(page, { ...asset, url: asset.url! })
+ expect(resource.fromServiceWorker).toBe(true)
+ expect(resource.bytes).toBeGreaterThan(0)
+ await expectCachedBody(page, resource)
+ resources.push(resource)
+ }
+ const buildId = blogBuildIdFromScripts(await page.locator("script").allTextContents().then(values => values.join("\n")))
+ for (const name of ["_buildManifest.js", "_clientMiddlewareManifest.js", "_ssgManifest.js"]) {
+ const asset = { url: new URL(`/blog-static/_next/static/${buildId}/${name}`, page.url()).href, cacheName: names.code }
+ const response = page.waitForResponse(response => response.url() === asset.url && response.request().method() === "GET")
+ const resource = await readResource(page, asset)
+ const headers = (await response).headers()
+ expect(headers["content-type"]).toMatch(/^(?:text|application)\/javascript(?:;|$)/)
+ expect(headers["cache-control"]).toMatch(/\bpublic\b/)
+ expect(headers["cache-control"]).not.toMatch(/\b(?:private|no-store|no-cache)\b/)
+ expect(resource.fromServiceWorker).toBe(true)
+ expect(resource.bytes).toBeGreaterThan(0)
+ await expectCachedBody(page, resource)
+ resources.push(resource)
+ }
+ return resources
+}
+
+async function attachReceipt(page: Page, testInfo: TestInfo, name: string, resources: Resource[] = []) {
+ const worker = await page.evaluate(async () => {
+ const response = await fetch("/sw.js", { credentials: "omit", cache: "no-store", redirect: "error" })
+ if (response.status !== 200) throw new Error(`SW source returned ${response.status}`)
+ const bytes = await response.arrayBuffer()
+ return {
+ url: response.url, bytes: bytes.byteLength,
+ sha256: [...new Uint8Array(await crypto.subtle.digest("SHA-256", bytes))].map(value => value.toString(16).padStart(2, "0")).join(""),
+ }
+ })
+ await testInfo.attach(name, { body: JSON.stringify({ worker, resources, caches: await publicCacheKeys(page) }, null, 2), contentType: "application/json" })
+ return worker
+}
+
+test.describe("production public static resource cache", () => {
+ test("a first Blog visit registers the root worker and installs the added Main public resources", async ({ page }, testInfo) => {
+ await page.goto("/blog")
+ await expect(page.getByRole("heading", { name: "Blog", exact: true })).toBeVisible()
+ await expectRootWorker(page)
+ const keys = await publicCacheKeys(page)
+ const paths = keys[publicWorkerCacheNames().precache]?.map(url => new URL(url).pathname) ?? []
+ expect(paths).toEqual(expect.arrayContaining([
+ "/favicon.ico", "/manifest.json", "/contact/gus-photo.jpeg", "/fonts/jgs5/jgs5.ttf",
+ "/landing/people/alex.webp", "/landing/people/lin.webp", "/landing/people/nina.webp",
+ "/landing/people/priya.webp", "/landing/people/sam.webp", "/social/readme-card-58c75311e5ba.png",
+ ]))
+ expect(paths.some(path => /^\/_next\/static\/media\/pdf\.worker\.min\.[a-zA-Z0-9_-]+\.mjs$/.test(path))).toBe(true)
+ for (const name of ["_buildManifest.js", "_clientMiddlewareManifest.js", "_ssgManifest.js"]) expect(paths.some(path => path.startsWith("/_next/static/") && path.endsWith(`/${name}`))).toBe(true)
+ for (const url of Object.values(keys).flat()) {
+ const path = new URL(url).pathname
+ expect(path.startsWith("/api/") || path === "/blog" || path.startsWith("/c/")).toBe(false)
+ }
+ await page.goto("/contact")
+ const favicon = page.locator('link[rel="icon"][href^="/favicon.ico?"]').first()
+ await expect(favicon).toHaveAttribute("href", /^\/favicon\.ico\?favicon\.[a-z0-9]+\.ico$/)
+ const faviconURL = new URL((await favicon.getAttribute("href"))!, page.url()).href
+ const faviconKey = keys[publicWorkerCacheNames().precache]?.find(url => new URL(url).pathname === "/favicon.ico")
+ expect(faviconKey).toBeTruthy()
+ const resource = await readResource(page, { url: faviconURL, cacheName: publicWorkerCacheNames().precache })
+ expect(resource.fromServiceWorker).toBe(true)
+ await expectCachedBody(page, { ...resource, url: faviconKey! })
+ for (const url of [`${faviconURL}=`, `${faviconURL}&qa-query=one`, new URL(`/icon-192.png${new URL(faviconURL).search}`, page.url()).href]) {
+ expect((await readResource(page, { url, cacheName: publicWorkerCacheNames().precache })).fromServiceWorker, url).toBe(false)
+ }
+ expect((await publicCacheKeys(page))[publicWorkerCacheNames().precache]).toEqual(keys[publicWorkerCacheNames().precache])
+ await attachReceipt(page, testInfo, "blog-first-root-worker", [resource])
+ })
+
+ test("rendered Blog code, CSS, font and image stay readable offline without caching documents or query variants", async ({ page, context }, testInfo) => {
+ const resources = await warmBlogResources(page)
+ await attachReceipt(page, testInfo, "blog-static-online", resources)
+ await runWithCleanup(async () => {
+ await context.setOffline(true)
+ await expectOfflineResources(page, context, resources)
+ const rejected = await page.evaluate(async paths => Promise.all(paths.map(async path => {
+ try { await fetch(path, { credentials: "omit", cache: "no-store" }); return false }
+ catch { return true }
+ })), ["/blog", "/api/auth/get-session", `${resources[0].url}?qa-query=uncached`])
+ expect(rejected).toEqual([true, true, true])
+ }, () => context.setOffline(false))
+ await attachReceipt(page, testInfo, "blog-static-after-offline", resources)
+ })
+
+ test("public runtime bytes survive a browser-process restart in the same isolated profile", async ({ caseResources, baseURL, browserName }, testInfo) => {
+ expect(browserName).toBe("chromium")
+ expect(baseURL).toBeTruthy()
+ const deadline = Date.now() + testInfo.timeout
+ const launches: Array<{ close: () => Promise<void> }> = []
+ const profile = caseResources.own({ directory: undefined as string | undefined }, async state => {
+ const closed = await Promise.allSettled(launches.map(launch => launch.close()))
+ const failures = closed.filter(result => result.status === "rejected").map(result => result.reason)
+ if (failures.length) throw new AggregateError(failures, "Persistent browser cleanup failed; profile retained")
+ if (state.directory) await rm(state.directory, { recursive: true, force: true })
+ })
+ await runWithCleanup(async () => {
+ const directory = profile.value.directory = mkdtempSync(join(tmpdir(), "alook-public-static-222-"))
+ const launch = () => {
+ const owner = caseResources.own({ pending: undefined as Promise<BrowserContext> | undefined }, async state => {
+ if (state.pending) await (await state.pending).close()
+ })
+ launches.push(owner)
+ const remaining = deadline - Date.now()
+ if (remaining <= 0) throw new Error("Persistent browser launch exceeded the original test budget")
+ owner.value.pending = chromium.launchPersistentContext(directory, { baseURL, headless: true, timeout: remaining })
+ return { context: owner.value.pending, close: owner.close }
+ }
+ const first = launch()
+ const firstContext = await first.context
+ const firstPage = firstContext.pages()[0] ?? await firstContext.newPage()
+ const resources = await warmBlogResources(firstPage)
+ const before = await attachReceipt(firstPage, testInfo, "before-process-restart", resources)
+ await first.close()
+ const second = launch()
+ const secondContext = await second.context
+ const page = secondContext.pages()[0] ?? await secondContext.newPage()
+ await page.goto("/robots.txt")
+ await expectRootWorker(page)
+ expect(await page.evaluate(() => performance.getEntriesByType("resource").some(entry => /\/(?:blog-static\/_next\/static|blog\/[^/]+\/)/.test(new URL(entry.name).pathname)))).toBe(false)
+ await runWithCleanup(async () => {
+ await secondContext.setOffline(true)
+ await expectOfflineResources(page, secondContext, resources)
+ }, () => secondContext.setOffline(false))
+ const after = await attachReceipt(page, testInfo, "after-process-restart", resources)
+ expect(after).toEqual(before)
+ await second.close()
+ }, () => profile.close())
+ })
+})
diff --git a/src/web/src/test/e2e-ui/_fixtures/case-fixture.ts b/src/web/src/test/e2e-ui/_fixtures/case-fixture.ts
index f7dcab1c9..06b7c0a8e 100644
--- a/src/web/src/test/e2e-ui/_fixtures/case-fixture.ts
+++ b/src/web/src/test/e2e-ui/_fixtures/case-fixture.ts
@@ -3,7 +3,7 @@ import type { APIResponse, BrowserContext, BrowserContextOptions, Page, Response
import { createHash } from "crypto"
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
import { resolve } from "path"
-import { loginAndSaveState } from "../_setup/auth"
+import { loginAndSaveState, signIn } from "../_setup/auth"
import { AUTH_DIR, SERVICE_STATE_PATH, WEB_URL } from "../_setup/paths"
import { readLifecycleState } from "../_setup/service-lifecycle"
import { USER_KEYS, type SeededUser, type UserKey } from "../_setup/users"
@@ -15,6 +15,7 @@ import { retrySeedRequest } from "./seed-retry"
export type CaseActors = {
asUser: (key: UserKey, options?: Omit<BrowserContextOptions, "storageState">) => Promise<{ context: BrowserContext; page: Page }>
asNaturalUser: (key: UserKey, viewport: { width: number; height: number }) => Promise<{ context: BrowserContext; page: Page }>
+ signInOnPage: (key: UserKey, page: Page) => Promise<void>
userId: (key: UserKey) => string
userName: (key: UserKey) => string
sessionCookie: (key: UserKey) => string
@@ -28,6 +29,7 @@ export type SignedOutEvidence = { context: BrowserContext; page: Page; protected
export type ServerDeletionEvidence = { page: Page; response: PlaywrightResponse }
export type OwnedCaseResource<T> = { readonly value: T; close: () => Promise<void> }
export type CaseResources = {
+ readonly deadline: number
newContext: (options?: BrowserContextOptions) => Promise<BrowserContext>
closeContext: (context: BrowserContext) => Promise<void>
own: <T>(value: T, dispose: (value: T) => Promise<void>) => OwnedCaseResource<T>
@@ -69,14 +71,15 @@ async function checkCleanupFailure(testInfo: TestInfo): Promise<void> {
throw new Error(`Case fixture blocked by unresolved cleanup: ${failure.identity.testId}, worker ${failure.identity.workerIndex}`)
}
-export const test = base.extend<{ caseActors: CaseExitOwner["allocate"]; caseResources: CaseResources; caseExitOwner: CaseExitOwner; caseCleanupGuard: void }>({
+export const test = base.extend<{ caseActors: CaseExitOwner["allocate"]; caseResources: CaseResources; caseExitOwner: CaseExitOwner; caseCleanupGuard: number }>({
serviceGuard: async ({ caseCleanupGuard, serviceGuard }, provide) => {
void caseCleanupGuard
await provide(serviceGuard)
},
caseCleanupGuard: [async ({ }, provide, testInfo) => {
+ const deadline = Date.now() + testInfo.timeout
await checkCleanupFailure(testInfo)
- await provide()
+ await provide(deadline)
}, { auto: true }],
caseActors: async ({ caseExitOwner }, provide) => { await provide(caseExitOwner.allocate) },
caseResources: async ({ caseExitOwner }, provide) => { await provide(caseExitOwner.resources) },
@@ -171,7 +174,7 @@ export const test = base.extend<{ caseActors: CaseExitOwner["allocate"]; caseRes
operationCount += 1
return resource
}
- const caseResources: CaseResources = { newContext: (options) => newContext(options, null), closeContext, own }
+ const caseResources: CaseResources = { deadline: caseCleanupGuard, newContext: (options) => newContext(options, null), closeContext, own }
const sessionCookie = (key: UserKey): string => {
const state = JSON.parse(readFileSync(user(key).storageState, "utf8")) as { cookies: Array<{ name: string; value: string }> }
return state.cookies.filter((cookie) => cookie.name.startsWith("better-auth")).map((cookie) => `${cookie.name}=${cookie.value}`).join("; ")
@@ -331,6 +334,54 @@ export const test = base.extend<{ caseActors: CaseExitOwner["allocate"]; caseRes
}
await testInfo.attach("case-actor-ownership", { body: JSON.stringify({ identity, actors: roles.map((key) => ({ key, userId: user(key).userId })) }), contentType: "application/json" })
return {
+ signInOnPage: async (key, page) => {
+ const actor = user(key)
+ const context = page.context()
+ const registration = contexts.get(context)
+ if (exiting || !registration || closedContexts.has(context) || page.isClosed()) throw new Error("Same-device login requires an open page owned by this case")
+ if ((await context.cookies()).some(cookie => cookie.name.startsWith("better-auth"))) throw new Error("Same-device login requires the previous UI session to be signed out")
+ const session = { cookie: "", submitted: false, captureFailed: false, role: key, userId: actor.userId, pending: undefined as Promise<void> | undefined }
+ own(session, async session => {
+ await session.pending?.catch(() => {})
+ if (session.captureFailed) throw new Error("Additional case session capture failed; retirement remains unknown")
+ if (!session.cookie) {
+ if (session.submitted) throw new Error("Additional case login was submitted without a captured cookie; server session retirement remains UNKNOWN")
+ return
+ }
+ if (session.cookie === originalSessions.get(key)) throw new Error("Additional-session cleanup must not retire the original resource session")
+ const signal = () => {
+ const remaining = caseCleanupGuard - Date.now()
+ if (remaining <= 0) throw new Error("Additional case session retirement exceeded its original deadline")
+ return AbortSignal.timeout(remaining)
+ }
+ const probe = () => fetch(`${WEB_URL}/api/ws/token?refresh=1`, { signal: signal(), headers: { Cookie: session.cookie, Origin: WEB_URL } })
+ const before = await probe()
+ if (before.status === 401) {
+ if (before.headers.get("content-type") !== "text/plain;charset=UTF-8" || await before.text() !== "Unauthorized") throw new Error("Additional case session returned an abnormal unauthorized response")
+ return
+ }
+ if (!before.ok || (await before.json() as { userId?: string }).userId !== session.userId) throw new Error("Additional case session has an unexpected identity during cleanup")
+ const response = await fetch(`${WEB_URL}/api/auth/sign-out`, { method: "POST", signal: signal(), headers: { Cookie: session.cookie, Origin: WEB_URL, "Content-Type": "application/json" }, body: "{}" })
+ const after = await probe()
+ if (!response.ok || after.status !== 401 || after.headers.get("content-type") !== "text/plain;charset=UTF-8" || await after.text() !== "Unauthorized") throw new Error("Additional case session retirement failed")
+ })
+ session.pending = (async () => {
+ const errors: unknown[] = []
+ try { await signIn(page, actor.email, { singleAttempt: true, onSubmission: () => { session.submitted = true } }) } catch (error) { errors.push(error) }
+ try { session.cookie = (await context.cookies()).filter(value => value.name.startsWith("better-auth")).map(value => `${value.name}=${value.value}`).join("; ") }
+ catch (error) { session.captureFailed = true; errors.push(error) }
+ if (errors.length === 1) throw errors[0]
+ if (errors.length > 1) throw new AggregateError(errors, "Additional case login and session capture failed")
+ })()
+ await session.pending
+ const cookie = session.cookie
+ if (exiting || !contexts.has(context) || !cookie || cookie === originalSessions.get(key)) throw new Error("Same-device UI login completed outside its owned live session")
+ const me = await context.request.get(`${WEB_URL}/api/ws/token`)
+ if (!me.ok() || (await me.json() as { userId?: string }).userId !== actor.userId) throw new Error("Same-device UI login returned a different case actor")
+ if (exiting || !contexts.has(context) || closedContexts.has(context)) throw new Error("Additional case session identity completed after retirement")
+ contexts.set(context, { ...registration, role: key, kind: "actor" })
+ await testInfo.attach("case-same-device-login", { body: JSON.stringify({ identity, contextOrdinal: registration.ordinal, role: key, userId: actor.userId, sessionFingerprint: createHash("sha256").update(cookie).digest("hex"), originalResourceSessionPreserved: true }), contentType: "application/json" })
+ },
asUser: async (key, options) => {
const context = await newContext({ ...options, storageState: user(key).storageState }, key)
const page = await context.newPage()
diff --git a/src/web/src/test/e2e-ui/_fixtures/public-static-qa.ts b/src/web/src/test/e2e-ui/_fixtures/public-static-qa.ts
new file mode 100644
index 000000000..5d62f5ee6
--- /dev/null
+++ b/src/web/src/test/e2e-ui/_fixtures/public-static-qa.ts
@@ -0,0 +1,932 @@
+import { createHash } from "node:crypto"
+import { resolve } from "node:path"
+import { expect, type BrowserContext, type CDPSession, type Page, type Route, type TestInfo, type Worker } from "@playwright/test"
+import { readPhaseFile, writePhaseFile } from "../../../../scripts/dev-zone-processes.mjs"
+import { publicWorkerRuntimeCacheNames } from "../../../lib/service-worker/cache-identity"
+import { isPublicAssetResponse, publicRuntimeAssetKind } from "../../../lib/service-worker/public-cache"
+import { publicStaticQaManifest } from "../_setup/services"
+import type { PublicStaticPhaseReceipt } from "../_setup/service-lifecycle"
+import type { CaseActors, CaseResources } from "./case-fixture"
+import { WEB_URL, MACHINE_WS_URL } from "../_setup/paths"
+import { communityFrameEvents, proxyCommunityWebSockets } from "./community-ws-proxy"
+import { gotoAfterUserWsAuth } from "./actions"
+import { tid } from "./testids"
+
+export type ReleaseName = "N" | "Nplus1"
+export type QaAsset = { url: string; path: string; zone: "main" | "blog"; bytes: number; sha256: string; kind?: "code" | "fonts" | "images"; precache?: boolean; revision?: string | null }
+export type NativeResource = { url: string; bytes: number; sha256: string; fromServiceWorker: boolean }
+export type ExpirationRow = { id: string; cacheName: string; url: string; timestamp: number }
+
+export function createPublicStaticForensicRecord(options: { now?: () => number; monotonic?: () => string; limit?: number } = {}) {
+ const now = options.now ?? Date.now
+ const monotonic = options.monotonic ?? (() => process.hrtime.bigint().toString())
+ const limit = options.limit ?? 512
+ if (!Number.isInteger(limit) || limit < 1 || limit > 4096) throw new Error("Invalid forensic record limit")
+ const rows: Array<Record<string, string | number>> = []
+ const pending = new Set<number>()
+ let sequence = 0, request = 0, dropped = 0, observerFailures = 0
+ let phase = "setup"
+ const allowedPhases = new Set(["setup", "before", "after", "activated", "cleanup"])
+ const record = (event: string, values: Record<string, string | number> = {}) => {
+ try {
+ if (rows.length >= limit) { dropped++; return }
+ rows.push({ ...values, sequence: ++sequence, event, phase, pid: process.pid, unixMS: now(), monotonicNS: monotonic() })
+ } catch { observerFailures++ }
+ }
+ const pathOf = (path: string) => {
+ if (["/api/community/machines", "/api/community/machines/pair", "/api/community/daemon/activate"].includes(path)) return path
+ if (/^\/api\/community\/machines\/[^/?#]+$/.test(path)) return "/api/community/machines/:owned-machine"
+ if (/^\/api\/community\/channels\/[^/?#]+\/messages$/.test(path)) return "/api/community/channels/:owned-channel/messages"
+ return "other"
+ }
+ return {
+ phase(value: string) { phase = allowedPhases.has(value) ? value : "unknown" },
+ event(value: "ready-send-before" | "ready-send-returned" | "ws-open" | "ws-close" | "cleanup-start" | "activation-committed") { record(value) },
+ frame(value: string) { record("ws-frame", { type: value === "machine:heartbeat" ? value : "other" }) },
+ start(method: string, path: string) {
+ const id = ++request
+ pending.add(id)
+ record("http-start", { request: id, method: ["GET", "POST", "PATCH", "PUT", "DELETE"].includes(method) ? method : "other", path: pathOf(path) })
+ return id
+ },
+ response(id: number, response: Pick<Response, "status" | "headers">) {
+ try {
+ const timing = response.headers.get("server-timing") ?? ""
+ const metrics: string[] = []
+ let start = 0, quoted = false, escaped = false
+ if (timing.length <= 4096) for (let index = 0; index < timing.length; index++) {
+ const char = timing[index]
+ if (escaped) escaped = false
+ else if (quoted && char === "\\") escaped = true
+ else if (char === '"') quoted = !quoted
+ else if (char === "," && !quoted) { metrics.push(timing.slice(start, index).trim()); start = index + 1 }
+ }
+ if (timing.length <= 4096 && !quoted && !escaped) metrics.push(timing.slice(start).trim())
+ else metrics.length = 0
+ const named = (name: string) => metrics.filter(metric => metric.split(";", 1)[0]?.trim() === name)
+ const sampling = named("alook_sampling"), context = named("alook_server")
+ const selected = sampling.length === 1 ? /^alook_sampling;\s*desc="(selected|not_selected|unavailable)"$/.exec(sampling[0]!)?.[1] : undefined
+ const parent = context.length === 1 ? /^alook_server;\s*desc="(00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2})"$/.exec(context[0]!)?.[1] : undefined
+ const server = parent && !/^00-0{32}-/.test(parent) && !/-0{16}-/.test(parent) ? parent : undefined
+ record("http-response", { request: id, status: response.status, sampling: selected ?? "unavailable", ...(server ? { serverTraceContext: server } : {}) })
+ pending.delete(id)
+ } catch { observerFailures++ }
+ },
+ failed(id: number) { record("http-fetch-error", { request: id }); pending.delete(id) },
+ snapshot() { return { schema: 1, clocks: { unixMS: "Date.now; same-host wall clock, not monotonic", monotonicNS: "process.hrtime.bigint; same PID order only" }, qualification: "Local call-site observations only; ready send is not native receive/commit or SQLite concurrency", dropped, observerFailures, pendingRequests: pending.size, complete: dropped === 0 && observerFailures === 0 && pending.size === 0, rows: rows.map(row => ({ ...row })) } },
+ }
+}
+
+const forensicRecords = new WeakMap<CaseResources, ReturnType<typeof createPublicStaticForensicRecord>>()
+
+export function ownPublicStaticForensics(resources: CaseResources, testInfo: TestInfo) {
+ if (process.env.ALOOK_E2E_PUBLIC_STATIC_FORENSICS !== "1" || forensicRecords.has(resources)) return
+ const record = createPublicStaticForensicRecord()
+ forensicRecords.set(resources, record)
+ resources.own(record, async record => {
+ forensicRecords.delete(resources)
+ await attachQa(testInfo, "original-path-forensic-call-sites", record.snapshot())
+ })
+}
+
+export function blogBuildIdFromScripts(scripts: string) {
+ const normalized = scripts.replace(/\\"/g, '"')
+ const ids = [...new Set([...normalized.matchAll(/"b"\s*:\s*"([a-zA-Z0-9_-]+)"/g)].map(match => match[1]))]
+ if (ids.length !== 1) throw new Error("Rendered Blog Flight data must identify exactly one actual build ID")
+ return ids[0]!
+}
+
+export function requiredQaManifest() {
+ const manifest = publicStaticQaManifest(false)
+ if (!manifest) throw new Error("Supplementary public static QA has no managed release manifest")
+ return manifest
+}
+
+export function releaseAssets(name: ReleaseName): QaAsset[] {
+ return requiredQaManifest().releases[name].assets
+}
+
+export function runtimeName(kind: "code" | "fonts" | "images") {
+ return publicWorkerRuntimeCacheNames()[kind]
+}
+
+export function blocked(testInfo: TestInfo, reason: string): never {
+ testInfo.annotations.push({ type: "blocked", description: reason })
+ throw new Error(`BLOCKED: ${reason}`)
+}
+
+export function createQaPhaseOwner(resources: CaseResources, testInfo: TestInfo) {
+ const manifest = requiredQaManifest()
+ ownPublicStaticForensics(resources, testInfo)
+ const receiptPath = resolve(manifest.controlDirectory, "receipt.json")
+ const requestPath = resolve(manifest.controlDirectory, "request.json")
+ const caseId = testInfo.testId
+ const read = () => {
+ const receipt = readPhaseFile(receiptPath) as PublicStaticPhaseReceipt | null
+ if (!receipt || receipt.runId !== manifest.runId || receipt.manifestDigest !== manifest.digest || receipt.status !== "ready") throw new Error("Managed phase is absent, foreign or not ready")
+ if (receipt.caseId && receipt.caseId !== caseId) throw new Error("Managed release phase belongs to another case")
+ return receipt
+ }
+ let claimed = false
+ const command = async (to: ReleaseName, action: "switch" | "release") => {
+ const receipt = read()
+ if (action === "switch" && receipt.release === to) return
+ const remaining = resources.deadline - Date.now()
+ if (remaining <= 0) throw new Error("Phase operation exceeded the original case-owner deadline")
+ const request = { action, runId: manifest.runId, manifestDigest: manifest.digest, sequence: receipt.sequence + 1, from: receipt.release, to, caseId, deadline: resources.deadline }
+ claimed = true
+ writePhaseFile(requestPath, request)
+ await expect.poll(() => {
+ const current = readPhaseFile(receiptPath) as PublicStaticPhaseReceipt | null
+ if (current?.status === "failed" || current?.status === "stopped") throw new Error(current.failure ?? "Managed phase owner stopped")
+ return current?.runId === manifest.runId && current.manifestDigest === manifest.digest && current.sequence === request.sequence && current.status === "ready" && current.release === to && (action === "release" ? !current.caseId : current.caseId === caseId)
+ }, { timeout: remaining }).toBe(true)
+ await testInfo.attach(`managed-phase-${request.sequence}`, { body: JSON.stringify(readPhaseFile(receiptPath)), contentType: "application/json" })
+ if (action === "release") claimed = false
+ }
+ const owner = resources.own({}, async () => {
+ if (!claimed) return
+ if (read().release !== manifest.defaultRelease) await command(manifest.defaultRelease, "switch")
+ await command(manifest.defaultRelease, "release")
+ })
+ return { switchTo: (release: ReleaseName) => command(release, "switch"), current: () => read().release, close: owner.close }
+}
+
+export async function rootWorker(page: Page) {
+ const script = new URL("/sw.js", page.url()).href
+ await expect.poll(() => page.evaluate(async () => {
+ const registrations = await navigator.serviceWorker.getRegistrations()
+ return registrations.map(value => ({ scope: value.scope, script: value.active?.scriptURL, updateViaCache: value.updateViaCache }))
+ })).toEqual([{ scope: new URL("/", page.url()).href, script, updateViaCache: "none" }])
+ await expect.poll(() => page.evaluate(() => navigator.serviceWorker.controller?.scriptURL)).toBe(script)
+}
+
+export async function readNativeResource(page: Page, path: string): Promise<NativeResource> {
+ const url = new URL(path, page.url()).href
+ let accept: (response: import("@playwright/test").Response) => void = () => {}
+ let reject: (error: Error) => void = () => {}
+ const observed = new Promise<import("@playwright/test").Response>((resolveResponse, rejectResponse) => { accept = resolveResponse; reject = rejectResponse })
+ void observed.catch(() => {})
+ const listener = (response: import("@playwright/test").Response) => { if (response.url() === url && response.request().method() === "GET") accept(response) }
+ const closed = () => reject(new Error("Owned native resource page closed"))
+ page.on("response", listener)
+ page.once("close", closed)
+ try {
+ const [body, response] = await Promise.all([page.evaluate(async target => {
+ const response = await fetch(target, { credentials: "omit", cache: "no-store", redirect: "error" })
+ if (response.status !== 200) throw new Error(`Public static GET returned ${response.status}`)
+ const bytes = await response.arrayBuffer()
+ return { bytes: bytes.byteLength, sha256: [...new Uint8Array(await crypto.subtle.digest("SHA-256", bytes))].map(value => value.toString(16).padStart(2, "0")).join("") }
+ }, url), observed])
+ return { url, ...body, fromServiceWorker: response.fromServiceWorker() }
+ } finally { page.off("response", listener); page.off("close", closed) }
+}
+
+export async function publicNativeSnapshot(page: Page, onlyCaches?: string[]) {
+ return page.evaluate(async onlyCaches => {
+ const cachesByName: Record<string, Array<{ url: string; bytes: number; sha256: string; date: string | null }>> = {}
+ for (const name of await caches.keys()) {
+ if (onlyCaches && !onlyCaches.includes(name)) continue
+ const cache = await caches.open(name)
+ cachesByName[name] = []
+ for (const request of await cache.keys()) {
+ const response = await cache.match(request)
+ if (!response) throw new Error("Native cache entry disappeared during snapshot")
+ const bytes = await response.arrayBuffer()
+ cachesByName[name].push({ url: request.url, bytes: bytes.byteLength, date: response.headers.get("date"), sha256: [...new Uint8Array(await crypto.subtle.digest("SHA-256", bytes))].map(value => value.toString(16).padStart(2, "0")).join("") })
+ }
+ cachesByName[name].sort((a, b) => a.url.localeCompare(b.url))
+ }
+ const databases = await indexedDB.databases()
+ const expiration = databases.some(value => value.name === "serwist-expiration") ? await new Promise<Array<{ id: string; cacheName: string; url: string; timestamp: number }>>((resolveRows, reject) => {
+ const request = indexedDB.open("serwist-expiration")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result
+ if (!db.objectStoreNames.contains("cache-entries")) { db.close(); reject(new Error("Installed expiration store is absent")); return }
+ const tx = db.transaction("cache-entries", "readonly")
+ const rows = tx.objectStore("cache-entries").getAll()
+ tx.oncomplete = () => { db.close(); resolveRows(rows.result) }
+ tx.onerror = () => { db.close(); reject(tx.error) }
+ }
+ }) : []
+ return { caches: cachesByName, expiration: expiration.sort((a, b) => a.id.localeCompare(b.id)) }
+ }, onlyCaches)
+}
+
+export async function seedNativeExpiration(page: Page, cacheName: string, url: string, timestamp: number) {
+ if (!Object.values(publicWorkerRuntimeCacheNames()).includes(cacheName)) throw new Error("Artificial age fixture must use a current owned runtime cache")
+ return page.evaluate(async ({ cacheName, target, timestamp }) => {
+ const url = new URL(target, location.href).href
+ const cache = await caches.open(cacheName)
+ const response = await cache.match(url)
+ if (!response) throw new Error("Age fixture requires a real SW-created cached body")
+ const headers = new Headers(response.headers)
+ headers.set("date", new Date(timestamp).toUTCString())
+ await cache.put(url, new Response(await response.arrayBuffer(), { status: response.status, statusText: response.statusText, headers }))
+ await new Promise<void>((resolveWrite, reject) => {
+ const request = indexedDB.open("serwist-expiration")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction("cache-entries", "readwrite"), store = tx.objectStore("cache-entries")
+ const key = `${cacheName}|${url}`, read = store.get(key)
+ read.onsuccess = () => {
+ if (!read.result || read.result.cacheName !== cacheName || read.result.url !== url) { tx.abort(); return }
+ store.put({ ...read.result, timestamp })
+ }
+ tx.oncomplete = () => { db.close(); resolveWrite() }
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error ?? new Error("Owned expiration row is absent")) }
+ }
+ })
+ return { preparation: "artificial aged native state", cacheName, url, timestamp }
+ }, { cacheName, target: url, timestamp })
+}
+
+export function exactSwRequest(request: { url(): string; serviceWorker(): Worker | null }, url: string, worker: Worker) {
+ return request.url() === url && request.serviceWorker() === worker
+}
+
+export async function withinQaDeadline<T>(operation: Promise<T>, deadline: number, signal?: AbortSignal): Promise<T> {
+ let timer: ReturnType<typeof setTimeout> | undefined
+ let abort: (() => void) | undefined
+ try {
+ return await Promise.race([operation, new Promise<never>((_, reject) => {
+ abort = () => reject(new Error("Owned QA operation cancelled"))
+ if (signal?.aborted) { abort(); return }
+ signal?.addEventListener("abort", abort, { once: true })
+ timer = setTimeout(() => reject(new Error("Owned QA operation exceeded the original deadline")), Math.max(1, deadline - Date.now()))
+ })])
+ } finally {
+ if (timer) clearTimeout(timer)
+ if (abort) signal?.removeEventListener("abort", abort)
+ }
+}
+
+export function assertFaultBase(asset: QaAsset | undefined, status: number, headers: Record<string, string>, body: Buffer) {
+ if (!asset || status !== 200 || body.length !== asset.bytes || createHash("sha256").update(body).digest("hex") !== asset.sha256
+ || !/\bpublic\b/i.test(headers["cache-control"] ?? "") || /\b(?:private|no-store|no-cache)\b/i.test(headers["cache-control"] ?? "")
+ || !/^(?:text|application)\/(?:javascript|ecmascript)(?:;|$)/i.test(headers["content-type"] ?? "")
+ || !isPublicAssetResponse(new Request(new URL(asset.url, WEB_URL)), new Response(null, { status, headers }))) throw new Error("Fault base resource does not match its real bound public JS response")
+}
+
+export async function ownSwFault(resources: CaseResources, context: BrowserContext, url: string, candidate: Promise<Worker>, action: "404" | "offline" | "mime" | "private" | "no-store" | "hold", asset?: QaAsset) {
+ const records: Array<{ url: string; worker: string; action: string; originalStatus?: number; originalSha256?: string; completed?: boolean }> = []
+ let releaseHold: (() => void) | undefined
+ let held: Route | undefined
+ let disposing = false
+ const cancellation = new AbortController()
+ const acquisition = { pending: undefined as Promise<void> | undefined }
+ const failures: unknown[] = []
+ const gate = new Promise<void>(resolveGate => { releaseHold = resolveGate })
+ const tasks = new Set<Promise<void>>()
+ const handler = (route: Route) => {
+ const task = (async () => {
+ const request = route.request()
+ if (!request.serviceWorker()) { await route.continue(); return }
+ let worker: Worker
+ try { worker = await withinQaDeadline(candidate, resources.deadline, cancellation.signal) }
+ catch (error) { await route.abort("aborted"); if (!disposing) throw error; return }
+ if (!exactSwRequest(request, url, worker)) { await route.continue(); return }
+ const record: typeof records[number] = { url, worker: request.serviceWorker()!.url(), action }
+ records.push(record)
+ if (action === "hold") {
+ held = route
+ await withinQaDeadline(gate, resources.deadline, cancellation.signal).catch(error => { if (!disposing) throw error })
+ if (disposing) await route.abort("aborted")
+ else await route.continue()
+ } else if (action === "offline") await route.abort("internetdisconnected")
+ else if (action === "404") await route.fulfill({ status: 404, contentType: "text/plain", body: "owned partial-precache fault" })
+ else {
+ const remaining = resources.deadline - Date.now()
+ if (remaining <= 0) throw new Error("SW fault base fetch exceeded its original deadline")
+ const actual = await withinQaDeadline(route.fetch({ maxRedirects: 0, timeout: remaining }), resources.deadline, cancellation.signal)
+ const body = await withinQaDeadline(actual.body(), resources.deadline, cancellation.signal)
+ assertFaultBase(asset, actual.status(), actual.headers(), body)
+ record.originalStatus = actual.status()
+ record.originalSha256 = createHash("sha256").update(body).digest("hex")
+ const headers = { ...actual.headers(), ...(action === "mime" ? { "content-type": "text/html" } : { "cache-control": action === "private" ? "private, max-age=60" : "no-store" }) }
+ await withinQaDeadline(route.fulfill({ response: actual, headers }), resources.deadline, cancellation.signal)
+ }
+ record.completed = true
+ })().catch(async error => {
+ try { await route.abort("aborted") } catch (abortError) { if (!disposing) failures.push(abortError) }
+ throw error
+ })
+ tasks.add(task)
+ void task.catch(error => { failures.push(error) }).finally(() => tasks.delete(task))
+ return task
+ }
+ const owner = resources.own({}, async () => {
+ disposing = true
+ cancellation.abort()
+ releaseHold!()
+ const cleanupErrors: unknown[] = []
+ try { if (acquisition.pending) await withinQaDeadline(acquisition.pending, resources.deadline) } catch (error) { cleanupErrors.push(error) }
+ try { await withinQaDeadline(context.unroute(url, handler), resources.deadline) } catch (error) { cleanupErrors.push(error) }
+ const results = await Promise.allSettled([...tasks])
+ for (const result of results) if (result.status === "rejected" && !failures.includes(result.reason)) cleanupErrors.push(result.reason)
+ if (failures.length || cleanupErrors.length) throw new AggregateError([...failures, ...cleanupErrors], "Owned SW fault cleanup failed")
+ })
+ acquisition.pending = context.route(url, handler).then(async () => { if (disposing) await context.unroute(url, handler) })
+ await withinQaDeadline(acquisition.pending, resources.deadline)
+ if (disposing) { await context.unroute(url, handler); throw new Error("SW fault registration completed after its owner retired") }
+ return { records, held: () => held !== undefined, release: () => releaseHold!(), close: owner.close }
+}
+
+async function ownCdpAcquisition(resources: CaseResources, acquire: () => Promise<CDPSession>) {
+ const state = { retired: false, session: undefined as CDPSession | undefined, pending: undefined as Promise<CDPSession> | undefined }
+ const owner = resources.own(state, async state => {
+ state.retired = true
+ if (state.session) await withinQaDeadline(state.session.detach(), resources.deadline)
+ else if (state.pending) await withinQaDeadline(state.pending.then(() => {}), resources.deadline)
+ })
+ state.pending = acquire().then(async session => {
+ state.session = session
+ if (state.retired) { await session.detach(); throw new Error("CDP acquisition completed after owner retirement") }
+ return session
+ })
+ return { session: await withinQaDeadline(state.pending, resources.deadline), close: owner.close }
+}
+
+export async function ownedCdp(resources: CaseResources, context: BrowserContext, page: Page) {
+ return ownCdpAcquisition(resources, () => context.newCDPSession(page))
+}
+
+export async function runQaScope<T>(work: (onClose: (close: () => Promise<void>) => void) => Promise<T>): Promise<T> {
+ const closers: Array<() => Promise<void>> = [], errors: unknown[] = []
+ let value: T | undefined
+ try { value = await work(close => closers.push(close)) } catch (error) { errors.push(error) }
+ for (const close of closers.reverse()) try { await close() } catch (error) { errors.push(error) }
+ if (errors.length === 1) throw errors[0]
+ if (errors.length > 1) throw new AggregateError(errors, "QA operation and owned cleanup failed")
+ return value as T
+}
+
+export async function executingWorkerIdentity(page: Page, resources: CaseResources, testInfo: TestInfo, status: "activated" | "installed") {
+ return runQaScope(async onClose => {
+ const browser = page.context().browser()
+ if (!browser || browser.browserType().name() !== "chromium") return blocked(testInfo, "Executing worker identity requires the owned Chromium")
+ const rootCdp = await ownCdpAcquisition(resources, () => browser.newBrowserCDPSession())
+ onClose(rootCdp.close)
+ const root = rootCdp.session
+ const pageCdp = await ownedCdp(resources, page.context(), page)
+ onClose(pageCdp.close)
+ const versions = new Map<string, { versionId: string; registrationId: string; scriptURL: string; targetId?: string; status: string; runningStatus: string }>()
+ const registrations = new Map<string, { registrationId: string; scopeURL: string; isDeleted: boolean }>()
+ pageCdp.session.on("ServiceWorker.workerVersionUpdated", event => { for (const version of event.versions) versions.set(version.versionId, version) })
+ pageCdp.session.on("ServiceWorker.workerRegistrationUpdated", event => { for (const registration of event.registrations) registrations.set(registration.registrationId, registration) })
+ const pageTarget = (await withinQaDeadline(pageCdp.session.send("Target.getTargetInfo"), resources.deadline)).targetInfo
+ await withinQaDeadline(pageCdp.session.send("ServiceWorker.enable"), resources.deadline)
+ const scriptURL = new URL("/sw.js", page.url()).href
+ const scopeURL = new URL("/", page.url()).href
+ const matches = () => [...versions.values()].filter(value => value.scriptURL === scriptURL && value.status === status && value.runningStatus === "running" && value.targetId
+ && registrations.get(value.registrationId)?.scopeURL === scopeURL && !registrations.get(value.registrationId)?.isDeleted)
+ await expect.poll(() => matches().length).toBe(1).catch(() => blocked(testInfo, "The installed protocol cannot identify one running owned version"))
+ const targets = (await withinQaDeadline(root.send("Target.getTargets"), resources.deadline)).targetInfos
+ const choices = matches().filter(value => targets.some(target => target.targetId === value.targetId && target.type === "service_worker" && target.url === scriptURL && target.browserContextId === pageTarget.browserContextId))
+ const unique = [...new Map(choices.map(value => [value.versionId, value])).values()]
+ if (unique.length !== 1) return blocked(testInfo, "Active/waiting version does not map to exactly one running owned target")
+ const version = unique[0]!
+ const attached = { retired: false, pending: undefined as Promise<{ sessionId: string }> | undefined }
+ const targetOwner = resources.own(attached, async state => {
+ state.retired = true
+ if (state.pending) {
+ const target = await withinQaDeadline(state.pending, resources.deadline)
+ await withinQaDeadline(root.send("Target.detachFromTarget", { sessionId: target.sessionId }), resources.deadline)
+ }
+ })
+ onClose(targetOwner.close)
+ attached.pending = root.send("Target.attachToTarget", { targetId: version.targetId!, flatten: false }).then(async target => {
+ if (attached.retired) { await root.send("Target.detachFromTarget", { sessionId: target.sessionId }); throw new Error("Worker CDP attachment completed after retirement") }
+ return target
+ })
+ const { sessionId } = await withinQaDeadline(attached.pending, resources.deadline)
+ let nextId = 0
+ const parsed: Array<{ scriptId: string; url: string }> = []
+ const pending = new Map<number, { resolve: (value: Record<string, unknown>) => void; reject: (error: Error) => void }>()
+ const received = (event: { sessionId: string; message: string }) => {
+ if (event.sessionId !== sessionId) return
+ const message = JSON.parse(event.message) as { id?: number; method?: string; params?: { scriptId: string; url: string }; result?: Record<string, unknown>; error?: { message: string } }
+ if (message.method === "Debugger.scriptParsed" && message.params) parsed.push(message.params)
+ if (message.id !== undefined) {
+ const operation = pending.get(message.id)
+ if (operation) { pending.delete(message.id); if (message.error) operation.reject(new Error(message.error.message)); else operation.resolve(message.result ?? {}) }
+ }
+ }
+ root.on("Target.receivedMessageFromTarget", received)
+ const commands = resources.own(pending, async entries => {
+ root.off("Target.receivedMessageFromTarget", received)
+ for (const operation of entries.values()) operation.reject(new Error("Read-only worker identity session closed"))
+ entries.clear()
+ })
+ onClose(commands.close)
+ const send = async (method: string, params = {}) => {
+ const id = ++nextId
+ const result = new Promise<Record<string, unknown>>((resolve, reject) => pending.set(id, { resolve, reject }))
+ const response = withinQaDeadline(result, resources.deadline)
+ void response.catch(() => {})
+ try {
+ await withinQaDeadline(root.send("Target.sendMessageToTarget", { sessionId, message: JSON.stringify({ id, method, params }) }), resources.deadline)
+ return await response
+ } catch (error) {
+ pending.get(id)?.reject(error instanceof Error ? error : new Error(String(error)))
+ await response.catch(() => {})
+ throw error
+ } finally { pending.delete(id) }
+ }
+ await send("Debugger.enable")
+ await expect.poll(() => parsed.filter(value => value.url === scriptURL).length).toBe(1)
+ const source = await send("Debugger.getScriptSource", { scriptId: parsed.find(value => value.url === scriptURL)!.scriptId })
+ if (typeof source.scriptSource !== "string") return blocked(testInfo, "Owned target did not supply its parsed source")
+ const target = (await withinQaDeadline(root.send("Target.getTargetInfo", { targetId: version.targetId! }), resources.deadline)).targetInfo
+ const current = versions.get(version.versionId)
+ const registration = registrations.get(version.registrationId)
+ if (!current || current.status !== status || current.runningStatus !== "running" || current.targetId !== version.targetId || current.registrationId !== version.registrationId
+ || registration?.scopeURL !== scopeURL || registration.isDeleted || target.type !== "service_worker" || target.url !== scriptURL || target.browserContextId !== pageTarget.browserContextId) blocked(testInfo, "Worker identity changed while its parsed source was read")
+ return { ...version, browserContextId: pageTarget.browserContextId, bytes: Buffer.byteLength(source.scriptSource), sha256: createHash("sha256").update(source.scriptSource).digest("hex") }
+ })
+}
+
+export function actualRuntimeAsset(release: ReleaseName, kind: "code" | "fonts" | "images", exclude: string[] = []) {
+ const asset = releaseAssets(release).find(value => value.zone === "blog" && publicRuntimeAssetKind(value.url) === kind && !exclude.includes(value.url))
+ if (!asset) throw new Error(`Bound release has no actual ${kind} asset`)
+ return asset
+}
+
+export function releaseDelta(from: QaAsset[], to: QaAsset[]) {
+ const old = new Map(from.filter(asset => asset.precache).map(asset => [asset.url, asset]))
+ const next = new Map(to.filter(asset => asset.precache).map(asset => [asset.url, asset]))
+ return {
+ added: [...next.keys()].filter(url => !old.has(url)).sort(),
+ deleted: [...old.keys()].filter(url => !next.has(url)).sort(),
+ changed: [...next.keys()].filter(url => old.has(url) && old.get(url)!.sha256 !== next.get(url)!.sha256).sort(),
+ unchanged: [...next.keys()].filter(url => old.has(url) && old.get(url)!.sha256 === next.get(url)!.sha256).sort(),
+ }
+}
+
+export async function expectNativeBody(page: Page, cacheName: string, resource: NativeResource) {
+ await expect.poll(async () => (await publicNativeSnapshot(page, [cacheName])).caches[cacheName]?.find(value => value.url === resource.url)).toMatchObject({ url: resource.url, bytes: resource.bytes, sha256: resource.sha256 })
+}
+
+export async function warmBoundAsset(page: Page, asset: QaAsset) {
+ const resource = await readNativeResource(page, asset.url)
+ expect(resource.fromServiceWorker).toBe(true)
+ expect(resource).toMatchObject({ bytes: asset.bytes, sha256: asset.sha256 })
+ const kind = publicRuntimeAssetKind(asset.url)
+ if (kind) await expectNativeBody(page, runtimeName(kind), resource)
+ return resource
+}
+
+export async function ownedOffline(resources: CaseResources, context: BrowserContext, work: () => Promise<void>) {
+ const owner = resources.own({}, async () => { await context.setOffline(false) })
+ await runQaScope(async onClose => {
+ onClose(owner.close)
+ await context.setOffline(true)
+ await work()
+ })
+}
+
+export function qaDeadlineSignal(resources: CaseResources) {
+ const remaining = resources.deadline - Date.now()
+ if (remaining <= 0) throw new Error("QA HTTP operation exceeded its original case deadline")
+ return AbortSignal.timeout(remaining)
+}
+
+export async function qaApi<T>(actors: CaseActors, resources: CaseResources, role: "alice" | "bob", path: string, init?: RequestInit): Promise<{ status: number; body: T }> {
+ const observation = forensicRecords.get(resources)
+ const request = observation?.start(init?.method ?? "GET", path)
+ let response: Response
+ try {
+ response = await fetch(`${WEB_URL}${path}`, { ...init, signal: qaDeadlineSignal(resources), headers: { Cookie: actors.sessionCookie(role), Origin: WEB_URL, ...(init?.body ? { "content-type": "application/json" } : {}), ...init?.headers } })
+ } catch (error) {
+ if (request !== undefined) observation!.failed(request)
+ throw error
+ }
+ if (request !== undefined) observation!.response(request, response)
+ if (!response.ok) throw new Error(`QA ${init?.method ?? "GET"} ${path} returned ${response.status}`)
+ return { status: response.status, body: await response.json() as T }
+}
+
+export async function ownedBusiness(actors: CaseActors, role: "alice" | "bob", resources: CaseResources) {
+ const serverId = await actors.seed.seedServer(role, `public-static-${role}-${Date.now()}`)
+ const channelId = await actors.seed.seedChannel(role, serverId, "native-cache-qa")
+ const { page, context } = await actors.asUser(role)
+ const proxy = await proxyCommunityWebSockets(context)
+ await gotoAfterUserWsAuth(page, `/c/channels/${serverId}/${channelId}`)
+ const write = async (text: string) => {
+ const offset = proxy.frames.length
+ const result = await qaApi<{ message: { id: string; seq: number; content: string } }>(actors, resources, role, `/api/community/channels/${channelId}/messages`, { method: "POST", body: JSON.stringify({ content: text }) })
+ expect(result.status).toBe(201)
+ expect(result.body.message.seq).toBeGreaterThan(0)
+ await expect.poll(() => proxy.frames.slice(offset).flatMap(communityFrameEvents).some(event => event.type === "community:message.create" && (event.messageId === result.body.message.id || event.message?.id === result.body.message.id))).toBe(true)
+ await expect(page.getByText(text, { exact: true }).first()).toBeVisible()
+ return { ...result.body.message, status: result.status }
+ }
+ const read = async (id: string) => {
+ const result = await qaApi<{ messages: Array<{ id: string; seq: number; content: string }> }>(actors, resources, role, `/api/community/channels/${channelId}/messages`)
+ const message = result.body.messages.find(value => value.id === id)
+ expect(message).toBeTruthy()
+ return message!
+ }
+ return { page, context, proxy, serverId, channelId, write, read, close: () => resources.closeContext(context) }
+}
+
+export async function ownD1WsProbe(actors: CaseActors, resources: CaseResources, testInfo: TestInfo) {
+ const observation = forensicRecords.get(resources)
+ const hostname = `qa222-${createHash("sha256").update(testInfo.testId).digest("hex").slice(0, 20)}`
+ const state = { machineId: "", pairingRequested: false, tokenId: "", tokenConsumed: false, activationOutcome: "not_attempted", activationPending: undefined as Promise<void> | undefined, socket: undefined as WebSocket | undefined, closed: undefined as Promise<void> | undefined, opened: 0, closes: 0, frames: [] as Array<{ type: string }> }
+ const owner = resources.own(state, async state => {
+ observation?.phase("cleanup")
+ observation?.event("cleanup-start")
+ const errors: unknown[] = []
+ await state.activationPending?.catch(error => errors.push(error))
+ if (state.pairingRequested && !state.tokenConsumed) errors.push(new Error(`Pairing token consumption remains UNKNOWN (${state.activationOutcome}); no public revocation proof is available`))
+ try {
+ if (state.socket && state.socket.readyState !== WebSocket.CLOSED) {
+ if (state.socket.readyState === WebSocket.CONNECTING) state.socket.close()
+ else state.socket.close(1000, "case owner cleanup")
+ await withinQaDeadline(state.closed!, resources.deadline)
+ }
+ } catch (error) { errors.push(error) }
+ try {
+ const machines = await qaApi<{ machines: Array<{ id: string; hostname: string }> }>(actors, resources, "alice", "/api/community/machines")
+ const ids = new Set([state.machineId, ...machines.body.machines.filter(value => value.hostname.startsWith(hostname)).map(value => value.id)].filter(Boolean))
+ for (const id of ids) {
+ const deleted = await fetch(`${WEB_URL}/api/community/machines/${id}`, { method: "DELETE", signal: qaDeadlineSignal(resources), headers: { Cookie: actors.sessionCookie("alice"), Origin: WEB_URL } })
+ expect(deleted.status).toBe(204)
+ }
+ expect((await qaApi<{ machines: Array<{ id: string; hostname: string }> }>(actors, resources, "alice", "/api/community/machines")).body.machines.some(value => ids.has(value.id) || value.hostname.startsWith(hostname))).toBe(false)
+ } catch (error) { errors.push(error) }
+ if (errors.length) throw new AggregateError(errors, "D1/WS capability resource retirement failed")
+ })
+ state.activationPending = (async () => {
+ state.pairingRequested = true
+ const pair = await qaApi<{ tokenId: string }>(actors, resources, "alice", "/api/community/machines/pair", { method: "POST" })
+ state.tokenId = pair.body.tokenId
+ if (!state.tokenId) throw new Error("Pairing response did not identify its issued token")
+ state.activationOutcome = "unknown"
+ const activationRequest = observation?.start("POST", "/api/community/daemon/activate")
+ const response = await fetch(`${WEB_URL}/api/community/daemon/activate`, {
+ method: "POST", signal: qaDeadlineSignal(resources), headers: { Authorization: `Bearer ${state.tokenId}`, Origin: WEB_URL, "content-type": "application/json" },
+ body: JSON.stringify({ hostname, platform: "linux", arch: "x64", osRelease: "qa", daemonVersion: "0.1.53", runtimeReport: [] }),
+ })
+ if (activationRequest !== undefined) observation!.response(activationRequest, response)
+ expect(response.status).toBe(200)
+ const activation = await response.json() as { machineId: string; credential: string; sessionOutcome: string }
+ state.machineId = activation.machineId
+ state.activationOutcome = activation.sessionOutcome
+ expect(activation.sessionOutcome).toBe("committed")
+ expect(activation.machineId).toBeTruthy()
+ expect(activation.credential).toBeTruthy()
+ state.tokenConsumed = true
+ observation?.event("activation-committed")
+ const NativeSocket = globalThis.WebSocket as unknown as new (url: string, options: { headers: Record<string, string> }) => WebSocket
+ state.socket = new NativeSocket(MACHINE_WS_URL.replace(/^http/, "ws"), { headers: { Authorization: `Bearer ${activation.credential}`, Origin: WEB_URL } })
+ state.closed = new Promise(resolveClose => state.socket!.addEventListener("close", () => { state.closes++; observation?.event("ws-close"); resolveClose() }, { once: true }))
+ state.socket.addEventListener("message", event => {
+ const frame = JSON.parse(String(event.data)) as { type: string; nonce?: string }
+ state.frames.push({ type: frame.type })
+ observation?.frame(frame.type)
+ if (frame.type === "machine:heartbeat") state.socket!.send(JSON.stringify({ type: "machine_heartbeat_ack", nonce: frame.nonce }))
+ })
+ await withinQaDeadline(new Promise<void>((resolveOpen, reject) => {
+ state.socket!.addEventListener("open", () => { state.opened++; observation?.event("ws-open"); resolveOpen() }, { once: true })
+ state.socket!.addEventListener("error", () => reject(new Error("Real same-origin machine WS failed")), { once: true })
+ state.socket!.addEventListener("close", () => reject(new Error("Real same-origin machine WS closed before ready")), { once: true })
+ }), resources.deadline)
+ })()
+ await state.activationPending
+ const writeAndRead = async (phase: string) => {
+ observation?.phase(phase)
+ const socket = state.socket!
+ const observedFrom = Date.now()
+ expect(state.socket!.readyState).toBe(WebSocket.OPEN)
+ expect(state.opened).toBe(1)
+ expect(state.closes).toBe(0)
+ const changedHostname = `${hostname}-${phase}`
+ observation?.event("ready-send-before")
+ state.socket!.send(JSON.stringify({ type: "ready", runtimeReport: [], runningAgents: [], capabilities: ["control-heartbeat-v1"], hostname: changedHostname, platform: "linux", arch: "x64", osRelease: "qa", daemonVersion: "0.1.53" }))
+ observation?.event("ready-send-returned")
+ await expect.poll(async () => (await qaApi<{ machines: Array<{ id: string; hostname: string; status: string }> }>(actors, resources, "alice", "/api/community/machines")).body.machines.find(value => value.id === state.machineId)).toMatchObject({ id: state.machineId, hostname: changedHostname, status: "online" })
+ expect(state.socket).toBe(socket)
+ expect(socket.readyState).toBe(WebSocket.OPEN)
+ expect(state.opened).toBe(1)
+ expect(state.closes).toBe(0)
+ return { machineId: state.machineId, hostname: changedHostname, observedFrom, observedThrough: Date.now(), sameSocketInstance: true, opened: state.opened, closes: state.closes, frameTypes: state.frames.map(value => value.type), nodeVersion: process.versions.node }
+ }
+ return { writeAndRead, close: owner.close }
+}
+
+export function observeCandidate(context: BrowserContext, resources: CaseResources) {
+ const existing = new Set(context.serviceWorkers())
+ let resolveWorker: (worker: Worker) => void = () => {}
+ const promise = new Promise<Worker>(resolve => { resolveWorker = resolve })
+ const listener = (worker: Worker) => { if (!existing.has(worker) && new URL(worker.url()).pathname === "/sw.js") resolveWorker(worker) }
+ const owner = resources.own({}, async () => { context.off("serviceworker", listener) })
+ context.on("serviceworker", listener)
+ return { promise, close: owner.close }
+}
+
+export async function updateRoot(page: Page) {
+ return page.evaluate(async () => {
+ const registration = await navigator.serviceWorker.getRegistration("/")
+ if (!registration?.active) throw new Error("Real update requires an existing active root registration")
+ const states: string[] = []
+ const found = () => {
+ const worker = registration.installing
+ if (!worker) return
+ states.push(worker.state)
+ worker.addEventListener("statechange", () => states.push(worker.state))
+ }
+ registration.addEventListener("updatefound", found, { once: true })
+ const state = window as unknown as { __qa222WorkerStates?: string[] }
+ state.__qa222WorkerStates = states
+ try { await registration.update(); return { update: "resolved" } }
+ catch (error) { return { update: "rejected", error: String(error) } }
+ })
+}
+
+export async function rootStates(page: Page) {
+ return page.evaluate(async () => {
+ const registration = await navigator.serviceWorker.getRegistration("/")
+ return { active: registration?.active?.state, waiting: registration?.waiting?.state, installing: registration?.installing?.state, observed: (window as unknown as { __qa222WorkerStates?: string[] }).__qa222WorkerStates ?? [] }
+ })
+}
+
+export async function expectReleasePrecache(page: Page, release: ReleaseName) {
+ const expected = releaseAssets(release).filter(asset => asset.precache)
+ if (!expected.length) throw new Error("Bound release has no complete precache catalogue")
+ const snapshot = await publicNativeSnapshot(page)
+ const actual = Object.entries(snapshot.caches).filter(([name]) => name.startsWith("alook-public-precache-")).flatMap(([, entries]) => entries)
+ expect(actual.map(entry => new URL(entry.url).pathname).sort()).toEqual(expected.map(asset => asset.url).sort())
+ for (const asset of expected) {
+ const entry = actual.find(entry => new URL(entry.url).pathname === asset.url)!
+ expect(entry).toMatchObject({ bytes: asset.bytes, sha256: asset.sha256 })
+ const key = new URL(entry.url)
+ expect(key.origin).toBe(new URL(page.url()).origin)
+ expect([...key.searchParams.values()]).toEqual(asset.revision === null ? [] : [asset.revision])
+ }
+ return snapshot
+}
+
+export async function activateAfterClientExit(context: BrowserContext, resources: CaseResources, pages: Page[], versionId: string, testInfo: TestInfo) {
+ const scopeURL = new URL("/", pages[0]!.url()).href
+ const page = await context.newPage()
+ await runQaScope(async onClose => {
+ const cdp = await ownedCdp(resources, context, page)
+ onClose(cdp.close)
+ const versions = new Map<string, { versionId: string; registrationId: string; status: string }>()
+ const scopes = new Map<string, string>()
+ cdp.session.on("ServiceWorker.workerRegistrationUpdated", event => { for (const value of event.registrations) if (!value.isDeleted) scopes.set(value.registrationId, value.scopeURL) })
+ cdp.session.on("ServiceWorker.workerVersionUpdated", event => { for (const value of event.versions) versions.set(value.versionId, value) })
+ await withinQaDeadline(cdp.session.send("ServiceWorker.enable"), resources.deadline)
+ await expect.poll(() => {
+ const candidate = versions.get(versionId)
+ return candidate && scopes.get(candidate.registrationId) === scopeURL ? candidate.status : null
+ }).toBe("installed").catch(() => blocked(testInfo, "The uncontrolled owned page cannot observe the exact waiting version"))
+ expect(page.url()).toBe("about:blank")
+ for (const old of pages) await old.close()
+ await expect.poll(() => versions.get(versionId)?.status).toBe("activated").catch(() => blocked(testInfo, "Exact waiting version did not naturally activate after its controlled clients closed"))
+ })
+ await page.goto("/robots.txt")
+ await rootWorker(page)
+ return page
+}
+
+export async function attachQa(testInfo: TestInfo, name: string, value: unknown) {
+ await testInfo.attach(name, { body: JSON.stringify(value, null, 2), contentType: "application/json" })
+}
+
+export async function seedNativeOverflow(page: Page, kind: "code" | "fonts", count: number, realUrl: string) {
+ const cacheName = runtimeName(kind)
+ return page.evaluate(async ({ kind, count, cacheName, realUrl }) => {
+ const cache = await caches.open(cacheName)
+ const template = await cache.match(new URL(realUrl, location.href).href)
+ if (!template) throw new Error("Overflow preparation requires a real worker-created body")
+ const bytes = await template.arrayBuffer()
+ const base = Date.now() - 60_000
+ const entries = Array.from({ length: count }, (_, index) => ({ url: new URL(`/blog-static/_next/static/${kind === "code" ? "chunks" : "media"}/qa222-overflow-${String(index).padStart(3, "0")}.${kind === "code" ? "js" : "woff2"}`, location.href).href, timestamp: base + index }))
+ for (const entry of entries) await cache.put(entry.url, new Response(bytes.slice(0), { headers: template.headers }))
+ await new Promise<void>((resolveWrite, reject) => {
+ const request = indexedDB.open("serwist-expiration")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction("cache-entries", "readwrite"), store = tx.objectStore("cache-entries")
+ const real = store.get(`${cacheName}|${new URL(realUrl, location.href).href}`)
+ real.onsuccess = () => {
+ if (!real.result || real.result.cacheName !== cacheName) { tx.abort(); return }
+ for (const entry of entries) store.put({ id: `${cacheName}|${entry.url}`, cacheName, ...entry })
+ }
+ tx.oncomplete = () => { db.close(); resolveWrite() }
+ tx.onerror = tx.onabort = () => { db.close(); reject(tx.error ?? new Error("Native overflow metadata template is absent")) }
+ }
+ })
+ return { preparation: "artificial native overflow, not distinct real downloads", cacheName, entries }
+ }, { kind, count, cacheName, realUrl })
+}
+
+export async function prepareUnrelatedNativeState(page: Page, caseId: string) {
+ const sentinel = `qa:222:${createHash("sha256").update(caseId).digest("hex").slice(0, 20)}`
+ await page.evaluate(async sentinel => {
+ for (const [name, storeName] of [["keyval-store", "keyval"], [sentinel, "sentinel"]]) {
+ await new Promise<void>((resolveWrite, reject) => {
+ const request = indexedDB.open(name)
+ request.onupgradeneeded = () => { if (!request.result.objectStoreNames.contains(storeName)) request.result.createObjectStore(storeName) }
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction(storeName, "readwrite")
+ tx.objectStore(storeName).put({ sentinel, intact: true }, sentinel)
+ tx.oncomplete = () => { db.close(); resolveWrite() }
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error) }
+ }
+ })
+ }
+ await new Promise<void>((resolveWrite, reject) => {
+ const request = indexedDB.open("serwist-expiration")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction("cache-entries", "readwrite")
+ const url = new URL(`/${sentinel}`, location.href).href
+ tx.objectStore("cache-entries").put({ id: `${sentinel}|${url}`, cacheName: sentinel, url, timestamp: 222 })
+ tx.oncomplete = () => { db.close(); resolveWrite() }
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error) }
+ }
+ })
+ }, sentinel)
+ return sentinel
+}
+
+export async function expectUnrelatedNativeState(page: Page, sentinel: string) {
+ const records = await page.evaluate(async sentinel => {
+ const values = []
+ for (const [name, storeName] of [["keyval-store", "keyval"], [sentinel, "sentinel"]]) {
+ values.push(await new Promise<unknown>((resolveRead, reject) => {
+ const request = indexedDB.open(name)
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction(storeName, "readonly"), read = tx.objectStore(storeName).get(sentinel)
+ tx.oncomplete = () => { db.close(); resolveRead(read.result) }
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error) }
+ }
+ }))
+ }
+ return values
+ }, sentinel)
+ expect(records).toEqual([{ sentinel, intact: true }, { sentinel, intact: true }])
+ const rows = (await publicNativeSnapshot(page)).expiration.filter(row => row.cacheName === sentinel)
+ expect(rows).toEqual([{ id: `${sentinel}|${new URL(`/${sentinel}`, page.url()).href}`, cacheName: sentinel, url: new URL(`/${sentinel}`, page.url()).href, timestamp: 222 }])
+}
+
+export async function seedOldPublicNamespace(page: Page, realUrl: string) {
+ const sourceCache = runtimeName("code")
+ return page.evaluate(async ({ realUrl, sourceCache }) => {
+ const url = new URL(realUrl, location.href).href
+ const response = await (await caches.open(sourceCache)).match(url)
+ if (!response) throw new Error("Old-namespace fixture requires a real captured current public body")
+ const names = ["alook-public-v0-blog-code", "alook-public-precache-v0"]
+ for (const name of names) await (await caches.open(name)).put(url, response.clone())
+ await new Promise<void>((resolveWrite, reject) => {
+ const request = indexedDB.open("serwist-expiration")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction("cache-entries", "readwrite"), cacheName = names[0]!
+ tx.objectStore("cache-entries").put({ id: `${cacheName}|${url}`, cacheName, url, timestamp: Date.now() })
+ tx.oncomplete = () => { db.close(); resolveWrite() }
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error) }
+ }
+ })
+ return { preparation: "artificial discoverable old public namespaces using a captured real current body; not historical residue evidence", names, url }
+ }, { realUrl, sourceCache })
+}
+
+export type DeviceSnapshot = { epoch: string; records: Array<{ key: string; encoded: string }>; payloadKeys: string[] }
+
+export async function deviceSnapshot(page: Page): Promise<DeviceSnapshot> {
+ return page.evaluate(async () => new Promise<{ epoch: string; records: Array<{ key: string; encoded: string }>; payloadKeys: string[] }>((resolveRead, reject) => {
+ const request = indexedDB.open("keyval-store")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction("keyval", "readonly"), store = tx.objectStore("keyval")
+ const keys = store.getAllKeys(), values = store.getAll()
+ tx.oncomplete = () => {
+ const records = keys.result.map((key, index) => ({ key: String(key), encoded: JSON.stringify(values.result[index], (_, value) => value instanceof Map ? { nativeMap: [...value.entries()].sort(([a], [b]) => String(a).localeCompare(String(b))) } : value) })).sort((a, b) => a.key.localeCompare(b.key))
+ const epoch = values.result[keys.result.indexOf("alook:qc:device-epoch")]
+ db.close()
+ resolveRead({ epoch, records, payloadKeys: records.filter(record => /^alook:qc:[^:]+:[^:]+(?::application)?:client$/.test(record.key)).map(record => record.key) })
+ }
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error) }
+ }
+ }))
+}
+
+export function preservedDeviceMetadata(snapshot: DeviceSnapshot) {
+ return snapshot.records.filter(record => record.key !== "alook:qc:device-epoch" && !snapshot.payloadKeys.includes(record.key))
+}
+
+export async function insertArtificialRetiredResidual(page: Page, userId: string, captured: DeviceSnapshot) {
+ const key = `alook:qc:cache:${userId}:client`
+ const record = captured.records.find(record => record.key === key)
+ if (!record) throw new Error("Retired residual requires a captured real account payload")
+ const payload = JSON.parse(record.encoded) as unknown
+ if (typeof payload !== "string") throw new Error("Captured native community payload is not its real JSON string")
+ const envelope = JSON.parse(payload) as { version: number; buster: string; clientState: unknown; channelFences: unknown[] }
+ if (envelope.version !== 6 || envelope.buster !== "6" || !envelope.clientState || !Array.isArray(envelope.channelFences)) throw new Error("Captured real persistence envelope is not the forwarded version 6 contract")
+ const keys = [key, `alook:qc:cache:${userId}:application:client`, `alook:qc:legacy:${userId}:client`]
+ const channelFixture = { channelId: crypto.randomUUID(), epoch: crypto.randomUUID(), deleted: true }
+ await page.evaluate(async ({ keys, payload, userId, channelFixture }) => new Promise<void>((resolveWrite, reject) => {
+ const request = indexedDB.open("keyval-store")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ const db = request.result, tx = db.transaction("keyval", "readwrite"), store = tx.objectStore("keyval")
+ const fenceKey = `alook:qc:cache:${userId}:account-epoch:channels`, read = store.get(fenceKey)
+ read.onsuccess = () => {
+ if (!(read.result instanceof Map)) { tx.abort(); return }
+ const map = read.result as Map<string, { epoch: string; deleted: boolean }>
+ map.set(channelFixture.channelId, { epoch: channelFixture.epoch, deleted: true })
+ store.put(map, fenceKey)
+ for (const key of keys) store.put(payload, key)
+ }
+ tx.oncomplete = () => { db.close(); resolveWrite() }
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error) }
+ }
+ }), { keys, payload, userId, channelFixture })
+ return { preparation: "artificial retired account residual and native Map tombstone for an unused fixture channel UUID; compatibility keys copy captured community bytes only for physical deletion", keys, channelFixture, bytes: Buffer.byteLength(payload), sha256: createHash("sha256").update(payload).digest("hex") }
+}
+
+export async function observeDeviceClear(page: Page, resources: CaseResources, previousEpoch: string) {
+ if (typeof previousEpoch !== "string" || !/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(previousEpoch)) throw new Error("Device Clear requires a captured native UUID epoch")
+ const owner = resources.own({}, async () => {
+ if (!page.isClosed()) await page.evaluate(() => { (window as unknown as { __qa222Clear?: { cancel(): void } }).__qa222Clear?.cancel() })
+ })
+ await page.evaluate(({ previousEpoch, deadline }) => {
+ const state = window as unknown as { __qa222Clear?: { cancel(): void; result: Promise<{ epoch: string; records: Array<{ key: string; encoded: string }>; payloadKeys: string[] }> } }
+ if (state.__qa222Clear) throw new Error("Device-clear observer already exists")
+ let db: IDBDatabase | undefined, retired = false
+ const timerState = { value: undefined as ReturnType<typeof setTimeout> | undefined }
+ let rejectResult: (error: Error) => void = () => {}
+ const result = new Promise<{ epoch: string; records: Array<{ key: string; encoded: string }>; payloadKeys: string[] }>((resolveRead, reject) => {
+ rejectResult = reject
+ const request = indexedDB.open("keyval-store")
+ request.onerror = () => reject(request.error)
+ request.onsuccess = () => {
+ db = request.result
+ if (retired) { db.close(); return }
+ const probe = () => {
+ if (retired) return
+ const tx = db!.transaction("keyval", "readonly"), store = tx.objectStore("keyval"), keys = store.getAllKeys(), values = store.getAll()
+ tx.onabort = tx.onerror = () => { retired = true; clearTimeout(timerState.value); db!.close(); reject(tx.error) }
+ tx.oncomplete = () => {
+ const epoch = values.result[keys.result.indexOf("alook:qc:device-epoch")]
+ if (epoch === previousEpoch) { probe(); return }
+ const records = keys.result.map((key, index) => ({ key: String(key), encoded: JSON.stringify(values.result[index], (_, value) => value instanceof Map ? { nativeMap: [...value.entries()].sort(([a], [b]) => String(a).localeCompare(String(b))) } : value) })).sort((a, b) => a.key.localeCompare(b.key))
+ retired = true
+ clearTimeout(timerState.value)
+ db!.close()
+ resolveRead({ epoch, records, payloadKeys: records.filter(record => /^alook:qc:[^:]+:[^:]+(?::application)?:client$/.test(record.key)).map(record => record.key) })
+ }
+ }
+ probe()
+ }
+ })
+ void result.catch(() => {})
+ const cancel = () => { retired = true; clearTimeout(timerState.value); db?.close(); rejectResult(new Error("Owned device-clear observer retired")) }
+ timerState.value = setTimeout(cancel, Math.max(1, deadline - Date.now()))
+ state.__qa222Clear = { result, cancel }
+ }, { previousEpoch, deadline: resources.deadline })
+ return { result: () => page.evaluate(() => (window as unknown as { __qa222Clear: { result: Promise<DeviceSnapshot> } }).__qa222Clear.result), close: owner.close }
+}
+
+export async function clearThroughSettings(page: Page) {
+ return runQaScope(async onClose => {
+ await page.getByTestId(tid.userSettingsOpen).click()
+ await page.getByTestId(tid.settingsTab("advanced")).click()
+ const sizeBefore = await page.getByTestId(tid.settingsCacheSize).innerText()
+ const dialogs: string[] = []
+ const dismissals: Promise<void>[] = []
+ const accept = (dialog: import("@playwright/test").Dialog) => {
+ dialogs.push(dialog.message())
+ const pending = Promise.resolve().then(() => dialog.dismiss())
+ dismissals.push(pending)
+ void pending.catch(() => {})
+ }
+ page.on("dialog", accept)
+ onClose(async () => {
+ page.off("dialog", accept)
+ const results = await Promise.allSettled(dismissals)
+ const failures = results.filter(result => result.status === "rejected").map(result => result.reason)
+ if (failures.length) throw new AggregateError(failures, "Settings error-dialog dismissal failed")
+ })
+ await page.getByRole("button", { name: "Clear local cache", exact: true }).click()
+ const dialog = page.getByRole("dialog").or(page.getByRole("alertdialog")).filter({ hasText: "Clear local cache?" })
+ await expect(dialog).toBeVisible()
+ await dialog.getByRole("button", { name: "Clear cache", exact: true }).click()
+ await expect(dialog).toBeHidden()
+ expect(dialogs).toEqual([])
+ await expect(page.getByRole("alert").filter({ hasText: /(?:failed|error|could not)/i })).toHaveCount(0)
+ const sizeAfter = await page.getByTestId(tid.settingsCacheSize).innerText()
+ expect(sizeAfter.trim().length).toBeGreaterThan(0)
+ await page.keyboard.press("Escape")
+ await expect(page.getByTestId(tid.settingsTab("advanced"))).toBeHidden()
+ return { sizeBefore, sizeAfter, errorDialogs: dialogs }
+ })
+}
diff --git a/src/web/src/test/e2e-ui/_qa/public-static/clear.qa.ts b/src/web/src/test/e2e-ui/_qa/public-static/clear.qa.ts
new file mode 100644
index 000000000..7c1ec4a6d
--- /dev/null
+++ b/src/web/src/test/e2e-ui/_qa/public-static/clear.qa.ts
@@ -0,0 +1,143 @@
+import { expect, test } from "../../_fixtures/case-fixture"
+import { communityFrameEvents, proxyCommunityWebSockets } from "../../_fixtures/community-ws-proxy"
+import { gotoAfterUserWsAuth } from "../../_fixtures/actions"
+import { WEB_URL } from "../../_setup/paths"
+import { tid } from "../../_fixtures/testids"
+import {
+ actualRuntimeAsset, attachQa, clearThroughSettings, deviceSnapshot,
+ expectUnrelatedNativeState, insertArtificialRetiredResidual, observeDeviceClear,
+ ownSwFault, ownedBusiness, prepareUnrelatedNativeState, preservedDeviceMetadata,
+ publicNativeSnapshot, qaApi, qaDeadlineSignal, readNativeResource, releaseAssets, rootWorker,
+ runtimeName, seedOldPublicNamespace, warmBoundAsset, withinQaDeadline,
+} from "../../_fixtures/public-static-qa"
+
+test("real Alice logout and Bob login retain server facts while device Clear removes all represented payloads", async ({ caseActors, caseResources }, testInfo) => {
+ const actors = await caseActors(["alice", "bob"])
+ const aliceServer = await actors.seed.seedServer("alice", `clear-alice-${Date.now()}`)
+ const aliceChannel = await actors.seed.seedChannel("alice", aliceServer, "alice-private")
+ const aliceText = `Alice private native clear ${Date.now()}`
+ const aliceMessage = await actors.seed.seedMessage("alice", aliceChannel, aliceText)
+ const bobServer = await actors.seed.seedServer("bob", `clear-bob-${Date.now()}`)
+ const bobChannel = await actors.seed.seedChannel("bob", bobServer, "bob-private")
+ const bobText = `Bob native clear ${Date.now()}`
+ const bobMessage = await actors.seed.seedMessage("bob", bobChannel, bobText)
+ const context = await caseResources.newContext()
+ const page = await context.newPage()
+ await actors.signInOnPage("alice", page)
+ await gotoAfterUserWsAuth(page, `/c/channels/${aliceServer}/${aliceChannel}`)
+ await expect(page.getByText(aliceText, { exact: true }).first()).toBeVisible()
+ const aliceKey = `alook:qc:cache:${actors.userId("alice")}:client`
+ await expect.poll(async () => (await deviceSnapshot(page)).payloadKeys).toContain(aliceKey)
+ const captured = await deviceSnapshot(page)
+ const aliceUiCookie = (await context.cookies()).filter(cookie => cookie.name.startsWith("better-auth")).map(cookie => `${cookie.name}=${cookie.value}`).join("; ")
+ expect(Boolean(aliceUiCookie)).toBe(true)
+ expect(aliceUiCookie === actors.sessionCookie("alice")).toBe(false)
+ await page.getByTestId(tid.userSettingsOpen).click()
+ await page.getByRole("button", { name: "Log out", exact: true }).click()
+ await page.waitForURL(/\/sign-in/, { waitUntil: "commit" })
+ await expect.poll(async () => (await context.cookies()).some(cookie => cookie.name.startsWith("better-auth"))).toBe(false)
+ const retired = await fetch(`${WEB_URL}/api/ws/token?refresh=1`, { signal: qaDeadlineSignal(caseResources), headers: { Cookie: aliceUiCookie, Origin: WEB_URL } })
+ expect(retired.status).toBe(401)
+ expect(retired.headers.get("content-type")).toBe("text/plain;charset=UTF-8")
+ expect(await retired.text()).toBe("Unauthorized")
+ expect((await qaApi<{ userId: string }>(actors, caseResources, "alice", "/api/ws/token?refresh=1")).body.userId).toBe(actors.userId("alice"))
+ const afterLogout = await deviceSnapshot(page)
+ expect(afterLogout.payloadKeys).not.toContain(aliceKey)
+ expect(afterLogout.epoch).toBe(captured.epoch)
+ await actors.signInOnPage("bob", page)
+ const proxy = await proxyCommunityWebSockets(context)
+ await gotoAfterUserWsAuth(page, `/c/channels/${bobServer}/${bobChannel}`)
+ await expect(page.getByText(bobText, { exact: true }).first()).toBeVisible()
+ await expect(page.getByText(aliceText, { exact: true })).toHaveCount(0)
+ const bobKey = `alook:qc:cache:${actors.userId("bob")}:client`
+ await expect.poll(async () => (await deviceSnapshot(page)).payloadKeys).toContain(bobKey)
+ const blog = await context.newPage()
+ await blog.goto("/blog")
+ await rootWorker(blog)
+ const code = actualRuntimeAsset("Nplus1", "code")
+ await warmBoundAsset(blog, code)
+ await warmBoundAsset(blog, actualRuntimeAsset("Nplus1", "images"))
+ const oldNamespaces = await seedOldPublicNamespace(blog, code.url)
+ await blog.close()
+ const sentinel = await prepareUnrelatedNativeState(page, testInfo.testId)
+ const residual = await insertArtificialRetiredResidual(page, actors.userId("alice"), captured)
+ const before = await deviceSnapshot(page)
+ expect(before.payloadKeys).toEqual(expect.arrayContaining([...residual.keys, bobKey]))
+ const bobMessagesBefore = await qaApi<{ messages: Array<{ id: string; seq: number; content: string }> }>(actors, caseResources, "bob", `/api/community/channels/${bobChannel}/messages`)
+ const original = bobMessagesBefore.body.messages.find(message => message.id === bobMessage)!
+ expect(original).toBeTruthy()
+ const observer = await observeDeviceClear(page, caseResources, before.epoch)
+ const ui = await clearThroughSettings(page)
+ const committed = await observer.result()
+ expect(committed.epoch).not.toBe(before.epoch)
+ expect(committed.payloadKeys).toEqual([])
+ expect(preservedDeviceMetadata(committed)).toEqual(preservedDeviceMetadata(before))
+ await observer.close()
+ const nativeAfter = await publicNativeSnapshot(page)
+ expect(Object.keys(nativeAfter.caches).filter(name => name.startsWith("alook-public-"))).toEqual([])
+ expect(nativeAfter.expiration.filter(row => row.cacheName.startsWith("alook-public-"))).toEqual([])
+ await expectUnrelatedNativeState(page, sentinel)
+ const identity = await context.request.get("/api/ws/token?refresh=1")
+ expect(identity.status()).toBe(200)
+ expect((await identity.json()).userId).toBe(actors.userId("bob"))
+ const denied = await context.request.get(`/api/community/channels/${aliceChannel}/messages`)
+ expect([403, 404]).toContain(denied.status())
+ const retained = (await qaApi<{ messages: typeof bobMessagesBefore.body.messages }>(actors, caseResources, "bob", `/api/community/channels/${bobChannel}/messages`)).body.messages.find(message => message.id === bobMessage)
+ expect(retained).toEqual(original)
+ expect((await qaApi<{ messages: Array<{ id: string }> }>(actors, caseResources, "alice", `/api/community/channels/${aliceChannel}/messages`)).body.messages.some(message => message.id === aliceMessage)).toBe(true)
+ const frameStart = proxy.frames.length
+ const post = await qaApi<{ message: { id: string; seq: number } }>(actors, caseResources, "bob", `/api/community/channels/${bobChannel}/messages`, { method: "POST", body: JSON.stringify({ content: "Bob post-clear real message" }) })
+ expect(post.status).toBe(201)
+ expect(post.body.message.seq).toBeGreaterThan(original.seq)
+ await expect.poll(() => proxy.frames.slice(frameStart).flatMap(communityFrameEvents).some(event => event.type === "community:message.create" && (event.message?.id === post.body.message.id || event.messageId === post.body.message.id))).toBe(true)
+ await expect(page.getByText(aliceText, { exact: true })).toHaveCount(0)
+ await attachQa(testInfo, "device-clear-natural-login-and-artificial-residual", { ui, sameDeviceContextCount: 1, naturalLogoutPayloadAbsent: !afterLogout.payloadKeys.includes(aliceKey), oldNamespaces, residual, beforeEpoch: before.epoch, committed, uiCompletedPayloadKeys: (await deviceSnapshot(page)).payloadKeys, original, retained, newMessage: post.body.message, permissionStatus: denied.status(), nativeAfter, limitation: "Native metadata preservation and cross-account visibility are covered; no new stale-persister API is injected into the product" })
+})
+
+test("real settings Clear reports an already-held image writer separately from later public repopulation", async ({ caseActors, caseResources }, testInfo) => {
+ const actors = await caseActors(["bob"])
+ const business = await ownedBusiness(actors, "bob", caseResources)
+ const original = await business.write("pending writer original message")
+ const imagePage = await business.context.newPage()
+ await imagePage.goto("/blog")
+ await rootWorker(imagePage)
+ await warmBoundAsset(imagePage, actualRuntimeAsset("Nplus1", "code"))
+ const sentinel = await prepareUnrelatedNativeState(business.page, testInfo.testId)
+ const snapshot = await publicNativeSnapshot(imagePage, [runtimeName("images")])
+ const existing = new Set(snapshot.caches[runtimeName("images")]?.map(entry => new URL(entry.url).pathname))
+ const image = releaseAssets("Nplus1").find(asset => asset.kind === "images" && !existing.has(asset.url)) ?? actualRuntimeAsset("Nplus1", "images")
+ const worker = business.context.serviceWorkers().find(worker => worker.url() === new URL("/sw.js", imagePage.url()).href)
+ expect(worker).toBeTruthy()
+ const fault = await ownSwFault(caseResources, business.context, new URL(image.url, imagePage.url()).href, Promise.resolve(worker!), "hold")
+ const operation = caseResources.own({ pending: undefined as Promise<Awaited<ReturnType<typeof readNativeResource>>> | undefined, accepted: false }, async state => {
+ if (!state.accepted) await fault.close()
+ if (state.pending) await state.pending.catch(error => { if (state.accepted) throw error })
+ })
+ operation.value.pending = readNativeResource(imagePage, image.url)
+ void operation.value.pending.catch(() => {})
+ await expect.poll(() => fault.held()).toBe(true)
+ const before = { device: await deviceSnapshot(business.page), public: await publicNativeSnapshot(imagePage) }
+ const observer = await observeDeviceClear(business.page, caseResources, before.device.epoch)
+ const ui = await clearThroughSettings(business.page)
+ const committed = await observer.result()
+ expect(committed.epoch).not.toBe(before.device.epoch)
+ expect(committed.payloadKeys).toEqual([])
+ expect(preservedDeviceMetadata(committed)).toEqual(preservedDeviceMetadata(before.device))
+ await observer.close()
+ const afterUi = await publicNativeSnapshot(imagePage)
+ expect(Object.keys(afterUi.caches).filter(name => name.startsWith("alook-public-"))).toEqual([])
+ expect(afterUi.expiration.filter(row => row.cacheName.startsWith("alook-public-"))).toEqual([])
+ fault.release()
+ const resource = await withinQaDeadline(operation.value.pending, caseResources.deadline)
+ expect(resource).toMatchObject({ bytes: image.bytes, sha256: image.sha256, fromServiceWorker: true })
+ operation.value.accepted = true
+ await operation.close()
+ await expect.poll(() => fault.records.every(record => record.completed)).toBe(true)
+ await fault.close()
+ const afterRelease = await publicNativeSnapshot(imagePage)
+ await expectUnrelatedNativeState(business.page, sentinel)
+ expect(await business.read(original.id)).toMatchObject({ id: original.id, seq: original.seq, content: original.content })
+ const next = await business.write("pending writer post-clear message")
+ expect(next.seq).toBeGreaterThan(original.seq)
+ await attachQa(testInfo, "clear-with-real-in-flight-writer", { ui, before, committed, afterUi, afterRelease, heldRequest: fault.records, resource, original, next, classification: "Any matching entry after release belongs to the pre-clear held request; permanent public-cache emptiness is not asserted" })
+})
diff --git a/src/web/src/test/e2e-ui/_qa/public-static/expiration.qa.ts b/src/web/src/test/e2e-ui/_qa/public-static/expiration.qa.ts
new file mode 100644
index 000000000..1a6cc1346
--- /dev/null
+++ b/src/web/src/test/e2e-ui/_qa/public-static/expiration.qa.ts
@@ -0,0 +1,87 @@
+import { expect, test } from "../../_fixtures/case-fixture"
+import { publicRuntimeAssetKind } from "../../../../lib/service-worker/public-cache"
+import {
+ actualRuntimeAsset, attachQa, expectNativeBody, expectUnrelatedNativeState, ownedOffline,
+ prepareUnrelatedNativeState, publicNativeSnapshot, readNativeResource, releaseAssets,
+ rootWorker, runtimeName, seedNativeExpiration, seedNativeOverflow, warmBoundAsset,
+} from "../../_fixtures/public-static-qa"
+
+for (const kind of ["code", "fonts", "images"] as const) {
+ test(`native ${kind} honors its unchanged age limit using explicitly artificial aged state`, async ({ caseResources }, testInfo) => {
+ const context = await caseResources.newContext()
+ const page = await context.newPage()
+ await page.goto("/blog")
+ await rootWorker(page)
+ await page.goto("/robots.txt")
+ const asset = actualRuntimeAsset("Nplus1", kind)
+ const resource = await warmBoundAsset(page, asset)
+ const sentinel = await prepareUnrelatedNativeState(page, testInfo.testId)
+ const name = runtimeName(kind), ageMs = (kind === "images" ? 7 : 30) * 86_400_000
+ const fresh = await seedNativeExpiration(page, name, asset.url, Date.now() - ageMs + 60_000)
+ await ownedOffline(caseResources, context, async () => { expect(await readNativeResource(page, asset.url)).toEqual(resource) })
+ const stale = await seedNativeExpiration(page, name, asset.url, Date.now() - ageMs - 60_000)
+ await ownedOffline(caseResources, context, async () => {
+ expect(await page.evaluate(async url => { try { await fetch(url, { credentials: "omit", cache: "no-store" }); return "resolved" } catch { return "rejected" } }, asset.url)).toBe("rejected")
+ })
+ await expect.poll(async () => (await publicNativeSnapshot(page, [name])).caches[name]?.some(entry => entry.url === resource.url) ?? false).toBe(false)
+ await warmBoundAsset(page, asset)
+ const onlineStale = await seedNativeExpiration(page, name, asset.url, Date.now() - ageMs - 60_000)
+ const replacement = await warmBoundAsset(page, asset)
+ expect(replacement).toEqual(resource)
+ await expect.poll(async () => (await publicNativeSnapshot(page, [name])).expiration.find(row => row.cacheName === name && row.url === resource.url)?.timestamp ?? 0).toBeGreaterThan(Date.now() - 60_000)
+ expect(await readNativeResource(page, asset.url)).toEqual(resource)
+ await expectNativeBody(page, name, resource)
+ await expectUnrelatedNativeState(page, sentinel)
+ await attachQa(testInfo, `native-${kind}-age`, { fresh, stale, onlineStale, resource, replacement, policy: { maxAgeDays: kind === "images" ? 7 : 30 }, after: await publicNativeSnapshot(page, [name]) })
+ })
+}
+
+test("129 actual image downloads enforce the production 128 entry bound", async ({ caseResources }, testInfo) => {
+ const context = await caseResources.newContext()
+ const page = await context.newPage()
+ await page.goto("/blog")
+ await rootWorker(page)
+ await page.goto("/robots.txt")
+ const name = runtimeName("images")
+ const assets = releaseAssets("Nplus1").filter(asset => asset.zone === "blog" && publicRuntimeAssetKind(asset.url) === "images").slice(0, 129)
+ expect(assets).toHaveLength(129)
+ const first = await warmBoundAsset(page, assets[0]!)
+ const sentinel = await prepareUnrelatedNativeState(page, testInfo.testId)
+ await seedNativeExpiration(page, name, first.url, Date.now() - 60_000)
+ const downloads = [first]
+ for (const asset of assets.slice(1)) {
+ const resource = await readNativeResource(page, asset.url)
+ expect(resource).toMatchObject({ bytes: asset.bytes, sha256: asset.sha256, fromServiceWorker: true })
+ downloads.push(resource)
+ }
+ await expect.poll(async () => {
+ const snapshot = await publicNativeSnapshot(page, [name])
+ return { entries: snapshot.caches[name]?.length, rows: snapshot.expiration.filter(row => row.cacheName === name).length, oldestPresent: snapshot.caches[name]?.some(entry => entry.url === first.url) }
+ }).toEqual({ entries: 128, rows: 128, oldestPresent: false })
+ await expectNativeBody(page, name, downloads.at(-1)!)
+ await expectUnrelatedNativeState(page, sentinel)
+ await attachQa(testInfo, "129-distinct-real-image-downloads", { preparation: "129 distinct admitted release URLs, no query aliases", policy: { maxEntries: 128 }, downloads, after: await publicNativeSnapshot(page, [name]) })
+})
+
+for (const [kind, maxEntries] of [["code", 80], ["fonts", 160]] as const) {
+ test(`native ${kind} evicts artificial overflow to the unchanged ${maxEntries} entry bound`, async ({ caseResources }, testInfo) => {
+ const context = await caseResources.newContext()
+ const page = await context.newPage()
+ await page.goto("/blog")
+ await rootWorker(page)
+ await page.goto("/robots.txt")
+ const asset = actualRuntimeAsset("Nplus1", kind)
+ const resource = await warmBoundAsset(page, asset)
+ const sentinel = await prepareUnrelatedNativeState(page, testInfo.testId)
+ const fixture = await seedNativeOverflow(page, kind, maxEntries + 1, asset.url)
+ expect(fixture.entries).toHaveLength(maxEntries + 1)
+ expect(await readNativeResource(page, asset.url)).toEqual(resource)
+ await expect.poll(async () => {
+ const snapshot = await publicNativeSnapshot(page, [runtimeName(kind)])
+ return { entries: snapshot.caches[runtimeName(kind)]?.length, rows: snapshot.expiration.filter(row => row.cacheName === runtimeName(kind)).length, oldestPresent: snapshot.caches[runtimeName(kind)]?.some(entry => entry.url === fixture.entries[0]!.url) }
+ }).toEqual({ entries: maxEntries, rows: maxEntries, oldestPresent: false })
+ await expectNativeBody(page, runtimeName(kind), resource)
+ await expectUnrelatedNativeState(page, sentinel)
+ await attachQa(testInfo, `artificial-${kind}-overflow`, { fixture, realTemplate: resource, maxEntries, after: await publicNativeSnapshot(page, [runtimeName(kind)]) })
+ })
+}
diff --git a/src/web/src/test/e2e-ui/_qa/public-static/install-failure.qa.ts b/src/web/src/test/e2e-ui/_qa/public-static/install-failure.qa.ts
new file mode 100644
index 000000000..33a105e5a
--- /dev/null
+++ b/src/web/src/test/e2e-ui/_qa/public-static/install-failure.qa.ts
@@ -0,0 +1,94 @@
+import { expect, test } from "../../_fixtures/case-fixture"
+import {
+ activateAfterClientExit, attachQa, blocked, createQaPhaseOwner, executingWorkerIdentity,
+ expectReleasePrecache, observeCandidate, ownSwFault, ownedBusiness, ownedCdp, ownedOffline,
+ publicNativeSnapshot, readNativeResource, releaseAssets, releaseDelta,
+ requiredQaManifest, rootStates, rootWorker, runQaScope, updateRoot, withinQaDeadline,
+} from "../../_fixtures/public-static-qa"
+
+for (const action of ["404", "offline", "mime", "private", "no-store", "quota"] as const) {
+ test(`candidate installation rejects actual ${action} and recovers after that owned fault is removed`, async ({ caseActors, caseResources }, testInfo) => {
+ const phase = createQaPhaseOwner(caseResources, testInfo)
+ await phase.switchTo("N")
+ const actors = await caseActors(["alice"])
+ const business = await ownedBusiness(actors, "alice", caseResources)
+ const originalMessage = await business.write(`${action} original server message`)
+ const connections = business.proxy.connectionCount()
+ const context = business.context
+ const page = await context.newPage()
+ await page.goto("/contact")
+ await rootWorker(page)
+ const manifest = requiredQaManifest()
+ const active = await executingWorkerIdentity(page, caseResources, testInfo, "activated")
+ expect(active.sha256).toBe(manifest.releases.N.swSha256)
+ const delta = releaseDelta(releaseAssets("N"), releaseAssets("Nplus1"))
+ const oldAsset = releaseAssets("N").find(asset => asset.precache && delta.unchanged.includes(asset.url))!
+ const before = await readNativeResource(page, oldAsset.url)
+ expect(before).toMatchObject({ bytes: oldAsset.bytes, sha256: oldAsset.sha256, fromServiceWorker: true })
+ const asset = releaseAssets("Nplus1").find(asset => asset.zone === "main" && asset.precache && delta.added.includes(asset.url) && /\.m?js$/.test(asset.url))
+ if (!asset) blocked(testInfo, "This actual pair has no candidate-added public JS precache request")
+ const candidate = observeCandidate(context, caseResources)
+ let fault: Awaited<ReturnType<typeof ownSwFault>> | undefined
+ let quota: { close: () => Promise<void> } | undefined
+ let quotaProof: unknown
+ if (action === "quota") {
+ const cdp = await ownedCdp(caseResources, context, page)
+ const origin = new URL(page.url()).origin
+ quota = caseResources.own({}, async () => runQaScope(async onClose => {
+ onClose(cdp.close)
+ await withinQaDeadline(cdp.session.send("Storage.overrideQuotaForOrigin", { origin }), caseResources.deadline)
+ const restored = await withinQaDeadline(cdp.session.send("Storage.getUsageAndQuota", { origin }), caseResources.deadline)
+ expect(restored.overrideActive).toBe(false)
+ }))
+ try {
+ await withinQaDeadline(cdp.session.send("Storage.overrideQuotaForOrigin", { origin, quotaSize: 1 }), caseResources.deadline)
+ const report = await withinQaDeadline(cdp.session.send("Storage.getUsageAndQuota", { origin }), caseResources.deadline)
+ expect(report.overrideActive).toBe(true)
+ const error = await page.evaluate(async () => {
+ const name = "qa-222-quota-control", key = new URL("/qa-222-quota-control", location.href).href
+ try { const cache = await caches.open(name); await cache.put(key, new Response(new Uint8Array(4096))); return null }
+ catch (error) { return error instanceof DOMException ? error.name : String(error) }
+ finally { await caches.delete(name) }
+ })
+ if (error !== "QuotaExceededError") blocked(testInfo, "Installed Chromium quota override did not produce a native QuotaExceededError")
+ quotaProof = { preparation: "artificial origin quota in this case-owned context", ...report, nativeControlError: error }
+ } catch (error) {
+ if (String(error).includes("BLOCKED")) throw error
+ blocked(testInfo, `Installed owned quota primitive unavailable: ${String(error)}`)
+ }
+ } else fault = await ownSwFault(caseResources, context, new URL(asset.url, page.url()).href, candidate.promise, action, asset)
+ await phase.switchTo("Nplus1")
+ const update = await updateRoot(page)
+ await withinQaDeadline(candidate.promise, caseResources.deadline)
+ await expect.poll(async () => (await rootStates(page)).observed).toContain("redundant")
+ const failed = await rootStates(page)
+ expect(failed.waiting).toBeUndefined()
+ expect(failed.active).toBe("activated")
+ if (fault) {
+ expect(fault.records.length).toBeGreaterThan(0)
+ expect(fault.records.every(record => record.completed)).toBe(true)
+ }
+ if (quota) await quota.close()
+ await ownedOffline(caseResources, context, async () => { expect(await readNativeResource(page, oldAsset.url)).toEqual(before) })
+ const retained = await executingWorkerIdentity(page, caseResources, testInfo, "activated")
+ expect(retained.sha256).toBe(active.sha256)
+ expect(retained.versionId).toBe(active.versionId)
+ expect(await business.read(originalMessage.id)).toMatchObject({ id: originalMessage.id, seq: originalMessage.seq, content: originalMessage.content })
+ const nextMessage = await business.write(`${action} failed candidate retained business connection`)
+ expect(nextMessage.seq).toBeGreaterThan(originalMessage.seq)
+ expect(business.proxy.connectionCount()).toBe(connections)
+ await attachQa(testInfo, `actual-${action}-installation-failure`, { action, asset, update, failed, active, retained, oldResource: before, originalMessage, nextMessage, connections, connectionFrames: business.proxy.connectionFrames, fault: fault?.records, quotaProof, nativeAfterFailure: await publicNativeSnapshot(page) })
+ if (fault) await fault.close()
+ await candidate.close()
+ await updateRoot(page)
+ await expect.poll(() => rootStates(page)).toMatchObject({ waiting: "installed", active: "activated" })
+ const recoveredWaiting = await executingWorkerIdentity(page, caseResources, testInfo, "installed")
+ expect(recoveredWaiting.sha256).toBe(manifest.releases.Nplus1.swSha256)
+ const neutral = await activateAfterClientExit(context, caseResources, [page, business.page], recoveredWaiting.versionId, testInfo)
+ const recovered = await executingWorkerIdentity(neutral, caseResources, testInfo, "activated")
+ expect(recovered.sha256).toBe(manifest.releases.Nplus1.swSha256)
+ await expectReleasePrecache(neutral, "Nplus1")
+ await attachQa(testInfo, `ordinary-${action}-recovery`, { recovered, forcedActivation: false })
+ await phase.close()
+ })
+}
diff --git a/src/web/src/test/e2e-ui/_qa/public-static/versions.qa.ts b/src/web/src/test/e2e-ui/_qa/public-static/versions.qa.ts
new file mode 100644
index 000000000..10820bcad
--- /dev/null
+++ b/src/web/src/test/e2e-ui/_qa/public-static/versions.qa.ts
@@ -0,0 +1,113 @@
+import { expect, test } from "../../_fixtures/case-fixture"
+import { gotoAfterUserWsAuth } from "../../_fixtures/actions"
+import {
+ activateAfterClientExit, attachQa, createQaPhaseOwner, executingWorkerIdentity,
+ expectReleasePrecache, observeCandidate, ownD1WsProbe, ownedBusiness,
+ readNativeResource, releaseAssets, releaseDelta, requiredQaManifest, rootStates,
+ rootWorker, updateRoot, withinQaDeadline,
+} from "../../_fixtures/public-static-qa"
+
+test("@phase-capability Main and stable WS share bidirectional D1 and retain their actual connections", async ({ caseActors, caseResources }, testInfo) => {
+ const phase = createQaPhaseOwner(caseResources, testInfo)
+ await phase.switchTo("N")
+ const actors = await caseActors(["alice"])
+ const business = await ownedBusiness(actors, "alice", caseResources)
+ const machine = await ownD1WsProbe(actors, caseResources, testInfo)
+ const before = await machine.writeAndRead("before")
+ const message = await business.write("phase capability before switch")
+ const connections = business.proxy.connectionCount()
+ expect(connections).toBe(1)
+ await phase.switchTo("Nplus1")
+ const after = await machine.writeAndRead("after")
+ expect(await business.read(message.id)).toMatchObject({ id: message.id, seq: message.seq, content: message.content })
+ const next = await business.write("phase capability after switch")
+ expect(next.seq).toBeGreaterThan(message.seq)
+ expect(business.proxy.connectionCount()).toBe(connections)
+ await attachQa(testInfo, "real-bidirectional-d1-and-continuous-ws", { before, after, messages: [message, next], humanConnectionCount: connections, connectionFrames: business.proxy.connectionFrames })
+ await machine.close()
+ await phase.close()
+})
+
+for (const [from, to] of [["N", "Nplus1"], ["Nplus1", "N"]] as const) {
+ test(`${from} to ${to} waits for real clients, activates naturally and preserves business facts`, async ({ caseActors, caseResources }, testInfo) => {
+ const phase = createQaPhaseOwner(caseResources, testInfo)
+ await phase.switchTo(from)
+ const actors = await caseActors(["alice"])
+ const business = await ownedBusiness(actors, "alice", caseResources)
+ const machine = await ownD1WsProbe(actors, caseResources, testInfo)
+ const message = await business.write(`${from} retained message`)
+ await rootWorker(business.page)
+ const me = await business.context.newPage()
+ await me.goto("/c/me")
+ const article = await business.context.newPage()
+ await article.goto("/blog")
+ const link = article.locator('a[href^="/blog/"]').first()
+ await expect(link).toBeVisible()
+ const href = await link.getAttribute("href")
+ expect(href).toMatch(/^\/blog\/.+/)
+ await article.goto(href!)
+ const manifest = requiredQaManifest()
+ const beforeIdentity = await executingWorkerIdentity(business.page, caseResources, testInfo, "activated")
+ expect(beforeIdentity.sha256).toBe(manifest.releases[from].swSha256)
+ const delta = releaseDelta(releaseAssets(from), releaseAssets(to))
+ const beforeNative = await expectReleasePrecache(business.page, from)
+ expect(delta.added.length + delta.deleted.length + delta.changed.length).toBeGreaterThan(0)
+ const selected = releaseAssets(from).find(asset => asset.precache && delta.unchanged.includes(asset.url))!
+ expect(selected).toBeTruthy()
+ const beforeResource = await readNativeResource(business.page, selected.url)
+ expect(beforeResource).toMatchObject({ bytes: selected.bytes, sha256: selected.sha256, fromServiceWorker: true })
+ const oldBranchResources = []
+ for (const url of [...delta.changed, ...delta.deleted.slice(0, 1)]) {
+ const asset = releaseAssets(from).find(asset => asset.url === url)!
+ const resource = await readNativeResource(business.page, url)
+ expect(resource).toMatchObject({ bytes: asset.bytes, sha256: asset.sha256, fromServiceWorker: true })
+ oldBranchResources.push(resource)
+ }
+ const beforeD1 = await machine.writeAndRead("before")
+ const connections = business.proxy.connectionCount()
+ const candidate = observeCandidate(business.context, caseResources)
+ await phase.switchTo(to)
+ await updateRoot(business.page)
+ await expect.poll(() => rootStates(business.page)).toMatchObject({ active: "activated", waiting: "installed" })
+ await withinQaDeadline(candidate.promise, caseResources.deadline)
+ const waiting = await executingWorkerIdentity(business.page, caseResources, testInfo, "installed")
+ expect(waiting.sha256).toBe(manifest.releases[to].swSha256)
+ const active = await executingWorkerIdentity(business.page, caseResources, testInfo, "activated")
+ expect(active.sha256).toBe(beforeIdentity.sha256)
+ expect(await readNativeResource(business.page, selected.url)).toEqual(beforeResource)
+ for (const resource of oldBranchResources) expect(await readNativeResource(business.page, resource.url)).toEqual(resource)
+ expect(await business.read(message.id)).toMatchObject({ id: message.id, seq: message.seq, content: message.content })
+ const next = await business.write(`${to} server with old controlled client`)
+ const afterD1 = await machine.writeAndRead("after")
+ expect(business.proxy.connectionCount()).toBe(connections)
+ await attachQa(testInfo, "retained-clients-and-waiting-worker", { from, to, delta, beforeIdentity, active, waiting, beforeD1, afterD1, messages: [message, next], connections, connectionFrames: business.proxy.connectionFrames })
+ await candidate.close()
+ const neutral = await activateAfterClientExit(business.context, caseResources, [business.page, me, article], waiting.versionId, testInfo)
+ const activated = await executingWorkerIdentity(neutral, caseResources, testInfo, "activated")
+ expect(activated.sha256).toBe(manifest.releases[to].swSha256)
+ expect(activated.versionId).not.toBe(beforeIdentity.versionId)
+ const afterNative = await expectReleasePrecache(neutral, to)
+ const nativeEntry = (snapshot: typeof beforeNative, url: string) => Object.entries(snapshot.caches).filter(([name]) => name.startsWith("alook-public-precache-")).flatMap(([, entries]) => entries).find(entry => new URL(entry.url).pathname === url)
+ const changedKeys = delta.changed.map(url => {
+ const before = nativeEntry(beforeNative, url)!, after = nativeEntry(afterNative, url)!
+ expect(after.url).not.toBe(before.url)
+ expect(after.sha256).not.toBe(before.sha256)
+ const previousRevision = releaseAssets(from).find(asset => asset.url === url)!.revision
+ const nextRevision = releaseAssets(to).find(asset => asset.url === url)!.revision
+ expect(nextRevision).not.toBe(previousRevision)
+ return { url, previousRevision, nextRevision, before, after }
+ })
+ for (const url of delta.unchanged) expect(nativeEntry(afterNative, url)?.url).toBe(nativeEntry(beforeNative, url)?.url)
+ for (const url of delta.deleted) expect(nativeEntry(afterNative, url)).toBeUndefined()
+ for (const path of [...delta.added.slice(0, 1), ...delta.changed, ...delta.unchanged.slice(0, 1)]) {
+ const asset = releaseAssets(to).find(asset => asset.url === path)!
+ expect(await readNativeResource(neutral, path)).toMatchObject({ bytes: asset.bytes, sha256: asset.sha256, fromServiceWorker: true })
+ }
+ await gotoAfterUserWsAuth(neutral, `/c/channels/${business.serverId}/${business.channelId}`)
+ await expect(neutral.getByText(message.content, { exact: true }).first()).toBeVisible()
+ expect(await business.read(message.id)).toMatchObject({ id: message.id, seq: message.seq })
+ await attachQa(testInfo, "naturally-activated-release", { from, to, activated, oldBranchResources, changedKeys, beforeNative, afterNative, changedRevisionStatus: changedKeys.length ? "actual emitted revisions, complete native keys and bound bytes differ" : "NOT_PROVEN: this real pair has no same-URL changed body", finalD1: await machine.writeAndRead("activated") })
+ await machine.close()
+ await phase.close()
+ })
+}
diff --git a/src/web/src/test/e2e-ui/_setup/auth.ts b/src/web/src/test/e2e-ui/_setup/auth.ts
index 6679496c2..d8bfbdc31 100644
--- a/src/web/src/test/e2e-ui/_setup/auth.ts
+++ b/src/web/src/test/e2e-ui/_setup/auth.ts
@@ -11,8 +11,8 @@ type LoginContextOwner = {
closed: (context: BrowserContext) => void
}
-async function signIn(page: Page, email: string): Promise<void> {
- const attempts = [`${WEB_URL}/c`, `${WEB_URL}/sign-in?redirect=/c`]
+export async function signIn(page: Page, email: string, options: { singleAttempt?: boolean; onSubmission?: () => void } = {}): Promise<void> {
+ const attempts = options.singleAttempt ? [`${WEB_URL}/sign-in?redirect=/c`] : [`${WEB_URL}/c`, `${WEB_URL}/sign-in?redirect=/c`]
let lastError: unknown
for (const url of attempts) {
@@ -21,6 +21,7 @@ async function signIn(page: Page, email: string): Promise<void> {
const emailInput = page.getByRole("textbox", { name: "Email" })
await emailInput.waitFor({ state: "visible", timeout: 15_000 })
await emailInput.fill(email)
+ options.onSubmission?.()
await page.getByRole("button", { name: "Sign in", exact: true }).click()
await page.waitForURL((current) => !current.pathname.startsWith("/sign-in"), {
timeout: 15_000,
diff --git a/src/web/src/test/e2e-ui/_setup/global-setup.ts b/src/web/src/test/e2e-ui/_setup/global-setup.ts
index ba3b8b08b..116245808 100644
--- a/src/web/src/test/e2e-ui/_setup/global-setup.ts
+++ b/src/web/src/test/e2e-ui/_setup/global-setup.ts
@@ -7,11 +7,13 @@ import {
prepareServices,
startServices,
stopServicesAndRestore,
+ publicStaticQaManifest,
} from "./services"
import { createLifecycleState, writeLifecycleState } from "./service-lifecycle"
import { USER_KEYS, type RunManifest, type SeededUser, type UserKey } from "./users"
export default async function globalSetup(): Promise<void> {
+ publicStaticQaManifest()
rmSync(AUTH_DIR, { recursive: true, force: true })
mkdirSync(AUTH_DIR, { recursive: true })
writeLifecycleState(SERVICE_STATE_PATH, createLifecycleState())
diff --git a/src/web/src/test/e2e-ui/_setup/service-lifecycle.ts b/src/web/src/test/e2e-ui/_setup/service-lifecycle.ts
index 97cc206cc..5022b92fc 100644
--- a/src/web/src/test/e2e-ui/_setup/service-lifecycle.ts
+++ b/src/web/src/test/e2e-ui/_setup/service-lifecycle.ts
@@ -38,6 +38,40 @@ export interface ServiceLifecycleState {
failure?: ServiceFailure
}
+export interface PublicStaticPhaseReceipt {
+ schema: 1
+ runId: string
+ manifestDigest: string
+ commandDigest?: string
+ sequence: number
+ release: "N" | "Nplus1"
+ status: "ready" | "switching" | "failed" | "stopped"
+ caseId?: string
+ deadline?: number
+ expectedStops?: Array<{ label: string; pid: number }>
+ failure?: string
+}
+
+export function phaseReadinessFailure(receipt: PublicStaticPhaseReceipt | null, now: number): string | null {
+ if (!receipt) return "Public static phase owner has no receipt"
+ if (receipt.status === "failed" || receipt.status === "stopped") return receipt.failure ?? `Public static phase owner is ${receipt.status}`
+ if (receipt.status === "ready") return null
+ if (receipt.status !== "switching" || !Number.isSafeInteger(receipt.deadline) || now >= receipt.deadline!) return "Public static phase transition expired"
+ const stops = receipt.expectedStops ?? []
+ if (!Array.isArray(stops) || stops.length !== 2 || !stops.every(value => value && ["main backend", "Blog backend"].includes(value.label) && Number.isInteger(value.pid) && value.pid > 0)
+ || new Set(stops.map(value => value.label)).size !== 2
+ || stops[0].pid === stops[1].pid || !receipt.caseId) return "Public static phase has invalid expected children"
+ return null
+}
+
+export function phaseProbeCrossed(before: PublicStaticPhaseReceipt, after: PublicStaticPhaseReceipt | null, now: number) {
+ const failure = phaseReadinessFailure(after, now)
+ if (failure) throw new Error(failure)
+ if (!after || after.runId !== before.runId || after.manifestDigest !== before.manifestDigest || !Number.isSafeInteger(after.sequence) || after.sequence < before.sequence
+ || (after.sequence === before.sequence && after.status === "ready" && before.status === "ready" && after.release !== before.release)) throw new Error("Health sample has a foreign or regressed phase identity")
+ return after.status === "switching" || before.status === "switching" || after.sequence !== before.sequence
+}
+
export function createLifecycleState(restorePending = false): ServiceLifecycleState {
return { status: "starting", restorePending, services: [], health: {} }
}
diff --git a/src/web/src/test/e2e-ui/_setup/services.ts b/src/web/src/test/e2e-ui/_setup/services.ts
index 8918d450f..df31b37ad 100644
--- a/src/web/src/test/e2e-ui/_setup/services.ts
+++ b/src/web/src/test/e2e-ui/_setup/services.ts
@@ -1,8 +1,10 @@
import { spawn, spawnSync, type ChildProcess } from "child_process"
import { createRequire } from "module"
-import { closeSync, existsSync, lstatSync, mkdirSync, openSync, renameSync, rmSync, statSync } from "fs"
+import { createHash } from "node:crypto"
+import { chmodSync, closeSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "fs"
import { dirname, resolve } from "path"
-import { QUEUE_URL, REPO_ROOT, resolveWsUrl, SERVICE_LOG_DIR, SERVICE_STATE_PATH, SINGLE_RUNTIME, WEB_URL } from "./paths"
+import { assertPublicStaticWriteSeparation, canonicalPhasePath, readPhaseFile, readPublicStaticManifest } from "../../../../scripts/dev-zone-processes.mjs"
+import { AUTH_DIR, QUEUE_URL, REPO_ROOT, resolveWsUrl, SERVICE_LOG_DIR, SERVICE_STATE_PATH, SINGLE_RUNTIME, WEB_URL } from "./paths"
import {
addServiceProcess,
applyHealthProbe,
@@ -10,9 +12,101 @@ import {
failLifecycle,
readLifecycleState,
writeLifecycleState,
+ phaseReadinessFailure,
+ phaseProbeCrossed,
+ type PublicStaticPhaseReceipt,
type ServiceLifecycleState,
} from "./service-lifecycle"
+export function publicStaticQaManifest(verify = true) {
+ const forensics = process.env.ALOOK_E2E_PUBLIC_STATIC_FORENSICS
+ if (forensics !== undefined && forensics !== "1") throw new Error("Invalid public static forensic opt-in")
+ if (forensics === "1" && process.env.ALOOK_PUBLIC_STATIC_QA !== "1") throw new Error("Public static forensics requires supplementary QA")
+ const file = process.env.ALOOK_E2E_RELEASE_MANIFEST
+ if (process.env.ALOOK_PUBLIC_STATIC_QA === "1" && !file) throw new Error("Supplementary public static QA requires ALOOK_E2E_RELEASE_MANIFEST")
+ if (!file) return null
+ if (process.env.ALOOK_PUBLIC_STATIC_QA !== "1" || process.env.ALOOK_E2E_PREBUILT !== "1") throw new Error("Release phases require explicit supplementary QA and prebuilt inputs")
+ if (canonicalPhasePath(file).startsWith(`${canonicalPhasePath(AUTH_DIR)}/`)) throw new Error("Release input manifest must survive the ordinary auth-directory reset")
+ const manifest = readPublicStaticManifest(file, AUTH_DIR, verify)
+ if (manifest.defaultRelease !== "Nplus1") throw new Error("The complete candidate QA run requires Nplus1 as its default release")
+ assertPublicStaticWriteSeparation(manifest, [AUTH_DIR, STATE_DIR, STATE_BACKUP_DIR, SERVICE_LOG_DIR, resolve(REPO_ROOT, "src/web/blog/.wrangler/state"), resolve(REPO_ROOT, ".wrangler/registry")])
+ return manifest
+}
+
+export function capturePublicStaticRuntimeState(options: { runtimeRoot: string; ownerRoot: string; controlDirectory: string; runId: string; manifestDigest: string }) {
+ const owner = canonicalPhasePath(options.ownerRoot)
+ const control = canonicalPhasePath(options.controlDirectory)
+ if (!control.startsWith(`${owner}/`)) throw new Error("Forensic output must belong to the case owner")
+ const target = resolve(control, "forensics-stopped-state")
+ const roots = ["src/web/.wrangler/state", "src/web/blog/.wrangler/state", "src/web/e2e-service-logs", ".wrangler/registry"]
+ const sources = roots.map(path => ({ path, absolute: resolve(options.runtimeRoot, path) }))
+ for (const source of sources) {
+ const actual = canonicalPhasePath(source.absolute)
+ if (target === actual || target.startsWith(`${actual}/`) || actual.startsWith(`${target}/`)) throw new Error("Forensic state and output overlap")
+ let parent = source.absolute
+ while (parent !== resolve(options.runtimeRoot)) {
+ if (lstatSync(parent, { throwIfNoEntry: false })?.isSymbolicLink()) throw new Error("Forensic state cannot contain symlinks")
+ parent = dirname(parent)
+ }
+ }
+ if (lstatSync(target, { throwIfNoEntry: false })) throw new Error("Forensic output already exists")
+ mkdirSync(target, { mode: 0o700 })
+ const files: Array<{ path: string; type: "file" | "directory"; mode: number; bytes?: number; sha256?: string }> = []
+ const copy = (source: string, relative: string) => {
+ const stat = lstatSync(source)
+ const destination = resolve(target, relative)
+ if (stat.isSymbolicLink()) throw new Error("Forensic state cannot contain symlinks")
+ if (stat.isDirectory()) {
+ mkdirSync(destination, { recursive: true, mode: 0o700 })
+ for (const name of readdirSync(source).sort()) copy(resolve(source, name), `${relative}/${name}`)
+ chmodSync(destination, stat.mode & 0o777)
+ files.push({ path: relative, type: "directory", mode: stat.mode & 0o777 })
+ } else if (stat.isFile()) {
+ mkdirSync(dirname(destination), { recursive: true, mode: 0o700 })
+ const body = readFileSync(source)
+ const sha256 = createHash("sha256").update(body).digest("hex")
+ writeFileSync(destination, body, { flag: "wx", mode: stat.mode & 0o777 })
+ chmodSync(destination, stat.mode & 0o777)
+ if (createHash("sha256").update(readFileSync(source)).digest("hex") !== sha256 || createHash("sha256").update(readFileSync(destination)).digest("hex") !== sha256) throw new Error("Forensic state changed during capture")
+ files.push({ path: relative, type: "file", mode: stat.mode & 0o777, bytes: body.length, sha256 })
+ } else throw new Error("Forensic state contains an unsupported entry")
+ }
+ const presence = sources.map(source => {
+ const present = !!lstatSync(source.absolute, { throwIfNoEntry: false })
+ if (present) copy(source.absolute, source.path)
+ return { path: source.path, present }
+ })
+ const receipt = { schema: 1, runId: options.runId, manifestDigest: options.manifestDigest, at: new Date().toISOString(), qualification: "Stopped terminal state before original restoration, not an in-flight snapshot or proof of native trace completeness", sources: presence, files }
+ writeFileSync(resolve(target, "capture.private.json"), `${JSON.stringify(receipt, null, 2)}\n`, { flag: "wx", mode: 0o600 })
+ return receipt
+}
+
+export async function cleanupWithPublicStaticEvidence(state: ServiceLifecycleState, dependencies: Parameters<typeof cleanupLifecycle>[1] & { capture: () => void; persist: (state: ServiceLifecycleState) => void }) {
+ if (state.status === "stopped") return state
+ let attempted = false
+ let captureFailed = false
+ let captureFailure: unknown
+ const capture = () => {
+ if (attempted) return
+ attempted = true
+ try { dependencies.capture() } catch (error) { captureFailed = true; captureFailure = error }
+ }
+ let stopped: ServiceLifecycleState
+ try {
+ stopped = await cleanupLifecycle(state, { ...dependencies, restore: () => { capture(); dependencies.restore() } })
+ } catch (error) {
+ if (captureFailed) throw new AggregateError([error, captureFailure], "Original cleanup and forensic capture failed")
+ throw error
+ }
+ if (!state.restorePending) capture()
+ try { dependencies.persist(stopped) } catch (error) {
+ if (captureFailed) throw new AggregateError([error, captureFailure], "Lifecycle persistence and forensic capture failed")
+ throw error
+ }
+ if (captureFailed) throw captureFailure
+ return stopped
+}
+
export interface ManagedService {
name: string
proc: ChildProcess
@@ -264,8 +358,10 @@ export function serviceBuildCommands(
}
export function prepareServices(): void {
+ const phaseManifest = publicStaticQaManifest()
+ const builds = phaseManifest ? [] : serviceBuildCommands()
backupState()
- for (const build of serviceBuildCommands()) {
+ for (const build of builds) {
const res = spawnSync("pnpm", build.args, {
cwd: REPO_ROOT,
stdio: "inherit",
@@ -375,6 +471,7 @@ export function serviceDefinitions(singleRuntime = SINGLE_RUNTIME): ServiceDefin
}
const runtime = resolveE2EWranglerRuntime()
+ const phaseManifest = publicStaticQaManifest(false)
return [{
name: "web-zones",
command: "pnpm",
@@ -385,6 +482,7 @@ export function serviceDefinitions(singleRuntime = SINGLE_RUNTIME): ServiceDefin
"--with-ws-do",
"--wrangler-entry",
runtime.entry,
+ ...(phaseManifest ? ["--public-static-manifest", resolve(process.env.ALOOK_E2E_RELEASE_MANIFEST!), "--public-static-owner-root", AUTH_DIR] : []),
],
healthUrl: webHealth,
expectedStatus: 200,
@@ -454,7 +552,43 @@ function startHealthSupervisor(services: ManagedService[]): void {
healthProbeInFlight = true
try {
for (const service of services) {
+ const manifest = publicStaticQaManifest(false)
+ let sample: PublicStaticPhaseReceipt | undefined
+ const readExpectedPhase = () => {
+ const value = readPhaseFile(resolve(manifest!.controlDirectory, "receipt.json")) as PublicStaticPhaseReceipt | null
+ const failure = phaseReadinessFailure(value, Date.now())
+ if (failure || value?.runId !== manifest!.runId || value.manifestDigest !== manifest!.digest) throw new Error(failure ?? "Public static phase receipt has a foreign identity")
+ return value
+ }
+ if (manifest) {
+ readExpectedPhase()
+ const stable = await hasExactHealth("http://127.0.0.1:8789/health")
+ if (!stable.ok) {
+ const current = readLifecycleState(SERVICE_STATE_PATH)
+ if (current) writeLifecycleState(SERVICE_STATE_PATH, failLifecycle(current, { service: "stable-ws-queue", kind: "health", at: new Date().toISOString(), detail: stable.detail }))
+ stopHealthSupervisor()
+ return
+ }
+ sample = readExpectedPhase()
+ if (sample.status === "switching") continue
+ let blogFailure: string | undefined
+ try {
+ const blog = await fetch(`${WEB_URL}/blog`, { redirect: "manual", signal: AbortSignal.timeout(3_000) })
+ if (blog.status !== 200 || !(blog.headers.get("content-type") ?? "").includes("text/html")) blogFailure = "Blog phase health did not return its public document"
+ await blog.body?.cancel()
+ } catch (error) { blogFailure = error instanceof Error ? error.message : String(error) }
+ const after = readExpectedPhase()
+ if (phaseProbeCrossed(sample, after, Date.now())) continue
+ sample = after
+ if (blogFailure) {
+ const current = readLifecycleState(SERVICE_STATE_PATH)
+ if (current) writeLifecycleState(SERVICE_STATE_PATH, failLifecycle(current, { service: "blog", kind: "health", at: new Date().toISOString(), detail: blogFailure }))
+ stopHealthSupervisor()
+ return
+ }
+ }
const result = await hasExactHealth(service.healthUrl)
+ if (manifest && sample && phaseProbeCrossed(sample, readExpectedPhase(), Date.now())) continue
const state = readLifecycleState(SERVICE_STATE_PATH)
if (!state) return
const updated = applyHealthProbe(state, service.name, {
@@ -469,6 +603,10 @@ function startHealthSupervisor(services: ManagedService[]): void {
return
}
}
+ } catch (error) {
+ const state = readLifecycleState(SERVICE_STATE_PATH)
+ if (state) writeLifecycleState(SERVICE_STATE_PATH, failLifecycle(state, { service: "web-zones", kind: "health", at: new Date().toISOString(), detail: error instanceof Error ? error.message : String(error) }))
+ stopHealthSupervisor()
} finally {
healthProbeInFlight = false
}
@@ -555,8 +693,10 @@ export async function stopServicesAndRestore(): Promise<void> {
if (!state || state.status === "stopped") return
const stopping: ServiceLifecycleState = { ...state, status: "stopping" }
writeLifecycleState(SERVICE_STATE_PATH, stopping)
- const stopped = await cleanupLifecycle(stopping, {
+ let forced = false
+ const dependencies: Parameters<typeof cleanupLifecycle>[1] = {
terminate: (pid, signal) => {
+ if (signal === "SIGKILL") forced = true
try {
process.kill(process.platform === "win32" ? pid : -pid, signal)
} catch {
@@ -565,6 +705,30 @@ export async function stopServicesAndRestore(): Promise<void> {
},
waitForStop: waitForProcessesToStop,
restore: restoreState,
- })
+ }
+ if (process.env.ALOOK_E2E_PUBLIC_STATIC_FORENSICS === "1") {
+ await cleanupWithPublicStaticEvidence(stopping, {
+ ...dependencies,
+ capture: () => {
+ const manifest = publicStaticQaManifest(false)
+ if (!manifest) throw new Error("Forensic state capture requires a release manifest")
+ const path = resolve(manifest.controlDirectory, "forensics-cleanup.private.json")
+ const qualification = { forced, nativeTraceCompleteness: "UNKNOWN", physicalRelease: "INDEPENDENT_OPERATOR_CHECK_REQUIRED" }
+ writeFileSync(path, `${JSON.stringify({ ...qualification, status: "capture-started" }, null, 2)}\n`, { flag: "wx", mode: 0o600 })
+ try {
+ const receipt = capturePublicStaticRuntimeState({ runtimeRoot: REPO_ROOT, ownerRoot: AUTH_DIR, controlDirectory: manifest.controlDirectory, runId: manifest.runId, manifestDigest: manifest.digest })
+ writeFileSync(path, `${JSON.stringify({ ...qualification, status: "captured", capture: receipt }, null, 2)}\n`)
+ } catch (error) {
+ try { writeFileSync(path, `${JSON.stringify({ ...qualification, status: "capture-failed" }, null, 2)}\n`) } catch (receiptError) {
+ throw new AggregateError([error, receiptError], "Forensic capture and receipt failed")
+ }
+ throw error
+ }
+ },
+ persist: stopped => writeLifecycleState(SERVICE_STATE_PATH, stopped),
+ })
+ return
+ }
+ const stopped = await cleanupLifecycle(stopping, dependencies)
writeLifecycleState(SERVICE_STATE_PATH, stopped)
}