Files
order-notification-automation/tests/assistant/providers-ui.test.cjs
T

225 lines
11 KiB
JavaScript

'use strict';
const { test } = require('node:test'),
assert = require('node:assert/strict');
const { managementFixture } = require('./management-fixture.cjs');
test(
'assistant settings manage providers and default models without exposing a saved key',
{ timeout: 120000 },
async (t) => {
const { page, open, calls } = await managementFixture(t, { browser: true });
await open('#assistant-settings');
await page.getByRole('button', { name: '添加供应商', exact: true }).click();
await page.getByLabel('供应商类型', { exact: true }).selectOption('openai');
await page.getByLabel('供应商名称', { exact: true }).fill('测试供应商');
await page.getByLabel('API 根地址', { exact: true }).fill('https://models.example/v1');
await page.getByLabel('API 密钥', { exact: true }).fill('TEST_PROVIDER_UI_KEY');
await page.getByLabel('模型列表', { exact: true }).fill('test-a|测试模型 A\ntest-b|测试模型 B');
await page.getByRole('button', { name: '保存供应商', exact: true }).click();
await page.getByText('测试供应商', { exact: true }).waitFor();
assert.equal(calls(), 0);
assert.doesNotMatch(await page.locator('body').innerText(), /TEST_PROVIDER_UI_KEY/);
await page
.getByLabel('默认模型', { exact: true })
.selectOption({ label: '测试供应商 / 测试模型 A' });
await page.getByRole('button', { name: '保存默认模型', exact: true }).click();
await page.getByText('默认模型已保存。', { exact: true }).waitFor();
await page.getByRole('button', { name: '保存助手设置', exact: true }).click();
await require('node:fs/promises').mkdir('output/verification/dsh-providers', {
recursive: true,
});
await page.evaluate(() => scrollTo(0, 0));
await page.screenshot({
path: 'output/verification/dsh-providers/settings-desktop.png',
fullPage: true,
});
await page.goto(new URL('/#assistant', page.url()).href);
await page.getByRole('button', { name: /选择模型/ }).click();
await page.getByRole('option', { name: '测试模型 B', exact: true }).click();
await page.getByLabel('发送给 DSH', { exact: true }).fill('分析当前状态');
await page.getByRole('button', { name: '发送', exact: true }).click();
await page.getByText('使用模型 test-b', { exact: true }).waitFor();
assert.equal(calls(), 1);
await page.screenshot({
path: 'output/verification/dsh-providers/assistant-model-selected.png',
fullPage: true,
});
await page.setViewportSize({ width: 390, height: 844 });
await page.screenshot({
path: 'output/verification/dsh-providers/assistant-mobile.png',
fullPage: true,
});
},
);
test(
'the tool return button is prominent on desktop and mobile and returns to the tools section',
{ timeout: 90000 },
async (t) => {
const { page, open } = await managementFixture(t, { browser: true });
await open();
for (const width of [1440, 390]) {
await page.setViewportSize({ width, height: 900 });
const back = page.getByRole('link', { name: '返回工具', exact: true });
await back.waitFor();
const box = await back.boundingBox();
assert.ok(box.height >= 40);
assert.ok(box.width >= 100);
assert.equal(await back.getAttribute('href'), '#tools');
}
await page.getByRole('link', { name: '返回工具', exact: true }).click();
await page.getByRole('navigation', { name: '工具导航' }).waitFor();
assert.equal(await page.locator('.assistant-workspace').count(), 0);
},
);
test(
'opening another provider editor never silently discards an unsaved key or name',
{ timeout: 90000 },
async (t) => {
const { page, open } = await managementFixture(t, { browser: true });
await open('#assistant-settings');
await page.getByRole('button', { name: '添加供应商', exact: true }).click();
await page.getByLabel('供应商名称', { exact: true }).fill('未保存供应商');
await page.getByLabel('API 密钥', { exact: true }).fill('UNSAVED_TEST_KEY');
await page.getByRole('button', { name: '编辑', exact: true }).first().click();
await page.getByRole('dialog', { name: '未保存的修改', exact: true }).waitFor();
await page.getByRole('button', { name: '留在此页', exact: true }).click();
assert.equal(await page.getByLabel('供应商名称', { exact: true }).inputValue(), '未保存供应商');
assert.equal(
await page.getByLabel('API 密钥', { exact: true }).inputValue(),
'UNSAVED_TEST_KEY',
);
},
);
test(
'choosing the first model starts conversation updates and an external trash clears its draft',
{ timeout: 90000 },
async (t) => {
const f = await managementFixture(t, { browser: true }),
{ page, open, store } = f;
store.saveSettings({ ...store.settings(), apiKey: 'TEST_FIRST_MODEL_KEY' });
await open();
await page.getByRole('button', { name: /选择模型/ }).click();
const stream = page.waitForRequest((r) => r.url().includes('/stream?'), { timeout: 5000 });
await page.getByRole('option', { name: 'deepseek-flash', exact: true }).click();
await stream;
await page.getByRole('button', { name: '归档会话', exact: true }).waitFor();
const input = page.getByLabel('发送给 DSH', { exact: true });
await input.fill('UNSENT_DRAFT');
const owner = f.users.users().find((u) => u.username === 'admin').id,
c = store.listConversations(owner).items[0];
store.trash(c.id, owner, { version: store.conversation(c.id, owner).version });
await page.waitForFunction(() => document.querySelector('#assistant-input')?.value === '');
assert.equal(await page.locator('[data-assistant-conversation]').count(), 0);
},
);
test(
'provider save locks mutable fields until its acknowledged response completes',
{ timeout: 90000 },
async (t) => {
const { page, open, store } = await managementFixture(t, { browser: true });
await open('#assistant-settings');
await page.getByRole('button', { name: '添加供应商', exact: true }).click();
await page.getByLabel('供应商名称', { exact: true }).fill('NAME_SUBMITTED');
await page.getByLabel('API 密钥', { exact: true }).fill('TEST_PENDING_PROVIDER_KEY');
let release;
const gate = new Promise((r) => (release = r));
t.after(() => release());
await page.route('**/api/assistant/providers', async (route) => {
if (route.request().method() === 'POST') {
await gate;
await route.continue();
} else await route.continue();
});
const started = page.waitForRequest(
(r) => r.url().endsWith('/providers') && r.method() === 'POST',
);
await page.getByRole('button', { name: '保存供应商', exact: true }).click();
await started;
assert.equal(await page.getByLabel('供应商名称', { exact: true }).isDisabled(), true);
assert.equal(await page.getByLabel('API 密钥', { exact: true }).isDisabled(), true);
release();
await page.getByText('NAME_SUBMITTED', { exact: true }).waitFor();
assert.ok(store.providers.list().providers.some((p) => p.name === 'NAME_SUBMITTED'));
},
);
test('provider cancel asks once before discarding a dirty draft', { timeout: 90000 }, async (t) => {
const { page, open } = await managementFixture(t, { browser: true });
await open('#assistant-settings');
await page.getByRole('button', { name: '添加供应商', exact: true }).click();
await page.getByLabel('供应商名称', { exact: true }).fill('UNSAVED_NAME');
await page.getByRole('button', { name: '取消', exact: true }).click();
await page.getByRole('dialog', { name: '未保存的修改', exact: true }).waitFor();
await page.getByRole('button', { name: '放弃并继续', exact: true }).click();
await page.locator('#provider-edit-form').waitFor({ state: 'hidden' });
assert.equal(await page.getByRole('dialog', { name: '未保存的修改', exact: true }).count(), 0);
});
test(
'configured assistant sends without a separate enable switch and reuses the model directory',
{ timeout: 90000 },
async (t) => {
const { page, open, store } = await managementFixture(t, { browser: true });
store.saveSettings({
...store.settings(),
apiKey: 'TEST_CONFIGURED_ASSISTANT_KEY',
enabled: false,
});
let providerGets = 0;
await page.route('**/api/assistant/providers', async (route) => {
if (route.request().method() === 'GET') providerGets++;
await route.continue();
});
await open();
await page.getByRole('button', { name: /选择模型/ }).click();
await page.getByRole('button', { name: /选择模型/ }).press('Escape');
await page.getByRole('button', { name: /选择模型/ }).click();
await page.getByLabel('发送给 DSH', { exact: true }).fill('已配置可发送');
assert.equal(providerGets, 1);
assert.equal(await page.getByRole('button', { name: '发送', exact: true }).isDisabled(), false);
assert.equal(await page.getByRole('link', { name: '打开助手设置', exact: true }).count(), 0);
},
);
test(
'model directory failure offers an inline retry without losing the composer',
{ timeout: 90000 },
async (t) => {
const { page, open, store } = await managementFixture(t, { browser: true });
store.saveSettings({ ...store.settings(), apiKey: 'TEST_MODEL_DIRECTORY_KEY' });
let attempts = 0;
await page.route('**/api/assistant/providers', async (route) => {
if (route.request().method() === 'GET' && ++attempts === 1)
return route.fulfill({
status: 503,
contentType: 'application/json',
body: JSON.stringify({ error: '暂时不可用' }),
});
await route.continue();
});
await open();
await page.getByRole('button', { name: '重试加载模型', exact: true }).waitFor();
const input = page.getByLabel('发送给 DSH', { exact: true });
await input.fill('保留这段草稿');
await page.getByRole('button', { name: '重试加载模型', exact: true }).click();
await page.getByRole('option', { name: 'deepseek-flash', exact: true }).waitFor();
assert.equal(await input.inputValue(), '保留这段草稿');
assert.equal(attempts, 2);
},
);
test('conversation list failure offers a retry in place', { timeout: 90000 }, async (t) => {
const { page, open } = await managementFixture(t, { browser: true });
let attempts = 0;
await open();
await page.route('**/api/assistant/conversations**', async (route) => {
if (route.request().method() === 'GET' && ++attempts === 1)
return route.fulfill({
status: 503,
contentType: 'application/json',
body: JSON.stringify({ error: '暂时不可用' }),
});
await route.continue();
});
await page.getByPlaceholder('搜索会话标题').fill('触发失败');
await page.getByRole('button', { name: '重试加载会话', exact: true }).waitFor();
await page.getByRole('button', { name: '重试加载会话', exact: true }).click();
await page.getByText('没有匹配会话。', { exact: true }).waitFor();
assert.equal(attempts, 2);
});