初始化提交:go比特版本精简
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
async function loadStepWithResult(result) {
|
||||
const servicePath = require.resolve('../src/services/chatgpt_service');
|
||||
const screenshotPath = require.resolve('../src/utils/screenshot');
|
||||
const stepPath = require.resolve('../src/pipeline/steps/chatgpt_register');
|
||||
const service = require(servicePath);
|
||||
const screenshot = require(screenshotPath);
|
||||
const originalLogin = service.loginOrRegisterChatGPT;
|
||||
const originalScreenshot = screenshot.saveErrorScreenshot;
|
||||
service.loginOrRegisterChatGPT = async () => result;
|
||||
screenshot.saveErrorScreenshot = async () => null;
|
||||
delete require.cache[stepPath];
|
||||
const step = require(stepPath);
|
||||
service.loginOrRegisterChatGPT = originalLogin;
|
||||
screenshot.saveErrorScreenshot = originalScreenshot;
|
||||
return step;
|
||||
}
|
||||
|
||||
function makeContext({ saveResult = true, existingSession = null } = {}) {
|
||||
const writes = [];
|
||||
const statuses = [];
|
||||
const messages = [];
|
||||
const ctx = {
|
||||
account: { email: 'target@example.com' },
|
||||
page: { url: () => 'https://chatgpt.com/' },
|
||||
params: { age: 23, clearAuthState: false },
|
||||
log: () => {},
|
||||
results: {},
|
||||
shouldStop: false,
|
||||
dbWrite: async (name, args) => {
|
||||
writes.push({ name, args });
|
||||
if (name === 'getAccount') {
|
||||
return {
|
||||
chatgpt_session: existingSession,
|
||||
password_changed: 0,
|
||||
fa_changed: 0,
|
||||
};
|
||||
}
|
||||
if (name === 'updateAccountChatgptResult') {
|
||||
return args[1] === '失败' ? true : saveResult;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
updateStatus: async (_email, status) => { statuses.push(status); },
|
||||
updateMessage: async (_email, message) => { messages.push(message); },
|
||||
};
|
||||
return { ctx, writes, statuses, messages };
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const wrongSession = {
|
||||
user: { email: 'other@example.com' },
|
||||
expires: '2099-01-01T00:00:00.000Z',
|
||||
};
|
||||
const successExits = [
|
||||
{ name: 'existing-session', created: false, status: '已登录' },
|
||||
{ name: 'home-direct-session', created: false, status: '已登录' },
|
||||
{ name: 'home-probe-session', created: false, status: '已登录' },
|
||||
{ name: 'all-set-probe-session', created: true, status: '已注册' },
|
||||
];
|
||||
|
||||
for (const exit of successExits) {
|
||||
const step = await loadStepWithResult({
|
||||
ok: true,
|
||||
created: exit.created,
|
||||
status: exit.status,
|
||||
session: wrongSession,
|
||||
source: exit.name,
|
||||
});
|
||||
const { ctx, writes } = makeContext();
|
||||
await step(ctx);
|
||||
|
||||
assert.strictEqual(ctx.results.chatgptRegister.success, false, `${exit.name} 错邮箱Session不得成功`);
|
||||
assert.strictEqual(ctx.results.chatgptRegister.code, 'CHATGPT_ACCOUNT_MISMATCH');
|
||||
const persistedWrongSession = writes.some(item => item.name === 'updateAccountChatgptResult'
|
||||
&& item.args[1] !== '失败'
|
||||
&& item.args[2]);
|
||||
assert.strictEqual(persistedWrongSession, false, `${exit.name} 错邮箱Session不得落库`);
|
||||
}
|
||||
|
||||
const validStep = await loadStepWithResult({
|
||||
ok: true,
|
||||
created: false,
|
||||
status: '已登录',
|
||||
session: {
|
||||
user: { email: 'target@example.com' },
|
||||
expires: '2099-01-01T00:00:00.000Z',
|
||||
},
|
||||
});
|
||||
const validContext = makeContext();
|
||||
await validStep(validContext.ctx);
|
||||
assert.strictEqual(validContext.ctx.results.chatgptRegister.success, true);
|
||||
const validSave = validContext.writes.find(item => item.name === 'updateAccountChatgptResult'
|
||||
&& item.args[1] === '已登录');
|
||||
assert(validSave, '目标邮箱Session应正常保存');
|
||||
|
||||
const failedSaveStep = await loadStepWithResult({
|
||||
ok: true,
|
||||
created: false,
|
||||
status: '已登录',
|
||||
session: {
|
||||
user: { email: 'target@example.com' },
|
||||
expires: '2099-01-01T00:00:00.000Z',
|
||||
},
|
||||
});
|
||||
const failedSaveContext = makeContext({ saveResult: false });
|
||||
await failedSaveStep(failedSaveContext.ctx);
|
||||
assert.strictEqual(failedSaveContext.ctx.results.chatgptRegister.success, false);
|
||||
assert.strictEqual(failedSaveContext.ctx.results.chatgptRegister.code, 'DB_SAVE_FAILED');
|
||||
assert.strictEqual(failedSaveContext.ctx.shouldStop, true);
|
||||
assert(failedSaveContext.statuses.includes('ChatGPT失败'));
|
||||
assert(failedSaveContext.writes.some(item => item.name === 'updateAccountStatusReason'
|
||||
&& item.args[1] === 'DB_SAVE_FAILED'));
|
||||
|
||||
console.log('chatgpt_pipeline_session_guard.test.js: PASS');
|
||||
})().catch(error => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user