// Single responsibility: the two refusals of `deliverPush(session, …)` (`cdp-e2e-push.ts`) — // `X_TEST_PUSH_NO_ACTIVE_WORKER` and `X_TEST_PUSH_WORKER_AMBIGUOUS`. The CODES are declared in // `errors.ts`, the package's registry, which re-exports these classes. Split from `errors.ts` at // the 500-line ceiling. import { renderCauseValue, UltimateError } from '@ultimat3/core'; /** One registration a refusal reports: its scope (origin and path only) and its state. */ export interface ReportedRegistration { readonly scope: string; /** `deleted`, `no version`, or its versions' states joined by `/`. */ readonly state: string; } const REGISTER_AND_PUSH = 'await tab.evaluate("navigator.serviceWorker.register(\'/sw.js\').then(() => navigator.serviceWorker.ready)"); await deliverPush(session, { origin, data })'; /** * `deliverPush(session, { origin })` found no service worker to deliver to: none on that origin * (and scope) that is not deleted and has an activated version, by its budget — or nothing to look * in: an `origin` that is not one, or no open tab. Refused before `ServiceWorker.deliverPushMessage` * is sent: Chrome accepts that call for an origin or an id it does not know and delivers nothing, * so a silent send would read as a delivered push. Only the requested origin's registrations are * reported, as origin and path. */ export type PushNoActiveWorker = | { readonly reason: 'not-an-origin' } | { readonly reason: 'no-tab' } | { readonly reason: 'none-active'; readonly origin: string; readonly scope: string | undefined; readonly seen: readonly ReportedRegistration[]; readonly budgetMs: number; }; const noActiveWorker = (input: PushNoActiveWorker): { cause: string; fix: string } => { if (input.reason === 'not-an-origin') { return { cause: 'deliverPush was given an origin that is not a URL origin (scheme://host[:port]), or a scope that is not a URL; nothing was sent', fix: 'await deliverPush(session, { origin: new URL(tab.url()).origin, data })', }; } if (input.reason === 'no-tab') { return { cause: 'deliverPush found no open tab to read service worker registrations from; nothing was sent', fix: 'const tab = await session.newTab(); await tab.goto(appUrl); await tab.evaluate("navigator.serviceWorker.register(\'/sw.js\').then(() => navigator.serviceWorker.ready)"); await deliverPush(session, { origin, data })', }; } const reported = input.seen.length === 0 ? 'no registration' : input.seen.map((one) => `${renderCauseValue(one.scope)} (${one.state})`).join(', '); const where = input.scope === undefined ? '' : ` at scope ${renderCauseValue(input.scope)}`; return { cause: `no active service worker on ${renderCauseValue(input.origin)}${where} within ${String(input.budgetMs)}ms; the browser reported ${reported} on that origin. A page there registers one before the push; nothing was sent`, fix: REGISTER_AND_PUSH, }; }; export class PushNoActiveWorkerError extends UltimateError { constructor(input: PushNoActiveWorker) { super({ code: 'X_TEST_PUSH_NO_ACTIVE_WORKER', ...noActiveWorker(input) }); } } /** * `deliverPush(session, { origin })` with no `scope` found more than one active worker on the * origin — a root worker and a scoped one. Picking one would run a listener the test did not mean, * and the push would still read as delivered. */ export class PushWorkerAmbiguousError extends UltimateError { constructor(input: { readonly origin: string; readonly scopes: readonly string[] }) { super({ code: 'X_TEST_PUSH_WORKER_AMBIGUOUS', cause: `${String(input.scopes.length)} active service workers on ${renderCauseValue(input.origin)}: ${input.scopes.map(renderCauseValue).join(', ')}; a push naming only the origin cannot choose, so nothing was sent`, fix: "await deliverPush(session, { origin, scope: '', data })", }); } }