import assert from "node:assert/strict"; import test from "node:test"; import { sendWeComTextMessage } from "../src/wecomSender.js"; import { testConfig } from "./helpers.js"; test("sends a WeCom text message to the configured webhook", async () => { const requests: Array<{ url: string; init: RequestInit }> = []; const fetchImpl = async (url: string | URL | Request, init?: RequestInit) => { requests.push({ url: String(url), init: init ?? {} }); return new Response(JSON.stringify({ errcode: 0, errmsg: "ok" }), { status: 200 }); }; const result = await sendWeComTextMessage(testConfig(), "hello", fetchImpl as typeof fetch); assert.deepEqual(result, { errcode: 0, errmsg: "ok" }); assert.equal(requests.length, 1); assert.equal(requests[0]?.url, "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=test-key"); assert.deepEqual(JSON.parse(String(requests[0]?.init.body)), { msgtype: "text", text: { content: "hello" } }); }); test("throws when WeCom webhook returns an API error", async () => { const fetchImpl = async () => new Response(JSON.stringify({ errcode: 93000, errmsg: "invalid webhook" }), { status: 200 }); await assert.rejects( () => sendWeComTextMessage(testConfig(), "hello", fetchImpl as typeof fetch), /WeCom webhook failed: errcode=93000/ ); }); test("throws when WeCom webhook returns an HTTP error", async () => { const fetchImpl = async () => new Response("bad request", { status: 400 }); await assert.rejects( () => sendWeComTextMessage(testConfig(), "hello", fetchImpl as typeof fetch), /WeCom webhook failed: HTTP 400/ ); });