Files
order-notification-automation/tests/assistant/management-fixture.cjs
T

87 lines
3.3 KiB
JavaScript

'use strict';
const fs = require('node:fs/promises'),
os = require('node:os'),
path = require('node:path');
async function managementFixture(t, { browser = false } = {}) {
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-management-')),
users = new Store(path.join(dir, 'users'));
users.bootstrap('admin', 'Test-password-123');
users.addUser({ username: 'viewer', password: 'Test-password-123', role: 'viewer' }, 'admin');
const store = new AssistantStore(path.join(dir, 'assistant'));
let calls = 0,
lastConfig;
const assistant = new AssistantService({
store,
dashboard: users,
runtime: {
probe: async () => ({ available: true, version: '0.2.1-rc.2', message: '运行包已就绪' }),
chat: async ({ config }) => {
calls++;
lastConfig = config;
return JSON.stringify({ reply: '使用模型 ' + config.model, actions: [] });
},
},
}),
server = createDashboard({ store: users, assistant, outputDir: dir });
await new Promise((r) => server.listen(0, '127.0.0.1', r));
const base = 'http://127.0.0.1:' + server.address().port;
const call = (route, body, cookie) =>
fetch(base + '/api' + route, {
method: body === undefined ? 'GET' : 'POST',
headers: {
...(body === undefined ? {} : { Origin: base, 'Content-Type': 'application/json' }),
...(cookie ? { Cookie: cookie } : {}),
},
body: body === undefined ? undefined : JSON.stringify(body),
}),
login = async (username = 'admin') =>
(await call('/login', { username, password: 'Test-password-123' })).headers
.get('set-cookie')
.split(';')[0];
let engine, page;
if (browser) {
const { loadPlaywright, browserOptions } = require('../../src/platform/browser.cjs');
engine = await loadPlaywright().chromium.launch(browserOptions());
page = await engine.newPage({ viewport: { width: 1440, height: 1000 } });
page.setDefaultTimeout(10000);
}
t.after(async () => {
await engine?.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 });
});
async function open(route = '#assistant', username = 'admin') {
await page.goto(base + '/' + route);
await page.waitForFunction(
() => !!document.querySelector('#username') || !!document.querySelector('#logout'),
);
if (await page.getByLabel('账号', { exact: true }).count()) {
await page.getByLabel('账号', { exact: true }).fill(username);
await page.getByLabel('密码', { exact: true }).fill('Test-password-123');
await page.getByRole('button', { name: '登录', exact: true }).click();
}
if (route === '#assistant') await page.locator('.assistant-workspace').waitFor();
}
return {
base,
page,
open,
store,
users,
assistant,
call,
login,
calls: () => calls,
lastConfig: () => lastConfig,
};
}
module.exports = { managementFixture };