patchwise-agent-studio / web /src /pages /setup /integrations.test.tsx
EnergyVenom's picture
Tidy the web app: pages by area, CSS in load order, web/README.md (b2a039d)
fb43301 verified
Raw History Blame Contribute Delete
9 kB
import { fireEvent, screen, waitFor, within } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { jsonResponse, renderUi } from '../../test/utils';
import { IntegrationsPage } from './IntegrationsPage';
const urlOf = (input: RequestInfo | URL) => (typeof input === 'string' ? input : input instanceof URL ? input.href : input.url);
const SECRET_URL = 'https://hooks.slack.com/services/T000/B000/secret-token-xyz';
const ok = { at: '2026-11-04T10:00:00+00:00', ok: true, status: 200, error: null };
const failed = { at: '2026-11-04T10:00:00+00:00', ok: false, status: 404, error: 'Not found' };
function backend(tokens: unknown[] = []) {
const calls: { method: string; url: string; body?: unknown }[] = [];
vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => {
const u = urlOf(input);
const method = (init?.method ?? 'GET').toUpperCase();
calls.push({ method, url: u, body: init?.body ? JSON.parse(String(init.body)) : undefined });
if (u.endsWith('/api/notifications/settings')) {
return jsonResponse({ webhook_configured: false, webhook_host: null, signing: false, pending: 0, last_delivery: null,
telegram: { token_set: true, chat: { chat_id: '1', title: 'me' }, last_delivery: ok } });
}
if (u.endsWith('/api/integrations')) {
return jsonResponse({
slack: { configured: false, host: null, last_delivery: null },
teams: { configured: true, host: 'prod.workflows', last_delivery: ok },
google_chat: { configured: true, host: 'chat.googleapis.com', last_delivery: failed },
discord: { configured: false, host: null, last_delivery: null },
});
}
if (u.includes('/api/channels/telegram/status')) return jsonResponse({ token_set: true, bot_username: 'PatchwiseBot', polling: true, last_update_at: null, linked_users: 1 });
if (u.includes('/api/pods')) return jsonResponse([]);
if (u.endsWith('/api/tokens')) return jsonResponse({ items: tokens });
if (method === 'PUT' || method === 'DELETE') return new Response(null, { status: 204 });
return jsonResponse({});
});
return calls;
}
const status = async (id: string) => (await screen.findByTestId(`integration-${id}-status`)).textContent;
describe('Integrations page', () => {
afterEach(() => vi.restoreAllMocks());
it('groups the integrations in four sections, each with its status in words', async () => {
backend();
renderUi(<IntegrationsPage />);
for (const h of ['Messaging', 'AI assistants', 'Your own agents', 'Tools']) expect(screen.getByRole('heading', { level: 2, name: h })).toBeInTheDocument();
await waitFor(async () => expect(await status('teams')).toBe('Connected'));
expect(await status('telegram')).toBe('Connected');
expect(await status('google_chat')).toBe('Needs attention');
expect(await status('slack')).toBe('Not set up');
expect(await status('discord')).toBe('Not set up');
expect(await status('webhook')).toBe('Not set up');
for (const p of ['copilot', 'claude', 'github', 'cursor']) expect(await status(p)).toBe('Available');
expect(await status('chatgpt')).toBe('Coming next');
for (const p of ['jira', 'servicenow']) expect(await status(p)).toBe('Planned');
});
it('saves a Slack webhook URL and never shows it back', async () => {
const calls = backend();
renderUi(<IntegrationsPage />);
fireEvent.click(await screen.findByRole('button', { name: 'Set up: Slack' }));
const input = await screen.findByLabelText('Webhook URL');
fireEvent.change(input, { target: { value: SECRET_URL } });
fireEvent.click(screen.getByRole('button', { name: 'Save' }));
await waitFor(() => expect(calls.some((c) => c.method === 'PUT' && c.url.endsWith('/api/integrations/slack'))).toBe(true));
expect(calls.find((c) => c.method === 'PUT')?.body).toEqual({ webhook_url: SECRET_URL });
await waitFor(() => expect((screen.getByLabelText('Webhook URL') as HTMLInputElement).value).toBe(''));
expect(document.body.textContent).not.toContain('secret-token-xyz');
});
it('disconnects Microsoft Teams only after confirming', async () => {
const calls = backend();
renderUi(<IntegrationsPage />);
fireEvent.click(await screen.findByRole('button', { name: 'Manage: Microsoft Teams' }));
fireEvent.click(await screen.findByRole('button', { name: 'Disconnect' }));
expect(calls.some((c) => c.method === 'DELETE')).toBe(false);
fireEvent.click(within(screen.getByRole('group', { name: 'Disconnect Microsoft Teams?' })).getByRole('button', { name: 'Yes, disconnect' }));
await waitFor(() => expect(calls.some((c) => c.method === 'DELETE' && c.url.endsWith('/api/integrations/teams'))).toBe(true));
});
it('a planned integration explains what it will do and cannot be connected yet', async () => {
backend();
renderUi(<IntegrationsPage />);
fireEvent.click(await screen.findByRole('button', { name: 'Learn more: ServiceNow' }));
const drawer = await screen.findByTestId('planned-setup');
expect(drawer).toHaveTextContent('change request');
expect(drawer).toHaveTextContent('least rights');
expect(within(drawer).getByRole('button', { name: 'Connect' })).toBeDisabled();
expect(drawer).toHaveTextContent('Coming next');
});
it('an assistant is Connected once one of your active tokens was used in the last 30 days', async () => {
const recent = new Date(Date.now() - 2 * 24 * 3600 * 1000).toISOString();
const future = new Date(Date.now() + 30 * 24 * 3600 * 1000).toISOString();
backend([{ id: 1, name: 'Claude', scopes: 'read', last4: 'ab12', created_at: recent, last_used_at: recent, expires_at: future, revoked_at: null }]);
renderUi(<IntegrationsPage />);
await waitFor(async () => expect(await status('claude')).toBe('Connected'));
expect(await status('chatgpt')).toBe('Coming next');
});
it('an assistant drawer gives the /mcp address to copy, a token link and settings without any real token', async () => {
backend();
renderUi(<IntegrationsPage />);
fireEvent.click(await screen.findByRole('button', { name: 'Set up: Claude' }));
const drawer = await screen.findByTestId('assistant-setup-claude');
expect(within(drawer).getByTestId('mcp-url')).toHaveTextContent(/\/mcp$/);
expect(within(drawer).getByRole('button', { name: 'Copy the server address' })).toBeInTheDocument();
expect(within(drawer).getByRole('button', { name: 'Copy the Claude Code command' })).toBeInTheDocument();
expect(within(drawer).getByRole('link', { name: 'Create a token' })).toHaveAttribute('href', '/account#tokens');
expect(drawer).toHaveTextContent('$PATCHWISE_TOKEN');
expect(drawer).toHaveTextContent('can act, each step confirmed by you');
});
it('ChatGPT explains that its web connector needs OAuth, which is coming next', async () => {
backend();
renderUi(<IntegrationsPage />);
fireEvent.click(await screen.findByRole('button', { name: 'Learn more: ChatGPT' }));
expect(await screen.findByTestId('needs-oauth')).toHaveTextContent('coming next');
expect(screen.queryByTestId('mcp-url')).not.toBeInTheDocument();
});
it('A2A: your own agent gets the Agent Card and endpoint to copy, the skills, the approval rule and an example without any token', async () => {
backend();
renderUi(<IntegrationsPage />);
expect(await status('a2a')).toBe('Available');
fireEvent.click(await screen.findByRole('button', { name: 'Set up: Agent2Agent (A2A)' }));
const drawer = await screen.findByTestId('a2a-setup');
expect(within(drawer).getByTestId('a2a-card-url')).toHaveTextContent(/\/\.well-known\/agent-card\.json$/);
expect(within(drawer).getByTestId('a2a-endpoint')).toHaveTextContent(/\/a2a$/);
expect(within(drawer).getByRole('button', { name: 'Copy the Agent Card address' })).toBeInTheDocument();
expect(within(drawer).getByRole('button', { name: 'Copy the A2A endpoint' })).toBeInTheDocument();
expect(within(drawer).getByRole('button', { name: 'Copy the A2A example' })).toBeInTheDocument();
expect(within(drawer).getByRole('link', { name: 'Create a token' })).toHaveAttribute('href', '/account#tokens');
for (const skill of ['assess-readiness', 'design-tests', 'test-these-cases', 'run-tests', 'get-report']) expect(drawer).toHaveTextContent(skill);
expect(within(drawer).getByTestId('a2a-rule')).toHaveTextContent('A person always approves');
// the example reads the token from the environment; no token-like literal anywhere in the drawer
expect(drawer).toHaveTextContent('os.environ["PATCHWISE_TOKEN"]');
expect(drawer.textContent ?? '').not.toMatch(/(api[_-]?key|secret|password|token)\s*[:=]\s*['"][A-Za-z0-9_-]{16,}['"]/i);
expect(drawer.textContent ?? '').not.toMatch(/pwt_[A-Za-z0-9]/);
});
});