| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- const test = require('node:test');
- const assert = require('node:assert/strict');
- const fs = require('node:fs');
- test('background imports account run history module', () => {
- const source = fs.readFileSync('background.js', 'utf8');
- assert.match(source, /background\/account-run-history\.js/);
- });
- test('account run history module exposes a factory', () => {
- const source = fs.readFileSync('background/account-run-history.js', 'utf8');
- const globalScope = {};
- const api = new Function('self', `${source}; return self.MultiPageBackgroundAccountRunHistory;`)(globalScope);
- assert.equal(typeof api?.createAccountRunHistoryHelpers, 'function');
- });
- test('account run history helper upgrades old records, keeps stopped items and stores normalized failed snapshot records', async () => {
- const source = fs.readFileSync('background/account-run-history.js', 'utf8');
- const globalScope = {};
- const api = new Function('self', `${source}; return self.MultiPageBackgroundAccountRunHistory;`)(globalScope);
- let storedHistory = [
- { email: 'old@example.com', password: 'old-pass', status: 'success', recordedAt: '2026-04-17T00:00:00.000Z' },
- { email: 'stop@example.com', password: 'stop-pass', status: 'stopped', recordedAt: '2026-04-17T00:10:00.000Z' },
- ];
- let fetchCalled = false;
- global.fetch = async () => {
- fetchCalled = true;
- throw new Error('should not call fetch');
- };
- const helpers = api.createAccountRunHistoryHelpers({
- ACCOUNT_RUN_HISTORY_STORAGE_KEY: 'accountRunHistory',
- addLog: async () => {},
- buildLocalHelperEndpoint: (baseUrl, path) => `${baseUrl}${path}`,
- chrome: {
- storage: {
- local: {
- get: async () => ({ accountRunHistory: storedHistory }),
- set: async (payload) => {
- storedHistory = payload.accountRunHistory;
- },
- },
- },
- },
- getErrorMessage: (error) => error?.message || String(error || ''),
- getState: async () => ({
- email: ' latest@example.com ',
- password: ' secret ',
- autoRunning: true,
- autoRunCurrentRun: 2,
- autoRunTotalRuns: 10,
- autoRunAttemptRun: 3,
- accountRunHistoryTextEnabled: false,
- accountRunHistoryHelperBaseUrl: '',
- }),
- normalizeAccountRunHistoryHelperBaseUrl: (value) => String(value || '').trim(),
- });
- const record = helpers.buildAccountRunHistoryRecord(
- {
- email: ' latest@example.com ',
- password: ' secret ',
- autoRunning: true,
- autoRunCurrentRun: 2,
- autoRunTotalRuns: 10,
- autoRunAttemptRun: 3,
- },
- 'step8_failed',
- '步骤 8:认证页进入了手机号页面,当前不是 OAuth 同意页,无法继续自动授权。'
- );
- assert.deepStrictEqual(record, {
- recordId: 'latest@example.com',
- email: 'latest@example.com',
- password: 'secret',
- finalStatus: 'failed',
- finishedAt: record.finishedAt,
- retryCount: 2,
- failureLabel: '出现手机号验证',
- failureDetail: '步骤 8:认证页进入了手机号页面,当前不是 OAuth 同意页,无法继续自动授权。',
- failedStep: 8,
- source: 'auto',
- autoRunContext: {
- currentRun: 2,
- totalRuns: 10,
- attemptRun: 3,
- },
- });
- const appended = await helpers.appendAccountRunRecord('step8_failed', null, '步骤 8:认证页进入了手机号页面,当前不是 OAuth 同意页,无法继续自动授权。');
- assert.equal(appended.email, 'latest@example.com');
- assert.equal(appended.finalStatus, 'failed');
- assert.equal(appended.failureLabel, '出现手机号验证');
- assert.equal(storedHistory.length, 3, '旧的 stopped 记录应在新结构中保留');
- assert.equal(storedHistory.some((item) => item.email === 'stop@example.com' && item.finalStatus === 'stopped'), true);
- assert.equal(storedHistory.some((item) => item.email === 'latest@example.com' && item.retryCount === 2), true);
- assert.equal(storedHistory.some((item) => item.email === 'old@example.com'), true);
- assert.equal(fetchCalled, false);
- assert.equal(helpers.shouldAppendAccountRunTextFile({ accountRunHistoryTextEnabled: false, accountRunHistoryHelperBaseUrl: 'http://127.0.0.1:17373' }), false);
- assert.equal(helpers.shouldAppendAccountRunTextFile({ accountRunHistoryTextEnabled: true, accountRunHistoryHelperBaseUrl: 'http://127.0.0.1:17373' }), true);
- const stoppedRecord = helpers.buildAccountRunHistoryRecord({ email: 'a@b.com', password: 'x' }, 'stopped', 'stop');
- assert.equal(stoppedRecord.recordId, 'a@b.com');
- assert.equal(stoppedRecord.email, 'a@b.com');
- assert.equal(stoppedRecord.password, 'x');
- assert.equal(stoppedRecord.finalStatus, 'stopped');
- assert.equal(stoppedRecord.retryCount, 0);
- assert.equal(stoppedRecord.failureLabel, '流程已停止');
- assert.equal(stoppedRecord.failureDetail, 'stop');
- assert.equal(stoppedRecord.failedStep, null);
- assert.equal(stoppedRecord.source, 'manual');
- assert.equal(stoppedRecord.autoRunContext, null);
- assert.ok(stoppedRecord.finishedAt);
- });
- test('account run history helper clears persisted records and syncs full snapshot payload to local helper', async () => {
- const source = fs.readFileSync('background/account-run-history.js', 'utf8');
- const globalScope = {};
- const api = new Function('self', `${source}; return self.MultiPageBackgroundAccountRunHistory;`)(globalScope);
- let storedHistory = [{
- recordId: 'user@example.com',
- email: 'user@example.com',
- password: 'secret',
- finalStatus: 'failed',
- finishedAt: '2026-04-17T01:00:00.000Z',
- retryCount: 1,
- failureLabel: '步骤 6 失败',
- failureDetail: '步骤 6:判断失败后已重试 2 次,仍未成功。',
- failedStep: 6,
- source: 'auto',
- autoRunContext: {
- currentRun: 1,
- totalRuns: 5,
- attemptRun: 2,
- },
- }];
- const fetchCalls = [];
- global.fetch = async (url, options = {}) => {
- fetchCalls.push({
- url,
- options,
- });
- return {
- ok: true,
- json: async () => ({
- ok: true,
- filePath: 'C:/tmp/account-run-history.json',
- }),
- };
- };
- const logs = [];
- const helpers = api.createAccountRunHistoryHelpers({
- ACCOUNT_RUN_HISTORY_STORAGE_KEY: 'accountRunHistory',
- addLog: async (message, level) => {
- logs.push({ message, level });
- },
- buildLocalHelperEndpoint: (baseUrl, path) => `${baseUrl}${path}`,
- chrome: {
- storage: {
- local: {
- get: async () => ({ accountRunHistory: storedHistory }),
- set: async (payload) => {
- storedHistory = payload.accountRunHistory;
- },
- },
- },
- },
- getErrorMessage: (error) => error?.message || String(error || ''),
- getState: async () => ({
- accountRunHistoryTextEnabled: true,
- accountRunHistoryHelperBaseUrl: 'http://127.0.0.1:17373',
- }),
- normalizeAccountRunHistoryHelperBaseUrl: (value) => String(value || '').trim(),
- });
- const payload = helpers.buildAccountRunHistorySnapshotPayload(storedHistory);
- assert.deepStrictEqual(payload.summary, {
- total: 1,
- success: 0,
- failed: 1,
- stopped: 0,
- retryTotal: 1,
- });
- const clearResult = await helpers.clearAccountRunHistory();
- assert.deepStrictEqual(clearResult, { clearedCount: 1 });
- assert.deepStrictEqual(storedHistory, []);
- assert.equal(fetchCalls.length, 1);
- assert.equal(fetchCalls[0].url, 'http://127.0.0.1:17373/sync-account-run-records');
- assert.deepStrictEqual(JSON.parse(fetchCalls[0].options.body), {
- generatedAt: JSON.parse(fetchCalls[0].options.body).generatedAt,
- summary: {
- total: 0,
- success: 0,
- failed: 0,
- stopped: 0,
- retryTotal: 0,
- },
- records: [],
- });
- assert.equal(logs[0].message, '账号记录快照已同步到本地:C:/tmp/account-run-history.json');
- });
|