290 lines
14 KiB
JavaScript
290 lines
14 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 = [], userId = 'user-a', listens = [] } = {}) {
|
|
const replies = [];
|
|
const calls = [];
|
|
let requestIndex = 0;
|
|
let listenIndex = 0;
|
|
const ctx = {
|
|
console,
|
|
Buffer,
|
|
sender: {
|
|
reply: (msg) => replies.push(String(msg)),
|
|
getMessage: () => content,
|
|
getContent: () => content,
|
|
isAdmin: () => true,
|
|
getUserID: () => userId,
|
|
listen: () => (listenIndex < listens.length ? listens[listenIndex++] : 'q'),
|
|
},
|
|
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);
|
|
assert.equal(JSON.parse(r.bucketStore['hermes_qidian.qdreader_cookie_json'])['12345'], cookie);
|
|
assert.equal(JSON.parse(r.store.qdreader_user_bind_json)['12345'], 'user-a');
|
|
assert.equal(JSON.parse(r.bucketStore['hermes_qidian.qdreader_user_bind_json'])['12345'], 'user-a');
|
|
});
|
|
|
|
test('manual cookie save falls back to ywguid when QDHeader uid is null', () => {
|
|
const fallbackCookie = 'QDHeader=KG51bGwpfHg=; ywguid=54321; qdh=qdh; sessionyw=s; appId=10';
|
|
const r = runPlugin({ content: '启点ck ' + fallbackCookie });
|
|
assert.match(r.replies.join('\n'), /CK新增成功/);
|
|
assert.match(r.replies.join('\n'), /uid: 54321/);
|
|
assert.equal(JSON.parse(r.store.qdreader_cookie_json)['54321'], fallbackCookie);
|
|
});
|
|
|
|
test('manual cookie save accepts upstream JSON map format', () => {
|
|
const jsonCookie = 'QDHeader=KG51bGwpfHg=; ywguid=54321; qdh=qdh; sessionyw=s; appId=10';
|
|
const r = runPlugin({ content: '启点ck ' + JSON.stringify({ '475700975': jsonCookie }) });
|
|
assert.match(r.replies.join('\n'), /CK新增成功/);
|
|
assert.match(r.replies.join('\n'), /uid: 475700975/);
|
|
assert.equal(JSON.parse(r.store.qdreader_cookie_json)['475700975'], jsonCookie);
|
|
assert.equal(JSON.parse(r.bucketStore['hermes_qidian.qdreader_user_bind_json'])['475700975'], 'user-a');
|
|
});
|
|
|
|
test('manual cookie save falls back to QDHeader embedded UserId before ywguid', () => {
|
|
const streamQdheader = 'KG51bGwpfChudWxsKXwxMTc5fDI1NTZ8KG51bGwpfDE2LjEwfDB8aU9TL2lQaG9uZS8obnVsbCl8MHwobnVsbCl8KG51bGwpfDQ3NTcwMDk3NXwxNzc5MDA5MTM2MDAwfDB8KG51bGwpfHx8KG51bGwpfChudWxsKXww';
|
|
const streamCookie = `QDHeader=${streamQdheader}; ywguid=120098575768; qdh=qdh; sessionyw=s; appId=12`;
|
|
const r = runPlugin({ content: '启点ck ' + streamCookie });
|
|
assert.match(r.replies.join('\n'), /CK新增成功/);
|
|
assert.match(r.replies.join('\n'), /uid: 475700975/);
|
|
assert.equal(JSON.parse(r.store.qdreader_cookie_json)['475700975'], streamCookie);
|
|
});
|
|
|
|
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/);
|
|
assert.match(manual.replies[0], /记录时间:/);
|
|
assert.equal(JSON.parse(manual.store.qdreader_last_run_json).source, 'manual');
|
|
assert.equal(JSON.parse(manual.store.qdreader_last_run_json).okCount, 1);
|
|
assert.equal(JSON.parse(JSON.parse(manual.store.qdreader_last_result_json)['12345']).source, 'manual');
|
|
|
|
const cron = runPlugin({ content: '', store: { ...store } });
|
|
assert.equal(cron.calls.length, 2);
|
|
assert.match(cron.replies[0], /启点读书签到结果/);
|
|
assert.equal(JSON.parse(cron.store.qdreader_last_run_json).source, 'cron');
|
|
});
|
|
|
|
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['hermes_qidian.qdreader_cookie_json'])['12345'], cookie);
|
|
assert.equal(JSON.parse(bucketStore['hermes_qidian.qdreader_user_bind_json'])['12345'], undefined);
|
|
});
|
|
|
|
test('reads legacy otto bucket but writes new hermes_qidian bucket', () => {
|
|
const bucketStore = {
|
|
'otto.qdreader_cookie_json': JSON.stringify({ '12345': cookie }),
|
|
'otto.qdreader_user_bind_json': JSON.stringify({ '12345': 'user-a' }),
|
|
};
|
|
const r = runPlugin({ content: '启点账号', bucketStore, userId: 'user-a', listens: ['3', '1', '主号'] });
|
|
assert.match(r.replies.join('\n'), /已更新备注/);
|
|
assert.equal(JSON.parse(r.bucketStore['hermes_qidian.qdreader_remark_json'])['12345'], '主号');
|
|
});
|
|
|
|
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('account command shows only current user bound accounts without cookies', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie, '67890': cookie.replace(/12345/g, '67890') }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a', '67890': 'user-b' }),
|
|
qdreader_remark_json: JSON.stringify({ '12345': '主号', '67890': '副号' }),
|
|
};
|
|
const r = runPlugin({ content: '启点账号', store, userId: 'user-a', listens: ['1'] });
|
|
assert.match(r.replies[0], /启点账号管理/);
|
|
assert.match(r.replies[1], /当前用户共 1 个账号/);
|
|
assert.match(r.replies[1], /12\*\*\*45/);
|
|
assert.match(r.replies[1], /主号/);
|
|
assert.doesNotMatch(r.replies[1], /67\*\*\*90/);
|
|
assert.doesNotMatch(r.replies[1], /QDHeader=/);
|
|
});
|
|
|
|
test('query command shows last sign log not account management', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie, '67890': cookie.replace(/12345/g, '67890') }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a', '67890': 'user-b' }),
|
|
qdreader_remark_json: JSON.stringify({ '12345': '主号', '67890': '副号' }),
|
|
qdreader_last_result_json: JSON.stringify({
|
|
'12345': JSON.stringify({ ok: true, msg: '签到成功', time: '2026-05-17T00:00:00.000Z', source: 'cron', runId: 'r1' }),
|
|
'67890': JSON.stringify({ ok: false, msg: '失败', time: '2026-05-17T00:00:00.000Z', source: 'cron', runId: 'r1' }),
|
|
}),
|
|
qdreader_last_run_json: JSON.stringify({ source: 'cron', finishedAt: '2026-05-17T00:00:01.000Z', okCount: 1, failCount: 1, total: 2 }),
|
|
};
|
|
const r = runPlugin({ content: '启点查询', store, userId: 'user-a', listens: ['1'] });
|
|
assert.match(r.replies[0], /启点查询/);
|
|
assert.match(r.replies[1], /启点最近签到记录/);
|
|
assert.match(r.replies[1], /最近批次:定时任务/);
|
|
assert.match(r.replies[1], /12\*\*\*45/);
|
|
assert.match(r.replies[1], /主号/);
|
|
assert.match(r.replies[1], /签到成功/);
|
|
assert.doesNotMatch(r.replies[1], /67\*\*\*90/);
|
|
});
|
|
|
|
|
|
test('query menu can inspect a single bound account', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie, '67890': cookie.replace(/12345/g, '67890') }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a', '67890': 'user-a' }),
|
|
qdreader_remark_json: JSON.stringify({ '12345': '主号', '67890': '副号' }),
|
|
qdreader_last_result_json: JSON.stringify({
|
|
'12345': JSON.stringify({ ok: true, msg: '签到成功', time: '2026-05-17T00:00:00.000Z', source: 'cron', runId: 'r1' }),
|
|
'67890': JSON.stringify({ ok: false, msg: '失败', time: '2026-05-17T00:00:00.000Z', source: 'cron', runId: 'r1' }),
|
|
}),
|
|
qdreader_last_run_json: JSON.stringify({ source: 'cron', finishedAt: '2026-05-17T00:00:01.000Z', okCount: 1, failCount: 1, total: 2 }),
|
|
};
|
|
const r = runPlugin({ content: '启点查询', store, userId: 'user-a', listens: ['2', '2'] });
|
|
assert.match(r.replies[0], /启点查询/);
|
|
assert.match(r.replies[1], /请选择要查询的账号/);
|
|
assert.match(r.replies[1], /1\. 12\*\*\*45/);
|
|
assert.match(r.replies[1], /2\. 67\*\*\*90/);
|
|
assert.match(r.replies[2], /启点单账号最近签到记录/);
|
|
assert.match(r.replies[2], /67\*\*\*90/);
|
|
assert.match(r.replies[2], /副号/);
|
|
assert.doesNotMatch(r.replies[2], /12\*\*\*45/);
|
|
});
|
|
|
|
test('account menu prompts remark after add and can edit from detail', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a' }),
|
|
};
|
|
const add = runPlugin({ content: '启点账号', store: {}, userId: 'user-a', listens: ['2', cookie, '主号'] });
|
|
assert.match(add.replies.join('\n'), /是否为该账号添加备注/);
|
|
assert.equal(JSON.parse(add.store.qdreader_remark_json)['12345'], '主号');
|
|
const clear = runPlugin({ content: '启点账号', store: add.store, userId: 'user-a', listens: ['3', '1', '清空'] });
|
|
assert.match(clear.replies.join('\n'), /请选择要查看\/修改备注的账号/);
|
|
assert.match(clear.replies.join('\n'), /启点账号详情/);
|
|
assert.equal(JSON.parse(clear.store.qdreader_remark_json)['12345'], undefined);
|
|
});
|
|
|
|
|
|
test('failed sign exceptions are written to last execution log', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a' }),
|
|
};
|
|
const r = runPlugin({ content: '启点签到', store, userId: 'user-a', requests: [new Error('sign api down'), new Error('sign api down')] });
|
|
assert.match(r.replies[0], /sign api down/);
|
|
const lastRun = JSON.parse(r.store.qdreader_last_run_json);
|
|
assert.equal(lastRun.failCount, 1);
|
|
assert.equal(lastRun.items[0].error, true);
|
|
const lastResult = JSON.parse(JSON.parse(r.store.qdreader_last_result_json)['12345']);
|
|
assert.equal(lastResult.ok, false);
|
|
assert.equal(lastResult.msg, 'sign api down');
|
|
});
|
|
|
|
test('legacy ck query points to separated commands', () => {
|
|
const r = runPlugin({ content: '启点ck 查询' });
|
|
assert.match(r.replies[0], /请使用一级菜单:启点账号 或 启点查询/);
|
|
});
|
|
|
|
|
|
test('delete account uses numbered selection instead of uid input', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie, '67890': cookie.replace(/12345/g, '67890') }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a', '67890': 'user-a' }),
|
|
qdreader_remark_json: JSON.stringify({ '12345': '主号', '67890': '副号' }),
|
|
};
|
|
const r = runPlugin({ content: '启点账号', store, userId: 'user-a', listens: ['4', '2'] });
|
|
assert.match(r.replies[1], /请选择要删除的账号/);
|
|
assert.match(r.replies[1], /1\. 12\*\*\*45/);
|
|
assert.match(r.replies[1], /2\. 67\*\*\*90/);
|
|
assert.match(r.replies[2], /已删除 uid: 67890/);
|
|
assert.deepEqual(JSON.parse(r.store.qdreader_cookie_json), { '12345': cookie });
|
|
});
|
|
|
|
test('delete all command clears only current user saved cookies', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie, '67890': cookie.replace(/12345/g, '67890') }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a', '67890': 'user-b' }),
|
|
qdreader_remark_json: JSON.stringify({ '12345': '主号', '67890': '副号' }),
|
|
};
|
|
const r = runPlugin({ content: '启点账号', store, userId: 'user-a', listens: ['5'] });
|
|
assert.match(r.replies[0], /启点账号管理/);
|
|
assert.match(r.replies[1], /已删除全部/);
|
|
assert.deepEqual(JSON.parse(r.store.qdreader_cookie_json), { '67890': cookie.replace(/12345/g, '67890') });
|
|
assert.deepEqual(JSON.parse(r.store.qdreader_user_bind_json), { '67890': 'user-b' });
|
|
assert.deepEqual(JSON.parse(r.store.qdreader_remark_json), { '67890': '副号' });
|
|
});
|
|
|
|
test('manual sign only runs current user accounts while cron runs all accounts', () => {
|
|
const store = {
|
|
qdreader_cookie_json: JSON.stringify({ '12345': cookie, '67890': cookie.replace(/12345/g, '67890') }),
|
|
qdreader_user_bind_json: JSON.stringify({ '12345': 'user-a', '67890': 'user-b' }),
|
|
};
|
|
const manual = runPlugin({ content: '启点签到', store: { ...store }, userId: 'user-a' });
|
|
assert.equal(manual.calls.length, 2);
|
|
assert.match(manual.replies[0], /总计 1/);
|
|
const cron = runPlugin({ content: '', store: { ...store }, userId: 'cron' });
|
|
assert.equal(cron.calls.length, 4);
|
|
assert.match(cron.replies[0], /总计 2/);
|
|
});
|