import assert from 'node:assert/strict' import { registerHooks } from 'node:module' import { test } from 'node:test' // In-memory stand-in for kody:runtime so the app can run under node --test. const STUB = ` const store = globalThis.__factoryStore ??= new Map(); globalThis.__factoryWrites ??= { n: 0 }; export const packageStorage = () => ({ async get(k) { return store.get(k) ?? null }, async set(k, v) { globalThis.__factoryWrites.n++; store.set(k, JSON.stringify(v)) }, }); export const packageContext = { appBasePath: '/apps/factory/' }; export async function credential() { throw new Error('no credentials in tests') } export async function credentials() { return [] } export const kody = {}; export const workflows = null; ` registerHooks({ resolve(spec, ctx, next) { // Wake alerts can post to Discord; tests stub it (wake-transport tests also swap it out). if (spec === 'kody:@kody/discord/post-message') return { url: `data:text/javascript,${encodeURIComponent('export default async () => ({ ok: true })')}`, shortCircuit: true } if (spec === 'kody:runtime') { return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true } } return next(spec, ctx) }, }) const { default: app } = await import('./entry.ts') const { seedKentProjects, setProjectEnabled } = await import('../projects/config.ts') const { patchSettings } = await import('../lib/settings.ts') const { CLIENT_JS } = await import('./client.ts') const { renderShell } = await import('./ui.ts') const { parseCreateInput } = await import('./create-input.ts') const { normalizeProject, KENT_SEED_PROJECTS } = await import('../projects/model.ts') const g = globalThis as unknown as { __factoryWrites: { n: number } } const BASE = 'https://app.test/apps/factory' const get = (path: string) => app.fetch(new Request(BASE + path)) const postJson = (path: string, body: unknown) => app.fetch( new Request(BASE + path, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), }), ) await seedKentProjects() await setProjectEnabled('mediarss', false) await patchSettings({ cloudflareAccountId: 'abcdef0123456789abcdef0123456789' }) test('every view renders a non-blank shell without writing storage', async () => { const before = g.__factoryWrites.n for (const [path, marker] of [ ['/', 'Move work forward.'], ['/work', 'Work queue'], ['/projects', 'Every repository'], ['/projects/kody', 'Loading project'], ['/notifications', 'Notifications'], ['/settings', 'Run safely'], ['/manage', 'Manage projects'], ] as const) { const res = await get(path) assert.equal(res.status, 200, path) assert.match(res.headers.get('content-type') ?? '', /text\/html/) const html = await res.text() assert.ok(html.includes(marker), `${path} has ${marker}`) assert.ok(html.includes('data-base="/apps/factory"'), `${path} carries basePath`) assert.ok(html.includes('aria-label="Main"')) assert.doesNotMatch(html, /\son[a-z]+\s*=/i, `${path} has no inline handlers`) assert.doesNotMatch(html, /https?:\/\/fonts\./, 'no external fonts') } assert.equal(g.__factoryWrites.n, before, 'GET never writes') }) test('client script is valid plain JS with no template placeholders', () => { assert.ok(!CLIENT_JS.includes('`')) assert.ok(!CLIENT_JS.includes('${')) assert.doesNotThrow(() => new Function(CLIENT_JS)) }) test('shell escapes the base path', () => { const html = renderShell({ basePath: '">