Commit 97dfb046 by luoqi

Merge CLI channel isolation fix into test

parents 51438523 75233df0
Pipeline #3723 failed in 0 seconds
...@@ -14,4 +14,8 @@ ...@@ -14,4 +14,8 @@
*/ */
export function disableSchedulersForCli(): void { export function disableSchedulersForCli(): void {
process.env.PAC_SCHEDULER_DISABLED = '1'; process.env.PAC_SCHEDULER_DISABLED = '1';
// CLI 不订阅机器人消息,避免与常驻服务竞争连接或执行群聊请求。
// 设为空而非删除,防止 ConfigModule 再次读取 .env 时补回凭据。
process.env.PAC_WX_AIBOT_BOT_ID = '';
process.env.PAC_WX_AIBOT_SECRET = '';
} }
import AiBot from '@wecom/aibot-node-sdk';
import { disableSchedulersForCli } from '../src/cli/bootstrap-flags';
import { WeixinAibotService } from '../src/modules/weixin-aibot/weixin-aibot.service';
jest.mock('@wecom/aibot-node-sdk', () => ({
__esModule: true,
default: { WSClient: jest.fn() },
generateReqId: jest.fn(),
}));
describe('企微机器人启动隔离', () => {
const original = { ...process.env };
const make = () => new WeixinAibotService({} as never, {} as never, {} as never, {} as never);
beforeEach(() => {
process.env.PAC_WX_AIBOT_BOT_ID = 'test-bot';
process.env.PAC_WX_AIBOT_SECRET = 'test-secret';
jest.clearAllMocks();
});
afterEach(() => { process.env = { ...original }; });
it('维护 CLI 即使继承完整机器人配置,也不建立连接或注册消息处理', () => {
disableSchedulersForCli();
const service = make();
service.onModuleInit();
service.onModuleDestroy();
expect(AiBot.WSClient).not.toHaveBeenCalled();
expect(process.env.PAC_SCHEDULER_DISABLED).toBe('1');
});
it('常驻服务仍正常订阅,并在关闭时断开', () => {
const client = { on: jest.fn(), connect: jest.fn(), disconnect: jest.fn() };
jest.mocked(AiBot.WSClient).mockImplementation(() => client as never);
const service = make();
service.onModuleInit();
expect(client.on).toHaveBeenCalledWith('message.text', expect.any(Function));
expect(client.connect).toHaveBeenCalledTimes(1);
service.onModuleDestroy();
expect(client.disconnect).toHaveBeenCalledTimes(1);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment