File size: 5,029 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
68
69
70
71
72
73
74
75
76
77
78
79
80
import { describe, expect, it } from 'vitest';
import type { LiveStatus } from '../../api/types';
import { liveOverlay, RELOAD_AFTER_MS, type LiveCtx } from './LiveView';

const live = (over: Partial<LiveStatus> = {}): LiveStatus => ({
  run_id: 'r1', available: true, reason: null, active: true, stale: false, attempt: 0, action: null, action_n: 0, masked: false, chunk: null, chunks: null,
  scenario_id: 'P2P-REQ-001', scenario_title: 'Create a requisition', page_title: null, path: null, status: 'running', frame_ts: '2026-10-24T10:00:00+00:00',
  progress: { done: 0, total: 5 }, area: null, step: null, role: null, results: { passed: 0, failed: 0, recent: [] }, up_next: [], failed: null, ...over,
}) as LiveStatus;
const ctx = (over: Partial<LiveCtx> = {}): LiveCtx => ({ status: 'running', gate: null, canWatch: true, quick: false, everActive: true, slow: false, frozen: false, reconnectingFor: 0, frameAgeS: 2, ...over });

describe('the live browser states (liveOverlay)', () => {
  it('live: a clean picture with a LIVE badge, pause and fullscreen', () => {
    const o = liveOverlay(live(), ctx());
    expect(o.kind).toBe('live');
    expect(o.title).toBe('');
    expect(o.badge).toEqual({ label: 'LIVE', tone: 'red' });
    expect(o.controls).toEqual({ pause: true, jump: false, fullscreen: true, reload: false });
  });

  it('starting: the browser was asked to open and no picture has arrived; everything is disabled; a slow start says why', () => {
    const o = liveOverlay(live({ frame_ts: null }), ctx());
    expect(o.kind).toBe('starting');
    expect(o.title).toBe('Starting the browser…');
    expect(o.badge?.label).toBe('STARTING');
    expect(o.controls).toEqual({ pause: false, jump: false, fullscreen: false, reload: false });
    expect(liveOverlay(live({ frame_ts: null }), ctx({ slow: true })).text).toMatch(/taking longer than usual to show a picture/);
  });

  it('hidden sign-in: passwords are never shown, only fullscreen is offered', () => {
    const o = liveOverlay(live({ masked: true }), ctx());
    expect(o.kind).toBe('masked');
    expect(o.text).toBe('Sign-in screen hidden. Passwords are never shown here.');
    expect(o.badge?.label).toBe('LIVE');
    expect(o.controls).toEqual({ pause: false, jump: false, fullscreen: true, reload: false });
  });

  it('reconnecting: orange badge, the last picture age, and a reload button only after 30 seconds', () => {
    const early = liveOverlay(live({ stale: true }), ctx({ frameAgeS: 8, reconnectingFor: 5_000 }));
    expect(early.kind).toBe('reconnecting');
    expect(early.badge).toEqual({ label: 'RECONNECTING', tone: 'orange' });
    expect(early.text).toContain('(8 s ago)');
    expect(early.controls.reload).toBe(false);
    expect(liveOverlay(live({ stale: true }), ctx({ reconnectingFor: RELOAD_AFTER_MS })).controls.reload).toBe(true);
  });

  it('paused for your review: waiting on a gate says which decision it is waiting for', () => {
    const a = liveOverlay(live({ active: false }), ctx({ status: 'waiting_approval', gate: 'approve_cases' }));
    expect(a.kind).toBe('paused');
    expect(a.text).toContain('approve the scenarios');
    expect(a.badge?.label).toBe('PAUSED');
    expect(liveOverlay(live({ active: false }), ctx({ status: 'waiting_review', gate: 'human_review' })).text).toContain('decide the failure');
  });

  it('you paused the view: the picture is frozen, the test keeps running, Jump to live is offered', () => {
    const o = liveOverlay(live(), ctx({ frozen: true }));
    expect(o.kind).toBe('frozen');
    expect(o.text).toBe('View paused. The test is still running.');
    expect(o.controls.jump).toBe(true);
  });

  it('preparing the next batch, then ended', () => {
    const idle = live({ active: false });
    expect(liveOverlay(idle, ctx({ everActive: true })).text).toBe('Preparing the next batch of scenarios…');
    expect(liveOverlay(idle, ctx({ everActive: false })).text).toMatch(/browser opens when the scenarios start/);
    const done = liveOverlay(idle, ctx({ status: 'completed' }));
    expect(done.kind).toBe('ended');
    expect(done.badge?.label).toBe('ENDED');
    expect(done.title).toBe('Testing has finished');
  });

  it('unavailable, quick simulation and no permission replace the picture with words and have no controls', () => {
    const un = liveOverlay(live({ available: false, active: false, reason: 'Live view is switched off for this installation.' }), ctx());
    expect(un).toMatchObject({ kind: 'unavailable', replaces: true, badge: null, text: 'Live view is switched off for this installation.' });
    expect(liveOverlay(undefined, ctx({ quick: true })).kind).toBe('quick');
    expect(liveOverlay(live(), ctx({ canWatch: false })).text).toMatch(/consultant, QA approver or admin role/);
    for (const o of [un, liveOverlay(undefined, ctx({ quick: true }))]) expect(o.controls).toEqual({ pause: false, jump: false, fullscreen: false, reload: false });
  });
});