97 lines
3.8 KiB
JavaScript
97 lines
3.8 KiB
JavaScript
// 提示词封面编码(v3.26.34 抽到 src/image-prompt-cover.js):
|
||
// ① WebP 可用 ⇒ 用 WebP;
|
||
// ② WebP 不可用 ⇒ 用 JPEG(PNG 回退会导致 448px 仍 300KB);
|
||
// ③ 两者都不可用 ⇒ 原图兜底;
|
||
// ④ coverUrl:dataURL 直出、老引用走异步解析。
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const src = fs.readFileSync(path.join(__dirname, '..', 'src', 'image-prompt-cover.js'), 'utf8')
|
||
const sandbox = {}
|
||
new Function('window', src)(sandbox)
|
||
const { create } = sandbox.IWBPromptCover
|
||
|
||
const SRC = 'data:image/png;base64,AAAA'
|
||
const WEBP = 'data:image/webp;base64,' + 'W'.repeat(200)
|
||
const JPEG = 'data:image/jpeg;base64,' + 'J'.repeat(300)
|
||
const PNG = 'data:image/png;base64,' + 'P'.repeat(900)
|
||
|
||
;(async () => {
|
||
// ① WebP 可用 → 一次调用即返回 webp,不再尝试 jpeg
|
||
{
|
||
const calls = []
|
||
const api = create({
|
||
compress: async (s, c) => { calls.push(c.format); return c.format === 'webp' ? WEBP : JPEG },
|
||
resolve: async () => '',
|
||
})
|
||
const r = await api.put(SRC)
|
||
assert.strictEqual(r.ok, true)
|
||
assert.ok(r.ref.indexOf('data:image/webp') === 0, '应优先用 WebP,实际 ' + r.ref.slice(0, 24))
|
||
assert.deepStrictEqual(calls, ['webp'], 'WebP 成功时不应再编码 JPEG,实际 ' + calls.join(','))
|
||
}
|
||
|
||
// ② WebP 不可用(返回空)→ 落 JPEG
|
||
{
|
||
const calls = []
|
||
const api = create({
|
||
compress: async (s, c) => { calls.push(c.format); return c.format === 'webp' ? '' : JPEG },
|
||
resolve: async () => '',
|
||
})
|
||
const r = await api.put(SRC)
|
||
assert.ok(r.ref.indexOf('data:image/jpeg') === 0, 'WebP 不可用时应落 JPEG,实际 ' + r.ref.slice(0, 24))
|
||
assert.deepStrictEqual(calls, ['webp', 'jpeg'])
|
||
}
|
||
|
||
// ③ 编码器两种格式都退回 PNG → 若 JPEG 更小则取 JPEG(这就是 448px PNG 300KB 的修复点)
|
||
{
|
||
const api = create({
|
||
compress: async (s, c) => (c.format === 'webp' ? PNG : JPEG),
|
||
resolve: async () => '',
|
||
})
|
||
const r = await api.put(SRC)
|
||
assert.ok(r.ref.indexOf('data:image/jpeg') === 0, 'PNG 回退时应改用更小的 JPEG,实际 ' + r.ref.slice(0, 24))
|
||
assert.ok(r.ref.length < PNG.length, '结果应比 PNG 小')
|
||
}
|
||
|
||
// ④ 全部编码失败 → 原图兜底(不丢封面)
|
||
{
|
||
const api = create({ compress: async () => '', resolve: async () => '' })
|
||
const r = await api.put(SRC)
|
||
assert.strictEqual(r.ok, true)
|
||
assert.strictEqual(r.ref, SRC, '编码全失败时应保留原图')
|
||
}
|
||
|
||
// ⑤ 非 dataURL 输入 → 明确失败
|
||
{
|
||
const api = create({ compress: async () => WEBP, resolve: async () => '' })
|
||
const r = await api.put('https://example.com/a.png')
|
||
assert.strictEqual(r.ok, false, '外链不应被当作封面保存')
|
||
}
|
||
|
||
// ⑥ coverUrl:dataURL 直出,不触发 resolve
|
||
{
|
||
let resolved = 0
|
||
const api = create({ compress: async () => '', resolve: async () => { resolved++; return 'blob:x' } })
|
||
const r = await api.url(WEBP)
|
||
assert.strictEqual(r.ok, true)
|
||
assert.strictEqual(r.url, WEBP)
|
||
assert.strictEqual(resolved, 0, 'dataURL 不应走引用解析')
|
||
const r2 = await api.url({ __blob: 'imgblob_1' })
|
||
assert.strictEqual(r2.url, 'blob:x', '老引用应异步解析')
|
||
assert.strictEqual(resolved, 1)
|
||
}
|
||
|
||
// ⑦ 节点宽度映射(比例不定)
|
||
{
|
||
const api = create({ compress: async () => '', resolve: async () => '' })
|
||
assert.strictEqual(api.nodeWidthForAspect(2.2), 420)
|
||
assert.strictEqual(api.nodeWidthForAspect(1.6), 340)
|
||
assert.strictEqual(api.nodeWidthForAspect(1), 280)
|
||
assert.strictEqual(api.nodeWidthForAspect(0.7), 230)
|
||
assert.strictEqual(api.nodeWidthForAspect(0.45), 200)
|
||
}
|
||
|
||
console.log('image-prompt-cover checks passed(WebP优先 / JPEG兜底 / PNG回退取小 / 原图兜底 / 直出与引用 / 比例定宽)')
|
||
})()
|