fix(qdreader): split archive cron from sign cron
This commit is contained in:
@@ -3,9 +3,11 @@ 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 runPlugin({ content = '', store = {}, bucketStore = {}, requests = [], userId = 'user-a', listens = [], senderOverrides = {}, globals = {}, admin = true } = {}) {
|
||||
function runPlugin({ pluginCode = code, filename = pluginPath, content = '', store = {}, bucketStore = {}, requests = [], userId = 'user-a', listens = [], senderOverrides = {}, globals = {}, admin = true } = {}) {
|
||||
const replies = [];
|
||||
const calls = [];
|
||||
let requestIndex = 0;
|
||||
@@ -41,7 +43,7 @@ function runPlugin({ content = '', store = {}, bucketStore = {}, requests = [],
|
||||
...globals,
|
||||
};
|
||||
vm.createContext(ctx);
|
||||
vm.runInContext(code, ctx, { filename: pluginPath });
|
||||
vm.runInContext(pluginCode, ctx, { filename });
|
||||
return { replies, calls, store, bucketStore };
|
||||
}
|
||||
|
||||
@@ -225,16 +227,16 @@ test('quick submit rejects save when Autman JS has no user identity', () => {
|
||||
assert.equal(r.store.qdreader_user_bind_json, undefined);
|
||||
});
|
||||
|
||||
test('cron archives previous run log and clears daily logs before new midnight sign', () => {
|
||||
test('archive plugin archives previous run log and clears daily logs without running sign', () => {
|
||||
const previousRun = {
|
||||
runId: '20260518_cron',
|
||||
source: 'cron',
|
||||
startedAt: '2026-05-18T00:00:00.000Z',
|
||||
finishedAt: '2026-05-18T00:00:02.000Z',
|
||||
startedAt: '2026-05-18T07:15:00.000Z',
|
||||
finishedAt: '2026-05-18T07:15:02.000Z',
|
||||
total: 1,
|
||||
okCount: 1,
|
||||
failCount: 0,
|
||||
items: [{ uid: '12345', ok: true, msg: '昨日签到成功', time: '2026-05-18T00:00:01.000Z' }],
|
||||
items: [{ uid: '12345', ok: true, msg: '昨日签到成功', time: '2026-05-18T07:15:01.000Z' }],
|
||||
};
|
||||
const store = {
|
||||
qdreader_cookie_json: JSON.stringify({ '12345': cookie }),
|
||||
@@ -242,12 +244,14 @@ test('cron archives previous run log and clears daily logs before new midnight s
|
||||
qdreader_last_run_json: JSON.stringify(previousRun),
|
||||
qdreader_last_result_json: JSON.stringify({ '12345': JSON.stringify({ ok: true, msg: '昨日签到成功' }) }),
|
||||
};
|
||||
const r = runPlugin({ content: '', store, userId: 'cron' });
|
||||
const r = runPlugin({ pluginCode: archiveCode, filename: archivePluginPath, content: '', store, userId: 'cron' });
|
||||
assert.equal(r.calls.length, 0);
|
||||
assert.match(r.replies[0], /日志归档完成/);
|
||||
const archive = JSON.parse(r.store.qdreader_archive_json);
|
||||
assert.equal(archive['2026-05-18'].runId, '20260518_cron');
|
||||
assert.equal(archive['2026-05-18'].items[0].msg, '昨日签到成功');
|
||||
assert.equal(JSON.parse(r.store.qdreader_last_run_json).source, 'cron');
|
||||
assert.equal(JSON.parse(JSON.parse(r.store.qdreader_last_result_json)['12345']).msg, '签到成功');
|
||||
assert.deepEqual(JSON.parse(r.store.qdreader_last_run_json), {});
|
||||
assert.deepEqual(JSON.parse(r.store.qdreader_last_result_json), {});
|
||||
});
|
||||
|
||||
test('query falls back to archived sign results after daily log was cleared', () => {
|
||||
@@ -299,6 +303,7 @@ test('manual sign and cron sign both call sign gateway then qidian checkin', ()
|
||||
assert.equal(cron.calls.length, 2);
|
||||
assert.match(cron.replies[0], /启点读书签到结果/);
|
||||
assert.equal(JSON.parse(cron.store.qdreader_last_run_json).source, 'cron');
|
||||
assert.equal(cron.store.qdreader_archive_json, undefined);
|
||||
});
|
||||
|
||||
test('bucketGet/bucketSet fallback rejects when user identity is absent', () => {
|
||||
|
||||
Reference in New Issue
Block a user