-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathai.e2e.ts
More file actions
679 lines (601 loc) · 27.3 KB
/
Copy pathai.e2e.ts
File metadata and controls
679 lines (601 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import { expect, test, type Locator, type Page } from '@playwright/test'
import { createServer, type Server } from 'node:http'
import {
ANONYMOUS_STATE,
completeStepUp,
login,
loginAs,
} from './helpers'
import { openSiteEditor } from './helpers/editor'
const OFFLINE_OLLAMA_URL = 'http://127.0.0.1:1'
async function addOllamaCredential(
page: Page,
label: string,
baseUrl = OFFLINE_OLLAMA_URL,
) {
await page.getByRole('button', { name: 'Add credential' }).click()
const dialog = page.getByRole('dialog', { name: 'Add AI credential' })
await expect(dialog).toBeVisible()
await dialog.getByRole('combobox', { name: 'Provider' }).click()
await page.getByRole('option', { name: 'Ollama (local)' }).click()
await expect(dialog.getByLabel('Base URL')).toBeVisible()
await expect(dialog.getByLabel('Bearer token (optional)')).toBeVisible()
await expect(dialog.getByLabel('API key')).toHaveCount(0)
await dialog.getByLabel('Display label').fill(label)
await dialog.getByLabel('Base URL').fill(baseUrl)
await dialog.getByRole('button', { name: 'Add credential' }).click()
}
async function addOfflineOllamaCredential(page: Page, label: string) {
await addOllamaCredential(page, label)
}
interface FakeOllamaToolCall {
id: string
name: string
input: Record<string, unknown>
}
async function startFakeOllamaServer(
responseText = 'E2E audit reply.',
toolCall?: FakeOllamaToolCall,
): Promise<{
baseUrl: string
requests: { tags: number; chats: number; chatBodies: string[] }
close: () => Promise<void>
}> {
const requests = { tags: 0, chats: 0, chatBodies: [] as string[] }
const server = createServer((req, res) => {
if (req.method === 'GET' && req.url === '/api/tags') {
requests.tags += 1
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ models: [{ name: 'e2e-model' }] }))
return
}
if (req.method === 'POST' && req.url === '/v1/chat/completions') {
const chunks: Buffer[] = []
req.on('data', (chunk) => {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
})
req.on('end', () => {
requests.chats += 1
requests.chatBodies.push(Buffer.concat(chunks).toString('utf8'))
res.writeHead(200, {
'cache-control': 'no-cache',
'content-type': 'text/event-stream',
})
if (toolCall && requests.chats === 1) {
res.write(`data: ${JSON.stringify({
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: toolCall.id,
type: 'function',
function: {
name: toolCall.name,
arguments: JSON.stringify(toolCall.input),
},
},
],
},
finish_reason: null,
},
],
})}\n\n`)
res.write(`data: ${JSON.stringify({
choices: [{ delta: {}, finish_reason: 'tool_calls' }],
usage: { prompt_tokens: 20, completion_tokens: 5 },
})}\n\n`)
res.end('data: [DONE]\n\n')
return
}
res.write(`data: ${JSON.stringify({
choices: [{ delta: { content: responseText }, finish_reason: null }],
})}\n\n`)
res.write('data: {"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":123,"completion_tokens":45}}\n\n')
res.end('data: [DONE]\n\n')
})
return
}
res.writeHead(404, { 'content-type': 'text/plain' })
res.end('not found')
})
await new Promise<void>((resolve, reject) => {
server.once('error', reject)
server.listen(0, '127.0.0.1', resolve)
})
const address = server.address()
if (!address || typeof address === 'string') {
await closeServer(server)
throw new Error('Fake Ollama server did not bind to a TCP port.')
}
return {
baseUrl: `http://127.0.0.1:${address.port}`,
requests,
close: () => closeServer(server),
}
}
function closeServer(server: Server): Promise<void> {
return new Promise((resolve, reject) => {
server.close((err) => {
if (err) reject(err)
else resolve()
})
})
}
async function createRole(
page: Page,
name: string,
capabilityLabels: readonly string[],
): Promise<void> {
await page.goto('/admin/users')
await page.getByRole('button', { name: 'Roles', exact: true }).click()
await page.getByRole('button', { name: 'Create Role', exact: true }).click()
const dialog = page.getByRole('dialog', { name: 'Create Role' })
await dialog.getByLabel('Name', { exact: true }).fill(name)
for (const label of capabilityLabels) {
await setCapabilityChecked(dialog, label, true)
}
await page.locator('button[form="users-page-role-form"]').click()
await completeStepUp(page)
await expect(dialog).toBeHidden()
}
async function setRoleCapabilities(
page: Page,
roleName: string,
capabilityLabels: readonly string[],
): Promise<void> {
await page.goto('/admin/users')
await page.getByRole('button', { name: 'Roles', exact: true }).click()
await openRoleAction(page, roleName, 'Edit')
const dialog = page.getByRole('dialog', { name: 'Edit Role' })
await expect(dialog).toBeVisible()
const managedLabels = ['View site', 'Use AI chat', 'Manage AI providers']
for (const label of managedLabels) {
await setCapabilityChecked(dialog, label, capabilityLabels.includes(label))
}
await page.locator('button[form="users-page-role-form"]').click()
await completeStepUp(page)
await expect(dialog).toBeHidden()
}
async function createUser(
page: Page,
user: { email: string; displayName: string; password: string; role: string },
): Promise<void> {
await page.goto('/admin/users')
await page.getByRole('button', { name: 'Create User', exact: true }).click()
await page.locator('input[name="new-user-email-address"]').fill(user.email)
await page.locator('input[name="new-user-display-name"]').fill(user.displayName)
await page.locator('input[name="new-user-initial-password"]').fill(user.password)
await page.locator('select[name="new-user-role"]').selectOption({ label: user.role })
await page.locator('button[form="users-page-user-form"]').click()
await completeStepUp(page)
}
async function openReadableSiteEditor(page: Page): Promise<void> {
if (!(await page.getByTestId('canvas-root').isVisible({ timeout: 1_000 }).catch(() => false))) {
await page.goto('/admin/site')
}
await expect(page.getByTestId('canvas-root')).toBeVisible({ timeout: 20_000 })
}
async function setCapabilityChecked(
dialog: Locator,
label: string,
checked: boolean,
): Promise<void> {
const checkbox = dialog.getByRole('checkbox', {
name: new RegExp(`^${escapeRegExp(label)}\\b`),
})
await checkbox.setChecked(checked, { force: true })
}
async function openRoleAction(page: Page, roleName: string, action: string): Promise<void> {
await page.getByRole('button', { name: `Actions for ${roleName}` }).click()
await page
.getByRole('menu', { name: `Role actions for ${roleName}` })
.getByRole('menuitem', { name: action })
.click()
}
function toolNamesFromChatBody(rawBody: string): string[] {
const body: unknown = JSON.parse(rawBody)
if (!isRecord(body) || !Array.isArray(body.tools)) return []
return body.tools.flatMap((tool) => {
if (!isRecord(tool)) return []
const definition = tool.function
if (!isRecord(definition) || typeof definition.name !== 'string') return []
return [definition.name]
})
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
/**
* AI-001 — manage provider credentials without depending on external provider
* availability. The stable browser contract is that an operator can create an
* Ollama base-URL credential, see the safe credential projection, and delete it.
*/
test.describe('AI settings', () => {
test('creates and deletes an Ollama provider credential (AI-001)', async ({
page,
}) => {
const suffix = Date.now().toString(36)
const label = `E2E Ollama ${suffix}`
await page.goto('/admin/ai')
await expect(page.getByRole('heading', { name: 'AI' })).toBeVisible()
await expect(page.getByRole('tab', { name: 'Providers' })).toHaveAttribute(
'aria-selected',
'true',
)
await test.step('create an Ollama base URL credential', async () => {
await addOfflineOllamaCredential(page, label)
})
const credentialCard = page.locator('div').filter({ hasText: label }).first()
await expect(credentialCard).toBeVisible({ timeout: 20_000 })
await expect(credentialCard).toContainText('Ollama')
await expect(credentialCard).toContainText('Endpoint URL')
await expect(credentialCard.getByRole('button', { name: 'Test' })).toBeVisible()
await expect(credentialCard.getByRole('button', { name: 'Delete' })).toBeVisible()
await test.step('delete the created credential', async () => {
await credentialCard.getByRole('button', { name: 'Delete' }).click()
await expect(page.getByText(label)).toHaveCount(0)
})
})
test('sets and reloads a data-scope default model (AI-002)', async ({
page,
}) => {
const suffix = Date.now().toString(36)
const label = `E2E Defaults Ollama ${suffix}`
await page.goto('/admin/ai')
await expect(page.getByRole('heading', { name: 'AI' })).toBeVisible()
await test.step('create a credential for the defaults picker', async () => {
await addOfflineOllamaCredential(page, label)
await expect(page.getByText(label)).toBeVisible({ timeout: 20_000 })
})
await test.step('choose and save a Data default model', async () => {
await page.getByRole('tab', { name: 'Defaults' }).click()
await expect(page.getByRole('heading', { name: 'Per-scope defaults' })).toBeVisible()
const dataModelButton = page.getByRole('button', { name: 'Model for data' })
await dataModelButton.click()
await expect(page.getByRole('menuitemradio', { name: 'Llama 4' })).toBeVisible({
timeout: 20_000,
})
await page.getByRole('menuitemradio', { name: 'Llama 4' }).click()
await expect(dataModelButton).toContainText(`${label} · Llama 4`)
await page
.locator('div')
.filter({ hasText: /^dataUsed by the data workspace/ })
.getByRole('button', { name: 'Save' })
.click()
await expect(page.getByRole('status').filter({ hasText: 'Saved.' })).toBeVisible()
})
await test.step('reload and verify the saved default resolves', async () => {
await page.reload()
await expect(page.getByRole('heading', { name: 'AI' })).toBeVisible()
await page.getByRole('tab', { name: 'Defaults' }).click()
await expect(page.getByRole('button', { name: 'Model for data' })).toContainText(
`${label} · Llama 4`,
{ timeout: 20_000 },
)
})
await test.step('clear the default and delete the credential', async () => {
await page
.locator('div')
.filter({ hasText: /^dataUsed by the data workspace/ })
.getByRole('button', { name: 'Clear' })
.click()
await expect(page.getByRole('status').filter({ hasText: 'Cleared.' })).toBeVisible()
await expect(page.getByRole('button', { name: 'Model for data' })).toContainText(
'Choose a model',
)
await page.getByRole('tab', { name: 'Providers' }).click()
const credentialCard = page.locator('div').filter({ hasText: label }).first()
await expect(credentialCard).toBeVisible()
await credentialCard.getByRole('button', { name: 'Delete' }).click()
await expect(page.getByText(label)).toHaveCount(0)
})
})
test('streams a site chat and renders audit usage rollups (AI-004, AI-006)', async ({
page,
}) => {
const fakeOllama = await startFakeOllamaServer()
const suffix = Date.now().toString(36)
const label = `E2E Live Ollama ${suffix}`
try {
await test.step('create a live local credential for chat', async () => {
await page.goto('/admin/ai')
await expect(page.getByRole('heading', { name: 'AI' })).toBeVisible()
await addOllamaCredential(page, label, fakeOllama.baseUrl)
await expect(page.getByText(label)).toBeVisible({ timeout: 20_000 })
await expect.poll(() => fakeOllama.requests.tags).toBeGreaterThan(0)
})
await test.step('send a site assistant message through the fake provider', async () => {
await openSiteEditor(page)
await page.getByRole('button', { name: 'Open AI assistant panel' }).click()
const assistantPanel = page.getByRole('complementary', { name: 'AI Assistant' })
await expect(assistantPanel).toBeVisible()
const composer = assistantPanel.getByLabel('Message to AI assistant')
await expect(composer).toBeEnabled({ timeout: 20_000 })
await composer.fill('Summarize the current page for the audit test.')
await assistantPanel.getByRole('button', { name: 'Send' }).click()
await expect(assistantPanel.getByText('E2E audit reply.')).toBeVisible({
timeout: 20_000,
})
await expect.poll(() => fakeOllama.requests.chats).toBe(1)
})
await test.step('verify the Audit tab shows the persisted usage', async () => {
await page.goto('/admin/ai')
await page.getByRole('tab', { name: 'Audit' }).click()
await expect(page.getByRole('heading', { name: 'Usage audit' })).toBeVisible()
await expect(page.getByText('e2e-model')).toBeVisible({ timeout: 20_000 })
await expect(page.getByRole('heading', { name: 'By surface' })).toBeVisible()
await expect(page.getByRole('cell', { name: 'site' })).toBeVisible()
await expect(page.getByRole('cell', { name: '123' }).first()).toBeVisible()
await expect(page.getByRole('cell', { name: '45' }).first()).toBeVisible()
await expect(page.getByRole('heading', { name: 'Daily spend' })).toBeVisible()
})
await test.step('clear seeded defaults and delete the credential', async () => {
await page.evaluate(async () => {
const conversationsRes = await fetch('/admin/api/ai/conversations?scope=site')
if (!conversationsRes.ok) {
throw new Error(`Failed to list site conversations: ${conversationsRes.status}`)
}
const conversationsBody = await conversationsRes.json()
if (!Array.isArray(conversationsBody.conversations)) {
throw new Error('Conversation list response did not include an array.')
}
for (const conversation of conversationsBody.conversations) {
if (typeof conversation?.id !== 'string') {
throw new Error('Conversation list response included an invalid id.')
}
const res = await fetch(`/admin/api/ai/conversations/${conversation.id}`, {
method: 'DELETE',
})
if (!res.ok) throw new Error(`Failed to delete conversation ${conversation.id}: ${res.status}`)
}
for (const scope of ['site', 'content', 'data', 'plugin']) {
const res = await fetch(`/admin/api/ai/defaults/${scope}`, { method: 'DELETE' })
if (!res.ok) throw new Error(`Failed to clear ${scope} default: ${res.status}`)
}
})
await page.getByRole('tab', { name: 'Providers' }).click()
const credentialCard = page.locator('div').filter({ hasText: label }).first()
await expect(credentialCard).toBeVisible()
await credentialCard.getByRole('button', { name: 'Delete' }).click()
await expect(page.getByText(label)).toHaveCount(0)
})
} finally {
await fakeOllama.close()
}
})
test('returns a browser tool result to the model loop (AI-005)', async ({
page,
}) => {
const fakeOllama = await startFakeOllamaServer('E2E bridge reply.', {
id: 'call_read_document',
name: 'read_document',
input: {},
})
const suffix = Date.now().toString(36)
const label = `E2E Bridge Ollama ${suffix}`
try {
await test.step('create a live local credential for the tool loop', async () => {
await page.goto('/admin/ai')
await expect(page.getByRole('heading', { name: 'AI' })).toBeVisible()
await addOllamaCredential(page, label, fakeOllama.baseUrl)
await expect(page.getByText(label)).toBeVisible({ timeout: 20_000 })
await expect.poll(() => fakeOllama.requests.tags).toBeGreaterThan(0)
})
await test.step('send a prompt that triggers a browser-backed read tool', async () => {
await openSiteEditor(page)
await page.getByRole('button', { name: 'Open AI assistant panel' }).click()
const assistantPanel = page.getByRole('complementary', { name: 'AI Assistant' })
await expect(assistantPanel).toBeVisible()
const composer = assistantPanel.getByLabel('Message to AI assistant')
await expect(composer).toBeEnabled({ timeout: 20_000 })
await composer.fill('Read the current document, then summarize it.')
await assistantPanel.getByRole('button', { name: 'Send' }).click()
await expect(
assistantPanel.getByRole('status', { name: 'Completed read_document' }),
).toBeVisible({ timeout: 20_000 })
await expect(assistantPanel.getByText('E2E bridge reply.')).toBeVisible({
timeout: 20_000,
})
})
await test.step('verify the provider received the browser tool result turn', async () => {
await expect.poll(() => fakeOllama.requests.chats).toBe(2)
const firstBody = fakeOllama.requests.chatBodies[0] ?? ''
const secondBody = fakeOllama.requests.chatBodies[1] ?? ''
expect(firstBody).toContain('"tools"')
expect(firstBody).toContain('"read_document"')
expect(secondBody).toContain('"role":"tool"')
expect(secondBody).toContain('"tool_call_id":"call_read_document"')
})
await test.step('clear seeded conversations/defaults and delete the credential', async () => {
await page.evaluate(async () => {
const conversationsRes = await fetch('/admin/api/ai/conversations?scope=site')
if (!conversationsRes.ok) {
throw new Error(`Failed to list site conversations: ${conversationsRes.status}`)
}
const conversationsBody = await conversationsRes.json()
if (!Array.isArray(conversationsBody.conversations)) {
throw new Error('Conversation list response did not include an array.')
}
for (const conversation of conversationsBody.conversations) {
if (typeof conversation?.id !== 'string') {
throw new Error('Conversation list response included an invalid id.')
}
const res = await fetch(`/admin/api/ai/conversations/${conversation.id}`, {
method: 'DELETE',
})
if (!res.ok) throw new Error(`Failed to delete conversation ${conversation.id}: ${res.status}`)
}
for (const scope of ['site', 'content', 'data', 'plugin']) {
const res = await fetch(`/admin/api/ai/defaults/${scope}`, { method: 'DELETE' })
if (!res.ok) throw new Error(`Failed to clear ${scope} default: ${res.status}`)
}
})
await page.goto('/admin/ai')
const credentialCard = page.locator('div').filter({ hasText: label }).first()
await expect(credentialCard).toBeVisible()
await credentialCard.getByRole('button', { name: 'Delete' }).click()
await expect(page.getByText(label)).toHaveCount(0)
})
} finally {
await fakeOllama.close()
}
})
test('loads and deletes a saved site chat from conversation history (AI-003)', async ({
page,
}) => {
const fakeOllama = await startFakeOllamaServer('E2E conversation reply.')
const suffix = Date.now().toString(36)
const label = `E2E History Ollama ${suffix}`
const prompt = 'Summarize the current page for conversation history.'
try {
await test.step('create a live local credential for the conversation', async () => {
await page.goto('/admin/ai')
await expect(page.getByRole('heading', { name: 'AI' })).toBeVisible()
await addOllamaCredential(page, label, fakeOllama.baseUrl)
await expect(page.getByText(label)).toBeVisible({ timeout: 20_000 })
await expect.poll(() => fakeOllama.requests.tags).toBeGreaterThan(0)
})
await test.step('create a persisted site conversation', async () => {
await openSiteEditor(page)
await page.getByRole('button', { name: 'Open AI assistant panel' }).click()
const assistantPanel = page.getByRole('complementary', { name: 'AI Assistant' })
await expect(assistantPanel).toBeVisible()
const composer = assistantPanel.getByLabel('Message to AI assistant')
await expect(composer).toBeEnabled({ timeout: 20_000 })
await composer.fill(prompt)
await assistantPanel.getByRole('button', { name: 'Send' }).click()
await expect(assistantPanel.getByText(prompt)).toBeVisible()
await expect(assistantPanel.getByText('E2E conversation reply.')).toBeVisible({
timeout: 20_000,
})
await expect.poll(() => fakeOllama.requests.chats).toBe(1)
})
await test.step('start a fresh chat and reload the saved one from history', async () => {
const assistantPanel = page.getByRole('complementary', { name: 'AI Assistant' })
await assistantPanel.getByRole('button', { name: 'New chat' }).click()
await expect(assistantPanel.getByText('E2E conversation reply.')).toHaveCount(0)
await assistantPanel.getByRole('button', { name: 'Conversation history' }).click()
const menu = page.getByRole('menu', { name: 'Conversation history' })
const savedChat = menu
.getByRole('menuitemradio')
.filter({ hasText: 'New conversation' })
.first()
await expect(savedChat).toBeVisible()
await savedChat.click()
await expect(assistantPanel.getByText(prompt)).toBeVisible()
await expect(assistantPanel.getByText('E2E conversation reply.')).toBeVisible()
})
await test.step('delete the active conversation from history', async () => {
const assistantPanel = page.getByRole('complementary', { name: 'AI Assistant' })
await assistantPanel.getByRole('button', { name: 'Conversation history' }).click()
const menu = page.getByRole('menu', { name: 'Conversation history' })
await menu.getByRole('button', { name: 'Delete chat "New conversation"' }).click()
await expect(menu.getByText('No chats yet.')).toBeVisible()
await expect(assistantPanel.getByText('E2E conversation reply.')).toHaveCount(0)
})
await test.step('clear seeded defaults and delete the credential', async () => {
await page.evaluate(async () => {
for (const scope of ['site', 'content', 'data', 'plugin']) {
const res = await fetch(`/admin/api/ai/defaults/${scope}`, { method: 'DELETE' })
if (!res.ok) throw new Error(`Failed to clear ${scope} default: ${res.status}`)
}
})
await page.goto('/admin/ai')
const credentialCard = page.locator('div').filter({ hasText: label }).first()
await expect(credentialCard).toBeVisible()
await credentialCard.getByRole('button', { name: 'Delete' }).click()
await expect(page.getByText(label)).toHaveCount(0)
})
} finally {
await fakeOllama.close()
}
})
})
/**
* CAP-005 — a user with `ai.chat` but no `ai.tools.write` can use the Site
* assistant, while the model request only receives read/orientation tools.
*/
test.describe.serial('AI write-tool capability filtering', () => {
test.use({ storageState: ANONYMOUS_STATE })
test.setTimeout(180_000)
test('chat-only site assistant request omits mutating write tools (CAP-005)', async ({
page,
browser,
}) => {
const fakeOllama = await startFakeOllamaServer('E2E read-only tools reply.')
const suffix = Date.now().toString(36)
const roleName = `CAP AI Chat Only ${suffix}`
const email = `cap-ai-chat-only-${suffix}@example.com`
const password = 'cap-ai-chat-only-pass-12345'
const label = `CAP Chat Tools Ollama ${suffix}`
try {
await test.step('owner creates a temporary provider-setup chat persona', async () => {
await login(page)
await createRole(page, roleName, [
'View site',
'Use AI chat',
'Manage AI providers',
])
await createUser(page, {
email,
displayName: roleName,
password,
role: roleName,
})
})
const personaContext = await browser.newContext({ storageState: ANONYMOUS_STATE })
const personaPage = await personaContext.newPage()
try {
await test.step('persona creates its own disposable fake-provider credential', async () => {
await loginAs(personaPage, email, password)
await personaPage.goto('/admin/ai')
await expect(personaPage.getByRole('heading', { name: 'AI' })).toBeVisible()
await addOllamaCredential(personaPage, label, fakeOllama.baseUrl)
await expect(personaPage.getByText(label)).toBeVisible({ timeout: 20_000 })
await expect.poll(() => fakeOllama.requests.tags).toBeGreaterThan(0)
})
await test.step('owner removes provider setup so the persona keeps ai.chat only', async () => {
await setRoleCapabilities(page, roleName, [
'View site',
'Use AI chat',
])
})
await test.step('persona sends a site assistant message after the downgrade', async () => {
await openReadableSiteEditor(personaPage)
await personaPage.getByRole('button', { name: 'Open AI assistant panel' }).click()
const assistantPanel = personaPage.getByRole('complementary', { name: 'AI Assistant' })
await expect(assistantPanel).toBeVisible()
const composer = assistantPanel.getByLabel('Message to AI assistant')
await expect(composer).toBeEnabled({ timeout: 20_000 })
await composer.fill('Read the current document without changing anything.')
await assistantPanel.getByRole('button', { name: 'Send' }).click()
await expect(assistantPanel.getByText('E2E read-only tools reply.')).toBeVisible({
timeout: 20_000,
})
})
await test.step('provider request contains read tools but no mutating tools', async () => {
await expect.poll(() => fakeOllama.requests.chats).toBe(1)
const toolNames = toolNamesFromChatBody(fakeOllama.requests.chatBodies[0] ?? '')
expect(toolNames).toContain('read_document')
expect(toolNames).toContain('render_snapshot')
expect(toolNames).not.toContain('insertHtml')
expect(toolNames).not.toContain('replaceNodeHtml')
expect(toolNames).not.toContain('updateNodeProps')
expect(toolNames).not.toContain('applyCss')
expect(toolNames).not.toContain('write_code_asset')
expect(toolNames).not.toContain('addPage')
expect(toolNames).not.toContain('deletePage')
})
} finally {
await personaContext.close()
}
} finally {
await fakeOllama.close()
}
})
})