skillhub/plugins/dsh/test/integration/registry-config-probe.mjs
XiaoSeS b37140a2e9 feat(dsh): simplify source settings with unified cards
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-11-03 10:04:26 +08:00

83 lines
7.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from 'node:assert/strict';
import { chromium } from 'playwright';
const entry = process.env.DSH_PROBE_URL;
if (!entry) throw new Error('Set isolated component fixture URL');
const browser = await chromium.launch({ headless: true, executablePath: process.env.DSH_PROBE_CHROMIUM });
const page = await browser.newPage({ viewport: { width: 1100, height: 900 } });
const errors = [];
page.on('pageerror', error => errors.push(error.message));
try {
await page.goto(entry);
const settings = page.getByRole('form', { name: 'SkillHub 配置' });
const save = () => settings.getByRole('button', { name: '保存配置', exact: true }).click();
const sourceTab = () => settings.getByRole('tab', { name: '技能来源', exact: true }).click();
const displayTab = () => settings.getByRole('tab', { name: '页面显示', exact: true }).click();
const edit = index => settings.getByRole('button', { name: `编辑来源 ${index}`, exact: true }).click();
const input = (index, name) => settings.getByRole('textbox', { name: `来源 ${index} ${name}`, exact: true });
const selected = name => settings.getByRole('radio', { name, exact: true });
const saved = () => settings.getByText('配置已保存。', { exact: true }).waitFor();
if (process.env.DSH_PROBE_SCREENSHOT) await page.screenshot({ path: `${process.env.DSH_PROBE_SCREENSHOT}-desktop.png`, fullPage: true });
// Open a non-current editor, select it, then clear/retype its active address.
await edit(2);
await selected('选择来源 2:来源 B').check();
assert.equal(await page.evaluate(() => window.registryFixture.state().value.registryUrl), 'https://a.example');
await input(2, '地址').fill('');
await input(2, '地址').fill('https://new-b.example');
await save(); await saved();
assert.equal(await page.evaluate(() => window.registryFixture.state().value.registryUrl), 'https://new-b.example');
await edit(1);
await input(1, '名称').fill('临时名称'); await input(1, '名称').fill('来源 A');
assert.equal(await settings.getByRole('button', { name: '保存配置', exact: true }).isDisabled(), true);
await input(1, '名称').fill('跨页草稿'); await displayTab();
await settings.getByRole('combobox', { name: '版本', exact: true }).click();
await page.getByRole('option', { name: '隐藏', exact: true }).click();
assert.equal(await page.evaluate(() => window.registryFixture.state().value.detailTabs.versions), 'auto');
await settings.getByRole('tab', { name: '页面显示', exact: true }).focus(); await page.keyboard.press('ArrowLeft');
await input(1, '名称').waitFor(); assert.equal(await input(1, '名称').inputValue(), '跨页草稿');
await page.evaluate(() => window.registryFixture.holdWrites()); await save();
await settings.getByRole('button', { name: '保存中…', exact: true }).waitFor();
assert.equal(await input(1, '地址').isDisabled(), true); await displayTab();
assert.equal(await settings.getByRole('combobox', { name: '版本', exact: true }).isDisabled(), true);
await page.evaluate(() => window.registryFixture.releaseWrite()); await saved();
assert.equal(await page.evaluate(() => window.registryFixture.state().value.detailTabs.versions), 'hide');
await sourceTab(); await edit(1); await input(1, '名称').fill('来源 A'); await save(); await saved();
await edit(1); await input(1, '名称').fill('A 草稿');
await page.evaluate(() => window.registryFixture.externalSource()); await input(1, '名称').fill('来源 A');
assert.equal(await settings.getByRole('button', { name: '保存配置', exact: true }).isDisabled(), false);
await save(); await settings.getByText('配置未保存,可能已被其他页面修改。请重置修改后再试。').waitFor();
await settings.getByRole('button', { name: '重置修改', exact: true }).click();
// A pristine open editor must not bind to a different row after an external replacement.
await selected('选择来源 1:来源 A').check(); await save(); await saved(); await edit(1);
await page.evaluate(() => window.registryFixture.replaceSources());
await input(1, '名称').waitFor({ state: 'hidden' });
await edit(1); await input(1, '地址').fill('https://edited-replacement.example'); await save(); await saved();
assert.equal(await page.evaluate(() => window.registryFixture.state().value.registryUrl), 'https://b.example');
// Legacy current entry is not a saved index; selecting B explicitly retains it in draft.
await page.evaluate(() => window.registryFixture.legacyCurrent());
await selected('选择来源 1:未命名来源').waitFor(); assert.equal(await selected('选择来源 1:未命名来源').isChecked(), true);
await selected('选择来源 3:来源 B').check(); await save(); await saved();
const retained = await page.evaluate(() => window.registryFixture.state().value);
assert.equal(retained.registryUrl, 'https://b.example'); assert.equal(retained.savedRegistries[2].url, 'https://legacy.example');
assert.equal(retained.savedRegistries.some(source => 'savedIndex' in source), false);
await settings.getByRole('button', { name: '移除来源 3', exact: true }).click(); await save(); await saved();
assert.equal(await page.evaluate(() => window.registryFixture.state().value.registryUrl), 'https://b.example');
await settings.getByRole('button', { name: '移除来源 2', exact: true }).click(); await save(); await saved();
assert.equal(await page.evaluate(() => window.registryFixture.state().value.registryUrl), '');
// Validate a hidden editor and route the user to the exact failing row.
await settings.getByRole('button', { name: '添加来源', exact: true }).click(); await displayTab(); await save();
await input(2, '名称').waitFor(); await settings.getByText('请填写来源名称。', { exact: true }).waitFor();
await input(2, '名称').fill('重复'); await input(2, '地址').fill('https://a.example/'); await displayTab(); await save();
await settings.getByText('此来源地址已存在,请保留一个条目。').waitFor();
assert.equal(await settings.getByRole('tab', { name: '技能来源', exact: true }).getAttribute('aria-selected'), 'true');
assert.equal(await input(2, '地址').isVisible(), true);
await settings.getByRole('button', { name: '重置修改', exact: true }).click();
await page.evaluate(() => window.registryFixture.readonly());
assert.equal(await settings.getByRole('button', { name: '添加来源', exact: true }).isDisabled(), true);
await displayTab(); assert.equal(await settings.getByRole('combobox', { name: '版本', exact: true }).isDisabled(), true);
await page.setViewportSize({ width: 390, height: 844 });
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true);
if (process.env.DSH_PROBE_SCREENSHOT) await page.screenshot({ path: `${process.env.DSH_PROBE_SCREENSHOT}-mobile.png`, fullPage: true });
await page.evaluate(() => window.registryFixture.unavailable()); await page.getByText('当前连接无法编辑 Host 配置。', { exact: true }).waitFor();
assert.deepEqual(errors, []);
console.log(JSON.stringify({ passed: true, cases: ['single source rows and save-only selection', 'active edit survives empty URL', 'same-revision dirty reset', 'cross-tab draft and keyboard navigation', 'pending locks edits', 'CAS after external update', 'pristine editor invalidated after external row replacement', 'legacy current index preservation', 'active removal disconnect', 'hidden invalid row opens exact editor', 'readonly', 'narrow layout', 'missing service'] }));
} finally { await browser.close(); }