Add tests for backend asset handling and frontend boot sequence, remove outdated MOTD script, and add icons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
OverlordandClaude Bot committed 2026-10-13 15:53:41 +02:00
1 parent 0ad5b5f44f
commit 7937879ace
34 files changed
+613 -116

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
import { mkdtempSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { readTheme } from './assets.ts';
describe('readTheme', () => {
const base = Buffer.from(':root { --text: white; }');
it('appends the override, so its rules win', async () => {
const file = join(mkdtempSync(join(tmpdir(), 'theme-')), 'theme.css');
writeFileSync(file, ':root { --text: red; }');
expect((await readTheme(base, file))?.toString()).toBe(':root { --text: white; }\n:root { --text: red; }');
});
it('serves the built in theme without an override', async () => {
expect(await readTheme(base, '/nonexistent/theme.css')).toBe(base);
expect(await readTheme(undefined, '/nonexistent/theme.css')).toBeUndefined();
});
});
+23
View File
@@ -1,5 +1,7 @@
import { existsSync, readdirSync, readFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { extname, join, relative, sep } from 'node:path';
import type { Font } from '@homepage/indexer';
export type Asset = {
body: Buffer;
@@ -50,3 +52,24 @@ export function loadAssets(directory: string): Map<string, Asset>
if (index) assets.set('/', index);
return assets;
}
const FONT = /^\/assets\/jetbrains-mono-latin-(400|700)-normal-[^/]+\.woff2$/;
/** The Latin JetBrains Mono files of the frontend, so the static pages use the same font. */
export function siteFonts(assets: Map<string, Asset>): Font[]
{
return [...assets.keys()].flatMap((path) =>
{
const match = FONT.exec(path);
return match ? [{ url: path, weight: Number(match[1]) }] : [];
});
}
/** The built in theme followed by the optional override, whose rules win because they come later. */
export async function readTheme(base: Buffer | undefined, overrideFile: string): Promise<Buffer | undefined>
{
if (!base) return undefined;
const override = await readFile(overrideFile).catch(() => undefined);
return override ? Buffer.concat([base, Buffer.from('\n'), override]) : base;
}
+3 -2
View File
@@ -1,4 +1,4 @@
import { buildIndex, buildSite, reservedNames, type Site } from '@homepage/indexer';
import { buildIndex, buildSite, reservedNames, type Font, type Site } from '@homepage/indexer';
import type { BackendConfig } from './config.ts';
import type { SessionHub } from './hub.ts';
import type { Builtin } from './jobs.ts';
@@ -11,6 +11,7 @@ export type BuiltinDependencies = {
spawner: Pick<Spawner, 'kill'> & { list(): Promise<{ containerId: string; sessionId: string }[]> };
started: number;
setSite: (site: Site) => void;
fonts?: Font[];
log: (message: string) => void;
};
@@ -34,7 +35,7 @@ export function createBuiltins(dependencies: BuiltinDependencies): Record<string
if (index.hash === lastHash) return;
lastHash = index.hash;
dependencies.setSite(buildSite(index, { siteUrl: config.siteUrl, legal: config.legalPages }));
dependencies.setSite(buildSite(index, { siteUrl: config.siteUrl, legal: config.legalPages, fonts: dependencies.fonts }));
// The same warnings come back on every rebuild, so they are only logged when they change.
const warnings = index.warnings.join('\n');
+3
View File
@@ -9,6 +9,8 @@ export type BackendConfig = {
jobsFile: string;
/** Directory the jobs service writes triggers for builtin jobs into; empty disables them. */
triggerDir: string;
/** Optional CSS appended to the theme of the frontend, which overrides its colors. */
themeFile: string;
/** Absolute URL of the site, for the sitemap. */
siteUrl: string;
/** Route name to content file, from LEGAL_PAGES like "impressum:impressum.txt". */
@@ -62,6 +64,7 @@ export function loadConfig(env: NodeJS.ProcessEnv): BackendConfig
contentDir: env.CONTENT_DIR ?? '/content',
jobsFile: env.JOBS_FILE ?? '/config/jobs.toml',
triggerDir: env.TRIGGER_DIR ?? '',
themeFile: env.THEME_FILE ?? '/config/theme.css',
siteUrl: env.SITE_URL ?? 'https://security-command.org',
legalPages: legalPages(env.LEGAL_PAGES ?? 'impressum:impressum.txt,datenschutz:datenschutz.txt'),
statusDir: env.STATUS_DIR ?? '',
+4 -2
View File
@@ -1,7 +1,7 @@
import type { Site } from '@homepage/indexer';
import { watchTree, watchTriggers } from '@homepage/jobs';
import { SpawnerClient } from '@homepage/protocol';
import { loadAssets } from './assets.ts';
import { loadAssets, readTheme, siteFonts } from './assets.ts';
import { createBuiltins } from './builtins.ts';
import { loadConfig } from './config.ts';
import { SessionHub } from './hub.ts';
@@ -31,6 +31,7 @@ jobs = new BackendJobs(config.jobsFile, createBuiltins({
spawner,
started: Date.now(),
setSite: (next) => { site = next; },
fonts: siteFonts(assets),
log,
}), log);
await jobs.start();
@@ -57,7 +58,8 @@ async function healthy(): Promise<boolean>
return ok;
}
const server = createBackend(config, hub, { assets, healthy, site: () => site });
const theme = () => readTheme(assets.get('/theme.css')?.body, config.themeFile);
const server = createBackend(config, hub, { assets, healthy, site: () => site, theme });
server.listen(config.port, config.host, () => log(`listening on ${config.host}:${config.port}`));
if (config.metricsPort > 0) createMetricsServer(hub).listen(config.metricsPort, config.host);
+9 -1
View File
@@ -91,6 +91,7 @@ describe('backend', () => {
contentDir: '',
jobsFile: '',
triggerDir: '',
themeFile: '',
siteUrl: 'https://security-command.org',
legalPages: new Map(),
statusDir: '',
@@ -109,7 +110,7 @@ describe('backend', () => {
spawner = new FakeSpawner();
hub = new SessionHub(spawner, { ...config, helloTimeoutMs: 10_000, log: () => {} });
server = createBackend(config, hub, { assets: ASSETS, site: () => SITE });
server = createBackend(config, hub, { assets: ASSETS, site: () => SITE, theme: async () => Buffer.from(':root { --text: red; }') });
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
url = `ws://127.0.0.1:${(server.address() as AddressInfo).port}/ws`;
}
@@ -421,6 +422,13 @@ describe('backend', () => {
expect((await http('/%E0%A4%A')).status).toBe(404);
});
it('serves the theme without caching it', async () => {
const response = await http('/theme.css');
expect(await response.text()).toBe(':root { --text: red; }');
expect(response.headers.get('content-type')).toBe('text/css; charset=utf-8');
expect(response.headers.get('cache-control')).toBe('no-cache');
});
it('returns 404 for unknown paths', async () => {
expect((await http('/../../etc/passwd')).status).toBe(404);
expect((await http('/nothing')).status).toBe(404);
+6
View File
@@ -14,9 +14,12 @@ export type BackendOptions = {
healthy?: () => Promise<boolean>;
/** The static site of the content, rebuilt by the index job. */
site?: () => Site | undefined;
/** The theme with its override, read on every request so edits apply right away. */
theme?: () => Promise<Buffer | undefined>;
};
const MAX_FRAME_BYTES = 64 * 1024;
const THEME_PATH = '/theme.css';
const PING_INTERVAL_MS = 30_000;
// xterm.js creates style elements at runtime, so styles need 'unsafe-inline'.
@@ -80,6 +83,9 @@ export function createBackend(config: BackendConfig, hub: SessionHub, options: B
return send(response, ok ? 200 : 503, 'text/plain', ok ? 'ok' : 'degraded', { 'Cache-Control': 'no-store' }, head);
}
const theme = path === THEME_PATH ? await options.theme?.() : undefined;
if (theme) return send(response, 200, 'text/css; charset=utf-8', theme, { 'Cache-Control': 'no-cache' }, head);
const asset = assets.get(path);
if (asset)
{