fix(qdreader): keep archive and sign in one plugin

This commit is contained in:
2026-05-19 15:35:24 +08:00
parent 22357b0568
commit 5b0f20fa24
7 changed files with 93 additions and 1593 deletions
+18 -7
View File
@@ -3,9 +3,20 @@ 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 archivePluginPath = new URL('../plugins/qdreader/qdreader_archive_autman.js', import.meta.url).pathname;
const code = fs.readFileSync(pluginPath, 'utf8');
const archiveCode = fs.readFileSync(archivePluginPath, 'utf8');
function fixedDateClass(iso) {
const RealDate = Date;
return class FixedDate extends RealDate {
constructor(...args) {
if (args.length === 0) super(iso);
else super(...args);
}
static now() { return new RealDate(iso).getTime(); }
static parse(value) { return RealDate.parse(value); }
static UTC(...args) { return RealDate.UTC(...args); }
};
}
function runPlugin({ pluginCode = code, filename = pluginPath, content = '', store = {}, bucketStore = {}, requests = [], userId = 'user-a', listens = [], senderOverrides = {}, globals = {}, admin = true } = {}) {
const replies = [];
@@ -227,7 +238,7 @@ test('quick submit rejects save when Autman JS has no user identity', () => {
assert.equal(r.store.qdreader_user_bind_json, undefined);
});
test('archive plugin archives previous run log and clears daily logs without running sign', () => {
test('midnight cron archives previous run log and clears daily logs without running sign', () => {
const previousRun = {
runId: '20260518_cron',
source: 'cron',
@@ -244,7 +255,7 @@ test('archive plugin archives previous run log and clears daily logs without run
qdreader_last_run_json: JSON.stringify(previousRun),
qdreader_last_result_json: JSON.stringify({ '12345': JSON.stringify({ ok: true, msg: '昨日签到成功' }) }),
};
const r = runPlugin({ pluginCode: archiveCode, filename: archivePluginPath, content: '', store, userId: 'cron' });
const r = runPlugin({ content: '', store, userId: 'cron', globals: { Date: fixedDateClass('2026-05-19T00:00:00.000+08:00') } });
assert.equal(r.calls.length, 0);
assert.match(r.replies[0], /日志归档完成/);
const archive = JSON.parse(r.store.qdreader_archive_json);
@@ -299,7 +310,7 @@ test('manual sign and cron sign both call sign gateway then qidian checkin', ()
assert.equal(payload.qdh, 'qdh');
assert.equal(payload.qdheader, 'MTIzNDV8eA==');
const cron = runPlugin({ content: '', store: { ...store } });
const cron = runPlugin({ content: '', store: { ...store }, globals: { Date: fixedDateClass('2026-05-19T07:15:00.000+08:00') } });
assert.equal(cron.calls.length, 2);
assert.match(cron.replies[0], /启点读书签到结果/);
assert.equal(JSON.parse(cron.store.qdreader_last_run_json).source, 'cron');
@@ -480,7 +491,7 @@ test('manual sign only runs current user accounts while cron runs all accounts',
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' });
const cron = runPlugin({ content: '', store: { ...store }, userId: 'cron', globals: { Date: fixedDateClass('2026-05-19T07:15:00.000+08:00') } });
assert.equal(cron.calls.length, 4);
assert.match(cron.replies[0], /总计 2/);
});
@@ -494,7 +505,7 @@ test('unbound global cookies are not visible or usable by normal users', () => {
assert.equal(manual.calls.length, 0);
assert.match(manual.replies[0], /还没有保存启点读书CK/);
const cron = runPlugin({ content: '', store: { ...store }, userId: 'cron' });
const cron = runPlugin({ content: '', store: { ...store }, userId: 'cron', globals: { Date: fixedDateClass('2026-05-19T07:15:00.000+08:00') } });
assert.equal(cron.calls.length, 2);
assert.match(cron.replies[0], /总计 1/);
});