#!/usr/bin/env bun import { spawnSync } from 'node:child_process' import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { dirname, join, resolve } from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = dirname(fileURLToPath(import.meta.url)) const repoRoot = resolve(__dirname, '../..') const frontendDir = join(repoRoot, 'frontend') const clientDir = join(repoRoot, 'client') const bunBin = process.execPath function assert(condition, message) { if (!condition) { throw new Error(`client smoke assertion failed: ${message}`) } } function runChecked(cmd, args, options = {}) { const result = spawnSync(cmd, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], ...options, }) if ((result.status ?? 1) !== 0) { throw new Error( `Command failed (${cmd} ${args.join(' ')}): exit=${result.status}\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`, ) } return result } function checkOfflineEarthBundle() { runChecked(bunBin, ['run', 'build'], { cwd: clientDir }) const bundleRoot = join(clientDir, '.earth-bundle', 'earth') const htmlPath = join(bundleRoot, 'index.html') assert(existsSync(htmlPath), `missing bundled Earth HTML at ${htmlPath}`) const html = readFileSync(htmlPath, 'utf8') assert(!html.includes('esm.sh'), 'offline Earth bundle index.html must not contain esm.sh references') const importMapMatch = html.match(/]*>([\s\S]*?)<\/script>/i) assert(Boolean(importMapMatch), 'offline Earth bundle index.html must contain an importmap') const importMap = JSON.parse(importMapMatch[1]) const imports = importMap.imports || {} for (const requiredKey of ['three', 'simplex-noise', 'satellite.js', 'hls.js']) { assert(typeof imports[requiredKey] === 'string', `missing importmap key: ${requiredKey}`) assert(imports[requiredKey].startsWith('./vendor/'), `${requiredKey} must map to ./vendor/: ${imports[requiredKey]}`) } for (const [specifier, mappedPath] of Object.entries(imports)) { if (mappedPath.startsWith('./vendor/') && !mappedPath.endsWith('/')) { const target = join(bundleRoot, mappedPath.slice(2)) assert(existsSync(target), `mapped vendor file missing for ${specifier}: ${target}`) } } const mediaPipeDir = join(bundleRoot, 'vendor', 'mediapipe') for (const requiredFile of [ 'vision_bundle.mjs', 'wasm/vision_wasm_internal.js', 'wasm/vision_wasm_internal.wasm', ]) { const target = join(mediaPipeDir, requiredFile) assert(existsSync(target), `missing offline MediaPipe asset: ${target}`) } console.log('ok: offline Earth bundle contains pinned local vendors and MediaPipe WASM without esm.sh') } function checkAdminBuildSeparation() { const adminOutDir = mkdtempSync(join(tmpdir(), 'planet-admin-smoke-')) try { runChecked( bunBin, ['./node_modules/vite/bin/vite.js', 'build', '--outDir', adminOutDir], { cwd: frontendDir, env: { ...process.env, PLANET_FRONTEND_TARGET: 'admin' }, }, ) assert(existsSync(join(adminOutDir, 'index.html')), 'admin build output missing index.html') assert(existsSync(join(adminOutDir, 'favicon.ico')), 'admin build output missing favicon.ico') assert(existsSync(join(adminOutDir, 'favicon.png')), 'admin build output missing favicon.png') assert(!existsSync(join(adminOutDir, 'earth')), 'admin build output must exclude public/earth assets') console.log('ok: frontend admin target excludes public/earth assets and includes favicons') } finally { rmSync(adminOutDir, { recursive: true, force: true }) } } function checkElectronHeadlessSmoke() { const electronBin = join( clientDir, 'node_modules', '.bin', process.platform === 'win32' ? 'electron.cmd' : 'electron', ) assert(existsSync(electronBin), `missing Electron binary at ${electronBin}`) const mainEntry = join(clientDir, 'dist-electron', 'main', 'index.cjs') assert(existsSync(mainEntry), `missing built Electron main entry at ${mainEntry}`) const electronArgs = [mainEntry, '--headless-smoke', '--no-sandbox'] const hasDisplay = Boolean(process.env.DISPLAY || process.env.WAYLAND_DISPLAY) let result if (!hasDisplay && process.platform === 'linux') { const xvfb = spawnSync('which', ['xvfb-run'], { stdio: 'ignore' }) if (xvfb.status !== 0) { console.log('warn: no DISPLAY/WAYLAND_DISPLAY or xvfb-run; skipping Electron headless window check') return } result = runChecked('xvfb-run', ['-a', electronBin, ...electronArgs], { cwd: clientDir }) } else { result = runChecked(electronBin, electronArgs, { cwd: clientDir, shell: process.platform === 'win32', }) } const lines = result.stdout .split(/\r?\n/) .map((line) => line.trim()) .filter(Boolean) const jsonLine = [...lines].reverse().find((line) => line.startsWith('{') && line.endsWith('}')) assert(Boolean(jsonLine), `Electron headless smoke produced no JSON output: ${result.stdout}`) const report = JSON.parse(jsonLine) assert(report.href === 'planet://earth/earth/index.html', `unexpected Earth URL: ${report.href}`) assert(report.isElectron === true, `expected isElectron=true, got ${report.isElectron}`) assert(report.earthStartupReady === true, `expected earthStartupReady=true, got ${JSON.stringify(report)}`) assert(report.hasLayerPanel === true, 'expected #layer-toggles in Earth DOM') assert(report.hasToolbar === true, 'expected #right-toolbar-group in Earth DOM') assert( report.hasNoDesktopDomPatch === true, 'expected no desktop-only [data-desktop-server-settings] card in Earth DOM', ) assert(report.hasApplicationMenu === true, 'expected native application menu to be registered') assert(report.menuBarInitiallyHidden === true, 'expected native menu bar to start hidden') assert(report.afterPortraitToggle === 'portrait', `expected portrait toggle, got ${report.afterPortraitToggle}`) assert( report.portraitBounds && report.portraitBounds.width < report.portraitBounds.height && report.portraitBounds.width <= 820, `expected portrait window bounds <= 820px wide, got ${JSON.stringify(report.portraitBounds)}`, ) assert( report.portraitLayoutMode === 'mobile', `expected Earth layout mode 'mobile' in portrait, got ${report.portraitLayoutMode}`, ) assert( report.afterLandscapeToggle === 'landscape', `expected landscape toggle back, got ${report.afterLandscapeToggle}`, ) assert( report.restoredLayoutMode && report.restoredLayoutMode !== 'mobile', `expected Earth layout mode restored from mobile, got ${report.restoredLayoutMode}`, ) assert(report.splashCheck?.mounted === true, 'expected desktop splash Shadow DOM host to mount on replay') assert( report.splashCheck?.hasPresenterZh === true && report.splashCheck?.hasPresenterEn === true && report.splashCheck?.hasPresentedBy === true, `expected presenter lockup texts on splash, got ${JSON.stringify(report.splashCheck)}`, ) assert( report.splashCheck?.emblemLoaded === true && report.splashCheck?.emblemWidth === 283, `expected transparent ZJU emblem (283px) loaded in splash, got ${JSON.stringify(report.splashCheck)}`, ) assert( report.splashCheck?.activeTheme === 'singularity' && report.splashCheck?.hasNoThemeSwitcher === true, `expected unified singularity splash without theme switcher, got ${JSON.stringify(report.splashCheck)}`, ) console.log(`ok: Electron headless smoke passed (${jsonLine})`) } checkOfflineEarthBundle() checkAdminBuildSeparation() checkElectronHeadlessSmoke()