36 lines
2.2 KiB
TypeScript
36 lines
2.2 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import type { QqConfig } from "../src/config.js";
|
|
import { QqAdapter } from "../src/platforms/qq/adapter.js";
|
|
|
|
const config: QqConfig = {
|
|
type: "qq", connectionMode: "webhook", appId: "id", clientSecret: "secret", botSecret: "", verifySignature: false,
|
|
botNames: ["Bot"], intents: 33_554_432, shard: [0, 1]
|
|
};
|
|
|
|
test("normalizes GROUP and C2C author openid while ACK remains immediate", async () => {
|
|
const received: any[] = [];
|
|
const gateway = { receive: async (message: unknown) => { received.push(message); throw new Error("ACP failed"); } };
|
|
const adapter = new QqAdapter(config, gateway as never);
|
|
const group = await adapter.handleWebhook({ body: { op: 0, t: "GROUP_AT_MESSAGE_CREATE", d: { id: "m1", group_openid: "g1", author: { user_openid: "u1" }, content: "@Bot hi" } }, headers: {}, query: {}, req: {} as never });
|
|
const c2c = await adapter.handleWebhook({ body: { op: 0, t: "C2C_MESSAGE_CREATE", d: { id: "m2", author: { user_openid: "u2" }, content: "hello" } }, headers: {}, query: {}, req: {} as never });
|
|
assert.deepEqual(group.body, { op: 12 }); assert.deepEqual(c2c.body, { op: 12 });
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.equal(received[0].chatId, "group:g1"); assert.equal(received[0].userId, "u1");
|
|
assert.equal(received[1].chatId, "user:u2"); assert.equal(received[1].userId, "u2");
|
|
});
|
|
|
|
test("sendMessage uses nested author.user_openid for C2C endpoint", async () => {
|
|
const original = globalThis.fetch; const urls: string[] = [];
|
|
globalThis.fetch = (async (input: string | URL | Request) => {
|
|
urls.push(String(input));
|
|
if (String(input).includes("getAppAccessToken")) return new Response(JSON.stringify({ access_token: "token", expires_in: 7200 }), { status: 200 });
|
|
return new Response(JSON.stringify({ id: "sent" }), { status: 200 });
|
|
}) as typeof fetch;
|
|
try {
|
|
const adapter = new QqAdapter(config, { receive: async () => ({ ok: true }) } as never);
|
|
await adapter.sendMessage({ target: { platform: "qq", chatId: "user:u2", raw: { author: { user_openid: "u2" } } }, text: "reply", replyTo: "m2" });
|
|
assert.ok(urls.some((url) => url.endsWith("/v2/users/u2/messages")));
|
|
} finally { globalThis.fetch = original; }
|
|
});
|