Files
Clipboard/test/fav-list-limit.test.js
2026-09-12 13:59:12 +08:00

71 lines
3.0 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';
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');
const rendererSrc = fs.readFileSync(
path.join(__dirname, '..', 'renderer', 'renderer.js'), 'utf8'
);
const preloadSrc = fs.readFileSync(
path.join(__dirname, '..', 'preload.js'), 'utf8'
);
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('renderer 调用 favList 不传硬编码 limit', () => {
// displayLimit 改由配置驱动(默认 300renderer 不再硬编码 200。
// 主进程 clips:list / favorites:list 在 limit=0 时回退到 appConfig.displayLimit。
const reload = rendererSrc.match(/async\s+function\s+reload\s*\(\s*\)\s*\{[\s\S]*?\n\}/);
assert.ok(reload, '找不到 reload()');
const call = reload[0].match(/favList\s*\([^)]*\)/);
assert.ok(call, 'reload() 找不到 favList 调用');
assert.ok(
!/,\s*200\s*\)/.test(call[0]) && !/,\s*5000\s*\)/.test(call[0]),
`favList 不应再传硬编码 200/5000实际是: ${call[0]}`
);
});
check('renderer 收藏 Tab 的徽标不用 clips.length', () => {
// 切到 favorites 时,$favCount.textContent 不能来自 clips.length
// (被 200 截断后会失真)。
const favText = rendererSrc.match(/\$favCount\.textContent\s*=\s*[^;]+;/g) || [];
for (const line of favText) {
assert.ok(
!/clips\.length/.test(line),
`徽标计数值不能从 clips.length 取(限制 200 后会失真): ${line}`
);
}
});
check('setTab 切到 favorites 时先 updateFavCount 再 reload', () => {
const setTab = rendererSrc.match(/function\s+setTab\s*\(\s*\w+\s*\)\s*\{[\s\S]*?\n\}/);
assert.ok(setTab, '找不到 setTab');
// 切到 favorites 的分支里,两个调用都得出现;顺序 updateFavCount 必须先于 reload
const idxFav = setTab[0].indexOf("'favorites'");
assert.ok(idxFav >= 0, 'setTab 找不到 favorites 分支');
// 截 favorites 分支之后到函数末尾
const favBranch = setTab[0].slice(idxFav);
const idxCount = favBranch.indexOf('updateFavCount');
const idxReload = favBranch.indexOf('reload');
assert.ok(idxCount >= 0, '切到 favorites 时没调 updateFavCount()');
assert.ok(idxReload >= 0, '切到 favorites 时没调 reload()');
assert.ok(idxCount < idxReload, '必须先 updateFavCount 再 reload');
});
check('preload favList 把第二个参数 limit 透传给 IPC', () => {
// preload 的 favoritesApi.list 必须接收并转发 limit。
// 之前传 5000现在传 0主进程在 limit <= 0 时回退到 appConfig.displayLimit。
const call = preloadSrc.match(/list:\s*\(query,\s*limit\)\s*=>\s*ipcRenderer\.invoke\(\s*'favorites:list'[^)]*\)/);
assert.ok(call, 'preload favorites list 没有 (query, limit) 签名或没转发 limit');
assert.ok(
/limit:\s*limit\s*\|\|\s*0/.test(call[0]),
`preload favList 必须把 limit 透传limit: limit || 0实际: ${call[0]}`
);
});
console.log(`\n${passed} checks passed`);