Files
go_bt/tests/chatgpt_auth_reset.test.js

85 lines
3.6 KiB
JavaScript

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const chatgptService = require('../src/services/chatgpt_service');
(async () => {
const commands = [];
let clearCookiesCount = 0;
let detached = false;
let lastUrl = '';
const client = {
send: async (method, params) => {
commands.push({ method, params });
return {};
},
detach: async () => { detached = true; },
};
const context = {
clearCookies: async () => { clearCookiesCount++; },
newCDPSession: async () => client,
};
const page = {
context: () => context,
goto: async url => { lastUrl = url; },
};
await chatgptService.clearChatGPTAuthState(page, () => {});
assert.strictEqual(clearCookiesCount, 1, '应清除浏览器上下文的全部Cookie');
assert(commands.some(item => item.method === 'Network.clearBrowserCache'), '应清除浏览器缓存');
assert(commands.some(item => item.method === 'Network.clearBrowserCookies'), '应通过CDP再次清除浏览器Cookie');
assert(commands.some(item => item.method === 'Storage.clearDataForOrigin' && item.params.origin === 'https://chatgpt.com'), '应清除ChatGPT站点存储和Token');
assert.strictEqual(detached, true, '完成后应释放CDP会话');
assert.strictEqual(lastUrl, 'about:blank', '清理后不应继续停留在旧登录页面');
const servicePath = require.resolve('../src/services/chatgpt_service');
const stepPath = require.resolve('../src/pipeline/steps/chatgpt_register');
const serviceModule = require(servicePath);
const originalLogin = serviceModule.loginOrRegisterChatGPT;
const originalClear = serviceModule.clearChatGPTAuthState;
let resetCalled = 0;
serviceModule.clearChatGPTAuthState = async () => { resetCalled++; };
serviceModule.loginOrRegisterChatGPT = async () => ({
ok: true,
created: false,
status: '已登录',
session: { user: { email: 'reset@example.com' } },
});
delete require.cache[stepPath];
const chatgptRegisterStep = require(stepPath);
serviceModule.loginOrRegisterChatGPT = originalLogin;
serviceModule.clearChatGPTAuthState = originalClear;
const writes = [];
const ctx = {
account: { email: 'reset@example.com' },
page: { url: () => 'https://chatgpt.com/' },
params: { age: 23, clearAuthState: true },
log: () => {},
results: {},
dbWrite: async (name, args) => {
writes.push({ name, args });
if (name === 'getAccount') return { password_changed: 0, fa_changed: 0 };
return true;
},
updateStatus: async () => {},
updateMessage: async () => {},
};
await chatgptRegisterStep(ctx);
assert.strictEqual(resetCalled, 1, '勾选重新授权时应在登录前清理浏览器数据');
assert(writes.some(item => item.name === 'updateAccountChatgptResult' && item.args[1] === null && item.args[2] === null), '应清空数据库保存的ChatGPT Token');
const appSource = fs.readFileSync(path.join(__dirname, '..', 'public', 'app.js'), 'utf8');
const htmlSource = fs.readFileSync(path.join(__dirname, '..', 'public', 'index.html'), 'utf8');
assert(htmlSource.includes('showChatgptRunModal'), '点击ChatGPT按钮后应显示运行选项弹窗');
assert(htmlSource.includes('清除缓存、Cookie和Token'), '弹窗应提供清除登录数据的单选项');
assert(appSource.includes("const clearAuthState = this.chatgptAuthMode === 'clear'"), '前端应读取清理登录数据的选择');
assert(appSource.includes('clearAuthState,'), '前端应把清理选择传给任务');
console.log('chatgpt_auth_reset.test.js: PASS');
})().catch(error => {
console.error(error);
process.exitCode = 1;
});