import assert from 'node:assert/strict'; import test from 'node:test'; import { FeishuRuntime } from '../../../src/channels/feishu/feishu-runtime.mjs'; import { rememberConnectionTestTarget } from '../../../src/channels/shared/connection-test.mjs'; class FakeClient { static instances = []; static sent = []; constructor(options) { this.options = options; this.im = { v1: { message: { create: async (payload) => { FakeClient.sent.push(payload); return { code: 0, data: { message_id: `message-${FakeClient.sent.length}` } }; }, }, }, }; FakeClient.instances.push(this); } } class FakeDispatcher { register(handlers) { this.handlers = handlers; return this; } } class FakeWSClient { static instances = []; constructor(options) { this.options = options; this.state = 'idle'; FakeWSClient.instances.push(this); } async start() { this.state = 'connecting'; } becomeReady() { this.state = 'connected'; this.options.onReady(); } getConnectionStatus() { return { state: this.state }; } close() { this.state = 'closed'; } } function fakeLark() { FakeWSClient.instances.length = 0; FakeClient.instances.length = 0; FakeClient.sent.length = 0; return { Domain: { Feishu: 'feishu-domain', Lark: 'lark-domain' }, LoggerLevel: { info: 'info' }, Client: FakeClient, EventDispatcher: FakeDispatcher, WSClient: FakeWSClient, defaultHttpInstance: { request: async (options) => options, get: async (_url, options) => options, delete: async (_url, options) => options, head: async (_url, options) => options, options: async (_url, options) => options, post: async (_url, _data, options) => options, put: async (_url, _data, options) => options, patch: async (_url, _data, options) => options, }, }; } test('FeishuRuntime becomes chat-ready only after Harness and Feishu are connected', async () => { let harnessChecks = 0; let harnessSignal; const runtime = new FeishuRuntime({ lark: fakeLark(), appId: 'cli_test', appSecret: 'secret', ownerOpenIds: ['*', 'ou_owner'], harness: { async ensureRunning(options) { harnessChecks += 1; harnessSignal = options.signal; }, }, state: { hasSeen: () => false }, }); assert.equal(runtime.status.ready, false); let settled = false; const starting = runtime.start().then((value) => { settled = true; return value; }); await new Promise((resolve) => setImmediate(resolve)); assert.equal(settled, false); assert.equal(runtime.status.feishuLongConnectionState, 'connecting'); FakeWSClient.instances[0].becomeReady(); const status = await starting; assert.equal(harnessChecks, 1); assert.equal(status.ready, true); assert.equal(status.feishuLongConnectionState, 'connected'); assert.equal(status.harnessReachable, true); assert.equal(harnessSignal.aborted, false); assert.equal((await FakeClient.instances[0].options.httpInstance.request({ url: 'https://open.feishu.cn/test', })).timeout, 15_000); assert.deepEqual(await runtime.sendConnectionTest('连接测试'), { sent: true }); assert.deepEqual(FakeClient.sent, [{ params: { receive_id_type: 'open_id' }, data: { receive_id: 'ou_owner', msg_type: 'text', content: JSON.stringify({ text: '连接测试' }), }, }]); const stopped = await runtime.stop(); assert.equal(stopped.ready, false); assert.equal(stopped.feishuLongConnectionState, 'idle'); assert.equal(FakeWSClient.instances[0].state, 'closed'); assert.equal(harnessSignal.aborted, true); }); test('FeishuRuntime uses a remembered private target for wildcard-only manual bots', async () => { const state = { hasSeen: () => false }; const runtime = new FeishuRuntime({ lark: fakeLark(), appId: 'cli_manual', appSecret: 'secret', ownerOpenIds: ['*'], harness: { async ensureRunning() {} }, state, }); const starting = runtime.start(); await new Promise((resolve) => setImmediate(resolve)); FakeWSClient.instances[0].becomeReady(); await starting; await assert.rejects( runtime.sendConnectionTest('连接测试'), (error) => error?.code === 'test-target-unavailable', ); rememberConnectionTestTarget(state, { chatId: 'oc_manual_private' }); assert.deepEqual(await runtime.sendConnectionTest('连接测试'), { sent: true }); assert.deepEqual(FakeClient.sent, [{ params: { receive_id_type: 'chat_id' }, data: { receive_id: 'oc_manual_private', msg_type: 'text', content: JSON.stringify({ text: '连接测试' }), }, }]); await runtime.stop(); }); test('FeishuRuntime fails closed when the initial WebSocket handshake times out', async () => { const runtime = new FeishuRuntime({ lark: fakeLark(), appId: 'cli_test', appSecret: 'secret', ownerOpenId: 'ou_owner', harness: { async ensureRunning() {} }, state: { hasSeen: () => false }, connectTimeoutMs: 10, }); await assert.rejects(runtime.start(), /handshake timed out/); assert.equal(runtime.status.ready, false); assert.equal(runtime.status.feishuLongConnectionState, 'failed'); assert.equal(FakeWSClient.instances[0].state, 'closed'); }); test('FeishuRuntime fails closed when Harness is unavailable', async () => { const runtime = new FeishuRuntime({ lark: fakeLark(), appId: 'cli_test', appSecret: 'secret', ownerOpenId: 'ou_owner', harness: { async ensureRunning() { throw new Error('Harness unavailable'); }, }, state: { hasSeen: () => false }, }); await assert.rejects(runtime.start(), /Harness unavailable/); assert.equal(runtime.status.ready, false); assert.equal(runtime.status.feishuLongConnectionState, 'failed'); assert.equal(runtime.status.lastError, 'Harness unavailable'); });