|
1 | 1 | import { render, screen, act, fireEvent } from '@testing-library/react' |
2 | | -import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 2 | +import { describe, it, expect } from 'vitest' |
3 | 3 | import App from './App' |
| 4 | +import { createTestEmbedder } from './host/embeddedHost' |
| 5 | +import type { DocApprovalOutput } from './client/traverseOutput' |
4 | 6 |
|
5 | | -describe('App', () => { |
6 | | - beforeEach(() => vi.useFakeTimers()) |
7 | | - afterEach(() => { vi.useRealTimers(); vi.restoreAllMocks() }) |
| 7 | +const sampleOutput: DocApprovalOutput = { |
| 8 | + docType: 'invoice', |
| 9 | + parties: ['Acme Corp'], |
| 10 | + amounts: ['$500.00'], |
| 11 | + confidence: 0.88, |
| 12 | + recommendation: 'approve', |
| 13 | +} |
8 | 14 |
|
9 | | - it('renders UI shell', async () => { |
10 | | - vi.spyOn(globalThis, 'fetch').mockResolvedValue({ ok: true, json: () => Promise.resolve({}) } as Response) |
11 | | - await act(async () => { render(<App />) }) |
| 15 | +describe('App', () => { |
| 16 | + it('renders UI shell with Embedded zone 1', () => { |
| 17 | + render(<App embedder={createTestEmbedder(sampleOutput)} />) |
12 | 18 | expect(screen.getByText('Doc Approval')).toBeInTheDocument() |
| 19 | + expect(screen.getByText('Embedded')).toBeInTheDocument() |
| 20 | + expect(screen.getByText('Ready')).toBeInTheDocument() |
13 | 21 | expect(screen.getByRole('heading', { name: 'Submit Document', level: 2 })).toBeInTheDocument() |
14 | | - expect(screen.getByRole('button', { name: 'Analyze Document' })).toBeInTheDocument() |
15 | 22 | }) |
16 | 23 |
|
17 | | - it('shows Offline when health check fails', async () => { |
18 | | - vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('Network error')) |
19 | | - render(<App />) |
20 | | - await act(async () => { await vi.runOnlyPendingTimersAsync() }) |
21 | | - expect(screen.getByText('Offline')).toBeInTheDocument() |
| 24 | + it('shows Unavailable when embedder is null', () => { |
| 25 | + render(<App embedder={null} />) |
| 26 | + expect(screen.getByText('Unavailable')).toBeInTheDocument() |
22 | 27 | expect(screen.getByRole('button', { name: 'Analyze Document' })).toBeDisabled() |
23 | | - expect(screen.getByText(/The runtime is offline/)).toBeInTheDocument() |
24 | | - }) |
25 | | - |
26 | | - it('shows Online when health check succeeds', async () => { |
27 | | - vi.spyOn(globalThis, 'fetch').mockResolvedValue({ ok: true, json: () => Promise.resolve({}) } as Response) |
28 | | - render(<App />) |
29 | | - await act(async () => { await vi.runOnlyPendingTimersAsync() }) |
30 | | - expect(screen.getByText('Online')).toBeInTheDocument() |
| 28 | + expect(screen.getByText(/doc-approval manifests require issue #112/)).toBeInTheDocument() |
31 | 29 | }) |
32 | 30 |
|
33 | | - it('submits document and displays analysis fields', async () => { |
34 | | - const output = { |
35 | | - docType: 'invoice', |
36 | | - parties: ['Acme Corp'], |
37 | | - amounts: ['$500.00'], |
38 | | - confidence: 0.88, |
39 | | - recommendation: 'approve', |
40 | | - } |
41 | | - vi.spyOn(globalThis, 'fetch').mockImplementation((url, init) => { |
42 | | - const path = String(url) |
43 | | - if (path.includes('/healthz')) { |
44 | | - return Promise.resolve({ ok: true } as Response) |
45 | | - } |
46 | | - if (path.includes('/execute') && init?.method === 'POST') { |
47 | | - return Promise.resolve({ |
48 | | - ok: true, |
49 | | - json: () => Promise.resolve({ execution_id: 'exec-1' }), |
50 | | - } as Response) |
51 | | - } |
52 | | - if (path.includes('/executions/exec-1')) { |
53 | | - return Promise.resolve({ |
54 | | - ok: true, |
55 | | - json: () => Promise.resolve({ execution_id: 'exec-1', status: 'succeeded', output }), |
56 | | - } as Response) |
57 | | - } |
58 | | - if (path.includes('/traces/exec-1')) { |
59 | | - return Promise.resolve({ ok: true, json: () => Promise.resolve([]) } as Response) |
60 | | - } |
61 | | - return Promise.resolve({ ok: false, status: 404 } as Response) |
| 31 | + it('submits document via test double and displays analysis fields', async () => { |
| 32 | + render(<App embedder={createTestEmbedder(sampleOutput)} />) |
| 33 | + fireEvent.change(screen.getByPlaceholderText(/Paste or type document/i), { |
| 34 | + target: { value: 'Invoice #123' }, |
| 35 | + }) |
| 36 | + await act(async () => { |
| 37 | + fireEvent.click(screen.getByRole('button', { name: 'Analyze Document' })) |
62 | 38 | }) |
63 | | - |
64 | | - render(<App />) |
65 | | - await act(async () => { await vi.runOnlyPendingTimersAsync() }) |
66 | | - |
67 | | - fireEvent.change(screen.getByPlaceholderText(/Paste or type document/i), { target: { value: 'Invoice #123' } }) |
68 | | - fireEvent.click(screen.getByRole('button', { name: 'Analyze Document' })) |
69 | | - |
70 | | - await act(async () => { await Promise.resolve() }) |
71 | | - await act(async () => { await vi.advanceTimersByTimeAsync(1000) }) |
72 | | - |
73 | 39 | expect(screen.getByText('Document Type')).toBeInTheDocument() |
74 | 40 | expect(screen.getByText('invoice')).toBeInTheDocument() |
75 | 41 | expect(screen.getByText('88%')).toBeInTheDocument() |
76 | 42 | }) |
77 | 43 |
|
78 | 44 | it('reset returns to idle', async () => { |
79 | | - const output = { |
80 | | - docType: 'invoice', |
81 | | - parties: [], |
82 | | - amounts: [], |
83 | | - confidence: 0.5, |
84 | | - recommendation: 'review', |
85 | | - } |
86 | | - vi.spyOn(globalThis, 'fetch').mockImplementation((url) => { |
87 | | - const path = String(url) |
88 | | - if (path.includes('/healthz')) return Promise.resolve({ ok: true } as Response) |
89 | | - if (path.includes('/execute')) { |
90 | | - return Promise.resolve({ ok: true, json: () => Promise.resolve({ execution_id: 'exec-1' }) } as Response) |
91 | | - } |
92 | | - if (path.includes('/executions/exec-1')) { |
93 | | - return Promise.resolve({ |
94 | | - ok: true, |
95 | | - json: () => Promise.resolve({ execution_id: 'exec-1', status: 'succeeded', output }), |
96 | | - } as Response) |
97 | | - } |
98 | | - if (path.includes('/traces/exec-1')) { |
99 | | - return Promise.resolve({ ok: true, json: () => Promise.resolve([]) } as Response) |
100 | | - } |
101 | | - return Promise.resolve({ ok: false, status: 404 } as Response) |
| 45 | + render(<App embedder={createTestEmbedder(sampleOutput)} />) |
| 46 | + fireEvent.change(screen.getByPlaceholderText(/Paste or type document/i), { |
| 47 | + target: { value: 'doc' }, |
| 48 | + }) |
| 49 | + await act(async () => { |
| 50 | + fireEvent.click(screen.getByRole('button', { name: 'Analyze Document' })) |
102 | 51 | }) |
103 | | - |
104 | | - render(<App />) |
105 | | - await act(async () => { await vi.runOnlyPendingTimersAsync() }) |
106 | | - fireEvent.change(screen.getByPlaceholderText(/Paste or type document/i), { target: { value: 'doc' } }) |
107 | | - fireEvent.click(screen.getByRole('button', { name: 'Analyze Document' })) |
108 | | - await act(async () => { await Promise.resolve() }) |
109 | | - await act(async () => { await vi.advanceTimersByTimeAsync(1000) }) |
110 | | - |
111 | 52 | fireEvent.click(screen.getByRole('button', { name: 'Reset' })) |
112 | 53 | expect(screen.getByText(/Submit a document above/)).toBeInTheDocument() |
113 | 54 | }) |
|
0 commit comments