// Whether the shell draws an app's frame on a server as a browser hydrates // it, at every width: `Shell` (server.ts) drawn with Svelte's `render` in // Node, with no browser's globals, for a sample app with every part of a // frame (an action, the account's picture and items, links, an aside), // signed out and in, its screen a tree of the renderer's server test // (Grain's feed) and a wide one (Grain's profile); then hydrated // (client.ts, Svelte's development build) in happy-dom's document at a // phone's width (390), a tablet's (900) and a desktop's (1440), each with // the same props. // // cd web && npm run ssr `just check` runs it, and CI's web job // // It fails where drawing throws or draws otherwise for a width (the band // is the stylesheet's to say, never code's); where hydrating warns, // throws, or changes what the server drew, the head's frame stylesheet // too; where the frame's stylesheet (frame.ts's `frameCss`) puts a width // in another band than its bar (under 600), its rail (600 to 1199) and // its sidebar (1200 on), or shows the aside where the navigation, the // column and the aside do not fit (900) or hides it where they do (1440); // where the column is not drawn at the top screen's measure before any // code runs (`data-measure`); and where the page made as the first history // entry is not the tab's own URL. Signed in, a screen whose Bar has a + // for the action's form and a gear: it fails where the + is not shown at // 390 or is shown at 900 and 1440 (the navigation's action is it there), // or the gear is not shown at every width, as the head's stylesheet says // in happy-dom. Then, signed in, in the browser alone, // with a fake server: it fails where Create does more than draw the // action's form over the screen on show (a screen pushed, a history entry // made), where a form opens again once cancelled (the page loaded again; // a screen a link opened at a form gone back from and forward to, or // loaded again; an entry an older shell kept with `compose` in it), where // a history entry holds `compose`, or where the action's form sent does // not open the screen of the record it made; and, a screen of pictures // pushed from a link and gone back from (and forward to, and back), where // focus does not move into it and back to the link, where the screen under // it is not inert or is aria-hidden, or where its pictures loading after it // is let go warn (Svelte's derived_inert); and a long feed, its foot shown // to a fake IntersectionObserver: where it does not load its next page // once, loads one under a screen over it (inert), or loads again after a // page failed until Try again is pressed; and a profile's panes, each a // Covers that pages itself: where the tree's own next is asked, a hidden // pane loads, the shown one loads other than once a page, a pane switched // away from and back loses its pages, or a failed page loads again before // Try again. Both builds go to a // directory of their own in the system's temporary directory, taken away // after. In the same world, a strip of stories opening a pager: it fails // where another's story tapped opens one's own page's form (read ahead // beside it, or swiped to), where one's own page with no pictures does not // stay shown, or where Your story's form opens other than once. A feed's // own pin, read again in place: it fails where a feed pushed over a tab // loses its back or title pinned or unpinned, or where one in Home's root // place by its strip (chosen there, or loaded at its URL) unpinned is not // pushed over Home's root at its URL. And the // app's own pages (`pages`): it fails where a page's path is not its // `describe_app` over Home, where the server does not draw its text and // its head (title, description, canonical URL, indexed), where a footer // link to it does not open it in the app with its URL, or where a link in // its text to another page on the app's site does not open that. And the // app's own sign-in screen (`signIn`), drawn at three widths and hydrated // as the pages are: it fails where its URL's `back` is not kept to the // app's origin, where the server does not draw its heading, links, form // (the handle, where it comes back to, Create one) and why it was sent // back, or its head (out of search); where it makes a history entry; or, // in the browser against a fake typeahead, where typing does not ask the // typeahead's own host, the arrows and Enter do not pick an account and // send the form with its handle, a picture does not fall back, or Escape // does not close the list. import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { svelte } from '@sveltejs/vite-plugin-svelte'; import { build, defaultClientConditions, defaultServerConditions } from 'vite'; const here = dirname(fileURLToPath(import.meta.url)); const web = resolve(here, '../..'); const trees = join(web, 'renderer/test/trees'); const out = mkdtempSync(join(tmpdir(), 'hud-shell-ssr-')); const say = (line) => console.log(`==> ${line}`); const problems = []; const NOW = Date.parse('2026-11-18T12:00:00.000Z'); const query = (method, params = {}) => ({ name: 'query', args: { method, params } }); const FEED = query('test.feed.getFeed', { feed: 'home' }); const PROFILE = { name: 'describe_app', args: { domain: 'app.test', actor: 'me' } }; const FOLLOWING = query('test.feed.getFeed', { feed: 'following' }); const PINNED = query('test.feed.getFeed', { feed: 'camera', camera: 'X100V' }); const UNPINNED = query('test.feed.getFeed', { feed: 'camera', camera: 'GR' }); /** The account's preferences, whose pinned feeds' paths choose Home's root (its `first`). */ const PREFS = query('test.actor.getPreferences'); /** Each tab's root, signed in, Home's `home`. */ const rootsWith = (home) => [home, query('test.explore'), query('test.notifications'), PROFILE]; /** An app with every part of a frame. */ const APP = { domain: 'app.test', name: 'Test', head: { siteName: 'Test', title: 'Test', description: 'A test app.', image: 'https://app.test/og.jpg' }, search: { typeahead: query('test.actor.typeahead'), results: '/search?q={q}', person: '/profile/{actor}' }, tabs: [ { label: 'Home', wordmark: 'test', path: '/', icon: 'feed', call: FEED, guest: 'call', first: { call: PREFS, at: 'preferences.@pinnedFeedsPref.items', path: 'path', after: 'test.actor.setPreference' } }, { label: 'Explore', icon: 'search', call: query('test.explore'), guest: 'call' }, { label: 'Notifications', icon: 'notification', call: query('test.notifications'), badge: { call: query('test.unread'), count: 'count' } }, { label: 'Profile', icon: 'people', call: PROFILE } ], routes: [ { path: '/explore', call: query('test.explore') }, { path: '/notifications', call: query('test.notifications') }, { path: '/settings', call: query('test.settings') }, { path: '/stories', call: query('test.stories') }, { path: '/profile/{actor}/post/{rkey}', call: query('test.getPost', { post: 'at://{actor}/test.post/{rkey}' }) }, { path: '/profile/{actor}/gallery/{rkey}', call: query('test.getGallery', { gallery: 'at://{actor}/test.gallery/{rkey}' }) }, { path: '/feeds/following', call: FOLLOWING }, { path: '/camera/{name}', tab: 'Explore', call: query('test.feed.getFeed', { feed: 'camera', camera: '{name}' }) }, { path: '/profile/{actor}', call: { name: 'describe_app', args: { domain: 'app.test', actor: '{actor}' } } } ], pages: [ { path: '/terms', title: 'Terms', description: 'The terms.', markdown: 'pages/terms.md' }, { path: '/privacy', title: 'Privacy', markdown: 'pages/privacy.md' } ], frame: { destinations: 'tabs', action: { label: 'Create', icon: 'imagePlus', call: { ...FEED, args: { ...FEED.args, compose: 'New' } } }, account: { picture: { tab: 'Profile', call: query('test.actor.getProfile', { actor: 'me' }), at: 'avatar' }, items: [ { label: 'Settings', icon: 'settings', call: query('test.settings') }, { host: 'signOut', label: 'Log out', icon: 'logOut' }, { host: 'signIn', label: 'Sign in', icon: 'logIn', about: 'Share your photography' } ] }, links: [ { label: 'Terms', href: '/terms' }, { label: 'AT Protocol', href: 'https://atproto.com' } ], aside: { call: query('test.aside') } }, signIn: { title: 'Log in with your atmosphere account', text: '[What is an atmosphere account?](https://atmosphereaccount.com/)', placeholder: 'e.g. user.bsky.social', legal: 'By signing in you agree to our [Terms](/terms).', create: { text: "Don't have an account?", label: 'Create one' }, typeahead: { url: 'https://typeahead.test', method: 'test.typeahead.searchActors', param: 'q', params: { limit: 5 }, items: 'actors', did: 'did', handle: 'handle', name: 'displayName', avatar: 'avatar', avatarFallback: 'avatarFallback' } } }; /** The sign-in screen as the server reads it from its URL (`signInScreen`): back to the account's profile, a handle refused. */ const SIGN_IN = { back: '/profile/me', handle: 'nobody.test', error: 'nobody.test does not resolve to an account' }; const tree = (name) => JSON.parse(readFileSync(join(trees, `${name}.json`), 'utf8')); const ASIDE = { v: 1, trusted: true, root: { c: 'Text', p: { runs: [{ text: 'Beside the column' }], style: 'body' } } }; /** A form of the action's (its `compose`, New), drawn as a Bar's +. */ const FORM = { c: 'Compose', p: { key: 'new', label: 'New', icon: 'plus', send: 'Post', fields: [{ name: 'title', kind: 'line', label: 'Title', max: 100 }], writes: { create: 'test.post' } } }; /** A screen whose Bar has a + for the action's form and a gear that opens a screen. */ const BARRED = { v: 1, trusted: true, root: { c: 'Stack', k: [ { c: 'Bar', k: [FORM, { c: 'Link', p: { label: 'My feeds', icon: 'settings', open: { call: query('test.settings') } } }] }, { c: 'Text', p: { runs: [{ text: 'A feed' }], style: 'body' } } ] } }; /** A feed with Home's strip, as Grain's feeds have it: Home's root, Following and a pinned camera, the one drawn `selected` (none, a camera not pinned). */ const STRIPPED = (selected, words) => ({ v: 1, trusted: true, root: { c: 'Stack', k: [ { c: 'Tabs', p: { style: 'strip', items: [ { label: 'For You', open: { call: FEED }, selected: selected === FEED }, { label: 'Following', open: { call: FOLLOWING }, selected: selected === FOLLOWING }, { label: 'X100V', open: { call: PINNED }, selected: selected === PINNED } ] } }, { c: 'Text', p: { runs: [{ text: words }], style: 'body' } } ] } }); /** One of the app's own pages, as HUD's `describe_app` of it draws it: its title in a Bar over its Markdown's blocks, its head its own, a link to another page on the app's site. */ const pageCall = (path) => ({ name: 'describe_app', args: { domain: 'app.test', page: path } }); const pageTree = (title, words) => ({ v: 1, trusted: true, title, head: { title: `${title} - Test`, description: `The ${title.toLowerCase()}.`, canonical: `/${title.toLowerCase()}` }, root: { c: 'Stack', p: { gap: 'md' }, k: [ { c: 'Bar', p: { title } }, { c: 'RichText', p: { blocks: [ { kind: 'heading', level: 2, runs: [{ text: 'Overview' }] }, { kind: 'paragraph', runs: [{ text: `${words} See the ` }, { text: 'privacy policy', link: 'https://app.test/privacy' }, { text: '.' }] } ] } } ] } }); const TERMS = pageCall('/terms'); const answered = (ui) => ({ result: { _meta: { ui } } }); const HOST = { endpoints: { mcp: '/api/mcp', upload: '/api/upload' }, signIn: '/auth/signin', signOut: '/auth/signout' }; /** A history that makes its entries in a list, and runs `entered` where `browser` says it hydrates. */ function history(browser) { const made = []; return { made, navigation: { here: undefined, go: async (url, state, replace) => void made.push({ url, tab: state.tab, replace }), entered: (run) => browser && queueMicrotask(run) } }; } /** The pages drawn: signed out and in, the feed (content), a wide profile, a screen with a Bar whose + is the action's form (`twin`), and the sign-in screen. */ const PAGES = [ { name: 'feed signed out', signedIn: false, path: '/', ui: tree('feed'), measure: 'content' }, { name: 'feed signed in', signedIn: true, path: '/', ui: tree('feed'), measure: 'content' }, { name: 'profile wide', signedIn: true, path: '/profile/me', ui: { ...tree('profile'), measure: 'wide' }, measure: 'wide', tab: 3 }, { name: 'a Bar with the action signed in', signedIn: true, path: '/', ui: BARRED, measure: 'content', twin: true }, { name: 'Following, a swap of Home', signedIn: true, path: '/feeds/following', ui: STRIPPED(FOLLOWING, 'The following feed'), measure: 'content', calls: [FOLLOWING], strip: true }, { name: "a wide pinned camera in Home's root place", signedIn: true, path: '/camera/X100V', ui: { ...STRIPPED(PINNED, 'The following feed'), measure: 'wide' }, measure: 'content', calls: [PINNED], strip: true, rootMeasure: 'content' }, { name: "the app's own page, its terms, over Home", signedIn: false, path: '/terms', ui: pageTree('Terms', 'The terms of using Test.'), measure: 'content', calls: [FEED, TERMS], page: true }, { name: 'Home at the first feed pinned, Following', signedIn: true, path: '/', ui: STRIPPED(FOLLOWING, 'The following feed'), measure: 'content', calls: [FOLLOWING], roots: rootsWith(FOLLOWING), strip: true }, { name: 'the sign-in screen', signedIn: false, path: '/auth/signin?back=%2Fprofile%2Fme', measure: 'content', signIn: SIGN_IN } ]; const props = (page, navigation) => ({ app: APP, host: HOST, navigation, screen: page.signIn ? { tab: -1, calls: [], top: null, aside: answered(ASIDE), names: [], missing: false, signIn: page.signIn } : { tab: page.tab ?? 0, calls: page.calls ?? [page.tab === 3 ? PROFILE : FEED], top: answered(page.ui), aside: answered(ASIDE), names: [], missing: false, ...(page.roots ? { roots: page.roots } : {}), ...(page.rootMeasure ? { measure: page.rootMeasure } : {}) }, under: null, signedIn: page.signedIn, origin: 'https://app.test', path: page.path, now: NOW, counts: page.signedIn ? Promise.resolve({ 2: 3 }) : null, picture: page.signedIn ? 'https://cdn.test/me.jpg' : null }); /** Where `a` and `b` first differ, a little of each around it. */ function difference(a, b) { let i = 0; while (i < a.length && a[i] === b[i]) i++; const at = Math.max(0, i - 80); return `server: …${a.slice(at, i + 80)}…\n hydrated: …${b.slice(at, i + 80)}…`; } /** The value a custom property has at the window's width, by the frame stylesheet's media rules that match (the last that sets it), on `selector`. */ function valueAt(css, property, selector, matches) { let found = null; for (const [, media, body] of css.matchAll(/@media ([^{]+)\{((?:[^{}]*\{[^{}]*\})*)\}/g)) { if (!matches(media)) continue; for (const [, on, rule] of body.matchAll(/([^{}]+)\{([^{}]*)\}/g)) { if (on.trim() !== selector) continue; const set = new RegExp(`${property}:([^;}]+)`).exec(rule); if (set) found = set[1]; } } return found; } /** Whether a width matches a media query of min-width and max-width alone. */ const matchesAt = (width) => (media) => [...media.matchAll(/\((min|max)-width: ([\d.]+)px\)/g)].every(([, side, px]) => (side === 'min' ? width >= Number(px) : width <= Number(px))); /** Each width, the band it is drawn in (the navigation's width) and whether the feed's aside shows. */ const WIDTHS = [ { width: 390, nav: '0px', aside: null }, { width: 900, nav: '72px', aside: null }, { width: 1440, nav: '245px', aside: '320px' } ]; try { // HUD's packages as their source (vite.config.ts). const options = () => ({ configFile: false, root: web, publicDir: false, logLevel: 'warn', resolve: { conditions: ['@hud/source', ...defaultClientConditions] }, plugins: [svelte({ compilerOptions: { runes: true } })] }); await build({ ...options(), ssr: { noExternal: true, resolve: { conditions: ['@hud/source', ...defaultServerConditions] } }, build: { ssr: join(here, 'server.ts'), outDir: join(out, 'server'), emptyOutDir: true, minify: false, rollupOptions: { output: { entryFileNames: 'server.mjs' } } } }); process.env.NODE_ENV = 'development'; await build({ ...options(), mode: 'development', build: { outDir: join(out, 'client'), emptyOutDir: true, minify: false, lib: { entry: join(here, 'client.ts'), formats: ['es'], fileName: 'client' } } }); for (const name of ['navigator', 'localStorage', 'sessionStorage']) Reflect.deleteProperty(globalThis, name); for (const name of ['window', 'document', 'navigator', 'localStorage']) if (typeof globalThis[name] !== 'undefined') throw new Error(`${name} is still there`); const server = await import(pathToFileURL(join(out, 'server/server.mjs')).href); // The frame's bands and its aside, by the stylesheet's media rules at each width. const css = server.frameCss(server.layoutOf(APP.frame)); for (const { width, nav, aside } of WIDTHS) { const at = matchesAt(width); const drawn = valueAt(css, '--hud-frame-nav', ':root', at); if (drawn !== nav) problems.push(`at ${width}: the navigation is ${drawn}, not ${nav}`); const shown = valueAt(css, '--hud-frame-aside-shown', '.hud-frame[data-measure="content"]', at); if (shown !== aside) problems.push(`at ${width}: the aside beside the column is ${shown ?? 'not shown'}, not ${aside ?? 'not shown'}`); if (valueAt(css, '--hud-frame-aside-shown', '.hud-frame[data-measure="wide"]', at) !== null) problems.push(`at ${width}: the aside is shown beside a wide screen, where it never fits`); } say('the frame is a bar at 390, a rail at 900 and a sidebar at 1440, the aside beside the column at 1440 alone'); // The account's own profile by its handle is the Profile tab at its root, not a profile pushed over Home; another's is pushed over Home. const caller = { call: async (name, args) => ({ _meta: { ui: ASIDE, viewer: { did: 'did:plc:me', handle: 'me.test' } }, structuredContent: { name, args } }), resolve: async (actor) => actor }; const own = (await server.screenAt(APP, new URL('https://app.test/profile/me.test'), caller, true)).screen; if (own.tab !== 3 || JSON.stringify(own.calls) !== JSON.stringify([PROFILE])) problems.push(`the account's own profile is drawn on tab ${own.tab} as ${JSON.stringify(own.calls)}, not the Profile tab's root`); const other = (await server.screenAt(APP, new URL('https://app.test/profile/bob.test'), caller, true)).screen; if (other.tab !== 0 || other.calls.length !== 2) problems.push(`another's profile is drawn on tab ${other.tab} as ${JSON.stringify(other.calls)}, not over Home`); say("the account's own profile, loaded, is the Profile tab at its root; another's is over Home"); // A feed Home's strip offers, and selects, is Home's root level in its root's place (no back, its strip shown), whatever its route's tab; one the strip does not select is pushed over its route's tab. const feeds = (ui) => ({ call: async () => ({ _meta: { ui } }), resolve: async (actor) => actor }); const swaps = [ ['/feeds/following', STRIPPED(FOLLOWING, 'Following'), 0, [FOLLOWING]], ['/camera/X100V', STRIPPED(PINNED, 'X100V'), 0, [PINNED]], ['/camera/GR', STRIPPED(null, 'GR'), 1, [query('test.explore'), UNPINNED]] ]; for (const [path, ui, tab, calls] of swaps) { const at = (await server.screenAt(APP, new URL(`https://app.test${path}`), feeds(ui), true)).screen; if (at.tab !== tab || JSON.stringify(at.calls) !== JSON.stringify(calls)) problems.push(`${path} is drawn on tab ${at.tab} as ${JSON.stringify(at.calls)}, not on tab ${tab} as ${JSON.stringify(calls)}`); } say("a feed Home's strip selects is Home's root level, pinned or not by its route; one it does not is pushed"); // In a tab's root's place, a screen is as wide as that root, whatever its own view asks: a wide camera feed under Home's strip at Home's reading width; pushed, at its own. const wide = { ...STRIPPED(PINNED, 'X100V'), measure: 'wide' }; const byFeed = { call: async (name, args) => ({ _meta: { ui: args.params?.feed === 'home' ? STRIPPED(FEED, 'Home') : args.params?.camera === 'GR' ? { ...STRIPPED(null, 'GR'), measure: 'wide' } : wide } }), resolve: async (actor) => actor }; const kept = (await server.screenAt(APP, new URL('https://app.test/camera/X100V'), byFeed, true)).screen; if (kept.measure !== 'content') problems.push(`a wide camera feed in Home's root place keeps ${kept.measure}, not Home's content`); const pushed = (await server.screenAt(APP, new URL('https://app.test/camera/GR'), byFeed, true)).screen; if (pushed.measure !== undefined) problems.push(`a camera feed pushed over Explore keeps ${pushed.measure}, not its own`); say("a screen in a tab's root's place keeps the root's measure; one pushed has its own"); // Signed in, Home's root is the first pinned path with a screen (its `first`), at `/` from the first paint: Following first, then reordered, Home's own path first; a camera first, its feed, at `/` as Home's root. Signed out, or the preferences failing, Home's call. const methods = []; const pinning = (paths, failing = false) => ({ call: async (name, args) => { methods.push(args.method); if (args.method === PREFS.args.method) { if (failing) throw new Error('refused'); return { structuredContent: { response: { preferences: [{ $type: 'test.actor.defs#themePref' }, { $type: 'test.actor.defs#pinnedFeedsPref', items: paths.map((path) => ({ path })) }] } } }; } const feed = query(args.method, args.params); return { _meta: { ui: feed.args.params.camera ? { ...STRIPPED(feed.args.params.camera === 'X100V' ? PINNED : null, 'A camera'), measure: 'wide' } : STRIPPED(feed.args.params.feed === 'following' ? FOLLOWING : FEED, 'A feed') } }; }, resolve: async (actor) => actor }); const homes = [ ['/', ['/groups', '/feeds/following', '/'], true, false, 0, [FOLLOWING], FOLLOWING], ['/', ['/', '/feeds/following'], true, false, 0, [FEED], FEED], ['/', ['/camera/X100V', '/feeds/following'], true, false, 0, [PINNED], PINNED], ['/', ['/feeds/following'], false, false, 0, [FEED], FEED], ['/', ['/feeds/following'], true, true, 0, [FEED], FEED], ['/camera/X100V', ['/feeds/following'], true, false, 0, [PINNED], FOLLOWING], ['/camera/X100V', ['/camera/X100V'], true, false, 0, [PINNED], PINNED], ['/feeds/following', ['/feeds/following'], true, false, 0, [FOLLOWING], FOLLOWING] ]; for (const [path, paths, signedIn, failing, tab, calls, root] of homes) { methods.length = 0; const { screen, status } = await server.screenAt(APP, new URL(`https://app.test${path}`), pinning(paths, failing), signedIn); const named = `${path} with ${JSON.stringify(paths)} pinned${signedIn ? '' : ', signed out'}${failing ? ', the preferences failing' : ''}`; if (status !== 200 || screen.tab !== tab || JSON.stringify(screen.calls) !== JSON.stringify(calls)) problems.push(`${named} is drawn on tab ${screen.tab} as ${JSON.stringify(screen.calls)}, not on tab ${tab} as ${JSON.stringify(calls)}`); if (JSON.stringify(screen.roots) !== JSON.stringify(signedIn ? rootsWith(root) : APP.tabs.map((t) => (t.guest === 'call' ? t.call : (t.guest ?? null))))) problems.push(`${named} carries the roots ${JSON.stringify(screen.roots)}`); if (methods.includes(PREFS.args.method) !== signedIn) problems.push(`${named}: the preferences were ${signedIn ? 'not ' : ''}asked`); } const under = (await server.screenAt(APP, new URL('https://app.test/camera/X100V'), pinning(['/feeds/following']), true)).screen; if (under.measure !== 'content') problems.push(`a wide camera feed in the place of Home's chosen root, Following, keeps ${under.measure}, not Following's content`); const camera = (await server.screenAt(APP, new URL('https://app.test/'), pinning(['/camera/X100V']), true)).screen; if (camera.measure !== undefined) problems.push(`a camera feed chosen as Home's root keeps ${camera.measure}, not its own`); say("Home's root is the first pinned path with a screen, at / from the first paint, and the strip's feeds are in its place"); // What a tab's first chooses of what is listed: a path of the app's own alone, its own URL its call, any other by its routes. const firstOf = (paths, tab = APP.tabs[0]) => server.chosenRoot(APP, tab, { preferences: [{ $type: 'pinnedFeedsPref', items: paths.map((path) => ({ path })) }] }, async (actor) => actor); const chosen = [ [['//evil.test/feeds/following', '/\\evil.test/feeds/following', 'https://evil.test/feeds/following', 'feeds/following', 5], null], [['/?utm_source=x'], FEED], [['/camera/Fuji%20X100V'], query('test.feed.getFeed', { feed: 'camera', camera: 'Fuji X100V' })], [['/camera/Fuji X100V'], query('test.feed.getFeed', { feed: 'camera', camera: 'Fuji X100V' })], [[], null] ]; for (const [paths, want] of chosen) { const got = await firstOf(paths); if (JSON.stringify(got) !== JSON.stringify(want)) problems.push(`${JSON.stringify(paths)} pinned chooses ${JSON.stringify(got)}, not ${JSON.stringify(want)}`); } if ((await firstOf(['/feeds/following'], APP.tabs[1])) !== null) problems.push('a tab with no first chose a root'); say("a tab's first chooses only the app's own paths, its own URL its call"); // An app's own page, by its path: its describe_app over Home's root, as a screen of any route is; a path no page or route has, nothing. const terms = await server.screenAt(APP, new URL('https://app.test/terms'), feeds(pageTree('Terms', 'Terms')), false); if (terms.status !== 200 || terms.screen.tab !== 0 || JSON.stringify(terms.screen.calls) !== JSON.stringify([FEED, TERMS])) problems.push(`/terms is drawn on tab ${terms.screen.tab} as ${JSON.stringify(terms.screen.calls)}, not the page's describe_app over Home`); const none = await server.screenAt(APP, new URL('https://app.test/cookies'), feeds(ASIDE), false); if (none.status !== 404) problems.push(`/cookies, no page's or route's, answers ${none.status}, not 404`); say("an app's own page, loaded, is its describe_app over Home; a path nothing has is not found"); // The sign-in screen, by its URL: where it comes back to (a path of this origin, else home), the handle and why it was sent back with. const asked = server.signInScreen(new URL('https://app.test/auth/signin?back=%2Fprofile%2Fme&handle=nobody.test&error=Nope')); if (JSON.stringify(asked.signIn) !== JSON.stringify({ back: '/profile/me', handle: 'nobody.test', error: 'Nope' }) || asked.tab !== -1 || asked.calls.length) problems.push(`the sign-in screen is read as ${JSON.stringify(asked)}`); const evil = server.signInScreen(new URL('https://app.test/auth/signin?back=javascript:alert(1)'), { handle: 'a.test', error: 'Refused' }).signIn; if (JSON.stringify(evil) !== JSON.stringify({ back: '/', handle: 'a.test', error: 'Refused' })) problems.push(`the sign-in screen, off the origin and posted, is read as ${JSON.stringify(evil)}`); say('the sign-in screen comes back to a path of its own origin, saying what it was sent back with'); const drawn = []; for (const page of PAGES) { let result; try { result = server.render(server.Shell, { props: props(page, history(false).navigation) }); } catch (e) { problems.push(`${page.name}: drawing it threw: ${e.stack}`); continue; } if (!result.body.includes(`data-measure="${page.measure}"`)) problems.push(`${page.name}: the frame is not drawn at its screen's measure, ${page.measure}`); if (!result.head.includes('--hud-frame-nav:245px')) problems.push(`${page.name}: the head has no frame stylesheet`); if (result.body.includes('>Create') !== page.signedIn) problems.push(`${page.name}: the action is ${page.signedIn ? 'not drawn signed in' : 'drawn signed out'}`); if (result.body.includes('Share your photography') === page.signedIn) problems.push(`${page.name}: signing in is ${page.signedIn ? 'offered signed in' : 'not offered signed out'}`); if (!result.body.includes('Beside the column')) problems.push(`${page.name}: the aside the server answered is not drawn`); if (page.signedIn && !result.body.includes('src="https://cdn.test/me.jpg"')) problems.push(`${page.name}: the account's picture is not drawn by the server`); if (page.strip && (result.body.includes('aria-label="Back"') || !result.body.includes('The following feed') || !result.body.includes('X100V'))) problems.push(`${page.name}: not drawn as Home's root with its strip and no back`); if (page.page) { if (!result.body.includes('Terms - Test', 'name="description" content="The terms."', 'rel="canonical" href="https://app.test/terms"', 'name="robots" content="max-image-preview:large"']) if (!result.head.includes(tag)) problems.push(`${page.name}: its head has no ${tag}`); } if (page.signIn) { for (const part of [ /Log in with your atmosphere account/, /href="https:\/\/atmosphereaccount\.com\/" target="_blank"/, /method="post" action="\/auth\/signin"/, /name="back" value="\/profile\/me"/, /name="handle"/, /value="nobody\.test"/, /placeholder="e\.g\. user\.bsky\.social"/, /role="alert">nobody\.test does not resolve to an account/, /]*>Terms<\/a>/, /class="cancel[^"]*" href="\/profile\/me"/, /name="create" value="1"/ ]) if (!part.test(result.body)) problems.push(`${page.name}: the server did not draw ${part}`); if (!result.body.includes('href="/auth/signin?back=%2Fprofile%2Fme"')) problems.push(`${page.name}: its navigation's Sign in does not come back where the screen does`); for (const tag of ['Sign in · Test', 'name="robots" content="noindex"']) if (!result.head.includes(tag)) problems.push(`${page.name}: its head has no ${tag}`); } drawn.push({ page, head: result.head, body: result.body }); say(`${page.name}: drawn`); } const { Window } = await import('happy-dom'); const window = new Window({ url: 'https://app.test/' }); for (const key of Object.getOwnPropertyNames(window)) if (!(key in globalThis)) globalThis[key] = window[key]; Object.assign(globalThis, { window, document: window.document, navigator: window.navigator, location: window.location }); const built = pathToFileURL(join(out, 'client', readdirSync(join(out, 'client')).find((f) => /^client\.m?js$/.test(f)))).href; let loads = 0; for (const { page, head, body } of drawn) { for (const { width } of WIDTHS) { // Each a page loaded afresh: its own modules, whose clock no tree mounted before has settled. const client = await import(`${built}?load=${loads++}`); window.happyDOM.setViewport({ width, height: 900 }); const name = `${page.name} at ${width}`; document.head.innerHTML = head; const target = document.createElement('div'); document.body.replaceChildren(target); target.innerHTML = body; const before = target.innerHTML.replaceAll(/ on(load|error)="this\.__e=event"/g, ''); const headBefore = document.head.innerHTML; const said = []; const { warn, error } = console; console.warn = console.error = (...args) => said.push(args.map(String).join(' ')); const { made, navigation } = history(true); let app; try { app = client.hydrate(client.Shell, { target, props: props(page, navigation) }); } catch (e) { said.push(`threw: ${e.stack}`); } finally { console.warn = warn; console.error = error; } const after = target.innerHTML; const headAfter = document.head.innerHTML; await new Promise((done) => setTimeout(done, 0)); // A screen over its tab's root is entered as two entries, the root's and its own, so its back goes to the root. const first = (page.calls?.length ?? 1) > 1 ? '/' : page.path; // The sign-in screen makes no entry: its form and links load pages anew. if (page.signIn && made.length) problems.push(`${name}: it made history entries, ${JSON.stringify(made)}`); if (!page.signIn && (made[0]?.url !== first || made[0]?.replace !== true)) problems.push(`${name}: its first history entry is ${JSON.stringify(made[0])}, not ${first}`); if (first !== page.path) { await new Promise((done) => setTimeout(done, 20)); if (made.at(-1)?.url !== page.path) problems.push(`${name}: its last history entry is ${JSON.stringify(made.at(-1))}, not the page's own, ${page.path}`); } if (page.twin) { const shown = (element) => element !== null && window.getComputedStyle(element).display !== 'none'; const bar = target.querySelector('.stack:not([hidden]) .tools'); const plus = bar?.querySelector('button[data-bar-form="New"]') ?? null; const gear = bar?.querySelector('[aria-label="My feeds"], [title="My feeds"]') ?? null; if (!plus) problems.push(`${name}: the Bar's + is not drawn`); else if (shown(plus) !== (width < 600)) problems.push(`${name}: the Bar's + is ${shown(plus) ? 'shown beside the action' : 'hidden with no action beside it'}`); if (!shown(gear)) problems.push(`${name}: the Bar's gear is not shown`); else say(`${name}: the gear shown, the + ${width < 600 ? 'shown' : 'hidden, the action beside it'}`); } if (app) client.unmount(app); for (const s of said) problems.push(`${name}: hydrating it: ${s}`); if (after !== before) problems.push(`${name}: hydrated, it changed:\n ${difference(before, after)}`); if (!headAfter.includes(headBefore.match(/