Files
Clipboard/test/clip-payload.test.js
2026-09-12 13:59:12 +08:00

65 lines
2.5 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.
'use strict';
/**
* 回归测试:图片条目复制到剪贴板
*
* 曾经的 bugcopyClipToClipboard 先 rowToClip(row) 再读 clip.image
* 但 rowToClip 产出的字段叫 imageBase64没有 .image。
* Buffer.from(undefined) 抛 TypeError被 catch 吞掉 —— 所有图片记录
* 的"复制"都静默失败,用户按 Ctrl+V 粘到的是上一次剪贴板里的东西。
*/
const assert = require('node:assert');
const { clipPayload } = require('../lib/clip-payload.js');
let passed = 0;
function check(name, fn) {
try { fn(); console.log(' ok -', name); passed++; }
catch (e) { console.error(' FAIL -', name, e.message); process.exitCode = 1; }
}
check('text row yields its text', () => {
const p = clipPayload({ type: 'text', text: 'hello', image: null });
assert.deepStrictEqual(p, { kind: 'text', text: 'hello' });
});
check('text row with null text yields empty string, not crash', () => {
assert.deepStrictEqual(clipPayload({ type: 'text', text: null }), { kind: 'text', text: '' });
});
check('image row yields the raw BLOB as a Buffer', () => {
const blob = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
const p = clipPayload({ type: 'image', text: null, image: blob });
assert.strictEqual(p.kind, 'image');
assert.ok(Buffer.isBuffer(p.buffer));
assert.deepStrictEqual([...p.buffer], [0x89, 0x50, 0x4e, 0x47]);
});
check('image row survives a Uint8Array (what sqlite may hand back)', () => {
const p = clipPayload({ type: 'image', image: new Uint8Array([1, 2, 3]) });
assert.ok(Buffer.isBuffer(p.buffer));
assert.deepStrictEqual([...p.buffer], [1, 2, 3]);
});
// 这条正是当年的失败模式:把 rowToClip 的产物喂进来(只有 imageBase64没有 image
check('a row missing the image column throws a clear error, not a TypeError', () => {
const rowToClipShaped = { type: 'image', text: '', imageBase64: 'iVBORw0KGgo=', preview: '[图片]' };
assert.throws(() => clipPayload(rowToClipShaped), (e) => {
assert.ok(!(e instanceof TypeError), '不应是 Buffer.from(undefined) 那种 TypeError');
assert.match(e.message, /图片数据为空/);
return true;
});
});
check('empty image blob is rejected', () => {
assert.throws(() => clipPayload({ type: 'image', image: Buffer.alloc(0) }), /图片数据为空/);
});
check('missing row is rejected', () => {
assert.throws(() => clipPayload(null), /记录不存在/);
});
check('unknown type is rejected', () => {
assert.throws(() => clipPayload({ type: 'video' }), /未知的记录类型/);
});
console.log(`\n${passed} checks passed`);