Files
autman-plugins/tests/qdreader_plugin.test.mjs
T

121 lines
4.7 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import vm from 'node:vm';
const pluginPath = new URL('../plugins/qdreader/qdreader_sign_autman.js', import.meta.url).pathname;
const code = fs.readFileSync(pluginPath, 'utf8');
function runPlugin({ content = '', store = {}, bucketStore = {}, requests = [] } = {}) {
const replies = [];
const calls = [];
let requestIndex = 0;
const ctx = {
console,
Buffer,
sender: {
reply: (msg) => replies.push(String(msg)),
getMessage: () => content,
getContent: () => content,
isAdmin: () => true,
},
GetContent: () => content,
getContent: () => content,
get: (key) => store[key] || '',
set: (key, value) => { store[key] = String(value); },
bucketGet: (bucket, key) => bucketStore[`${bucket}.${key}`] || '',
bucketSet: (bucket, key, value) => { bucketStore[`${bucket}.${key}`] = String(value); },
request: (req) => {
calls.push(req);
const item = requests[requestIndex++];
if (item instanceof Error) throw item;
if (typeof item === 'function') return item(req, calls.length);
if (item !== undefined) return item;
if (String(req.url).includes('/sign')) return { body: { code: 0, data: { headers: { 'QD-Sign': 'sig' } } } };
return { statusCode: 200, body: { code: 0, msg: '签到成功' } };
},
};
vm.createContext(ctx);
vm.runInContext(code, ctx, { filename: pluginPath });
return { replies, calls, store, bucketStore };
}
const cookie = 'uid=12345; QDHeader=MTIzNDV8eA==; qdh=qdh; ywguid=guid; qimei=qimei; qimei36=qimei36; sessionyw=s; appId=10';
function test(name, fn) {
try {
fn();
console.log('ok -', name);
} catch (err) {
console.error('not ok -', name);
throw err;
}
}
test('manual cookie save masks cookie and stores by uid', () => {
const r = runPlugin({ content: '起点ck ' + cookie });
assert.match(r.replies.join('\n'), /CK新增成功/);
assert.match(r.replies.join('\n'), /uid: 12345/);
assert.doesNotMatch(r.replies.join('\n'), /sessionyw=s; appId=10/);
assert.equal(JSON.parse(r.store.qdreader_cookie_json)['12345'], cookie);
});
test('manual sign and cron sign both call sign gateway then qidian checkin', () => {
const store = { qdreader_cookie_json: JSON.stringify({ '12345': cookie }) };
const manual = runPlugin({ content: '起点签到', store: { ...store } });
assert.equal(manual.calls.length, 2);
assert.match(manual.calls[0].url, /api\.120399\.xyz\/sign$/);
assert.match(manual.calls[1].url, /h5\.if\.qidian\.com\/argus\/api\/v3\/checkin\/checkin$/);
assert.match(manual.replies[0], /✅ 12\*\*\*45/);
const cron = runPlugin({ content: '', store: { ...store } });
assert.equal(cron.calls.length, 2);
assert.match(cron.replies[0], /起点读书签到结果/);
});
test('bucketGet/bucketSet fallback works when get/set are unavailable in Autman-like env', () => {
const replies = [];
const bucketStore = {};
const ctx = {
console,
Buffer,
sender: { reply: (msg) => replies.push(String(msg)), getMessage: () => '起点ck ' + cookie },
bucketGet: (bucket, key) => bucketStore[`${bucket}.${key}`] || '',
bucketSet: (bucket, key, value) => { bucketStore[`${bucket}.${key}`] = String(value); },
request: () => ({ body: { code: 0, data: { headers: {} } } }),
};
vm.createContext(ctx);
vm.runInContext(code, ctx, { filename: pluginPath });
assert.match(replies.join('\n'), /CK新增成功/);
assert.equal(JSON.parse(bucketStore['otto.qdreader_cookie_json'])['12345'], cookie);
});
test('qidian request retries transient request errors', () => {
const store = { qdreader_cookie_json: JSON.stringify({ '12345': cookie }), qdreader_retry_count: '2' };
const r = runPlugin({
content: '起点签到',
store,
requests: [
{ body: { code: 0, data: { headers: { 'QD-Sign': 'sig' } } } },
new Error('timeout'),
{ statusCode: 200, body: { code: 0, msg: '重试后成功' } },
],
});
assert.equal(r.calls.length, 3);
assert.match(r.replies[0], /重试后成功/);
});
test('status command shows account count and masked uid list without cookies', () => {
const store = { qdreader_cookie_json: JSON.stringify({ '12345': cookie }) };
const r = runPlugin({ content: '起点ck 查询', store });
assert.match(r.replies[0], /共 1 个账号/);
assert.match(r.replies[0], /12\*\*\*45/);
assert.doesNotMatch(r.replies[0], /QDHeader=/);
});
test('delete all command clears saved cookies', () => {
const store = { qdreader_cookie_json: JSON.stringify({ '12345': cookie, '67890': cookie.replace('12345', '67890') }) };
const r = runPlugin({ content: '起点ck 删除 all', store });
assert.match(r.replies[0], /已删除全部/);
assert.deepEqual(JSON.parse(r.store.qdreader_cookie_json), {});
});