patchwise-agent-studio / web /src /chat /chat.test.tsx
EnergyVenom's picture
Patchwise Agent Studio: Oracle AI Agent Studio cycles
af1bee9 verified
Raw History Blame Contribute Delete
4.07 kB
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');
});
});