test: lock qdreader qx notification contract

This commit is contained in:
2026-05-17 17:09:21 +08:00
parent a079b5249e
commit e746135eca
+23 -9
View File
@@ -4,32 +4,33 @@ import vm from 'node:vm';
const code = fs.readFileSync(new URL('../js/qdreader_cookie.js', import.meta.url), 'utf8'); const code = fs.readFileSync(new URL('../js/qdreader_cookie.js', import.meta.url), 'utf8');
function run({ cookie = '', body = '' }) { function run({ cookie = '', body = '', headerName = 'Cookie' }) {
const notices = []; const notices = [];
const donePayloads = [];
const context = { const context = {
$request: { headers: { Cookie: cookie } }, $request: { headers: { [headerName]: cookie } },
$response: { body }, $response: { body },
$notify: (...args) => notices.push(args), $notify: (...args) => notices.push(args),
$done: () => {}, $done: (payload) => { donePayloads.push(payload); },
atob: (s) => Buffer.from(s, 'base64').toString('binary'), atob: (s) => Buffer.from(s, 'base64').toString('binary'),
encodeURIComponent, encodeURIComponent,
decodeURIComponent, decodeURIComponent,
escape, escape,
}; };
vm.runInNewContext(code, context); vm.runInNewContext(code, context);
return notices; return { notices, donePayloads };
} }
{ {
const qdheader = Buffer.from('123456|device|more').toString('base64').replace(/=+$/, ''); const qdheader = Buffer.from('123456|device|more').toString('base64').replace(/=+$/, '');
const notices = run({ cookie: `QDHeader=${encodeURIComponent(qdheader)}; QDH=abc` }); const { notices } = run({ cookie: `QDHeader=${encodeURIComponent(qdheader)}; QDH=abc` });
assert.equal(notices[0][1], '抓取成功'); assert.equal(notices[0][1], '抓取成功');
assert.match(notices[0][2], /^启点ck /); assert.match(notices[0][2], /^启点ck /);
assert.equal(notices[0].length, 3); assert.equal(notices[0].length, 3);
} }
{ {
const notices = run({ const { notices } = run({
cookie: 'cmfuToken=abc; QDH=def', cookie: 'cmfuToken=abc; QDH=def',
body: JSON.stringify({ Data: { UserId: 987654 } }), body: JSON.stringify({ Data: { UserId: 987654 } }),
}); });
@@ -37,7 +38,7 @@ function run({ cookie = '', body = '' }) {
} }
{ {
const notices = run({ const { notices } = run({
cookie: 'cmfuToken=abc; QDH=def', cookie: 'cmfuToken=abc; QDH=def',
body: JSON.stringify({ Data: { UserInfo: { UserId: 24680 } } }), body: JSON.stringify({ Data: { UserInfo: { UserId: 24680 } } }),
}); });
@@ -46,12 +47,25 @@ function run({ cookie = '', body = '' }) {
{ {
const qdheader = Buffer.from('(null)|(null)|1179|2556|(null)|16.10|0|iOS/iPhone/(null)|0|(null)|(null)|475700975|1779006621000|0|(null)|||(null)|(null)|0').toString('base64').replace(/=+$/, ''); const qdheader = Buffer.from('(null)|(null)|1179|2556|(null)|16.10|0|iOS/iPhone/(null)|0|(null)|(null)|475700975|1779006621000|0|(null)|||(null)|(null)|0').toString('base64').replace(/=+$/, '');
const notices = run({ cookie: `QDHeader=${qdheader}; ywguid=120098575768; QDH=abc` }); const { notices } = run({ cookie: `QDHeader=${qdheader}; ywguid=120098575768; QDH=abc` });
assert.equal(notices[0][1], '抓取成功'); assert.equal(notices[0][1], '抓取成功');
} }
{ {
const notices = run({ cookie: 'cmfuToken=abc; QDH=def', body: '{}' }); const { notices, donePayloads } = run({
cookie: 'cmfuToken=abc; QDH=def',
body: JSON.stringify({ Data: { UserId: 135790 } }),
headerName: 'cookie',
});
assert.equal(notices[0][0], '启点读书');
assert.equal(notices[0][1], '抓取成功');
assert.equal(notices[0][2], '启点ck cmfuToken=abc; QDH=def');
assert.equal(notices[0].length, 3);
assert.equal(JSON.stringify(donePayloads[0]), '{}');
}
{
const { notices } = run({ cookie: 'cmfuToken=abc; QDH=def', body: '{}' });
assert.equal(notices[0][1], 'Cookie 获取失败'); assert.equal(notices[0][1], 'Cookie 获取失败');
assert.match(notices[0][2], /无法识别 uid/); assert.match(notices[0][2], /无法识别 uid/);
} }