feat: 任务发布到企微客户群(企业群发)与资源库单条新增
- 任务中心新增「发布到企微群」:同步客户群清单(wecom_group_chats)、 按群主分组创建企业群发任务(add_msg_template)、发送记录落库 wecom_group_pushes,迁移 mysql/0009;lib/wecom-client.ts 补 listCustomerGroupChats/createGroupMsgTemplate - KOC 资源库支持单条新增:app/api/resources-insert + lib/resource-write, 拆出资源写入公共逻辑供导入复用;0008 补合作方外部联系人字段 - 环境变量示例补企微凭证与 SEED_DEMO_DATA;next.config 增加 allowedDevOrigins;CLAUDE.md 补充项目说明 - .gitignore 排除 .codegraph/ 与 .ipynb_checkpoints/
This commit is contained in:
@@ -2,6 +2,9 @@ import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
clearWecomAccessTokenCacheForTests,
|
||||
createGroupMsgTemplate,
|
||||
listCustomerGroupChats,
|
||||
listExternalContacts,
|
||||
resolveWecomConfig,
|
||||
sendWecomAppMessage,
|
||||
sendWecomRobotMessage,
|
||||
@@ -215,3 +218,390 @@ test("access_token is cached across calls", async () => {
|
||||
);
|
||||
assert.equal(tokenCalls.length, 1, "access_token cached for second call");
|
||||
});
|
||||
|
||||
function fakeExternalContacts() {
|
||||
const calls = [];
|
||||
const fetchImpl = async (input, init = {}) => {
|
||||
const url = new URL(String(input));
|
||||
calls.push({ url, init });
|
||||
if (url.pathname.endsWith("/cgi-bin/gettoken")) {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
access_token: "token-test",
|
||||
expires_in: 7200,
|
||||
});
|
||||
}
|
||||
if (url.pathname.endsWith("/cgi-bin/externalcontact/get_follow_user_list")) {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
follow_user: [{ userid: "owner-a" }, { userid: "owner-b" }],
|
||||
});
|
||||
}
|
||||
if (url.pathname.endsWith("/cgi-bin/externalcontact/list")) {
|
||||
const userid = url.searchParams.get("userid");
|
||||
if (userid === "owner-a") {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
external_userid: ["ext-1", "ext-2"],
|
||||
});
|
||||
}
|
||||
if (userid === "owner-b") {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
external_userid: ["ext-3"],
|
||||
});
|
||||
}
|
||||
return Response.json({ errcode: 60020, errmsg: "user not found" });
|
||||
}
|
||||
if (url.pathname.endsWith("/cgi-bin/externalcontact/get")) {
|
||||
const eid = url.searchParams.get("external_userid");
|
||||
if (eid === "ext-1") {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
external_contact: {
|
||||
external_userid: "ext-1",
|
||||
name: "张三",
|
||||
avatar: "https://example.com/a.png",
|
||||
corp_fullname: "ACME 公司",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (eid === "ext-2") {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
external_contact: {
|
||||
external_userid: "ext-2",
|
||||
name: "李四",
|
||||
avatar: "",
|
||||
corp_fullname: "",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (eid === "ext-3") {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
external_contact: {
|
||||
external_userid: "ext-3",
|
||||
name: "王五",
|
||||
avatar: "https://example.com/c.png",
|
||||
corp_fullname: "Other 公司",
|
||||
},
|
||||
});
|
||||
}
|
||||
return Response.json({ errcode: 60111, errmsg: "not found" });
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
};
|
||||
return { calls, fetchImpl };
|
||||
}
|
||||
|
||||
test("listExternalContacts flattens follow_user → list → get into a single array", async () => {
|
||||
const { fetchImpl } = fakeExternalContacts();
|
||||
const contacts = await listExternalContacts(
|
||||
resolveWecomConfig(baseBindings),
|
||||
fetchImpl,
|
||||
);
|
||||
assert.equal(contacts.length, 3);
|
||||
const first = contacts.find((c) => c.externalUserId === "ext-1");
|
||||
assert.ok(first);
|
||||
assert.equal(first.name, "张三");
|
||||
assert.equal(first.avatar, "https://example.com/a.png");
|
||||
assert.equal(first.corpName, "ACME 公司");
|
||||
assert.equal(first.ownerUserId, "owner-a");
|
||||
const second = contacts.find((c) => c.externalUserId === "ext-2");
|
||||
assert.ok(second);
|
||||
assert.equal(second.corpName, "李四");
|
||||
const third = contacts.find((c) => c.externalUserId === "ext-3");
|
||||
assert.ok(third);
|
||||
assert.equal(third.ownerUserId, "owner-b");
|
||||
});
|
||||
|
||||
test("listExternalContacts skips members with non-zero errcode", async () => {
|
||||
const { fetchImpl } = fakeExternalContacts();
|
||||
const contacts = await listExternalContacts(
|
||||
resolveWecomConfig(baseBindings),
|
||||
fetchImpl,
|
||||
);
|
||||
const ownerBIds = contacts
|
||||
.filter((c) => c.ownerUserId === "owner-b")
|
||||
.map((c) => c.externalUserId);
|
||||
assert.deepEqual(ownerBIds, ["ext-3"]);
|
||||
});
|
||||
|
||||
test("listExternalContacts returns empty array when app credentials missing", async () => {
|
||||
const { calls, fetchImpl } = fakeExternalContacts();
|
||||
const contacts = await listExternalContacts(
|
||||
resolveWecomConfig({
|
||||
...baseBindings,
|
||||
WECOM_CORP_ID: "",
|
||||
WECOM_AGENT_ID: "",
|
||||
WECOM_SECRET: "",
|
||||
}),
|
||||
fetchImpl,
|
||||
);
|
||||
assert.deepEqual(contacts, []);
|
||||
assert.equal(calls.length, 0, "no API calls without credentials");
|
||||
});
|
||||
|
||||
function fakeGroupPush(options = {}) {
|
||||
const calls = [];
|
||||
const pages =
|
||||
options.pages ??
|
||||
[
|
||||
{
|
||||
next_cursor: "page-2",
|
||||
group_chat_list: [
|
||||
{ chat_id: "wr-normal-1", status: 0 },
|
||||
{ chat_id: "wr-dissolved", status: 1 },
|
||||
],
|
||||
},
|
||||
{ next_cursor: "", group_chat_list: [{ chat_id: "wr-normal-2", status: 0 }] },
|
||||
];
|
||||
const details =
|
||||
options.details ??
|
||||
{
|
||||
"wr-normal-1": { name: "KOC 一群", owner: "zhangsan", member_count: 42 },
|
||||
"wr-normal-2": { name: "KOC 二群", owner: "lisi", member_count: 7 },
|
||||
};
|
||||
const fetchImpl = async (input, init = {}) => {
|
||||
const url = new URL(String(input));
|
||||
calls.push({ url, init });
|
||||
if (url.pathname.endsWith("/cgi-bin/gettoken")) {
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
access_token: "token-test",
|
||||
expires_in: 7200,
|
||||
});
|
||||
}
|
||||
if (url.pathname.endsWith("/cgi-bin/externalcontact/groupchat/list")) {
|
||||
const body = JSON.parse(String(init.body ?? "{}"));
|
||||
const index = body.cursor ? Number(body.cursor.replace("page-", "")) - 1 : 0;
|
||||
const page = pages[index] ?? { next_cursor: "", group_chat_list: [] };
|
||||
return Response.json({
|
||||
errcode: 0,
|
||||
errmsg: "ok",
|
||||
next_cursor: page.next_cursor,
|
||||
group_chat_list: page.group_chat_list,
|
||||
});
|
||||
}
|
||||
if (url.pathname.endsWith("/cgi-bin/externalcontact/groupchat/get")) {
|
||||
const body = JSON.parse(String(init.body ?? "{}"));
|
||||
const detail = details[body.chat_id];
|
||||
if (!detail) {
|
||||
return Response.json({ errcode: 60111, errmsg: "not found" });
|
||||
}
|
||||
return Response.json({ errcode: 0, errmsg: "ok", group_chat: detail });
|
||||
}
|
||||
if (url.pathname.endsWith("/cgi-bin/externalcontact/add_msg_template")) {
|
||||
return Response.json({
|
||||
errcode: options.templateErrcode ?? 0,
|
||||
errmsg: options.templateErrmsg ?? "ok",
|
||||
msgid: options.msgid ?? "msgGTEST",
|
||||
fail_list: options.failList ?? [],
|
||||
});
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
};
|
||||
return { calls, fetchImpl };
|
||||
}
|
||||
|
||||
test("listCustomerGroupChats paginates groupchat/list and keeps only normal groups", async () => {
|
||||
const { calls, fetchImpl } = fakeGroupPush();
|
||||
const groups = await listCustomerGroupChats(
|
||||
resolveWecomConfig(baseBindings),
|
||||
fetchImpl,
|
||||
);
|
||||
assert.equal(groups.length, 2);
|
||||
const first = groups.find((g) => g.chatId === "wr-normal-1");
|
||||
assert.ok(first);
|
||||
assert.equal(first.name, "KOC 一群");
|
||||
assert.equal(first.ownerUserId, "zhangsan");
|
||||
assert.equal(first.memberCount, 42);
|
||||
const second = groups.find((g) => g.chatId === "wr-normal-2");
|
||||
assert.ok(second);
|
||||
assert.equal(second.ownerUserId, "lisi");
|
||||
assert.equal(
|
||||
groups.some((g) => g.chatId === "wr-dissolved"),
|
||||
false,
|
||||
"non-normal status groups are filtered",
|
||||
);
|
||||
const listCalls = calls.filter((c) =>
|
||||
c.url.pathname.endsWith("/cgi-bin/externalcontact/groupchat/list"),
|
||||
);
|
||||
assert.equal(listCalls.length, 2, "cursor pagination followed");
|
||||
const secondBody = JSON.parse(String(listCalls[1].init.body));
|
||||
assert.equal(secondBody.cursor, "page-2");
|
||||
});
|
||||
|
||||
test("listCustomerGroupChats skips groups whose detail lookup fails", async () => {
|
||||
const { fetchImpl } = fakeGroupPush({
|
||||
pages: [
|
||||
{
|
||||
next_cursor: "",
|
||||
group_chat_list: [
|
||||
{ chat_id: "wr-normal-1", status: 0 },
|
||||
{ chat_id: "wr-gone", status: 0 },
|
||||
],
|
||||
},
|
||||
],
|
||||
details: {
|
||||
"wr-normal-1": { name: "KOC 一群", owner: "zhangsan", member_count: 42 },
|
||||
},
|
||||
});
|
||||
const groups = await listCustomerGroupChats(
|
||||
resolveWecomConfig(baseBindings),
|
||||
fetchImpl,
|
||||
);
|
||||
assert.deepEqual(
|
||||
groups.map((g) => g.chatId),
|
||||
["wr-normal-1"],
|
||||
);
|
||||
});
|
||||
|
||||
test("createGroupMsgTemplate posts group payload with text and link attachment", async () => {
|
||||
const { calls, fetchImpl } = fakeGroupPush();
|
||||
const result = await createGroupMsgTemplate(
|
||||
resolveWecomConfig(baseBindings),
|
||||
{
|
||||
sender: "zhangsan",
|
||||
chatIdList: ["wr-normal-1", "wr-normal-2"],
|
||||
text: "【新任务】测试任务",
|
||||
link: {
|
||||
title: "测试任务",
|
||||
desc: "品牌 A · 3 份",
|
||||
url: "https://portal.example.com/?task=tok-1",
|
||||
},
|
||||
},
|
||||
fetchImpl,
|
||||
);
|
||||
assert.equal(result.msgid, "msgGTEST");
|
||||
assert.deepEqual(result.failList, []);
|
||||
const sendCall = calls.find((c) =>
|
||||
c.url.pathname.endsWith("/cgi-bin/externalcontact/add_msg_template"),
|
||||
);
|
||||
assert.ok(sendCall, "add_msg_template was called");
|
||||
assert.equal(sendCall.url.searchParams.get("access_token"), "token-test");
|
||||
const body = JSON.parse(String(sendCall.init.body));
|
||||
assert.equal(body.chat_type, "group");
|
||||
assert.equal(body.sender, "zhangsan");
|
||||
assert.deepEqual(body.chat_id_list, ["wr-normal-1", "wr-normal-2"]);
|
||||
assert.equal(body.text.content, "【新任务】测试任务");
|
||||
assert.equal(body.attachments.length, 1);
|
||||
assert.equal(body.attachments[0].msgtype, "link");
|
||||
assert.equal(body.attachments[0].link.url, "https://portal.example.com/?task=tok-1");
|
||||
});
|
||||
|
||||
test("createGroupMsgTemplate omits text and attachments keys when absent", async () => {
|
||||
const { calls, fetchImpl } = fakeGroupPush();
|
||||
await createGroupMsgTemplate(
|
||||
resolveWecomConfig(baseBindings),
|
||||
{
|
||||
sender: "zhangsan",
|
||||
chatIdList: ["wr-normal-1"],
|
||||
link: { title: "只有图文", url: "https://portal.example.com/?task=tok-1" },
|
||||
},
|
||||
fetchImpl,
|
||||
);
|
||||
const sendCall = calls.find((c) =>
|
||||
c.url.pathname.endsWith("/cgi-bin/externalcontact/add_msg_template"),
|
||||
);
|
||||
assert.ok(sendCall, "add_msg_template was called");
|
||||
const body = JSON.parse(String(sendCall.init.body));
|
||||
assert.equal("text" in body, false);
|
||||
assert.equal(body.attachments[0].link.title, "只有图文");
|
||||
assert.equal(body.attachments[0].link.desc, "");
|
||||
});
|
||||
|
||||
test("createGroupMsgTemplate passes through fail_list from provider", async () => {
|
||||
const { fetchImpl } = fakeGroupPush({
|
||||
msgid: "msgFAIL",
|
||||
failList: ["wr-normal-2"],
|
||||
});
|
||||
const result = await createGroupMsgTemplate(
|
||||
resolveWecomConfig(baseBindings),
|
||||
{
|
||||
sender: "zhangsan",
|
||||
chatIdList: ["wr-normal-1", "wr-normal-2"],
|
||||
text: "内容",
|
||||
},
|
||||
fetchImpl,
|
||||
);
|
||||
assert.equal(result.msgid, "msgFAIL");
|
||||
assert.deepEqual(result.failList, ["wr-normal-2"]);
|
||||
});
|
||||
|
||||
test("createGroupMsgTemplate throws WecomClientError on provider error", async () => {
|
||||
const { fetchImpl } = fakeGroupPush({
|
||||
templateErrcode: 81053,
|
||||
templateErrmsg: "user not in visible scope",
|
||||
});
|
||||
await assert.rejects(
|
||||
() =>
|
||||
createGroupMsgTemplate(
|
||||
resolveWecomConfig(baseBindings),
|
||||
{ sender: "zhangsan", chatIdList: ["wr-normal-1"], text: "内容" },
|
||||
fetchImpl,
|
||||
),
|
||||
(error) => {
|
||||
assert.ok(error instanceof WecomClientError);
|
||||
assert.equal(error.status, 502);
|
||||
assert.match(error.message, /user not in visible scope/);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("createGroupMsgTemplate rejects empty text and link before calling API", async () => {
|
||||
const { calls, fetchImpl } = fakeGroupPush();
|
||||
await assert.rejects(
|
||||
() =>
|
||||
createGroupMsgTemplate(
|
||||
resolveWecomConfig(baseBindings),
|
||||
{ sender: "zhangsan", chatIdList: ["wr-normal-1"] },
|
||||
fetchImpl,
|
||||
),
|
||||
(error) => {
|
||||
assert.ok(error instanceof WecomClientError);
|
||||
assert.equal(error.status, 400);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
assert.equal(
|
||||
calls.filter((c) =>
|
||||
c.url.pathname.endsWith("/cgi-bin/externalcontact/add_msg_template"),
|
||||
).length,
|
||||
0,
|
||||
"no API call for invalid payload",
|
||||
);
|
||||
});
|
||||
|
||||
test("createGroupMsgTemplate rejects missing sender or empty chat list", async () => {
|
||||
const { fetchImpl } = fakeGroupPush();
|
||||
const config = resolveWecomConfig(baseBindings);
|
||||
await assert.rejects(
|
||||
() =>
|
||||
createGroupMsgTemplate(config, {
|
||||
sender: "",
|
||||
chatIdList: ["wr-normal-1"],
|
||||
text: "内容",
|
||||
}, fetchImpl),
|
||||
/群主/,
|
||||
);
|
||||
await assert.rejects(
|
||||
() =>
|
||||
createGroupMsgTemplate(config, {
|
||||
sender: "zhangsan",
|
||||
chatIdList: [],
|
||||
text: "内容",
|
||||
}, fetchImpl),
|
||||
/客户群列表为空/,
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user