// 风格库真实网络冒烟(开发用,不进 CI)。 // // harness.mjs 用 page.route 把资源和图片都本地化了 —— 好处是离线可复现,坏处是**掩盖了 CORS**。 // 这个脚本不拦截任何请求,直接打真实 GitHub Pages,验证: // 1) 线上 styles.json 能拉到 310 条(含真实体积 1.1MB) // 2) 缩略图真的能在 里解码出来(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 = `
` 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) })