Files
image-workbench-desktop/tests/iwb-download-cors.spec.js
T

82 lines
5.5 KiB
JavaScript
Raw 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.
// v3.87.34 契约:CORS 受限图床(豆包/火山 TOS)也能下载 —— 走图片代理兜底并真的触发下载
const { test, expect } = require('./_pw-coverage')
const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787'
test.use({ baseURL, viewport: { width: 1440, height: 1000 }, acceptDownloads: true })
const PNG = Buffer.from('89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000a49444154789c6360000002000100ffff03000006000557bfabd40000000049454e44ae426082', 'hex')
test('直连失败的图床(403/无 CORS):经图片代理下载成功', async ({ page }) => {
let proxyHits = 0
await page.route('**/cdn.example.com/**', async (route) => {
// 确定性失败(等价于火山 TOS 的 403/无 CORS 头):直连必然拿不到图
await route.fulfill({ status: 403, contentType: 'text/plain', body: 'blocked' })
})
// 本站同源代理(生产由 functions/api/img.js 实现;e2e 里直接拦截该路径)
await page.route('**/api/img?**', async (route) => {
proxyHits++
await route.fulfill({ status: 200, contentType: 'image/png', headers: { 'Access-Control-Allow-Origin': '*' }, body: PNG })
})
await page.addInitScript(() => { localStorage.clear() })
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForTimeout(700)
// 直接调用应用的下载函数(与预览/图库/自动保存共用同一实现)
const dl = page.waitForEvent('download', { timeout: 15000 })
await page.evaluate(() => window.__IWB_E2E_DOWNLOAD__('https://cdn.example.com/tos-blocked.png', 'doubao.png'))
const d = await dl
expect(d.suggestedFilename()).toBe('doubao.png')
expect(proxyHits, 'CORS 失败后应走本站同源代理(/api/img)').toBeGreaterThanOrEqual(1)
})
// v3.24.64(用户 2026-11-11 实测):上游已会返回 .svg,而下载入口一律传 'image.png' 这类硬编码名。
// 契约:下载名后缀必须跟着**真实格式**走,不得把矢量内容存成 .png。
test('SVG 结果:下载名后缀跟着真实格式(不再写死 .png)', async ({ page }) => {
const SVG = Buffer.from('<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="64" height="32"><rect width="64" height="32" fill="#0af"/></svg>', 'utf8')
await page.route('**/cdn.example.com/**', async (route) => {
await route.fulfill({ status: 200, contentType: 'image/svg+xml', headers: { 'Access-Control-Allow-Origin': '*' }, body: SVG })
})
await page.addInitScript(() => { localStorage.clear() })
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForFunction(() => typeof window.__IWB_E2E_DOWNLOAD__ === 'function')
const dl = page.waitForEvent('download', { timeout: 15000 })
await page.evaluate(() => window.__IWB_E2E_DOWNLOAD__('https://cdn.example.com/gen-1.svg', 'image.png'))
const d = await dl
expect(d.suggestedFilename()).toBe('image.svg')
})
// v3.24.65(用户 2026-11-11 实测):图库 / 大图预览 / 输出缩略图传进下载入口的是**本站自建的
// objectURL**(`blob:...`,字符串里只有 uuid,没有任何格式线索)⇒ 此前一律退回硬编码的 .png。
// 契约:blob: 地址必须从 Blob 注册表取回 type 再定后缀;且预览下载要拿**原图**而不是占位缩略图。
test('图库原图是 SVG:大图预览下载名 = image.svg(blob: 也按真实格式命名)', async ({ page }) => {
const svgText = '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="32"><rect width="64" height="32" fill="#0af"/></svg>'
const svgDataUrl = 'data:image/svg+xml;base64,' + Buffer.from(svgText, 'utf8').toString('base64')
await page.addInitScript(() => {
localStorage.clear()
localStorage.setItem('iwb_web_workspaces', JSON.stringify({
workspaces: [{ id: 'ws_svgdl', name: 'SVG下载板', nodes: [], edges: [], groups: [] }],
activeId: 'ws_svgdl', rev: 1, schemaVersion: 1,
}))
// 索引条目形态照抄线上(storeOutputThumb):缩略图 jpeg/png 存 out_,原图存 out_orig_
localStorage.setItem('iwb_web_outputs', JSON.stringify([
{ name: 'svg_out.png', url: 'https://cdn.example.com/svg_out.svg', size: 300, mtime: Date.now(), w: 64, h: 32, idb: 'out_svg_out.png', origIdb: 'out_orig_svg_out.png' },
]))
})
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForSelector('.iwb-home-card', { timeout: 25000 })
await page.locator('.iwb-home-card', { hasText: 'SVG下载板' }).first().dblclick()
await page.waitForSelector('.iwb-canvas-topbar', { timeout: 25000 })
await page.evaluate(async (dataUrl) => {
const c = document.createElement('canvas'); c.width = 64; c.height = 32
const g = c.getContext('2d'); g.fillStyle = '#2b7'; g.fillRect(0, 0, 64, 32)
const store = window.IWBStorageIndexedDB.create({})
await store.put('out_svg_out.png', c.toDataURL('image/png'))
await store.put('out_orig_svg_out.png', dataUrl)
}, svgDataUrl)
await page.locator('.iwb-top-nav-icon[title*="资源中心"]').click()
await page.waitForSelector('.iwb-res-tabs', { timeout: 15000 })
await page.locator('.iwb-fullmask .iwb-res-tab').filter({ hasText: '图库' }).click()
await page.waitForSelector('.iwb-history-item img', { timeout: 15000 })
await page.locator('.iwb-history-item img').first().dblclick()
await page.waitForSelector('.iwb-imgpreview img', { timeout: 20000 })
const dl = page.waitForEvent('download', { timeout: 20000 })
await page.locator('.iwb-prev-bar button').filter({ hasText: '下载' }).click()
expect((await dl).suggestedFilename()).toBe('image.svg')
})