163 lines
7.8 KiB
JavaScript
163 lines
7.8 KiB
JavaScript
// 风格库真实网络冒烟(开发用,不进 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: [], variants: [] }
|
||
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) },
|
||
// v1.3.0:样式节点变体(宿主能力名 nodeTypes → ctx.nodes)
|
||
nodes: { register: (v) => { window.__calls.variants.push(v); return true } },
|
||
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(() => '')
|
||
// 数量随上游数据增长会变(310 → 367);这里只要求「显示的是三位数总量」
|
||
check('总数显示三位数风格条数', /\b[1-9]\d{2}\b/.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').filter({ hasText: '插入参考图' }).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) : '')
|
||
|
||
// v1.3.0:样式节点(真实数据 + 真实模板)
|
||
const styleCall = await page.evaluate(async () => {
|
||
const v = window.__calls.variants[0]
|
||
const btn = [...document.querySelectorAll('#h .iwb-ss-dfoot .iwb-ss-btn')].find((b) => b.textContent === '样式节点')
|
||
if (!btn) return { noBtn: true }
|
||
window.__calls.nodes.length = 0
|
||
btn.click()
|
||
await new Promise((r) => setTimeout(r, 60))
|
||
return { hasRender: typeof v.render === 'function', ports: v.ports, call: window.__calls.nodes[0] || null }
|
||
})
|
||
check('详情页有「样式节点」按钮,且变体已注册(render + 只出不入)',
|
||
!styleCall.noBtn && styleCall.hasRender === true && styleCall.ports.in === false && styleCall.ports.out === true,
|
||
JSON.stringify({ noBtn: styleCall.noBtn, hasRender: styleCall.hasRender, ports: styleCall.ports }))
|
||
check('样式节点落的是 plugin:style-source:style,数据带 outKind/promptTpl/varMeta',
|
||
Boolean(styleCall.call) && styleCall.call.t === 'plugin:style-source:style' &&
|
||
styleCall.call.d.outKind === 'prompt' && typeof styleCall.call.d.promptTpl === 'string' &&
|
||
Array.isArray(styleCall.call.d.varMeta),
|
||
styleCall.call ? 't=' + styleCall.call.t + ' varMeta=' + (styleCall.call.d.varMeta || []).length : '未调用')
|
||
check('未填变量时 text 原样保留 {占位符}(不是被替换成空串)',
|
||
Boolean(styleCall.call) && (styleCall.call.d.varMeta.length === 0 || /\{[^{}\n]{1,40}\}/.test(String(styleCall.call.d.text))),
|
||
styleCall.call ? 'text=' + String(styleCall.call.d.text).slice(0, 48) : '未调用')
|
||
|
||
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) })
|