feat: 优化测试脚本,支持使用已有登录信息
- 添加检查已有登录信息的逻辑 - 如果发现已保存的账号,直接使用,跳过二维码登录 - 自动查找已保存的账号,无需手动指定账号ID - 修复网关连接超时问题 - 改进事件监听器设置顺序 测试体验优化: - 无需每次都重新扫码登录 - 自动复用已保存的 token - 提高测试效率 Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -103,7 +103,7 @@ function resolveModulePath() {
|
||||
throw new Error('找不到 weixinGateway 模块文件');
|
||||
}
|
||||
|
||||
let WeixinGateway, startWeixinLogin, waitWeixinLogin;
|
||||
let WeixinGateway, startWeixinLogin, waitWeixinLogin, resolveWeixinAccount, loadWeixinAccount;
|
||||
|
||||
try {
|
||||
const modulePath = resolveModulePath();
|
||||
@@ -112,6 +112,15 @@ try {
|
||||
startWeixinLogin = weixinGatewayModule.startWeixinLogin;
|
||||
waitWeixinLogin = weixinGatewayModule.waitWeixinLogin;
|
||||
|
||||
// 尝试从 channel 导入账号管理函数
|
||||
try {
|
||||
const channelModule = await import(path.join(weixinPluginPath, 'dist', 'weixin', 'channel.js'));
|
||||
resolveWeixinAccount = channelModule.resolveWeixinAccount;
|
||||
loadWeixinAccount = channelModule.loadWeixinAccount;
|
||||
} catch (err) {
|
||||
logWarn('无法导入账号管理函数,将跳过已有登录检查');
|
||||
}
|
||||
|
||||
logInfo('成功加载微信插件模块');
|
||||
} catch (err) {
|
||||
logError('加载微信插件模块失败:', err.message);
|
||||
@@ -170,16 +179,85 @@ function formatDuration(ms) {
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* 步骤 1: 二维码登录
|
||||
* 步骤 1: 检查登录状态或二维码登录
|
||||
*/
|
||||
async function testLogin() {
|
||||
logInfo('开始二维码登录流程...');
|
||||
logInfo('检查登录状态...');
|
||||
console.log('');
|
||||
|
||||
let targetAccountId = TEST_CONFIG.accountId || 'test-account';
|
||||
|
||||
// 1. 首先检查是否有已保存的登录信息
|
||||
if (resolveWeixinAccount && loadWeixinAccount) {
|
||||
try {
|
||||
// 如果没有指定账号ID,尝试查找已保存的账号
|
||||
if (!TEST_CONFIG.accountId) {
|
||||
const fs = await import('fs');
|
||||
const path = await import('path');
|
||||
const os = await import('os');
|
||||
|
||||
const stateDir = process.env.OPENCLAW_STATE_DIR || process.env.CLAWDBOT_STATE_DIR || path.join(os.homedir(), '.openclaw');
|
||||
const accountsDir = path.join(stateDir, 'openclaw-weixin', 'accounts');
|
||||
const registryPath = path.join(accountsDir, 'registered-accounts.json');
|
||||
|
||||
if (fs.existsSync(registryPath)) {
|
||||
const registry = JSON.parse(fs.readFileSync(registryPath, 'utf-8'));
|
||||
if (registry.accounts && registry.accounts.length > 0) {
|
||||
targetAccountId = registry.accounts[0];
|
||||
logInfo(`找到已保存的账号: ${targetAccountId}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查指定账号的登录信息
|
||||
const resolved = resolveWeixinAccount({}, targetAccountId);
|
||||
const accountData = loadWeixinAccount(targetAccountId);
|
||||
|
||||
// 检查是否有有效的登录信息(更宽松的检查)
|
||||
const hasValidLogin = accountData?.token ||
|
||||
(resolved.configured && resolved.token);
|
||||
|
||||
if (hasValidLogin) {
|
||||
const token = accountData?.token || resolved.token;
|
||||
const userId = accountData?.userId || resolved.userId;
|
||||
const baseUrl = accountData?.baseUrl || resolved.baseUrl;
|
||||
|
||||
logSuccess('发现已保存的登录信息!');
|
||||
console.log('');
|
||||
logInfo('账号信息:');
|
||||
logInfo(` - Account ID: ${targetAccountId}`);
|
||||
logInfo(` - User ID: ${userId || 'N/A'}`);
|
||||
logInfo(` - Base URL: ${baseUrl || 'N/A'}`);
|
||||
logInfo(` - Token: ${token.substring(0, 30)}...`);
|
||||
console.log('');
|
||||
logInfo('跳过二维码登录,直接使用已有登录信息');
|
||||
console.log('');
|
||||
|
||||
return {
|
||||
connected: true,
|
||||
accountId: targetAccountId,
|
||||
userId: userId,
|
||||
baseUrl: baseUrl || 'https://ilinkai.weixin.qq.com',
|
||||
botToken: token,
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
logWarn('检查已有登录信息失败:', err.message);
|
||||
logInfo('将继续进行二维码登录');
|
||||
console.log('');
|
||||
}
|
||||
} else {
|
||||
logWarn('账号管理函数不可用,跳过已有登录检查');
|
||||
}
|
||||
|
||||
// 2. 没有找到有效的登录信息,进行二维码登录
|
||||
logInfo('未找到有效的登录信息,开始二维码登录流程...');
|
||||
console.log('');
|
||||
|
||||
try {
|
||||
// 1. 获取二维码
|
||||
logInfo('正在获取登录二维码...');
|
||||
const { sessionKey, qrcodeUrl } = await startWeixinLogin(TEST_CONFIG.accountId || 'test-account');
|
||||
const { sessionKey, qrcodeUrl } = await startWeixinLogin(targetAccountId);
|
||||
|
||||
if (!qrcodeUrl) {
|
||||
throw new Error('获取二维码失败');
|
||||
@@ -285,16 +363,15 @@ async function testGateway() {
|
||||
|
||||
// 启动网关
|
||||
try {
|
||||
await gateway.start({
|
||||
enabled: true,
|
||||
debug: TEST_CONFIG.debug,
|
||||
});
|
||||
|
||||
// 等待连接
|
||||
// 先设置连接监听器,再启动网关
|
||||
await new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
gateway.off('connected');
|
||||
gateway.off('error');
|
||||
try {
|
||||
gateway.off('connected');
|
||||
gateway.off('error');
|
||||
} catch (e) {
|
||||
// 忽略移除监听器的错误
|
||||
}
|
||||
reject(new Error('连接超时'));
|
||||
}, 10000);
|
||||
|
||||
@@ -305,8 +382,19 @@ async function testGateway() {
|
||||
|
||||
gateway.once('error', (err) => {
|
||||
clearTimeout(timeout);
|
||||
try {
|
||||
gateway.off('connected');
|
||||
} catch (e) {
|
||||
// 忽略移除监听器的错误
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
|
||||
// 启动网关
|
||||
gateway.start({
|
||||
enabled: true,
|
||||
debug: TEST_CONFIG.debug,
|
||||
}).catch(reject);
|
||||
});
|
||||
|
||||
return gateway;
|
||||
|
||||
Reference in New Issue
Block a user