|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * E2E smoke for the Universal Assistant wiring (Phase E). |
| 5 | + * |
| 6 | + * Verifies that: |
| 7 | + * 1. The Studio page loads and bootstraps MSW + the in-browser ObjectKernel. |
| 8 | + * 2. The new ambient-assistant routes (`/api/v1/ai/assistant`, |
| 9 | + * `/api/v1/ai/assistant/skills`) are registered and respond with the |
| 10 | + * contract the new AiChatPanel relies on. |
| 11 | + * 3. The AiChatPanel module that the dev server actually serves contains the |
| 12 | + * Universal Assistant exports (no stale Agent-dropdown code). |
| 13 | + * |
| 14 | + * We intentionally do NOT drive the React UI through the auth flow — Studio |
| 15 | + * delegates login to a separate Account SPA, which makes pure-Studio E2E |
| 16 | + * coverage of the panel's visible state out of scope. The UI behaviour itself |
| 17 | + * is covered by the vitest unit tests in `test/ai-chat-panel.test.tsx`. |
| 18 | + */ |
| 19 | + |
| 20 | +import { test, expect } from '@playwright/test'; |
| 21 | + |
| 22 | +const STUDIO_PATH = '/?mode=msw'; |
| 23 | + |
| 24 | +async function waitForKernel(page: import('@playwright/test').Page) { |
| 25 | + await page.goto(STUDIO_PATH, { waitUntil: 'networkidle' }); |
| 26 | + // Wait for the in-browser kernel to log "Service started" — proves the |
| 27 | + // ObjectStack kernel + AI plugin finished bootstrapping inside the page. |
| 28 | + await page.waitForFunction( |
| 29 | + () => { |
| 30 | + const w = window as unknown as { __aiReady?: boolean }; |
| 31 | + return w.__aiReady === true; |
| 32 | + }, |
| 33 | + null, |
| 34 | + { timeout: 30_000 }, |
| 35 | + ).catch(() => { |
| 36 | + /* fall back to a fixed timeout if the marker hook isn't installed */ |
| 37 | + }); |
| 38 | + // Allow any post-init redirects (login redirect etc.) to settle. |
| 39 | + await page.waitForLoadState('networkidle').catch(() => {}); |
| 40 | + await page.waitForTimeout(500); |
| 41 | +} |
| 42 | + |
| 43 | +test.describe('Universal Assistant — server contract (in-browser kernel)', () => { |
| 44 | + test('GET /api/v1/ai/assistant returns { agent, skills, context }', async ({ page }) => { |
| 45 | + const consoleLogs: string[] = []; |
| 46 | + page.on('console', msg => { |
| 47 | + consoleLogs.push(`${msg.type()}: ${msg.text()}`); |
| 48 | + }); |
| 49 | + |
| 50 | + await waitForKernel(page); |
| 51 | + |
| 52 | + const result = await page.evaluate(async () => { |
| 53 | + const res = await fetch( |
| 54 | + '/api/v1/ai/assistant?appName=studio&objectName=view', |
| 55 | + { credentials: 'include' }, |
| 56 | + ); |
| 57 | + return { status: res.status, body: await res.json().catch(() => null) }; |
| 58 | + }); |
| 59 | + |
| 60 | + // Also try the older /api/v1/ai/agents to confirm AI routes are wired at all |
| 61 | + const agentsCheck = await page.evaluate(async () => { |
| 62 | + const res = await fetch('/api/v1/ai/agents', { credentials: 'include' }); |
| 63 | + return { status: res.status }; |
| 64 | + }); |
| 65 | + const aiChatCheck = await page.evaluate(async () => { |
| 66 | + const res = await fetch('/api/v1/ai/chat', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}', credentials: 'include' }); |
| 67 | + return { status: res.status }; |
| 68 | + }); |
| 69 | + const dataCheck = await page.evaluate(async () => { |
| 70 | + const res = await fetch('/api/v1/data/sys_user', { credentials: 'include' }); |
| 71 | + return { status: res.status }; |
| 72 | + }); |
| 73 | + const wellKnown = await page.evaluate(async () => { |
| 74 | + const res = await fetch('/.well-known/objectstack', { credentials: 'include' }); |
| 75 | + return { status: res.status, body: await res.text().catch(() => '') }; |
| 76 | + }); |
| 77 | + |
| 78 | + console.log('--- ALL page console logs ---'); |
| 79 | + consoleLogs.filter(l => /\[AI\]|\[MSW\]|\[KernelFactory\]|\[Console\]|service.*started|routes registered/i.test(l)).forEach(l => console.log(l)); |
| 80 | + const kernelDiag = await page.evaluate(async () => { |
| 81 | + const w: any = window; |
| 82 | + const k = w.__objectStackKernel || w.kernel || null; |
| 83 | + if (!k) return { hasKernel: false, keys: Object.keys(w).filter(x => /kernel|stack/i.test(x)) }; |
| 84 | + return { |
| 85 | + hasKernel: true, |
| 86 | + services: Array.from(k.services?.keys?.() ?? []), |
| 87 | + hasAiRoutes: Array.isArray(k.__aiRoutes), |
| 88 | + aiRoutesCount: k.__aiRoutes?.length ?? 0, |
| 89 | + aiRoutesPaths: (k.__aiRoutes ?? []).map((r: any) => `${r.method} ${r.path}`).slice(0, 20), |
| 90 | + }; |
| 91 | + }); |
| 92 | + console.log('--- kernel diag:', JSON.stringify(kernelDiag, null, 2)); |
| 93 | + console.log('--- /api/v1/ai/chat check:', aiChatCheck); |
| 94 | + console.log('--- /api/v1/data/sys_user check:', dataCheck); |
| 95 | + console.log('--- /.well-known/objectstack check:', wellKnown); |
| 96 | + console.log('--- /api/v1/ai/assistant result:', result); |
| 97 | + |
| 98 | + expect(result.status).toBe(200); |
| 99 | + expect(result.body).toMatchObject({ |
| 100 | + context: expect.objectContaining({ appName: 'studio' }), |
| 101 | + }); |
| 102 | + // `agent` may be null if no default agent is bound; that's fine. |
| 103 | + expect(result.body).toHaveProperty('agent'); |
| 104 | + expect(Array.isArray(result.body.skills)).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + test('GET /api/v1/ai/assistant/skills returns a skill list', async ({ page }) => { |
| 108 | + await waitForKernel(page); |
| 109 | + |
| 110 | + const result = await page.evaluate(async () => { |
| 111 | + const res = await fetch('/api/v1/ai/assistant/skills?appName=studio', { |
| 112 | + credentials: 'include', |
| 113 | + }); |
| 114 | + return { status: res.status, body: await res.json().catch(() => null) }; |
| 115 | + }); |
| 116 | + |
| 117 | + expect(result.status).toBe(200); |
| 118 | + expect(result.body).toHaveProperty('skills'); |
| 119 | + expect(Array.isArray(result.body.skills)).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + test('POST /api/v1/ai/assistant/chat accepts the new body shape', async ({ page }) => { |
| 123 | + await waitForKernel(page); |
| 124 | + |
| 125 | + const result = await page.evaluate(async () => { |
| 126 | + const res = await fetch('/api/v1/ai/assistant/chat', { |
| 127 | + method: 'POST', |
| 128 | + headers: { 'content-type': 'application/json' }, |
| 129 | + credentials: 'include', |
| 130 | + body: JSON.stringify({ |
| 131 | + messages: [{ role: 'user', content: 'ping' }], |
| 132 | + context: { appName: 'studio', objectName: 'view' }, |
| 133 | + stream: false, |
| 134 | + }), |
| 135 | + }); |
| 136 | + return { |
| 137 | + status: res.status, |
| 138 | + // The response may be a streamed data-stream; for this contract test |
| 139 | + // we only care that the route accepted the new body shape (i.e. did |
| 140 | + // not 404 / 400 on the new fields). |
| 141 | + contentType: res.headers.get('content-type') ?? '', |
| 142 | + }; |
| 143 | + }); |
| 144 | + |
| 145 | + // 200 = responded; 401 = auth-gated (still proves the route exists); |
| 146 | + // 500 = handler reached but model not configured. Anything except 404/400 |
| 147 | + // means the new body shape is accepted by the route layer. |
| 148 | + expect([200, 401, 500]).toContain(result.status); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +test.describe('Universal Assistant — bundle wiring', () => { |
| 153 | + test('AiChatPanel module exports the new Universal Assistant symbols', async ({ request }) => { |
| 154 | + const res = await request.get('/src/components/AiChatPanel.tsx'); |
| 155 | + expect(res.status()).toBe(200); |
| 156 | + const source = await res.text(); |
| 157 | + |
| 158 | + // New exports / wiring must be present: |
| 159 | + expect(source).toContain('ASSISTANT_CHAT_PATH'); |
| 160 | + expect(source).toContain('/api/v1/ai/assistant/chat'); |
| 161 | + expect(source).toContain('useAssistantContext'); |
| 162 | + expect(source).toContain('useAssistantResolution'); |
| 163 | + expect(source).toContain('assistant-status'); |
| 164 | + expect(source).toContain('skill-palette'); |
| 165 | + |
| 166 | + // Old Agent-dropdown wiring must be GONE: |
| 167 | + expect(source).not.toContain('AGENT_STORAGE_KEY'); |
| 168 | + expect(source).not.toContain('GENERAL_CHAT_VALUE'); |
| 169 | + expect(source).not.toContain('loadSelectedAgent'); |
| 170 | + expect(source).not.toContain('saveSelectedAgent'); |
| 171 | + }); |
| 172 | +}); |
0 commit comments