storebound/apps/desktop/tests/prefs.test.ts
CRT 7fab0a5b03 Desktop app shell (apps/desktop): Tauri 2 + React 19 around the storebound sidecar
- Rust host: spawns storebound serve --stdio with fixed args, hands over a per-session secret,
  mints confirm tokens only after a native dialog showing the summary and the dry run
- every app command re-checks window label and page origin; one capability (main window, app
  commands + event listen only, no plugin API in the webview); strict CSP; tests/security.rs pins it
- store keys picked in native dialogs, kept in the OS keychain, passed per call in memory only
- plugins: shell (Rust only), single-instance, deep-link storebound://, window-state, opener,
  dialog, notification, updater (placeholder endpoint and key until signing), process
- app-centric UI: Portfolio, App (Releases, Catalog, Listings, Health, Reviews, TestFlight),
  App folders, Accounts, Dependencies, Settings; light / dark with the storebound.dev tokens,
  reduced motion, home folder shown as ~
- macOS menu: Cmd+Q quits, Edit menu, Window menu keeps the main window; Linux DMABUF fix
- Bun workspace (npm files unchanged), ci runs desktop typecheck, build, cargo test and clippy;
  scrub covers home paths; docs/desktop.md (protocol, safety, dependency sources and licenses)
2026-10-10 17:57:33 +00:00

66 lines
3.6 KiB
TypeScript

import { describe, expect, test } from 'bun:test';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { shortPath, tildeText } from '../src/paths';
import { applyPrefs, DEFAULT_PREFS, parsePrefs } from '../src/prefs';
import { checkState, phaseText, trainText } from '../src/screens/Portfolio';
describe('prefs', () => {
test('parse is tolerant and only keeps known values', () => {
expect(parsePrefs(null)).toEqual(DEFAULT_PREFS);
expect(parsePrefs('{broken')).toEqual(DEFAULT_PREFS);
expect(parsePrefs('{"theme":"dark","motion":"reduce"}')).toEqual({ theme: 'dark', motion: 'reduce' });
expect(parsePrefs('{"theme":"neon","motion":"fast"}')).toEqual(DEFAULT_PREFS);
});
test('apply sets and clears data attributes', () => {
const root = { dataset: {} as DOMStringMap };
applyPrefs({ theme: 'light', motion: 'reduce' }, root);
expect(root.dataset).toEqual({ theme: 'light', motion: 'reduce' });
applyPrefs(DEFAULT_PREFS, root);
expect(root.dataset).toEqual({});
});
});
describe('paths', () => {
test('home shown as ~', () => {
expect(shortPath('/home/you/games/a', '/home/you')).toBe('~/games/a');
expect(shortPath('/home/you', '/home/you/')).toBe('~');
expect(shortPath('/home/you-2/a', '/home/you')).toBe('/home/you-2/a');
expect(shortPath('C:\\Users\\you\\a', 'C:\\Users\\you')).toBe('~\\a');
expect(shortPath('/x', null)).toBe('/x');
expect(tildeText('in /home/you/a and /home/you/b', '/home/you')).toBe('in ~/a and ~/b');
});
});
describe('portfolio', () => {
const base = { project: '/g', ok: false, summary: null, errors: [] as string[] };
test('check states', () => {
expect(checkState({ ...base, check: null })).toEqual({ text: 'not loaded', tone: 'bad' });
expect(checkState({ ...base, check: { ok: false, problems: ['a', 'b'], warnings: [] } })).toEqual({ text: '2 problems', tone: 'bad' });
expect(checkState({ ...base, check: { ok: true, problems: [], warnings: ['w'] } })).toEqual({ text: '1 warning', tone: 'warn' });
expect(checkState({ ...base, check: { ok: true, problems: [], warnings: [] } })).toEqual({ text: 'config ok', tone: 'ok' });
});
test('release phases and trains', () => {
const r = { store: 'android' as const, version: '2.3.0', build: '231', state: 'inProgress', phase: 'rollout' as const, fraction: 0.2, track: 'production' };
expect(phaseText(r)).toEqual({ text: 'rollout 20%', tone: 'local' });
expect(phaseText(null)).toEqual({ text: 'not on this store', tone: 'muted' });
expect(trainText(null, 'none')).toBe('not read');
expect(trainText(null, 'live')).toBe('none');
expect(trainText({ version: '2.3.0', ios: null, android: r }, 'live')).toBe('2.3.0');
});
});
describe('webview rules', () => {
const src = (f: string) => readFileSync(join(import.meta.dir, '..', f), 'utf8');
test('only app commands: no plugin JS APIs, no direct network', () => {
for (const f of ['src/api.ts', 'src/App.tsx', 'src/main.tsx', 'src/screens/Portfolio.tsx', 'src/screens/AppView.tsx', 'src/screens/AppFolders.tsx', 'src/screens/Accounts.tsx', 'src/screens/Settings.tsx', 'src/screens/Dependencies.tsx']) {
const s = src(f);
expect(s).not.toMatch(/@tauri-apps\/plugin-/);
expect(s).not.toMatch(/\bfetch\(|XMLHttpRequest|WebSocket|eval\(|new Function|dangerouslySetInnerHTML/);
}
expect(JSON.parse(src('package.json')).dependencies).toEqual({ '@tauri-apps/api': expect.any(String), react: expect.any(String), 'react-dom': expect.any(String) });
});
test('the confirm token never appears in the webview code', () => {
expect(src('src/api.ts')).not.toMatch(/confirmToken|credentials/);
});
});