import assert from 'node:assert/strict'
import test from 'node:test'
import { renderAppPage } from './page.ts'
import { serviceWorkerScript } from './sw-script.ts'
test('inline app script resolves API URLs against location.origin', () => {
const html = renderAppPage({
appBasePath: '/packages/lineage',
hostedUrl: 'https://kentcdodds.kody.run/packages/lineage',
repoLabel: 'kentcdodds/kody',
})
assert.match(html, /data-base="\/packages\/lineage\/"/)
assert.match(html, /new URL\(base \|\| '\/', location\.origin\)/)
assert.ok(
!html.includes("base.endsWith('/') ? base : base + '/'"),
'must not use a relative path as the URL constructor base',
)
const refreshAt = html.indexOf('refresh();')
const swAt = html.indexOf("navigator.serviceWorker.register")
assert.ok(refreshAt > 0 && swAt > 0 && swAt < refreshAt, 'service worker must register before first refresh')
assert.match(html, /data-dual-range/)
assert.match(html, /data-story/)
assert.match(html, /class="swatch"/)
assert.match(html, /data-loading hidden/)
assert.match(html, /data-status-mark/)
assert.match(html, /data-status-text/)
assert.match(html, /class="title-row"/)
assert.match(html, /<h1>Lineage<\/h1>\s*<span class="status-mark"/)
assert.match(html, /const height = 570/)
assert.doesNotMatch(html, /const height = 380/)
assert.match(html, /aria-busy="false"/)
assert.match(html, /<h2 data-loading-title>Loading countable history<\/h2>/)
assert.doesNotMatch(html, /Showing the last saved series while storage updates/)
assert.doesNotMatch(html, /Offline cache/)
assert.match(html, /statusUpdating/)
assert.match(html, /whileRefreshing/)
assert.match(html, /chip-skeleton/)
assert.match(html, /data-about/)
assert.match(html, /data-toast/)
assert.match(html, /data-install-hint/)
assert.match(html, /data-install-popover/)
assert.match(html, />\s*Install\s*</)
assert.doesNotMatch(html, /data-install-open[^>]*hidden/)
assert.match(html, /shouldCanonicalizeClientPath/)
assert.match(html, /location\.replace\(location\.pathname \+ '\/'/)
assert.match(html, /apple-touch-icon\.png/)
assert.match(html, /og-image\.jpg/)
assert.match(html, /apple-mobile-web-app-status-bar-style/)
assert.match(html, /beforeinstallprompt/)
assert.match(html, /promptEvent\.prompt/)
assert.doesNotMatch(html, /waitForInstallPrompt/)
assert.match(html, /navigator\.install/)
assert.match(html, /serviceWorkerScope/)
assert.doesNotMatch(html, /Use your browser install \/ Add to Home Screen control to install Lineage/)
const promptAt = html.indexOf("addEventListener('beforeinstallprompt'")
assert.ok(promptAt > 0 && promptAt < swAt, 'install prompt must be captured before the service worker registers')
const chartAt = html.indexOf('class="chart-wrap"')
const controlsAt = html.indexOf('class="controls"')
const storyAt = html.indexOf('class="story"')
assert.ok(
chartAt > 0 && controlsAt > chartAt && storyAt > controlsAt,
'graph must come before filters, story after filters',
)
assert.match(html, /svg\.chart \{ width: 100%; height: auto;/)
assert.match(html, /\.panel\.is-loading \.chart-wrap \{ min-height:/)
assert.doesNotMatch(html, /svg\.chart \{[^}]*height: min\(58vw/)
assert.match(html, /visibilitychange/)
assert.match(html, /pageshow/)
assert.match(html, /onAppOpened/)
assert.match(html, /shouldRunUpdateCheck/)
assert.match(html, /class="mark"/)
assert.match(html, /class="brand"/)
assert.match(html, /pwa-192\.png" width="40"/)
assert.doesNotMatch(html, /Filter language and type/)
assert.doesNotMatch(html, /Kent C\. Dodds ·/)
assert.match(html, /commits · /)
assert.match(html, /timeAxisTicks/)
assert.match(html, /axis-x/)
})
test('service worker waits for the user before taking over an update', () => {
const source = serviceWorkerScript('/packages/lineage')
assert.match(source, /lineage-v8/)
assert.match(source, /SKIP_WAITING/)
assert.match(source, /\/packages\/lineage\/manifest\.webmanifest/)
assert.match(source, /\/packages\/lineage\/apple-touch-icon\.png/)
assert.doesNotMatch(source, /new URL\('app\.js'/)
assert.match(source, /if \(!self\.registration\.active\) return self\.skipWaiting/)
})