|
| 1 | +/** |
| 2 | + * Approval dialog wiring: |
| 3 | + * - renders the target-Space chip (so a cross-Space write is obvious), and |
| 4 | + * - survives a freeform `diff` shape (`{ added_tools }`) without crashing — |
| 5 | + * a regression guard for the earlier "Cannot read properties of undefined |
| 6 | + * (reading 'length')" bug. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 10 | +import { render, screen, act } from '@testing-library/react'; |
| 11 | + |
| 12 | +const { handlers } = vi.hoisted(() => ({ |
| 13 | + handlers: new Map<string, (e: { payload: unknown }) => void>(), |
| 14 | +})); |
| 15 | + |
| 16 | +// Capture the dialog's `listen('meta-tool-approval-request', cb)` so the test |
| 17 | +// can deliver synthetic requests. |
| 18 | +vi.mock('@tauri-apps/api/event', () => ({ |
| 19 | + listen: vi.fn((name: string, cb: (e: { payload: unknown }) => void) => { |
| 20 | + handlers.set(name, cb); |
| 21 | + return Promise.resolve(() => handlers.delete(name)); |
| 22 | + }), |
| 23 | +})); |
| 24 | +vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn().mockResolvedValue(undefined) })); |
| 25 | + |
| 26 | +import { MetaToolApprovalDialog } from '@/features/metaTools/MetaToolApprovalDialog'; |
| 27 | + |
| 28 | +async function emitRequest(payload: Record<string, unknown>) { |
| 29 | + await act(async () => { |
| 30 | + handlers.get('meta-tool-approval-request')?.({ |
| 31 | + payload: { |
| 32 | + request_id: 'req-1', |
| 33 | + client_id: 'client-1', |
| 34 | + payload, |
| 35 | + expires_at_unix_secs: 9_999_999_999, |
| 36 | + }, |
| 37 | + }); |
| 38 | + await Promise.resolve(); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +describe('MetaToolApprovalDialog', () => { |
| 43 | + beforeEach(() => handlers.clear()); |
| 44 | + |
| 45 | + it('names the target Space when present', async () => { |
| 46 | + render(<MetaToolApprovalDialog />); |
| 47 | + await emitRequest({ |
| 48 | + tool_name: 'mcpmux_manage_feature_set', |
| 49 | + summary: "Create FeatureSet 'X' in Space 'Personal'", |
| 50 | + space_name: 'Personal', |
| 51 | + diff: null, |
| 52 | + raw_args: {}, |
| 53 | + affects_other_clients: false, |
| 54 | + }); |
| 55 | + const chip = await screen.findByTestId('meta-tool-approval-space'); |
| 56 | + expect(chip).toHaveTextContent('Personal'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('omits the Space chip when no target Space is given', async () => { |
| 60 | + render(<MetaToolApprovalDialog />); |
| 61 | + await emitRequest({ |
| 62 | + tool_name: 'mcpmux_manage_feature_set', |
| 63 | + summary: 'Create FeatureSet', |
| 64 | + diff: null, |
| 65 | + raw_args: {}, |
| 66 | + affects_other_clients: false, |
| 67 | + }); |
| 68 | + expect(screen.getByTestId('meta-tool-approval-dialog')).toBeInTheDocument(); |
| 69 | + expect(screen.queryByTestId('meta-tool-approval-space')).toBeNull(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('renders a freeform { added_tools } diff without crashing', async () => { |
| 73 | + render(<MetaToolApprovalDialog />); |
| 74 | + await emitRequest({ |
| 75 | + tool_name: 'mcpmux_manage_feature_set', |
| 76 | + summary: 'Create FeatureSet', |
| 77 | + diff: { added_tools: ['github_create_issue', 'slack_send'] }, |
| 78 | + raw_args: {}, |
| 79 | + affects_other_clients: false, |
| 80 | + }); |
| 81 | + expect(screen.getByTestId('meta-tool-approval-dialog')).toBeInTheDocument(); |
| 82 | + expect(screen.getByText(/github_create_issue/)).toBeInTheDocument(); |
| 83 | + expect(screen.getByText(/slack_send/)).toBeInTheDocument(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments