276 lines
11 KiB
JavaScript
276 lines
11 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
fs = require('node:fs/promises'),
|
|
os = require('node:os'),
|
|
path = require('node:path');
|
|
async function fixture(t) {
|
|
const { Store } = require('../../src/modules/dashboard/store.cjs'),
|
|
{ AssistantStore } = require('../../src/modules/assistant/store.cjs'),
|
|
{ AssistantService } = require('../../src/modules/assistant/service.cjs'),
|
|
{ createDashboard } = require('../../src/app/dashboard.cjs');
|
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-workspace-ui-')),
|
|
users = new Store(path.join(dir, 'users'));
|
|
users.bootstrap('admin', 'Test-password-123');
|
|
const store = new AssistantStore(path.join(dir, 'assistant'));
|
|
let calls = 0;
|
|
const assistant = new AssistantService({
|
|
store,
|
|
dashboard: users,
|
|
runtime: {
|
|
chat: async ({ onEvent }) => {
|
|
calls++;
|
|
onEvent?.({ kind: 'model', data: { phase: 'step_start' } });
|
|
await new Promise((r) => setTimeout(r, 150));
|
|
return JSON.stringify({
|
|
reply: '## 诊断结论\n\n连接状态正常。\n\n| 项目 | 结果 |\n|---|---|\n| 存储 | 正常 |',
|
|
actions: [],
|
|
});
|
|
},
|
|
},
|
|
});
|
|
const server = createDashboard({ store: users, assistant, outputDir: dir });
|
|
await new Promise((r) => server.listen(0, '127.0.0.1', r));
|
|
const { loadPlaywright, browserOptions } = require('../../src/platform/browser.cjs'),
|
|
browser = await loadPlaywright().chromium.launch(browserOptions());
|
|
const page = await browser.newPage({ viewport: { width: 1366, height: 768 } });
|
|
page.setDefaultTimeout(10000);
|
|
t.after(async () => {
|
|
await browser.close();
|
|
server.closeAssistantStreams?.();
|
|
await assistant.stop();
|
|
server.closeAllConnections();
|
|
await new Promise((r) => server.close(r));
|
|
store.close();
|
|
await fs.rm(dir, { recursive: true, force: true });
|
|
});
|
|
await page.goto('http://127.0.0.1:' + server.address().port + '/#assistant');
|
|
await page.getByLabel('账号', { exact: true }).fill('admin');
|
|
await page.getByLabel('密码', { exact: true }).fill('Test-password-123');
|
|
await page.getByRole('button', { name: '登录', exact: true }).click();
|
|
await page.locator('.assistant-workspace').waitFor();
|
|
return { page, store, assistant, calls: () => calls };
|
|
}
|
|
test(
|
|
'three-column workspace keeps input visible and diagnostic commands have no hidden side effects',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { page, calls } = await fixture(t);
|
|
await page.locator('.assistant-workspace').waitFor();
|
|
const input = page.getByLabel('发送给 DSH', { exact: true }),
|
|
rect = await input.boundingBox();
|
|
assert.ok(rect.y + rect.height <= 768);
|
|
await input.fill('/help');
|
|
await input.press('Enter');
|
|
await page.getByText('固定诊断命令', { exact: true }).waitFor();
|
|
assert.equal(calls(), 0);
|
|
await input.fill('/status');
|
|
await input.press('Enter');
|
|
await page.getByRole('heading', { name: '诊断概况', exact: true }).waitFor();
|
|
assert.equal(calls(), 0);
|
|
await input.fill('/shell');
|
|
await input.press('Enter');
|
|
await page.locator('#assistant-error').filter({ hasText: '此命令未开放' }).waitFor();
|
|
assert.equal(await input.inputValue(), '/shell');
|
|
assert.equal(calls(), 0);
|
|
await input.fill('/status');
|
|
await input.press('Enter');
|
|
await page.getByRole('heading', { name: '诊断概况', exact: true }).waitFor();
|
|
await fs.mkdir('output/verification/dsh-workspace', { recursive: true });
|
|
for (const [width, height] of [
|
|
[1600, 900],
|
|
[1366, 768],
|
|
[1024, 768],
|
|
[390, 844],
|
|
]) {
|
|
await page.setViewportSize({ width, height });
|
|
assert.equal(
|
|
await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
|
|
true,
|
|
);
|
|
await page.screenshot({
|
|
path: `output/verification/dsh-workspace/workspace-${width}.png`,
|
|
fullPage: true,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
test(
|
|
'replies render structured content and preserve DOM nodes across context refresh',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { page, store } = await fixture(t);
|
|
store.saveSettings({
|
|
...store.settings(),
|
|
enabled: true,
|
|
runtimePath: process.execPath,
|
|
apiBase: 'https://fixture.example/anthropic',
|
|
model: 'fixture',
|
|
apiKey: 'TEST_ONLY_KEY',
|
|
allowActions: false,
|
|
});
|
|
await page.reload();
|
|
const input = page.getByLabel('发送给 DSH', { exact: true });
|
|
await input.fill('诊断连接');
|
|
await page.getByRole('button', { name: '发送', exact: true }).click();
|
|
await page.locator('.assistant-answer table').waitFor();
|
|
assert.equal(await page.locator('.assistant-answer h2').textContent(), '诊断结论');
|
|
await page.locator('.assistant-answer').evaluate((node) => {
|
|
node.dataset.identity = 'retained';
|
|
});
|
|
await page.getByRole('button', { name: '刷新概况', exact: true }).click();
|
|
assert.equal(await page.locator('.assistant-answer').getAttribute('data-identity'), 'retained');
|
|
await page.getByRole('button', { name: '诊断依据', exact: true }).click();
|
|
await page.locator('#aw-panel-evidence').filter({ hasText: '实际用于该轮分析' }).waitFor();
|
|
await input.fill('未发送的草稿');
|
|
await page.getByRole('button', { name: '新建对话', exact: true }).click();
|
|
await page.locator('#assistant-title').filter({ hasText: '新对话' }).waitFor();
|
|
assert.match(await page.locator('#aw-panel-evidence').textContent(), /点击回答/);
|
|
await input.fill('另一个草稿');
|
|
await page.locator('[data-assistant-conversation]').filter({ hasText: '诊断连接' }).click();
|
|
assert.equal(await input.inputValue(), '未发送的草稿');
|
|
},
|
|
);
|
|
test(
|
|
'native-style action presentation keeps unknown and blocked results distinct from success',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { page } = await fixture(t);
|
|
const labels = await page.evaluate(() => ({
|
|
unknown: AssistantConversation.actionState({ status: 'needs_review' }),
|
|
blocked: AssistantConversation.actionState({
|
|
status: 'success',
|
|
result: { status: 'blocked' },
|
|
}),
|
|
}));
|
|
assert.equal(labels.unknown, '结果待核验');
|
|
assert.equal(labels.blocked, '操作受阻');
|
|
},
|
|
);
|
|
test(
|
|
'a failed late submission cannot replace the draft or error of a newer conversation',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { page, store } = await fixture(t);
|
|
store.saveSettings({
|
|
...store.settings(),
|
|
enabled: true,
|
|
runtimePath: process.execPath,
|
|
apiBase: 'https://fixture.example/anthropic',
|
|
model: 'fixture',
|
|
apiKey: 'TEST_ONLY_KEY',
|
|
allowActions: false,
|
|
});
|
|
await page.reload();
|
|
await page.getByRole('button', { name: '新建对话', exact: true }).click();
|
|
let release;
|
|
const gate = new Promise((r) => (release = r));
|
|
t.after(() => release());
|
|
await page.route('**/api/assistant/conversations/*/messages', async (route) => {
|
|
await gate;
|
|
await route.fulfill({ status: 503, json: { error: 'OLD_REQUEST_FAILED' } });
|
|
});
|
|
const input = page.getByLabel('发送给 DSH', { exact: true });
|
|
await input.fill('旧会话请求');
|
|
const started = page.waitForRequest((r) => r.url().endsWith('/messages'));
|
|
await page.getByRole('button', { name: '发送', exact: true }).click();
|
|
await started;
|
|
await page.getByRole('button', { name: '新建对话', exact: true }).click();
|
|
await input.fill('新会话草稿');
|
|
const returned = page.waitForResponse(
|
|
(r) => r.url().endsWith('/messages') && r.status() === 503,
|
|
);
|
|
release();
|
|
await returned;
|
|
await page.evaluate(() => new Promise((r) => setTimeout(r, 100)));
|
|
assert.equal(await input.inputValue(), '新会话草稿');
|
|
assert.equal(await page.locator('#assistant-error').textContent(), '');
|
|
},
|
|
);
|
|
test(
|
|
'local status commands clear an old diagnostic target before the next ordinary question',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { page, store, assistant } = await fixture(t);
|
|
assistant.deps.workspace = {
|
|
snapshot: () => ({
|
|
connections: [{ id: 'bpm-a', name: '测试连接 A', owner: 'admin', status: 'connected' }],
|
|
exceptions: [],
|
|
}),
|
|
getConnection: () => ({ id: 'bpm-a', owner: 'admin' }),
|
|
};
|
|
store.saveSettings({
|
|
...store.settings(),
|
|
enabled: true,
|
|
runtimePath: process.execPath,
|
|
apiBase: 'https://fixture.example/anthropic',
|
|
model: 'fixture',
|
|
apiKey: 'TEST_ONLY_KEY',
|
|
allowActions: false,
|
|
});
|
|
await page.reload();
|
|
const input = page.getByLabel('发送给 DSH', { exact: true });
|
|
await input.fill('/bpm');
|
|
await input.press('Enter');
|
|
await page.getByRole('option', { name: /测试连接 A/ }).click();
|
|
await input.fill('/status');
|
|
await input.press('Enter');
|
|
await page.getByRole('heading', { name: '诊断概况', exact: true }).waitFor();
|
|
await input.fill('查看全部运行状态');
|
|
const submitted = page.waitForRequest((r) => r.url().endsWith('/messages'));
|
|
await page.getByRole('button', { name: '发送', exact: true }).click();
|
|
assert.equal((await submitted).postDataJSON().diagnostic, undefined);
|
|
},
|
|
);
|
|
test(
|
|
'assistant Markdown keeps text and safe links without loading images or executable HTML',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { page } = await fixture(t);
|
|
const result = await page.evaluate(() => {
|
|
const host = document.createElement('div'),
|
|
escape = (value) =>
|
|
String(value).replace(
|
|
/[&<>"']/g,
|
|
(c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c],
|
|
);
|
|
host.innerHTML = AssistantConversation.markdown(
|
|
'<img src=x onerror=alert(1)>\n\n[bad](javascript:alert%281%29)  [safe](https://example.com/docs)\n\n```html\n<script>alert(1)</script>\n```',
|
|
escape,
|
|
);
|
|
return {
|
|
images: host.querySelectorAll('img').length,
|
|
scripts: host.querySelectorAll('script').length,
|
|
links: [...host.querySelectorAll('a')].map((a) => ({ href: a.href, rel: a.rel })),
|
|
text: host.textContent,
|
|
};
|
|
});
|
|
assert.equal(result.images, 0);
|
|
assert.equal(result.scripts, 0);
|
|
assert.deepEqual(result.links, [
|
|
{ href: 'https://example.com/docs', rel: 'noopener noreferrer' },
|
|
]);
|
|
assert.match(result.text, /<img src=x onerror=alert\(1\)>/);
|
|
},
|
|
);
|
|
test(
|
|
'a stream authorization change clears the entire old workspace until identity is reloaded',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { page, store } = await fixture(t);
|
|
const owner = store.db.prepare('SELECT owner FROM conversations LIMIT 1').get()?.owner;
|
|
const me = await (await page.request.get(new URL('/api/me', page.url()).href)).json(),
|
|
id = owner || me.user.id;
|
|
store.create(id);
|
|
await page.route('**/api/assistant/conversations/*/stream?*', (route) =>
|
|
route.fulfill({
|
|
contentType: 'text/event-stream',
|
|
body: 'event: auth_required\ndata: {"status":401}\n\n',
|
|
}),
|
|
);
|
|
await page.reload();
|
|
await page.getByRole('button', { name: '重新加载', exact: true }).waitFor();
|
|
assert.equal(await page.locator('.assistant-workspace').count(), 0);
|
|
},
|
|
);
|