import fs from 'node:fs'; import path from 'node:path'; import zlib from 'node:zlib'; import { describe, expect, it } from 'vitest'; import { appendBoundedStreamText } from '../src/client/ts/state.js'; import { SettingsSchema, THEME_IDS } from '../src/shared/schemas/settings.schema.js'; describe('Phase 5: 8 Theme Presets, Code-Splitting Budgets & Galaxy J7 Mobile Optimization', () => { it('supports all 8 theme presets in SettingsSchema and dist/public/themes.css', () => { expect(THEME_IDS).toEqual([ 'phosphor-green', 'amber-term', 'nord-dark', 'gruvbox-dark', 'paper-light', 'solarized-dark', 'tokyo-night', 'high-contrast', ]); const themesCss = fs.readFileSync(path.resolve('dist/public/themes.css'), 'utf8'); for (const id of THEME_IDS) { const parsed = SettingsSchema.parse({ theme: id }); expect(parsed.theme).toBe(id); expect(themesCss).toMatch(new RegExp(`\\[data-theme="?${id}"?\\]`)); } }); it('meets <50KB core gzipped bundle (including static sync chunks), splits lazy chunks, preserves PWA & mobile CSS, and caps DOM streams', () => { const publicDir = path.resolve('dist/public'); // Recursively follow static ESM imports from main.js to measure true eager core bundle const visitedJs = new Set(['main.js']); const queue = ['main.js']; while (queue.length > 0) { const rel = queue.shift()!; const abs = path.join(publicDir, rel); if (!fs.existsSync(abs)) continue; const code = fs.readFileSync(abs, 'utf8'); const importRe = /\bfrom\s*["']\.\/(chunks\/[^"']+\.js)["']|\bimport\s*["']\.\/(chunks\/[^"']+\.js)["']/g; let m: RegExpExecArray | null; while ((m = importRe.exec(code)) !== null) { const target = m[1] ?? m[2]!; if (!visitedJs.has(target)) { visitedJs.add(target); queue.push(target); } } } const coreFiles = ['index.html', 'themes.css', 'base.css', ...visitedJs]; let totalCoreGzip = 0; for (const f of coreFiles) { const buf = fs.readFileSync(path.join(publicDir, f)); totalCoreGzip += zlib.gzipSync(buf, { level: 9 }).length; } expect(totalCoreGzip).toBeLessThan(50 * 1024); // Heavy/optional views & overlays stay code-split out of main.js const mainJs = fs.readFileSync(path.join(publicDir, 'main.js'), 'utf8'); expect(mainJs).not.toContain('ai-drawer-dialog'); expect(mainJs).not.toContain('xterm-helper-textarea'); expect(mainJs).not.toContain('Fleet Monitor'); const chunks = fs.readdirSync(path.join(publicDir, 'chunks')); expect(chunks.some((c) => c.startsWith('ai-drawer-') && c.endsWith('.js'))).toBe(true); expect(chunks.some((c) => c.startsWith('command-palette-') && c.endsWith('.js'))).toBe(true); expect(chunks.some((c) => c.startsWith('dashboard-view-') && c.endsWith('.js'))).toBe(true); expect(chunks.some((c) => c.startsWith('settings-view-') && c.endsWith('.js'))).toBe(true); expect(chunks.some((c) => c.startsWith('settings-ops-panel-') && c.endsWith('.js'))).toBe(true); expect(chunks.some((c) => c.startsWith('terminal-view-') && c.endsWith('.js'))).toBe(true); expect(chunks.some((c) => c.startsWith('workflows-view-') && c.endsWith('.js'))).toBe(true); expect(chunks.some((c) => c.startsWith('alerts-view-') && c.endsWith('.js'))).toBe(true); for (const c of chunks) { if (c.startsWith('terminal-view-')) continue; const gzLen = zlib.gzipSync(fs.readFileSync(path.join(publicDir, 'chunks', c)), { level: 9, }).length; expect(gzLen).toBeLessThan(20 * 1024); } const manifest = JSON.parse(fs.readFileSync(path.join(publicDir, 'manifest.json'), 'utf8')) as { display: string; }; expect(manifest.display).toBe('standalone'); const baseCss = fs.readFileSync(path.join(publicDir, 'base.css'), 'utf8'); expect(baseCss).toContain('overscroll-behavior:contain'); expect(baseCss).toContain('content-visibility:auto'); expect(baseCss).toContain('min-height:44px'); // Long-running log stream buffer remains strictly bounded const mockPre = { textContent: '' }; for (let i = 0; i < 40; i++) { appendBoundedStreamText(mockPre, `line-${i}:${'x'.repeat(200)}\n`, 1024); } expect(mockPre.textContent.length).toBeLessThanOrEqual(1024); expect(mockPre.textContent).toContain('[truncated older stream output]'); expect(mockPre.textContent).toContain('line-39:'); }); });