Files
qiqi 38a34ff533 feat(style-source): 风格库插件(资源库面板浏览 310 个 AI 绘画风格)
- 直连 malongan.github.io 的 styles.json 与预览图(CORS *),无后端、不改宿主 CSP
- 面板宽度自适应:网格 auto-fill 列数,详情按 480px 拐点切「覆盖 body / 右侧栏」
  判定用 ResizeObserver(拖侧栏时视口不变,@media 不响应)
- 按容器建实例:宿主侧栏与放大窗口可同时渲染同一面板,各自独立 RO,数据走模块级共享
- 变量替换:{占位符} 按 variables 字段生成输入框,留空保留原占位符
- 插入:预览图 blob→dataURL→createImageNode(ref);提示词 createNode('prompt') 插选中节点右侧
- 修 grid-auto-rows 默认 auto 导致图片未加载时卡片塌成横条的坑(显式 max-content)
- 加 tools/harness.mjs(离线合成宿主)与 tools/e2e-real.mjs(真实网络冒烟)
- 已发布 KV:plugin-style-source.fd634473ee66.js(v1.0.0,integrity 校验通过)
2026-11-02 09:18:05 +08:00

138 lines
5.9 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.
// 风格库真实网络冒烟(开发用,不进 CI)。
//
// harness.mjs 用 page.route 把资源和图片都本地化了 —— 好处是离线可复现,坏处是**掩盖了 CORS**。
// 这个脚本不拦截任何请求,直接打真实 GitHub Pages,验证:
// 1) 线上 styles.json 能拉到 310 条(含真实体积 1.1MB)
// 2) 缩略图真的能在 <img> 里解码出来(naturalWidth > 0,说明 CORS + referrerpolicy 没问题)
// 3) 「插入参考图」能把真实 webp 转成 dataURL 交给画布(长度 > 5KB,说明不是空图)
//
// 运行:node style-source/tools/e2e-real.mjs
// 需要外网(走系统代理时给 Chromium 传 proxy 即可;脚本会自动读 HTTP(S)_PROXY)
import { createRequire } from 'node:module'
import { createServer } from 'node:http'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const HERE = path.dirname(fileURLToPath(import.meta.url))
const REPO = path.resolve(HERE, '../..')
const HOST_REPO = '/Users/qiqi/open/github/image-workbench-web'
const PLUGIN_ENTRY = path.resolve(HERE, '../src/index.js')
let chromium = null
for (const base of [process.env.IWB_PLAYWRIGHT_ROOT, REPO, HOST_REPO, process.cwd()].filter(Boolean)) {
try { chromium = createRequire(path.join(base, 'package.json'))('playwright').chromium; break } catch { /* 试下一个 */ }
}
if (!chromium) {
console.error('✗ 找不到 playwright,设置 IWB_PLAYWRIGHT_ROOT 指向含 node_modules 的目录')
process.exit(2)
}
let failed = 0
const check = (name, ok, detail) => {
if (!ok) failed++
console.log((ok ? '✓ ' : '✗ ') + name + (detail ? ' ' + detail : ''))
}
const poll = async (fn, timeout = 40000, step = 250) => {
const end = Date.now() + timeout
for (;;) {
const v = await fn()
if (v) return v
if (Date.now() > end) return null
await new Promise((r) => setTimeout(r, step))
}
}
const HTML = `<!doctype html><html><head><meta charset="utf-8">
<style>html,body{margin:0;height:100%}#stage{display:flex;height:100vh}</style></head>
<body><div id="stage"></div></body></html>`
async function main() {
const server = createServer((req, res) => { res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' }); res.end(HTML) })
await new Promise((r) => server.listen(0, '127.0.0.1', r))
const port = server.address().port
const proxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy
const browser = await chromium.launch(proxy ? { proxy: { server: proxy } } : {})
const page = await browser.newPage({ viewport: { width: 1280, height: 900 } })
const netFail = []
page.on('requestfailed', (req) => {
if (req.url().includes('malongan.github.io')) netFail.push(req.url().slice(-40) + ' ' + (req.failure() || {}).errorText)
})
const errors = []
page.on('pageerror', (e) => errors.push(String(e)))
await page.goto(`http://127.0.0.1:${port}/`)
await page.evaluate(() => {
window.__calls = { panels: [], images: [], nodes: [] }
window.__nodes = [{ id: 'n1', type: 'prompt', x: 100, y: 100, data: { text: 'x' } }]
window.__sel = { id: 'n1', type: 'prompt', name: 'x' }
window.IWBPluginHost = { register(m, s) { window.__m = m; window.__s = s } }
})
await page.addScriptTag({ path: PLUGIN_ENTRY })
await page.evaluate(() => {
const ctx = {
id: window.__m.id,
settings: { get: (k) => (window.__m.settings[k] || {}).default },
storage: null,
ui: { registerPanel: (p) => window.__calls.panels.push(p) },
canvas: {
getNodes: () => window.__nodes,
getSelectedNode: () => window.__sel,
createNode: (t, x, y, d) => { window.__calls.nodes.push({ t, x, y, d }); return 'n_new' },
createImageNode: (p) => { window.__calls.images.push(p); return 'n_img' },
},
}
window.__s(ctx)
const host = document.createElement('div')
host.id = 'h'
host.style.cssText = 'width:980px;height:720px;display:flex;min-width:0'
document.getElementById('stage').appendChild(host)
window.__calls.panels[0].mount(host)
})
const gotData = await poll(async () => {
const n = await page.locator('#h .iwb-ss-card').count()
return n > 0 ? n : null
})
check('真实网络拉到数据并渲染卡片', Boolean(gotData), 'cards=' + gotData)
const status = await page.locator('#h .iwb-ss-status').innerText().catch(() => '')
check('总数显示 310', /310/.test(status), status.replace(/\n/g, ' '))
// 真实缩略图必须真的解码出来
const decoded = await poll(async () => {
const n = await page.evaluate(() => {
const imgs = Array.from(document.querySelectorAll('#h .iwb-ss-thumb'))
return imgs.filter((i) => i.complete && i.naturalWidth > 0).length
})
return n >= 3 ? n : null
}, 45000)
check('缩略图真实解码成功(≥3 张 naturalWidth>0)', Boolean(decoded), 'decoded=' + decoded)
// 真实 webp → dataURL
await page.locator('#h .iwb-ss-card').first().click()
await page.waitForTimeout(300)
await page.locator('#h .iwb-ss-dfoot .iwb-ss-btn').first().click()
const img = await poll(() => page.evaluate(() => window.__calls.images[0] || null), 30000)
check('真实图插入画布(createImageNode 被调用)', Boolean(img))
check('dataURL 是真实内容而非空图', Boolean(img) && String(img.dataUrl).length > 5000,
img ? 'len=' + String(img.dataUrl).length + ' head=' + String(img.dataUrl).slice(0, 22) : '')
check('无 GitHub 资源请求失败', netFail.length === 0, netFail.slice(0, 2).join(' | '))
check('无页面 JS 异常', errors.length === 0, errors.slice(0, 2).join(' | '))
const shot = path.resolve(HERE, 'shot-real.png')
await page.locator('#h').screenshot({ path: shot }).catch(() => {})
console.log('· 真实截图:' + shot)
await browser.close()
server.close()
console.log('')
console.log(failed === 0 ? '全部通过' : failed + ' 项失败')
process.exit(failed === 0 ? 0 : 1)
}
main().catch((e) => { console.error('崩溃:', e); process.exit(1) })