Files
image-workbench-desktop/tests/iwb-prompt-collect.spec.js
qiqi 2cd52d1d4e
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 43s
refactor(promptlib): 收藏入口归插件(宿主不再自带,随插件启停)
按项目所有人要求:提示词库插件负责「管理与收集」的完整能力,宿主只保留基础接口。

宿主侧移除(全部 4 处入口 + 实现):
- 节点气泡星标(app.js 单选气泡的「添加到提示词库」)
- 「选中操作 ▸ 添加到提示词库」、「该节点 ▸ 添加该提示词」
- 提示词节点标题栏星标(src/ui-node-card.js)
- src/runtime-workbench-logic.js 的 addPromptToLibrary(约 45 行)与 ui-canvas-nodes 的 onFavorite 注入

宿主侧保留:promptlib 能力(list/save/insert/copy/notify/onChanged/file*)、prompts 状态与 api('prompts')(备份/迁移)、插件数据通道。

宿主侧补回(此前缺失):右键菜单重新渲染 ctx.commands 注册的插件命令(「插件命令」分区)。

插件 promptlib 1.13.1 → 1.14.0:
- capabilities 增 nodeActions / commands
- nodeActions「收藏为提示词」:仅对 type=prompt 且 text 非空的节点出现(气泡按钮)
- commands「收藏选中的提示词」:右键「插件命令」入口
- 收藏写入插件数据 plugin-data/promptlib.json(以现有数据为基底,保留 cover:{img} 与分组),
  新条目 group:'' 即未分组,并 notify 回执

测试:
- tests/iwb-prompt-collect.spec.js 改为 3 条新契约:插件提供气泡入口 + 宿主无自带入口;
  右键插件命令可用且宿主旧条目已移除;插件停用后两个入口同时消失
- canvas-meta-notice 的旧「点按钮报错回执」用例改为「宿主不得再渲染该入口」
- tests/_helpers/wait.js openDockPanel 加固(等插件宿主初始化,修并行偶发)
- 棘轮下调:app.js 8324 / Workbench 2178

验证:npm run verify:ci 全绿(含 e2e 冒烟 31 条)。
2026-11-27 10:06:03 +08:00

79 lines
4.5 KiB
JavaScript

// 提示词收藏(2026-11-27 起归插件):入口随插件启停,宿主不再自带
//
// 契约:
// ① 提示词节点选中后,气泡里的「收藏为提示词」由 promptlib 插件注册(nodeActions);
// ② 右键菜单出现插件命令「收藏选中的提示词」(ctx.commands);
// ③ 宿主不得保留自带的入库入口(气泡星标 / 右键「添加该提示词」/「添加到提示词库」);
// ④ 收藏写入插件数据(plugin-data/promptlib.json),默认 group 为空(未分组);
// ⑤ 插件停用 → 两个入口同时消失。
const { test, expect } = require('./_pw-coverage')
const W = require('./_helpers/wait')
test.use({ baseURL: process.env.IWB_BASE_URL || 'http://127.0.0.1:8787', viewport: { width: 1440, height: 1000 } })
const WS = { workspaces: [{ id: 'w1', name: 'W', nodes: [{ id: 'p1', type: 'prompt', x: 200, y: 200, data: { text: '收藏用提示词' } }], edges: [], groups: [] }], activeId: 'w1', rev: 1 }
async function boot (page) {
await page.addInitScript((ws) => {
localStorage.clear()
localStorage.setItem('iwb_web_workspaces', JSON.stringify(ws))
localStorage.setItem('iwb_ws_active', 'w1')
localStorage.setItem('iwb_ws_w1', JSON.stringify(ws.workspaces[0]))
}, WS)
await page.goto('/', { waitUntil: 'domcontentloaded' })
await W.waitHomeReady(page)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await page.waitForSelector('.iwb-canvas-topbar', { timeout: 20000 })
await page.waitForFunction(() => Boolean(window.IWBPluginHost) && window.IWBPluginHost.snapshot().initialized === true)
await page.waitForTimeout(800)
}
const readData = (page) => page.evaluate(async () => {
const d = await window.IWBPluginData.create('promptlib').read()
return { prompts: (d && d.prompts) || [], groups: (d && d.groups) || [] }
})
const pluginAction = (page) => page.locator('.iwb-node-floatbar-plugin[data-tip="收藏为提示词"]')
const hostEntry = (page) => page.locator('.iwb-node-floatbar button[aria-label="添加到提示词库"]')
test('气泡入口由插件提供;宿主不再自带;收藏进未分组', async ({ page }) => {
await boot(page)
await page.locator('[data-node-id="p1"] .iwb-node-head').click()
await expect(pluginAction(page), '插件应提供「收藏为提示词」气泡按钮').toBeVisible({ timeout: 10000 })
await expect(hostEntry(page), '宿主不应再有自带收藏入口').toHaveCount(0)
await pluginAction(page).click()
await expect.poll(async () => (await readData(page)).prompts.length, { timeout: 8000 }).toBe(1)
const d = await readData(page)
expect(d.prompts[0].text).toBe('收藏用提示词')
expect(String(d.prompts[0].group || ''), '默认未分组').toBe('')
})
test('右键「插件命令」里有收藏入口,且宿主菜单无旧条目', async ({ page }) => {
await boot(page)
await page.locator('[data-node-id="p1"] .iwb-node-head').click()
await page.locator('[data-node-id="p1"]').click({ button: 'right' })
const main = '.iwb-ctxmenu:not(.iwb-ctx-sub)'
await expect(page.locator(main)).toBeVisible({ timeout: 8000 })
const cmd = page.locator(main + ' .iwb-ctx-item', { hasText: '收藏选中的提示词' })
await expect(cmd, '插件命令应出现在右键菜单').toHaveCount(1)
await expect(page.locator(main + ' .iwb-ctx-item', { hasText: '添加该提示词' }), '宿主旧条目应已移除').toHaveCount(0)
await cmd.click()
await expect.poll(async () => (await readData(page)).prompts.length, { timeout: 8000 }).toBe(1)
expect(String((await readData(page)).prompts[0].group || '')).toBe('')
})
test('插件停用后两个入口同时消失', async ({ page }) => {
await boot(page)
await page.locator('[data-node-id="p1"] .iwb-node-head').click()
await expect(pluginAction(page)).toBeVisible({ timeout: 10000 })
await page.evaluate(() => window.IWBPluginHost.setEnabled('promptlib', false))
await page.waitForTimeout(400)
await page.locator('.iwb-canvas').click({ position: { x: 1200, y: 800 } })
await page.locator('[data-node-id="p1"] .iwb-node-head').click()
await expect(pluginAction(page), '停用后气泡入口应消失').toHaveCount(0)
await page.locator('[data-node-id="p1"]').click({ button: 'right' })
const main = '.iwb-ctxmenu:not(.iwb-ctx-sub)'
await expect(page.locator(main)).toBeVisible({ timeout: 8000 })
await expect(page.locator(main + ' .iwb-ctx-item', { hasText: '收藏选中的提示词' }), '停用后右键命令应消失').toHaveCount(0)
})