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

126 lines
4.6 KiB
JavaScript

'use strict';
const test = require('node:test'),
assert = require('node:assert/strict'),
path = require('node:path');
const { loadPlaywright, browserOptions } = require('../../src/platform/browser.cjs');
async function fixture(t) {
const browser = await loadPlaywright().chromium.launch(browserOptions());
t.after(() => browser.close());
const page = await browser.newPage();
await page.route('https://synthetic.invalid/**', (route) =>
route.fulfill({ body: '<div id="fixture"></div>', contentType: 'text/html' }),
);
await page.goto('https://synthetic.invalid/');
for (const name of [
'dsh-native',
'layout',
'conversation',
'commands',
'context-panel',
'events-client',
'providers',
'model-picker',
'view',
])
await page.addScriptTag({
path: path.resolve(__dirname, '../../web/modules/assistant/' + name + '.js'),
});
await page.evaluate(async () => {
window.requests = [];
window.failNext = false;
window.hold = false;
window.release = null;
const items = Array.from({ length: 26 }, (_, i) => ({
id: 'active-' + i,
title: 'Active ' + i,
updatedAt: '2026-10-21',
version: 1,
}));
const api = async (route) => {
if (route === '/assistant')
return {
settings: { enabled: false },
usage: { calls: 0, limit: 100 },
conversations: items,
};
if (route === '/assistant/providers') return { providers: [], defaultSelection: null };
if (route === '/assistant/context')
return { connections: [], servers: [], archives: [], runs: [] };
if (route.includes('/events?')) return { events: [], nextCursor: 0, hasMore: false };
if (route.includes('?')) {
const url = new URL(route, location.origin),
scope = url.searchParams.get('scope'),
page = Number(url.searchParams.get('page'));
window.requests.push(scope + ':' + page);
if (window.hold && scope === 'active' && page === 2)
await new Promise((resolve, reject) => {
window.release = () => reject(Error('Stale failure'));
});
if (window.failNext) {
window.failNext = false;
throw Error('Synthetic page failure');
}
const list =
scope === 'active'
? items
: [{ id: 'archived', title: 'Archived only', updatedAt: '2026-10-21' }];
return { items: list.slice((page - 1) * 25, page * 25), page, total: list.length };
}
return {
id: route.split('/').at(-1),
title: 'Synthetic',
version: 1,
turns: [],
archived: route.endsWith('archived'),
};
};
window.AssistantViews.configure({
api,
esc: String,
formatDate: String,
notify: () => {},
user: { username: 'synthetic', role: 'admin' },
});
await window.AssistantViews.render(document.querySelector('#fixture'));
});
return page;
}
test(
'failed assistant page restores navigation and retries the same page',
{ timeout: 30000 },
async (t) => {
const page = await fixture(t);
await page.evaluate(() => {
window.failNext = true;
});
await page.locator('#assistant-next').click();
await page.getByText('Synthetic page failure', { exact: true }).waitFor();
assert.equal(await page.locator('#assistant-next').isDisabled(), false);
assert.equal(await page.locator('#assistant-prev').isDisabled(), true);
await page.locator('#assistant-next').click();
await page.locator('#assistant-page').filter({ hasText: '2 / 2' }).waitFor();
assert.deepEqual(await page.evaluate(() => requests), ['active:1', 'active:2', 'active:2']);
assert.equal(await page.locator('[data-assistant-conversation]').count(), 1);
},
);
test(
'stale failed page cannot roll back the new scope or disable its navigation',
{ timeout: 30000 },
async (t) => {
const page = await fixture(t);
await page.evaluate(() => {
window.hold = true;
});
await page.locator('#assistant-next').click();
await page.waitForFunction(() => !!window.release);
await page.locator('#assistant-scope').selectOption('archived');
await page.getByText('Archived only', { exact: true }).waitFor();
await page.evaluate(() => window.release());
await page.evaluate(() => new Promise((r) => setTimeout(r, 0)));
assert.equal(await page.locator('#assistant-error').textContent(), '');
assert.equal(await page.locator('#assistant-page').textContent(), '1 / 1');
assert.equal(await page.locator('#assistant-next').isDisabled(), true);
assert.equal(await page.locator('#assistant-prev').isDisabled(), true);
},
);