| 1 | import assert from "node:assert/strict"; |
| 2 | import { execFile } from "node:child_process"; |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import test from "node:test"; |
| 7 | import { fileURLToPath } from "node:url"; |
| 8 | import { promisify } from "node:util"; |
| 9 | |
| 10 | const entry = fileURLToPath(new URL("../src/index.mjs", import.meta.url)); |
| 11 | const dataModule = (source) => `data:text/javascript;base64,${Buffer.from(source).toString("base64")}`; |
| 12 | const sdk = dataModule(` |
| 13 | export class Client { |
| 14 | im = { v1: { message: { |
| 15 | reply: async ({ path }) => { |
| 16 | console.log("fixture reply", path.message_id); |
| 17 | return { code: Number(process.env.FIXTURE_REPLY_CODE) }; |
| 18 | }, |
| 19 | create: async ({ data }) => { |
| 20 | console.log("fixture create", data.receive_id); |
| 21 | return { code: Number(process.env.FIXTURE_CREATE_CODE) }; |
| 22 | }, |
| 23 | } } }; |
| 24 | } |
| 25 | export class EventDispatcher { |
| 26 | register(handlers) { this.handlers = handlers; return this; } |
| 27 | } |
| 28 | export class WSClient { |
| 29 | start({ eventDispatcher }) { |
| 30 | void eventDispatcher.handlers["im.message.receive_v1"]({ |
| 31 | sender: { sender_id: { open_id: "fixture-sender" } }, |
| 32 | message: { |
| 33 | chat_id: "fixture-chat", message_id: "fixture-message", chat_type: "p2p", |
| 34 | message_type: "text", content: JSON.stringify({ text: "/help" }), |
| 35 | }, |
| 36 | }); |
| 37 | } |
| 38 | } |
| 39 | `); |
| 40 | const loader = dataModule(` |
| 41 | export function resolve(specifier, context, next) { |
| 42 | if (specifier === "@larksuiteoapi/node-sdk") { |
| 43 | return { url: ${JSON.stringify(sdk)}, shortCircuit: true }; |
| 44 | } |
| 45 | return next(specifier, context); |
| 46 | } |
| 47 | `); |
| 48 | |
| 49 | async function runBridge(t, replyCode, createCode) { |
| 50 | const root = await mkdtemp(join(tmpdir(), "cw-feishu-delivery-")); |
| 51 | t.after(() => rm(root, { recursive: true, force: true })); |
| 52 | return promisify(execFile)(process.execPath, ["--experimental-loader", loader, entry], { |
| 53 | cwd: root, |
| 54 | timeout: 7000, |
| 55 | env: { |
| 56 | PATH: process.env.PATH, |
| 57 | FEISHU_APP_ID: "fixture-app", |
| 58 | FEISHU_APP_SECRET: "fixture-only", |
| 59 | FEISHU_THREAD_MAP_PATH: join(root, "thread-map.json"), |
| 60 | CODEWHALE_RUNTIME_URL: "http://127.0.0.1:1", |
| 61 | CODEWHALE_RUNTIME_TOKEN: "fixture-only", |
| 62 | CODEWHALE_CHAT_ALLOWLIST: "fixture-chat", |
| 63 | FIXTURE_REPLY_CODE: String(replyCode), |
| 64 | FIXTURE_CREATE_CODE: String(createCode), |
| 65 | }, |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | test("Feishu falls back to chat delivery when the reply API reports an error", async (t) => { |
| 70 | const { stdout, stderr } = await runBridge(t, 230001, 0); |
| 71 | assert.match(stdout, /fixture reply fixture-message/); |
| 72 | assert.match(stdout, /fixture create fixture-chat/); |
| 73 | assert.doesNotMatch(stderr, /failed to handle incoming Feishu message/); |
| 74 | }); |
| 75 | |
| 76 | test("Feishu reports an error when the fallback API also rejects the message", async (t) => { |
| 77 | const { stdout, stderr } = await runBridge(t, 230001, 230002); |
| 78 | assert.match(stdout, /fixture create fixture-chat/); |
| 79 | assert.match(stderr, /Feishu message create failed \(230002\)/); |
| 80 | }); |
| 81 | |
| 82 | test("Feishu keeps a successfully delivered reply in its original conversation", async (t) => { |
| 83 | const { stdout } = await runBridge(t, 0, 0); |
| 84 | assert.match(stdout, /fixture reply fixture-message/); |
| 85 | assert.doesNotMatch(stdout, /fixture create/); |
| 86 | }); |
| 87 |