Files
archie/e2e/settings-save.cjs
T
sam f6eff43ca4 fix(ui): restore staged review for models and instructions
Apply reviewed edits through the module APIs one request at a time. Keep failed and untouched drafts for retry, and remove successful edits from the review.

(cherry picked from commit 952215e36325b76f6e3364d713263f502acf6d02)
2026-11-27 15:51:34 +11:00

142 lines
7.4 KiB
JavaScript

// Run against local Vite only: node e2e/settings-save.cjs [http://127.0.0.1:5173]
// Every API request is intercepted; no real settings can be changed.
const assert = require('node:assert/strict');
const { test } = require('node:test');
const { chromium } = require('playwright');
const base = new URL(process.argv[2] ?? 'http://127.0.0.1:5173');
assert.equal(base.hostname, '127.0.0.1', 'use a local Vite server');
const id = '11111111-1111-4111-8111-111111111111';
async function scenario(t, path) {
const browser = await chromium.launch({ executablePath: process.env.CHROME_BIN });
t.after(() => browser.close());
const page = await browser.newPage();
page.setDefaultTimeout(5000);
const provider = { id, name: 'Example', class: 'openai', base_url: 'https://provider.example' };
const saved = {
'/api/settings/agent-models': { default: { provider_id: id, model: 'original' } },
'/api/settings/model-providers': [provider],
'/api/settings/model-classes': [{ class: 'openai' }],
'/api/settings/model-policy': { workflow_aliases: null, chat_aliases: null },
'/api/settings/instructions': { text: 'Original instructions' },
};
const moduleWrites = {
'/api/settings/agent-model': (body) => (saved['/api/settings/agent-models'].default = body),
[`/api/settings/model-providers/${id}`]: (body) => Object.assign(provider, body),
'/api/settings/model-policy': (body) => (saved['/api/settings/model-policy'] = body),
'/api/settings/instructions': (body) => (saved['/api/settings/instructions'] = { text: body.text.trim() }),
'/api/settings/agent-model/test': (body) => ({ model: body.model, answer: 'Ready' }),
};
const writes = [];
const failed = new Set();
const legacy = [];
await page.route('**/*', async (r) => {
const request = r.request();
const url = new URL(request.url());
if (url.origin !== base.origin) return r.abort();
const path = url.pathname;
if (!path.startsWith('/api/')) return r.continue();
if (path.startsWith('/api/control-plane/')) legacy.push(path);
if (request.method() !== 'GET') {
const body = request.postDataJSON();
writes.push([request.method(), path, body]);
if (failed.delete(path)) {
return r.fulfill({ status: 503, contentType: 'application/problem+json', body: '{"detail":"Save temporarily unavailable"}' });
}
if (moduleWrites[path]) return r.fulfill({ json: moduleWrites[path](body) });
return r.fulfill({ status: 404 });
}
const context = {
'/api/auth/status': { setup_required: false, user: { id: 'u', display_name: 'Operator', kind: 'operator' }, instance: 'archie' },
'/api/authentication': { public_url: base.origin, oidc_issuer: '', oidc_client_id: '', oidc_audience: '', trusted_proxies: [] },
'/api/capabilities': { sections: { chat: false } },
'/api/orgs': { orgs: [{ id: 'test', name: 'Test', role: 'owner', version: 1 }], current: 'test', instance_admin: true },
'/api/alerts': { alerts: [] },
'/api/credentials': [],
};
return r.fulfill({ json: saved[path] ?? context[path] ?? {} });
});
await page.goto(new URL(path, base).href);
return { page, writes, failed, saved, legacy };
}
async function review(page) {
await page.getByRole('button', { name: 'Review and save', exact: true }).click({ timeout: 3500 });
return page.getByRole('dialog');
}
test('Models reviews only staged edits and retries only the unfinished writes', async (t) => {
const { page, writes, failed, legacy } = await scenario(t, '/settings/models');
const alias = page.locator('#model-default');
await alias.fill('new-model');
await page.getByRole('button', { name: /Example/ }).click();
await page.getByLabel('Name for Example').fill('Renamed');
await page.getByLabel('Aliases workflows may use').fill('default');
await page.getByLabel('Aliases chat may use').fill('default');
const nav = page.getByRole('navigation', { name: 'Instance', exact: true });
await nav.getByRole('link', { name: 'Authentication', exact: true }).click();
await nav.getByRole('link', { name: 'Models', exact: true }).click();
assert.equal(await alias.inputValue(), 'new-model');
const dialog = await review(page);
await dialog.getByText('new-model', { exact: false }).waitFor();
await dialog.getByText('Renamed', { exact: false }).waitFor();
await dialog.getByRole('button', { name: 'Revert chat', exact: true }).click();
assert.deepEqual(writes, [], 'editing and opening the review must not write');
const providerPath = `/api/settings/model-providers/${id}`;
failed.add(providerPath);
const confirm = dialog.getByRole('button', { name: 'Save', exact: true });
await confirm.click();
await dialog.getByText('Save temporarily unavailable').waitFor();
const aliasWrite = ['PUT', '/api/settings/agent-model', { provider_id: id, model: 'new-model' }];
const providerWrite = ['PUT', providerPath, { name: 'Renamed', class: 'openai', base_url: 'https://provider.example' }];
const policyWrite = ['PUT', '/api/settings/model-policy', { workflow_aliases: ['default'], chat_aliases: null }];
assert.deepEqual(writes, [aliasWrite, providerWrite], 'stop after the rejected write');
assert.equal(await dialog.getByText('new-model', { exact: false }).count(), 0, 'successful alias is no longer staged');
await confirm.click();
await dialog.waitFor({ state: 'hidden' });
assert.deepEqual(writes, [aliasWrite, providerWrite, providerWrite, policyWrite]);
assert.deepEqual(legacy, []);
});
test('Testing a model runs immediately without saving its draft', async (t) => {
const { page, writes } = await scenario(t, '/settings/models');
await page.locator('#model-default').fill('try-this');
await page.getByRole('button', { name: 'Test', exact: true }).click();
await page.getByText('try-this answered “Ready”').waitFor();
const dialog = await review(page);
await dialog.getByText('After: try-this').waitFor();
assert.deepEqual(writes, [['POST', '/api/settings/agent-model/test', { provider_id: id, model: 'try-this' }]]);
});
test('Instructions can be discarded or retried from the review', async (t) => {
const { page, writes, saved, failed, legacy } = await scenario(t, '/org/instructions');
const text = page.getByRole('textbox', { name: 'Instructions' });
await text.fill('Discard me');
let dialog = await review(page);
await dialog.getByText('Discard me', { exact: false }).waitFor();
await dialog.getByRole('button', { name: 'Discard all' }).click();
assert.equal(await text.inputValue(), 'Original instructions');
await text.fill('New instructions');
dialog = await review(page);
const path = '/api/settings/instructions';
assert.deepEqual(writes, [], 'discard and review do not write');
const confirm = dialog.getByRole('button', { name: 'Save', exact: true });
saved[path].text = 'Changed in another tab';
await confirm.click();
await dialog.getByText('Changed elsewhere. Latest values loaded.').waitFor();
await dialog.getByText('Changed in another tab').waitFor();
assert.deepEqual(writes, [], 'a newer value needs another review before it is overwritten');
failed.add(path);
await confirm.click();
await dialog.getByText('Save temporarily unavailable').waitFor();
const command = ['PUT', path, { text: 'New instructions' }];
assert.deepEqual(writes, [command]);
await confirm.click();
await dialog.waitFor({ state: 'hidden' });
assert.deepEqual(writes, [command, command]);
assert.equal(saved[path].text, 'New instructions');
assert.deepEqual(legacy, []);
});