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/); }); });