// 设置 → 生图服务:渠道 / 模型 的增删(2026-11-10 新增) // // 为什么加:SettingsModal 里 37 个 CRUD setter 共 223 行,是全仓最大的"无直接测试"状态簇 // (此前只有 1 条 e2e 覆盖「厂商类型」,增删渠道/模型完全没测)。 // 这组用例是任何后续拆分(例如抽 useSettingsDraft)的**前置安全网**: // 先把行为钉住,再谈搬家。 // // 选择器依据实际 DOM(2026-11-10 探针确认): // · 渠道卡片 .iwb-provider-card;删除渠道 = 文案「删除服务」 // · 模型行 .iwb-modelrow;删除模型 = title「删除模型」(aria-label 带模型名) // · 添加模型 = 模型输入框所在行里的按钮(输入框 placeholder「手动添加模型名」) const { test, expect } = require('./_pw-coverage') const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787' test.use({ baseURL, viewport: { width: 1600, height: 1100 } }) const CFG = { providers: [{ id: 'p-crud', name: 'CRUD 渠道', baseUrl: 'https://crud.example/v1', keys: [{ id: 'k-1', name: 'K', value: 'sk-test' }], models: [{ id: 'm-1', name: 'crud-model-1' }], }], canvas: {}, } async function openSettings (page) { const cands = [ page.locator('.iwb-home-side-item', { hasText: '设置' }), page.locator('.iwb-home-menu-item', { hasText: '设置' }), page.locator('[title="服务与 Key 设置"]'), ] for (const c of cands) if (await c.count() > 0) { await c.first().click(); break } await expect(page.locator('button', { hasText: '+ 添加服务' }).first()).toBeVisible({ timeout: 10000 }) } const readCfg = (page) => page.evaluate(() => { const raw = localStorage.getItem('iwb_web_cfg') return raw ? JSON.parse(raw) : {} }) test.beforeEach(async ({ page }) => { await page.addInitScript((cfg) => { localStorage.clear() localStorage.setItem('iwb_web_cfg', JSON.stringify(cfg)) }, CFG) await page.goto('/', { waitUntil: 'domcontentloaded' }) }) test('① 渠道可新增与删除,且改动会写入配置', async ({ page }) => { const errors = [] page.on('pageerror', (e) => errors.push(String((e && e.message) || e))) await openSettings(page) const cards = page.locator('.iwb-provider-card') await expect(cards).toHaveCount(1, { timeout: 8000 }) await page.locator('button', { hasText: '+ 添加服务' }).first().click() await expect(cards).toHaveCount(2, { timeout: 8000 }) // 删最后一个(不动种子渠道) await page.locator('.iwb-provider-card button', { hasText: '删除服务' }).last().click() await expect(cards).toHaveCount(1, { timeout: 8000 }) await page.locator('button', { hasText: '保存配置' }).first().click() await expect.poll(async () => ((await readCfg(page)).providers || []).length, { timeout: 10000 }).toBe(1) const cfg = await readCfg(page) expect((cfg.providers || [])[0].name, '种子渠道应保留').toBe('CRUD 渠道') expect(errors, '不应有页面异常').toEqual([]) }) test('② 模型可新增与删除,且改动会写入配置', async ({ page }) => { const errors = [] page.on('pageerror', (e) => errors.push(String((e && e.message) || e))) await openSettings(page) // 删除模型走 window.confirm(delModel 里显式确认)—— Playwright 默认**自动取消**对话框, // 不接住就表现为"点了没反应"(2026-11-10 踩到)。这里统一接受。 page.on('dialog', (d) => d.accept()) const card = page.locator('.iwb-provider-card').first() const rows = card.locator('.iwb-modelrow') await expect(rows).toHaveCount(1, { timeout: 8000 }) // 注意顺序:点「保存配置」会关闭弹窗 ⇒ 增删都要在保存**之前**做完(2026-11-10 踩到) const input = card.locator('input[placeholder="手动添加模型名"]').first() // 用 xpath 取「输入框所在行」的按钮 —— 比 filter({has: card.locator(...)}) 稳: // 后者把 card 作用域嵌进 has 里,第二次使用时解析不到(2026-11-10 踩到超时) const addBtn = card.locator('input[placeholder="手动添加模型名"]').first() .locator('xpath=..').locator('button').first() // 新增 → 2 行 await input.fill('crud-model-2') await addBtn.click() await expect(rows).toHaveCount(2, { timeout: 8000 }) // 删除刚加的 → 回到 1 行 await card.locator('[title="删除模型"]').last().click() await expect(rows).toHaveCount(1, { timeout: 8000 }) // 再加一次并保存 → 配置里应含新模型(同时验证"删了再加"不会把列表搞坏) await input.fill('crud-model-3') await addBtn.click() await expect(rows).toHaveCount(2, { timeout: 8000 }) await page.locator('button', { hasText: '保存配置' }).first().click() await expect.poll(async () => { const p = ((await readCfg(page)).providers || [])[0] || {} return (p.models || []).map((m) => m.name).sort().join(',') }, { timeout: 10000 }).toBe('crud-model-1,crud-model-3') expect(errors, '不应有页面异常').toEqual([]) })