File size: 3,825 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb43301
af1bee9
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { afterEach, describe, expect, it } from 'vitest';
import type { Schemas } from '../../api/client';
import { groupRuns } from './AppSidebar';
import { DEFAULT_PREFS, readPrefs } from './prefs';
import { runState } from './StatusDot';

type RunSummary = Schemas['RunSummary'];
const run = (id: string, status: string, extra: Partial<RunSummary> = {}): RunSummary => ({
  run_id: id, status, created_at: `2026-10-17T10:${id.padStart(2, '0')}:00`, modules: ['P2P'], decisions: {}, orchestrator: 'langgraph',
  playwright_mode: 'stub', trigger: 'api', ...extra,
} as RunSummary);

describe('shell', () => {
  afterEach(() => window.localStorage.clear());

  it('maps every run status onto one dot state', () => {
    expect(runState(run('1', 'queued'))).toBe('queued');
    expect(runState(run('1', 'running'))).toBe('running');
    expect(runState(run('1', 'waiting_approval', { pending_gate: 'approve_cases' }))).toBe('waiting');
    expect(runState(run('1', 'waiting_review', { pending_gate: 'human_review' }))).toBe('waiting');
    expect(runState(run('1', 'completed', { decisions: { P2P: 'GO' } }))).toBe('done-ready');
    expect(runState(run('1', 'completed', { decisions: { P2P: 'GO', O2C: 'NO-GO' } }))).toBe('done-notready');
    expect(runState(run('1', 'completed', { decision: 'HELD' }))).toBe('done-held');
    expect(runState(run('1', 'completed'))).toBe('done');
    expect(runState(run('1', 'failed'))).toBe('failed');
    expect(runState(run('1', 'killed'))).toBe('failed');
    expect(runState(run('1', 'cancelled'))).toBe('stopped');
    expect(runState(run('1', 'interrupted'))).toBe('paused');
  });

  it('groups sidebar runs: all that need you, then running, then recent, at most 12', () => {
    const items = [
      ...Array.from({ length: 10 }, (_, i) => run(String(i + 10), 'completed')),
      run('1', 'running'), run('2', 'waiting_approval', { pending_gate: 'approve_cases' }), run('3', 'queued'),
      run('4', 'waiting_review', { pending_gate: 'human_review' }),
    ];
    const g = groupRuns(items, 12);
    expect(g.map((x) => x.title)).toEqual(['Waiting for you', 'Running', 'Recent']);
    expect(g[0].runs.map((r) => r.run_id)).toEqual(['2', '4']);
    expect(g[1].runs.map((r) => r.run_id)).toEqual(['1', '3']);
    expect(g.flatMap((x) => x.runs)).toHaveLength(12);
    expect(g[2].runs[0].run_id).toBe('19');   // newest first
    expect(groupRuns([run('5', 'completed')]).map((x) => x.title)).toEqual(['Recent']);   // empty groups are hidden
  });

  it('reads per-user prefs safely, with defaults for missing or broken values', () => {
    expect(readPrefs('alice')).toEqual(DEFAULT_PREFS);
    window.localStorage.setItem('fpt.ui.alice', '{not json');
    expect(readPrefs('alice')).toEqual(DEFAULT_PREFS);
    window.localStorage.setItem('fpt.ui.alice', JSON.stringify({ sidebar: 'rail', panel: { width: 520, tab: 'nonsense' }, env: 3 }));
    expect(readPrefs('alice')).toEqual({ sidebar: 'rail', panel: { open: true, width: 520, tab: 'live' }, env: 3 });
    expect(readPrefs('bob')).toEqual(DEFAULT_PREFS);
  });
});

describe('run stage card', () => {
  it('is the one guide element, with a stage kicker and a progress line in words', async () => {
    const { renderUi } = await import('../../test/utils');
    const { StageCard } = await import('../../pages/run/RunStage');
    const { screen } = await import('@testing-library/react');
    renderUi(<StageCard stage="judge"><p>body</p></StageCard>);
    expect(screen.getByTestId('run-guide')).toHaveAttribute('data-stage', 'judge');
    expect(screen.getByText('Step 3 of 4: your decision')).toBeInTheDocument();
    expect(screen.getByRole('img', { name: 'Progress of this test run: stage 3 of 4, Your decision' })).toBeInTheDocument();
  });
});