Files
peoplelib/test-search.js
lofyerandfactory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> b8c8d24107 feat: 完善本地书库与发布更新流程
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2026-07-28 21:55:16 +08:00

106 lines
3.7 KiB
JavaScript

// 临时测试脚本:完全模拟 main.js 的初始化流程,跑全部数据源搜索
const { app, session } = require('electron');
const path = require('path');
app.setName('PeopleLib');
const userDataDir = path.join(app.getPath('appData'), 'PeopleLib');
app.setPath('userData', userDataDir);
const sources = require('./src/sources');
const zlibAuth = require('./src/sources/zlib-auth');
const settings = require('./src/settings');
const { setProxy, getProxy } = require('./src/sources/http');
zlibAuth.init(userDataDir);
settings.init(userDataDir);
// 允许通过命令行覆盖代理:electron test-search.js --proxy http://localhost:7897
const argv = process.argv.slice(2);
const pIdx = argv.indexOf('--proxy');
const proxyOverride = pIdx >= 0 ? argv[pIdx + 1] : null;
setProxy(proxyOverride !== null ? proxyOverride : settings.get('proxy', ''));
function mask(s) {
s = String(s || '');
return s.length > 8 ? s.slice(0, 4) + '***' + s.slice(-2) : '***';
}
const CASES = [
{ id: 'motw', kw: 'marx' },
{ id: 'scihub', kw: '10.1038/nature12373' },
{ id: 'libgen', kw: 'godel escher bach' },
{ id: 'zlib', kw: 'godel' },
{ id: 'gutenberg', kw: 'alice' },
{ id: 'arxiv', kw: 'transformer' }
];
async function runCase(c) {
const label = `[${c.id}]`;
const t0 = Date.now();
try {
const src = sources.getSource(c.id);
const r = await src.search(c.kw, 1);
const n = (r.items || []).length;
console.log(`${label} search("${c.kw}") -> ${n} items, maxPage=${r.maxPage} (${Date.now() - t0}ms)`);
if (!n) {
console.log(`${label} !! EMPTY RESULT`);
return { id: c.id, ok: false, reason: 'empty' };
}
for (const it of r.items.slice(0, 2)) {
console.log(`${label} - ${String(it.title).slice(0, 70)} | ${String(it.subtitle || '').slice(0, 40)}`);
}
// 顺带验证 detail + download 链路
const first = r.items[0];
try {
const d = await src.detail(first.postId);
console.log(`${label} detail OK: ${String(d.title).slice(0, 60)}`);
} catch (e) {
console.log(`${label} detail FAIL: ${e.message}`);
}
try {
const dl = await src.download(first.postId);
const files = (dl.files || []).length;
const links = (dl.links || []).length;
console.log(`${label} download OK: ${files} files, ${links} links`);
if (files) console.log(`${label} file: ${dl.files[0].name} -> ${String(dl.files[0].link).slice(0, 80)}`);
} catch (e) {
console.log(`${label} download FAIL: ${e.message}`);
}
return { id: c.id, ok: true, n };
} catch (e) {
console.log(`${label} SEARCH FAIL (${Date.now() - t0}ms): ${e.message}`);
return { id: c.id, ok: false, reason: e.message };
}
}
app.whenReady().then(async () => {
const p = getProxy();
console.log('=== 环境 ===');
console.log('userData:', userDataDir);
console.log('proxy(app):', p || '(空 -> 使用系统代理)');
if (p) {
await session.defaultSession.setProxy({ proxyRules: p }).catch(() => {});
}
const s = zlibAuth.getSession();
const creds = zlibAuth.read();
console.log('zlib creds:', creds && creds.email ? creds.email : '(无)');
console.log('zlib session:', s ? `userId=${s.userId} userKey=${mask(s.userKey)} mirror='${s.mirror}'` : '(无)');
console.log('');
const results = [];
for (const c of CASES) {
results.push(await runCase(c));
console.log('');
}
console.log('=== 汇总 ===');
for (const r of results) {
console.log(`${r.ok ? 'PASS' : 'FAIL'} ${r.id}${r.ok ? ` (${r.n})` : ` - ${r.reason}`}`);
}
const after = zlibAuth.getSession();
console.log('');
console.log('zlib session after:', after ? `mirror='${after.mirror}'` : '(无)');
app.exit(0);
});