import { mkdtempSync, readFileSync, rmSync } from 'node:fs' import os from 'node:os' import path from 'node:path' import { afterAll, beforeAll, describe, expect, test } from 'bun:test' import { DesktopConfigStore, normalizeSplashTheme, parseLaunchArgs } from '../src/main/config' import { createWindowShowGate } from '../src/main/show-gate' import { LANDMASK_HEIGHT, LANDMASK_WIDTH, unpackEarthLandMask } from '../src/preload/earth-landmask' import { computeSplashMorphPhases, evaluateBootSplashState } from '../src/preload/splash' let tempRoot = '' beforeAll(() => { tempRoot = mkdtempSync(path.join(os.tmpdir(), 'planet-splash-config-test-')) }) afterAll(() => { if (tempRoot) { rmSync(tempRoot, { recursive: true, force: true }) } }) describe('splash theme configuration and landmask', () => { test('a clean desktop launch from the Admin handoff uses the public service', () => { const store = new DesktopConfigStore({ userDataDir: tempRoot, exeDir: tempRoot, argv: ['Planet Earth', 'planet-earth://open'], env: {}, }) expect(store.get()).toMatchObject({ backendUrl: 'https://rclaw.top', adminUrl: 'https://admin.rclaw.top', wsUrl: 'wss://rclaw.top/ws', deepLinkUrl: 'planet-earth://open', }) }) test('normalizeSplashTheme normalizes unified singularity theme, legacy names, and off', () => { expect(normalizeSplashTheme('singularity')).toBe('singularity') expect(normalizeSplashTheme('HORIZON')).toBe('singularity') expect(normalizeSplashTheme('constellation')).toBe('singularity') expect(normalizeSplashTheme('off')).toBe('off') expect(normalizeSplashTheme('invalid', 'singularity')).toBe('singularity') expect(normalizeSplashTheme(undefined)).toBe('singularity') }) test('parses --splash-theme CLI flag and persists splashTheme without clobbering endpoints', () => { const parsed = parseLaunchArgs(['--splash-theme=off']) expect(parsed.flags.splashThemeOverride).toBe('off') const userDataDir = path.join(tempRoot, 'user-data') const exeDir = path.join(tempRoot, 'exe-dir') const store = new DesktopConfigStore({ userDataDir, exeDir, argv: [], env: {} }) expect(store.get().splashTheme).toBe('singularity') store.updateEndpoints({ backendUrl: 'https://api.planet.test:8443' }) store.updateSplashTheme('off') const reloaded = new DesktopConfigStore({ userDataDir, exeDir, argv: [], env: {} }) expect(reloaded.get().backendUrl).toBe('https://api.planet.test:8443') expect(reloaded.get().splashTheme).toBe('off') reloaded.updateSplashTheme('singularity') reloaded.updateEndpoints({ adminUrl: 'https://admin.planet.test:8443' }) const reloadedAgain = new DesktopConfigStore({ userDataDir, exeDir, argv: [], env: {} }) expect(reloadedAgain.get().splashTheme).toBe('singularity') expect(reloadedAgain.get().adminUrl).toBe('https://admin.planet.test:8443') }) test('computeSplashMorphPhases choreographs dawn to singularity reveal and climax burst', () => { const start = computeSplashMorphPhases(0, 0) expect(start.dawnIntensity).toBe(1) expect(start.coreReveal).toBe(0) expect(start.orbitReveal).toBe(0) expect(start.arcReveal).toBe(0) expect(start.burst).toBe(0) expect(start.fade).toBe(1) const midpoint = computeSplashMorphPhases(0.5, 0) expect(midpoint.dawnIntensity).toBeGreaterThan(0.5) expect(midpoint.dawnIntensity).toBeLessThan(1) expect(midpoint.coreReveal).toBeGreaterThan(0.5) expect(midpoint.orbitReveal).toBeGreaterThan(0) expect(midpoint.orbitReveal).toBeLessThan(0.5) const awakened = computeSplashMorphPhases(0.95, 0) expect(awakened.dawnIntensity).toBe(0) expect(awakened.coreReveal).toBe(1) expect(awakened.orbitReveal).toBe(1) expect(awakened.arcReveal).toBe(1) const exitScatter = computeSplashMorphPhases(1, 0.6) expect(exitScatter.dawnIntensity).toBe(0) expect(exitScatter.burst).toBeGreaterThan(0.45) expect(exitScatter.fade).toBeLessThan(0.8) expect(exitScatter.fade).toBeGreaterThan(0) }) test('unpackEarthLandMask reconstructs accurate continental and coastal pixels', () => { const grid = unpackEarthLandMask() expect(grid.length).toBe(LANDMASK_WIDTH * LANDMASK_HEIGHT) // Equirectangular projection helper matching 8k_earth_daymap sampling const toCell = (lat: number, lon: number): number => { const x = Math.min(LANDMASK_WIDTH - 1, Math.max(0, Math.floor(((lon + 180) / 360) * LANDMASK_WIDTH))) const y = Math.min(LANDMASK_HEIGHT - 1, Math.max(0, Math.floor(((90 - lat) / 180) * LANDMASK_HEIGHT))) return grid[y * LANDMASK_WIDTH + x] } // Hangzhou (30.27N, 120.15E) is land/coast; central China (34N, 104E) is interior/land; central Pacific (0N, 150W) is ocean expect(toCell(34.0, 104.0)).toBeGreaterThanOrEqual(1) expect(toCell(30.27, 120.15)).toBeGreaterThanOrEqual(1) expect(toCell(0.0, -150.0)).toBe(0) let coastCount = 0 for (let i = 0; i < grid.length; i += 1) { if (grid[i] === 2) coastCount += 1 } expect(coastCount).toBeGreaterThan(200) }) test('createWindowShowGate reveals only after both ready-to-show and splash first-paint when required', () => { let revealCount = 0 const immediateGate = createWindowShowGate({ requireSplashFirstPaint: false, onReveal: () => { revealCount += 1 }, }) immediateGate.markReadyToShow() expect(revealCount).toBe(1) expect(immediateGate.isRevealed()).toBe(true) // Order A: ready-to-show fires before splash first-paint let orderAReveals = 0 let scheduledCb: (() => void) | null = null let cleared = false const gateA = createWindowShowGate({ requireSplashFirstPaint: true, fallbackDelayMs: 1500, onReveal: () => { orderAReveals += 1 }, scheduleTimeout: (cb) => { scheduledCb = cb return 1 as unknown as ReturnType }, cancelTimeout: () => { cleared = true }, }) gateA.markReadyToShow() expect(orderAReveals).toBe(0) expect(gateA.isRevealed()).toBe(false) expect(scheduledCb).not.toBeNull() gateA.markSplashFirstPainted() expect(orderAReveals).toBe(1) expect(gateA.isRevealed()).toBe(true) expect(cleared).toBe(true) // Order B: splash first-paint fires before ready-to-show let orderBReveals = 0 const gateB = createWindowShowGate({ requireSplashFirstPaint: true, onReveal: () => { orderBReveals += 1 }, }) gateB.markSplashFirstPainted() expect(orderBReveals).toBe(0) gateB.markReadyToShow() expect(orderBReveals).toBe(1) // Fallback timer fires once if splash first-paint never arrives let fallbackReveals = 0 let fallbackCallback: (() => void) | null = null const gateFallback = createWindowShowGate({ requireSplashFirstPaint: true, fallbackDelayMs: 900, onReveal: () => { fallbackReveals += 1 }, scheduleTimeout: (cb) => { fallbackCallback = cb return 2 as unknown as ReturnType }, cancelTimeout: () => {}, }) gateFallback.markReadyToShow() expect(fallbackReveals).toBe(0) fallbackCallback?.() expect(fallbackReveals).toBe(1) gateFallback.markSplashFirstPainted() expect(fallbackReveals).toBe(1) // dispose() prevents delayed fallback reveal let disposedReveals = 0 let disposedTimerCleared = false const gateDisposed = createWindowShowGate({ requireSplashFirstPaint: true, fallbackDelayMs: 900, onReveal: () => { disposedReveals += 1 }, scheduleTimeout: () => 3 as unknown as ReturnType, cancelTimeout: () => { disposedTimerCleared = true }, }) gateDisposed.markReadyToShow() gateDisposed.dispose() expect(disposedTimerCleared).toBe(true) gateDisposed.markSplashFirstPainted() expect(disposedReveals).toBe(0) }) test('evaluateBootSplashState waits for earthStartupReady + post-ready frames and caps pre-ready progress', () => { const waitingLong = evaluateBootSplashState({ elapsedMs: 5000, minDurationMs: 1800, maxCapMs: 20000, startupReady: false, postReadyFrames: 0, startupProgress: 0.99, statusProgressHint: 1, }) expect(waitingLong.readyToExit).toBe(false) expect(waitingLong.targetProgress).toBeLessThanOrEqual(0.94) const needsTwoFrames = evaluateBootSplashState({ elapsedMs: 2500, minDurationMs: 1800, maxCapMs: 20000, startupReady: true, postReadyFrames: 1, startupProgress: 1, statusProgressHint: 0.78, }) expect(needsTwoFrames.readyToExit).toBe(false) expect(needsTwoFrames.targetProgress).toBeLessThanOrEqual(0.94) const waitsMinShowcase = evaluateBootSplashState({ elapsedMs: 1200, minDurationMs: 1800, maxCapMs: 20000, startupReady: true, postReadyFrames: 3, startupProgress: 1, statusProgressHint: 0.78, }) expect(waitsMinShowcase.readyToExit).toBe(false) expect(waitsMinShowcase.targetProgress).toBeLessThanOrEqual(0.94) const readyNow = evaluateBootSplashState({ elapsedMs: 2000, minDurationMs: 1800, maxCapMs: 20000, startupReady: true, postReadyFrames: 2, startupProgress: 1, statusProgressHint: 0.78, }) expect(readyNow).toEqual({ targetProgress: 1, readyToExit: true }) const capHit = evaluateBootSplashState({ elapsedMs: 20000, minDurationMs: 1800, maxCapMs: 20000, startupReady: false, postReadyFrames: 0, startupProgress: 0.2, statusProgressHint: 0, }) expect(capHit).toEqual({ targetProgress: 1, readyToExit: true }) }) test('splash exit choreography uses calm coherent optical dissolve without outward radial streaks', () => { const splashSrc = readFileSync(new URL('../src/preload/splash.ts', import.meta.url), 'utf8') expect(splashSrc).not.toContain('shardSpeed') expect(splashSrc).not.toContain('shardAngle') expect(splashSrc).not.toContain('radialBoost') expect(splashSrc).not.toContain('warp * 44') let prevFade = 1 for (const ratio of [0, 0.25, 0.5, 0.75, 1]) { const phase = computeSplashMorphPhases(1, ratio) expect(phase.fade).toBeLessThanOrEqual(prevFade + 1e-9) prevFade = phase.fade } expect(computeSplashMorphPhases(1, 1).fade).toBe(0) }) })