File size: 4,073 Bytes
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
import { fireEvent, screen, waitFor } from '@testing-library/react';
import { useState } from 'react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { jsonResponse, renderUi } from '../test/utils';
import { parseSse, type ChatAction } from './api';
import { ConfirmCard } from './ChatCards';

const action: ChatAction = {
  action_id: 'a-1', tool: 'approve_scenarios', status: 'proposed', verb: 'Approve 2 scenarios',
  title: 'I will approve 2 Requisition scenarios in test run Procure-to-Pay 路 26C. The other 1 waiting for review will be skipped for this patch.',
  items: [{ label: 'Requisition', value: 'Below threshold', id: 'P2P-APR-001', decision: 'approve' }, { label: 'Requisition', value: 'Rejected requisition', id: 'P2P-APR-002', decision: 'approve' },
    { label: 'Invoice / AP matching', value: 'Three-way match', id: 'P2P-INV-001', decision: 'skip' }],
  counts: { approve: 2, skip: 1 }, review: { label: 'Review them first', to: '/runs/r1/scenarios' },
  preview: { method: 'POST', path: '/api/runs/r1/gates/approve_cases', body: {} }, expires_at: '2026-10-11T09:57:00+00:00',
};

function Host({ initial }: { initial: ChatAction }) {
  const [a, setA] = useState(initial);
  return <ConfirmCard action={a} onChange={setA} />;
}

describe('chat', () => {
  afterEach(() => vi.restoreAllMocks());

  it('confirm card: Cancel has the default focus, the primary names verb and number, nothing runs before the click', async () => {
    const calls: string[] = [];
    vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => {
      const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
      calls.push(`${init?.method ?? 'GET'} ${url}`);
      return jsonResponse({ status: 'confirmed', result: { run_id: 'r1', status: 'queued', approved: 2, skipped: 1, by: 'consultant', at: '2026-10-11T09:52:00', to: '/runs/r1' } });
    });
    renderUi(<Host initial={action} />);
    expect(screen.getByRole('button', { name: 'Cancel' })).toHaveFocus();
    expect(screen.getByRole('button', { name: 'Approve 2 scenarios' })).toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Review them first' })).toHaveAttribute('href', '/runs/r1/scenarios');
    expect(screen.getByTestId('chat-confirm')).toHaveTextContent('2 to approve 路 1 skipped for this patch');
    fireEvent.click(screen.getByRole('button', { name: 'Show the list' }));           // "approve all" is wide: the card lists every scenario
    expect(screen.getByText(/Three-way match/)).toBeInTheDocument();
    expect(calls).toEqual([]);                                                         // no request until the user confirms
    fireEvent.click(screen.getByRole('button', { name: 'Approve 2 scenarios' }));
    await waitFor(() => expect(screen.getByTestId('chat-action-done')).toHaveTextContent('Done 路 approved 2, skipped 1 路 by consultant 路 09:52'));
    expect(calls).toEqual(['POST /api/chat/actions/a-1/confirm']);
    expect(screen.queryByRole('button', { name: 'Approve 2 scenarios' })).not.toBeInTheDocument();   // single use: the card collapses
  });

  it('confirm card: cancel and expiry change nothing and say so', async () => {
    vi.spyOn(globalThis, 'fetch').mockImplementation(async () => jsonResponse({ status: 'cancelled' }));
    renderUi(<Host initial={action} />);
    fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
    await waitFor(() => expect(screen.getByTestId('chat-action-closed')).toHaveTextContent('Cancelled. Nothing was changed.'));
  });

  it('parses server-sent events and keeps an incomplete frame for the next chunk', () => {
    const p = parseSse('event: intent\ndata: {"tool":"readiness","args":{}}\n\nevent: delta\ndata: {"text":"Procure-to-Pay: Not ready."}\n\nevent: done\ndata: {"mess');
    expect(p.events).toEqual([['intent', { tool: 'readiness', args: {} }], ['delta', { text: 'Procure-to-Pay: Not ready.' }]]);
    expect(p.rest).toBe('event: done\ndata: {"mess');
  });
});