@sleep2agi/commhub-server 0.9.0-preview.93 → 0.9.0-preview.94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/auth.ts +2 -2
- package/src/department-groups.ts +83 -7
- package/src/departments.ts +6 -6
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/requirement-due-reminders-http.test.ts +390 -0
- package/src/requirement-due-reminders.ts +310 -0
- package/src/requirements-errors.ts +2 -0
- package/src/requirements.ts +16 -1
- package/src/server.ts +7 -2
- package/src/tools.ts +28 -5
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// #491 / #492 / #494 —— 任务到期提醒 + 列表到期筛选。HTTP 集成测试(真实 Bun.serve,私有端口,临时库)。
|
|
2
|
+
// test2123 在真实 PostgreSQL 上原样再跑一遍(COMMHUB_TEST_PG_URL)。
|
|
3
|
+
//
|
|
4
|
+
// 假时钟:runDueReminders({ now }) 注入时刻;时区 = 默认 Asia/Shanghai。
|
|
5
|
+
// 网络 NET(boss)。owen = 负责人;pat、pia = 参与人;节点 NODE = 负责 Agent。
|
|
6
|
+
// 覆盖:三档各一次(收件人 = 负责人 + 参与人,发信别名 = 负责 Agent;节点收到一条 type=message 的消息,不是任务);
|
|
7
|
+
// 同一时刻跑两次 / 同一天跑两次不重发;逾期按天再提醒一次;完成 / 归档 / 清空 due 立即停;due 往后挪重新上膛;
|
|
8
|
+
// 时刻型 due;逾期上限;节点停了不发给节点;MCP / REST 的 overdue、due_within_days 筛选与严格参数。
|
|
9
|
+
|
|
10
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
11
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
|
|
15
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-due-rem-"));
|
|
16
|
+
process.env.COMMHUB_DB ||= join(DIR, "hub.db");
|
|
17
|
+
process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
|
|
18
|
+
process.env.HOST = "127.0.0.1";
|
|
19
|
+
delete process.env.COMMHUB_DUE_REMINDER_TZ;
|
|
20
|
+
delete process.env.COMMHUB_DUE_OVERDUE_MAX_DAYS;
|
|
21
|
+
delete process.env.COMMHUB_DUE_REMINDER_NETWORKS;
|
|
22
|
+
// 节点提醒默认关;这个文件的大部分用例要看节点那一路,先打开(「默认关」单独一个用例)。
|
|
23
|
+
process.env.COMMHUB_DUE_REMINDER_NODES = "1";
|
|
24
|
+
const PW = "DueRemindPassw0rd!x";
|
|
25
|
+
|
|
26
|
+
type U = { token: string; id: string; username: string };
|
|
27
|
+
let BASE = "";
|
|
28
|
+
let hub: any = null;
|
|
29
|
+
let NET = "";
|
|
30
|
+
let db: any;
|
|
31
|
+
let due: typeof import("./requirement-due-reminders.js");
|
|
32
|
+
let boss: U, owen: U, pat: U, pia: U;
|
|
33
|
+
const stamp = Date.now();
|
|
34
|
+
const NODE_ID = `node_due_${stamp}`;
|
|
35
|
+
const ALIAS = `due-agent-${stamp}`;
|
|
36
|
+
|
|
37
|
+
// 2026-10-10 10:00 东八(02:00Z)
|
|
38
|
+
const T0 = Date.UTC(2026, 9, 10, 2, 0, 0);
|
|
39
|
+
const DAY = 86_400_000;
|
|
40
|
+
|
|
41
|
+
type R = { status: number; body: any };
|
|
42
|
+
async function send(token: string, method: string, path: string, payload?: unknown): Promise<R> {
|
|
43
|
+
const res = await fetch(`${BASE}${path}`, {
|
|
44
|
+
method,
|
|
45
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
46
|
+
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
47
|
+
});
|
|
48
|
+
const text = await res.text();
|
|
49
|
+
let body: any = null;
|
|
50
|
+
try { body = JSON.parse(text); } catch {}
|
|
51
|
+
return { status: res.status, body };
|
|
52
|
+
}
|
|
53
|
+
async function mcp(token: string, name: string, args: Record<string, unknown>): Promise<any> {
|
|
54
|
+
const res = await fetch(`${BASE}/mcp`, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", Accept: "application/json, text/event-stream", "MCP-Protocol-Version": "2025-03-26" },
|
|
57
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }),
|
|
58
|
+
});
|
|
59
|
+
const raw = await res.text();
|
|
60
|
+
const lines = raw.split("\n").filter(x => x.startsWith("data:"));
|
|
61
|
+
const payload = lines.length ? JSON.parse(lines.at(-1)!.slice(5).trim()) : JSON.parse(raw);
|
|
62
|
+
if (payload.error) return { rpcError: payload.error };
|
|
63
|
+
const item = payload.result.content[0];
|
|
64
|
+
try { return JSON.parse(item.text); } catch { return { isError: payload.result.isError, text: item.text }; }
|
|
65
|
+
}
|
|
66
|
+
const userRef = (id: string) => ({ kind: "user", id });
|
|
67
|
+
|
|
68
|
+
async function card(name: string, dueValue: string, extra: Record<string, unknown> = {}): Promise<string> {
|
|
69
|
+
const r = await send(boss.token, "POST", "/api/requirements", {
|
|
70
|
+
network_id: NET, name, due: dueValue, owner: userRef(owen.id), participants: [userRef(pat.id), userRef(pia.id), userRef(owen.id)],
|
|
71
|
+
agent_owner: { kind: "node", id: NODE_ID }, ...extra,
|
|
72
|
+
});
|
|
73
|
+
expect(r.status).toBe(201);
|
|
74
|
+
return r.body.requirement.id as string;
|
|
75
|
+
}
|
|
76
|
+
const patch = (id: string, body: Record<string, unknown>) => send(boss.token, "PATCH", `/api/requirements/${id}?network_id=${NET}`, body);
|
|
77
|
+
|
|
78
|
+
/** 这张卡发给人的提醒:收件人 → [{content, from, kind}]。 */
|
|
79
|
+
function userNotices(reqId: string): Record<string, Array<{ content: string; from: string; reminder: string }>> {
|
|
80
|
+
const rows = db.all("SELECT user_id, from_session, content, meta_json FROM user_inbox WHERE network_id = ?1 AND kind = 'task_due' ORDER BY created_at, message_id", NET) as any[];
|
|
81
|
+
const out: Record<string, any[]> = {};
|
|
82
|
+
for (const r of rows) {
|
|
83
|
+
const meta = JSON.parse(r.meta_json);
|
|
84
|
+
if (meta?.task_notice?.requirement_id !== reqId) continue;
|
|
85
|
+
(out[r.user_id] ||= []).push({ content: r.content, from: r.from_session, reminder: meta.task_notice.due_reminder });
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
function nodeMessages(reqId: string): Array<{ type: string; content: string; from: string }> {
|
|
90
|
+
return (db.all("SELECT type, content, from_session FROM inbox WHERE session_name = ?1 AND network_id = ?2 ORDER BY created_at", ALIAS, NET) as any[])
|
|
91
|
+
.filter(r => String(r.content).includes(reqId)).map(r => ({ type: r.type, content: r.content, from: r.from_session }));
|
|
92
|
+
}
|
|
93
|
+
const sentFor = (out: Array<{ requirement_id: string; kind: string }>, id: string) => out.filter(x => x.requirement_id === id).map(x => x.kind);
|
|
94
|
+
|
|
95
|
+
beforeAll(async () => {
|
|
96
|
+
({ db } = await import("./db.js"));
|
|
97
|
+
const { addNetworkMember, register } = await import("./auth.js");
|
|
98
|
+
const mk = (name: string): U => {
|
|
99
|
+
const r = register(`dr_${name}_${stamp}`, PW);
|
|
100
|
+
expect(r.ok).toBe(true);
|
|
101
|
+
return { token: r.token!, id: r.user!.user_id, username: r.user!.username };
|
|
102
|
+
};
|
|
103
|
+
const b = register(`dr_boss_${stamp}`, PW);
|
|
104
|
+
boss = { token: b.token!, id: b.user!.user_id, username: b.user!.username };
|
|
105
|
+
NET = b.network_id!;
|
|
106
|
+
owen = mk("owen"); pat = mk("pat"); pia = mk("pia");
|
|
107
|
+
// pat 是受限成员(看不见任何 Agent):他的提醒发信别名不能泄露负责 Agent → 「任务提醒」。
|
|
108
|
+
addNetworkMember(NET, owen.id, "member", boss.id, { taskAccess: "all", agentAccess: "all" });
|
|
109
|
+
addNetworkMember(NET, pia.id, "member", boss.id, { taskAccess: "all", agentAccess: "all" });
|
|
110
|
+
addNetworkMember(NET, pat.id, "member", boss.id, { taskAccess: "all" });
|
|
111
|
+
db.run("INSERT INTO nodes (node_id, node_name, alias, runtime, network_id) VALUES (?1, ?2, ?2, 'claude-code', ?3)", [NODE_ID, ALIAS, NET]);
|
|
112
|
+
db.run("INSERT INTO sessions (resume_id, alias, status, node_id, network_id) VALUES (?1, ?2, 'idle', ?3, ?4)", [`r_due_${stamp}`, ALIAS, NODE_ID, NET]);
|
|
113
|
+
due = await import("./requirement-due-reminders.js");
|
|
114
|
+
const mod: any = await import("./server.js");
|
|
115
|
+
hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
116
|
+
BASE = `http://127.0.0.1:${hub.port}`;
|
|
117
|
+
// 基线:假装功能在 T0 之前 30 天就开着了(否则 T0 第一次扫描会把 NET 里所有逾期卡当「开通前就逾期」不发)。
|
|
118
|
+
due.runDueReminders({ now: T0 - 30 * DAY });
|
|
119
|
+
}, 30_000);
|
|
120
|
+
|
|
121
|
+
afterAll(() => {
|
|
122
|
+
try { hub?.stop?.(true); } catch {}
|
|
123
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("时区小工具", () => {
|
|
127
|
+
test("东八的今天 / 零点", () => {
|
|
128
|
+
expect(due.localDate(T0, "Asia/Shanghai")).toBe("2026-10-10");
|
|
129
|
+
expect(due.localDate(Date.UTC(2026, 9, 9, 16, 0), "Asia/Shanghai")).toBe("2026-10-10"); // 00:00 东八
|
|
130
|
+
expect(due.localDate(Date.UTC(2026, 9, 9, 15, 59), "Asia/Shanghai")).toBe("2026-10-09");
|
|
131
|
+
expect(new Date(due.localMidnightMs("2026-10-10", "Asia/Shanghai")).toISOString()).toBe("2026-10-09T16:00:00.000Z");
|
|
132
|
+
expect(new Date(due.localMidnightMs("2026-03-08", "America/New_York")).toISOString()).toBe("2026-03-08T05:00:00.000Z");
|
|
133
|
+
expect(due.addDays("2026-10-31", 1)).toBe("2026-11-01");
|
|
134
|
+
expect(due.dayDiff("2026-10-10", "2026-10-07")).toBe(3);
|
|
135
|
+
});
|
|
136
|
+
test("分档", () => {
|
|
137
|
+
const tz = "Asia/Shanghai";
|
|
138
|
+
expect(due.dueReminderFor("2026-10-11", T0, tz)?.kind).toBe("due_soon");
|
|
139
|
+
expect(due.dueReminderFor("2026-10-10", T0, tz)?.kind).toBe("due_today");
|
|
140
|
+
expect(due.dueReminderFor("2026-10-12", T0, tz)).toBeNull();
|
|
141
|
+
expect(due.dueReminderFor("2026-10-07", T0, tz)).toMatchObject({ kind: "overdue", overdueDays: 3, day: "2026-10-10" });
|
|
142
|
+
expect(due.dueReminderFor("2026-10-02", T0, tz)).toBeNull(); // 8 天 > 默认上限 7
|
|
143
|
+
expect(due.dueReminderFor("2026-10-10T08:00:00Z", T0, tz)?.kind).toBe("due_today"); // 16:00 东八,今天还没到
|
|
144
|
+
expect(due.dueReminderFor("2026-10-11T01:00:00Z", T0, tz)?.kind).toBe("due_soon"); // 明天 09:00,23 小时内
|
|
145
|
+
expect(due.dueReminderFor("2026-10-11T03:00:00Z", T0, tz)).toBeNull(); // 25 小时后
|
|
146
|
+
expect(due.dueReminderFor("2026-10-10T01:00:00Z", T0, tz)).toMatchObject({ kind: "overdue", overdueDays: 0 });
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe("提醒", () => {
|
|
151
|
+
test("即将到期:负责人 + 参与人各一条(发信 = 负责 Agent),节点收到一条不需回复的消息", async () => {
|
|
152
|
+
const id = await card("dr-明天", "2026-10-11");
|
|
153
|
+
const out = due.runDueReminders({ now: T0 });
|
|
154
|
+
expect(sentFor(out, id)).toEqual(["due_soon"]);
|
|
155
|
+
const n = userNotices(id);
|
|
156
|
+
expect(Object.keys(n).sort()).toEqual([owen.id, pat.id, pia.id].sort());
|
|
157
|
+
expect(n[pat.id][0].from).toBe(due.DUE_REMINDER_SENDER);
|
|
158
|
+
for (const uid of [owen.id, pat.id, pia.id]) {
|
|
159
|
+
expect(n[uid]).toHaveLength(1);
|
|
160
|
+
if (uid !== pat.id) expect(n[uid][0].from).toBe(ALIAS);
|
|
161
|
+
expect(n[uid][0].reminder).toBe("due_soon");
|
|
162
|
+
expect(n[uid][0].content).toContain("「dr-明天」即将到期");
|
|
163
|
+
}
|
|
164
|
+
expect(n[boss.id]).toBeUndefined(); // 创建者不是负责人 / 参与人:不提醒
|
|
165
|
+
const m = nodeMessages(id);
|
|
166
|
+
expect(m).toHaveLength(1);
|
|
167
|
+
expect(m[0].type).toBe("message");
|
|
168
|
+
expect(m[0].from).toBe(due.DUE_REMINDER_SENDER);
|
|
169
|
+
// 不是任务:tasks 表里没有这张卡的提醒
|
|
170
|
+
expect((db.all("SELECT task_id FROM tasks WHERE network_id = ?1 AND content LIKE ?2", NET, `%${id}%`) as any[]).length).toBe(0);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("今天到期 + 同一时刻 / 同一天再跑不重发(去重表)", async () => {
|
|
174
|
+
const id = await card("dr-今天", "2026-10-10");
|
|
175
|
+
expect(sentFor(due.runDueReminders({ now: T0 }), id)).toEqual(["due_today"]);
|
|
176
|
+
expect(sentFor(due.runDueReminders({ now: T0 }), id)).toEqual([]);
|
|
177
|
+
expect(sentFor(due.runDueReminders({ now: T0 + 6 * 3600_000 }), id)).toEqual([]);
|
|
178
|
+
expect(userNotices(id)[owen.id]).toHaveLength(1);
|
|
179
|
+
expect(nodeMessages(id)).toHaveLength(1);
|
|
180
|
+
const rows = db.all("SELECT kind, due_on, day FROM requirement_due_reminders WHERE requirement_id = ?1", id) as any[];
|
|
181
|
+
expect(rows).toEqual([{ kind: "due_today", due_on: "2026-10-10", day: "2026-10-10" }]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("已逾期 N 天:每天最多一次,第二天再一次(N+1)", async () => {
|
|
185
|
+
const id = await card("dr-逾期", "2026-10-07");
|
|
186
|
+
expect(sentFor(due.runDueReminders({ now: T0 }), id)).toEqual(["overdue"]);
|
|
187
|
+
expect(sentFor(due.runDueReminders({ now: T0 + 3600_000 }), id)).toEqual([]);
|
|
188
|
+
expect(sentFor(due.runDueReminders({ now: T0 + DAY }), id)).toEqual(["overdue"]);
|
|
189
|
+
const texts = userNotices(id)[pat.id].map(x => x.content).sort();
|
|
190
|
+
expect(texts).toHaveLength(2);
|
|
191
|
+
expect(texts[0]).toContain("已逾期 3 天");
|
|
192
|
+
expect(texts[1]).toContain("已逾期 4 天");
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("完成 / 归档 / 清空 due → 立即停", async () => {
|
|
196
|
+
const done = await card("dr-完成", "2026-10-08");
|
|
197
|
+
const arch = await card("dr-归档", "2026-10-08");
|
|
198
|
+
const cleared = await card("dr-清空", "2026-10-08");
|
|
199
|
+
expect((await patch(done, { column: "done" })).status).toBe(200);
|
|
200
|
+
expect((await patch(arch, { archived: true })).status).toBe(200);
|
|
201
|
+
expect((await patch(cleared, { due: "" })).status).toBe(200);
|
|
202
|
+
const out = due.runDueReminders({ now: T0 });
|
|
203
|
+
for (const id of [done, arch, cleared]) {
|
|
204
|
+
expect(sentFor(out, id)).toEqual([]);
|
|
205
|
+
expect(userNotices(id)).toEqual({});
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("逾期提醒发过后改完成:第二天不再提醒", async () => {
|
|
210
|
+
const id = await card("dr-逾期后完成", "2026-10-09");
|
|
211
|
+
expect(sentFor(due.runDueReminders({ now: T0 }), id)).toEqual(["overdue"]);
|
|
212
|
+
expect((await patch(id, { column: "done" })).status).toBe(200);
|
|
213
|
+
expect(sentFor(due.runDueReminders({ now: T0 + DAY }), id)).toEqual([]);
|
|
214
|
+
expect(userNotices(id)[owen.id]).toHaveLength(1);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("due 往后挪:旧档不再成立,新 due 重新上膛", async () => {
|
|
218
|
+
const id = await card("dr-挪", "2026-10-10");
|
|
219
|
+
expect(sentFor(due.runDueReminders({ now: T0 }), id)).toEqual(["due_today"]);
|
|
220
|
+
expect((await patch(id, { due: "2026-10-20" })).status).toBe(200);
|
|
221
|
+
expect(sentFor(due.runDueReminders({ now: T0 + 3600_000 }), id)).toEqual([]); // 太远:不发
|
|
222
|
+
expect((await patch(id, { due: "2026-10-11" })).status).toBe(200);
|
|
223
|
+
expect(sentFor(due.runDueReminders({ now: T0 + 2 * 3600_000 }), id)).toEqual(["due_soon"]);
|
|
224
|
+
expect(sentFor(due.runDueReminders({ now: T0 + DAY }), id)).toEqual(["due_today"]);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("时刻型 due:今天稍后 = 今天到期;23 小时后 = 即将到期;已过 = 已过预计完成时间", async () => {
|
|
228
|
+
const later = await card("dr-时刻今天", "2026-10-10T16:00:00+08:00");
|
|
229
|
+
const soon = await card("dr-时刻明早", "2026-10-11T09:00:00+08:00");
|
|
230
|
+
const past = await card("dr-时刻已过", "2026-10-10T09:00:00+08:00");
|
|
231
|
+
const out = due.runDueReminders({ now: T0 });
|
|
232
|
+
expect(sentFor(out, later)).toEqual(["due_today"]);
|
|
233
|
+
expect(sentFor(out, soon)).toEqual(["due_soon"]);
|
|
234
|
+
expect(sentFor(out, past)).toEqual(["overdue"]);
|
|
235
|
+
expect(userNotices(later)[owen.id][0].content).toContain("2026-10-10 16:00");
|
|
236
|
+
expect(userNotices(past)[owen.id][0].content).toContain("已过预计完成时间");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test("逾期超过上限不提醒;上限可配", async () => {
|
|
240
|
+
const id = await card("dr-老卡", "2026-09-30");
|
|
241
|
+
expect(sentFor(due.runDueReminders({ now: T0 }), id)).toEqual([]);
|
|
242
|
+
process.env.COMMHUB_DUE_OVERDUE_MAX_DAYS = "30";
|
|
243
|
+
try { expect(sentFor(due.runDueReminders({ now: T0 }), id)).toEqual(["overdue"]); }
|
|
244
|
+
finally { delete process.env.COMMHUB_DUE_OVERDUE_MAX_DAYS; }
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("没有负责 Agent:发信 = 任务提醒;节点停了:节点不收,人照收", async () => {
|
|
248
|
+
const noAgent = await card("dr-无Agent", "2026-10-11", { agent_owner: null });
|
|
249
|
+
db.run("UPDATE nodes SET lifecycle_state = 'stopped' WHERE node_id = ?1", [NODE_ID]);
|
|
250
|
+
try {
|
|
251
|
+
const stopped = await card("dr-节点停", "2026-10-11");
|
|
252
|
+
const out = due.runDueReminders({ now: T0 + 3 * 3600_000 });
|
|
253
|
+
expect(out.find(x => x.requirement_id === noAgent)?.node).toBeNull();
|
|
254
|
+
expect(userNotices(noAgent)[owen.id][0].from).toBe(due.DUE_REMINDER_SENDER);
|
|
255
|
+
expect(out.find(x => x.requirement_id === stopped)?.node).toBeNull();
|
|
256
|
+
expect(nodeMessages(stopped)).toEqual([]);
|
|
257
|
+
expect(userNotices(stopped)[pia.id]).toHaveLength(1);
|
|
258
|
+
} finally {
|
|
259
|
+
db.run("UPDATE nodes SET lifecycle_state = 'active' WHERE node_id = ?1", [NODE_ID]);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
describe("上线护栏", () => {
|
|
265
|
+
const mkNet = (tag: string) => {
|
|
266
|
+
const r = (globalThis as any).__register(`dr_${tag}_${stamp}`, PW);
|
|
267
|
+
return { token: r.token as string, id: r.user.user_id as string, net: r.network_id as string };
|
|
268
|
+
};
|
|
269
|
+
async function cardIn(o: { token: string; id: string; net: string }, name: string, dueValue: string, extra: Record<string, unknown> = {}) {
|
|
270
|
+
const r = await send(o.token, "POST", "/api/requirements", { network_id: o.net, name, due: dueValue, owner: userRef(o.id), ...extra });
|
|
271
|
+
expect(r.status).toBe(201);
|
|
272
|
+
return r.body.requirement.id as string;
|
|
273
|
+
}
|
|
274
|
+
beforeAll(async () => { (globalThis as any).__register = (await import("./auth.js")).register; });
|
|
275
|
+
|
|
276
|
+
test("网络白名单:设了只发白名单里的网络;不设 = 所有网络", async () => {
|
|
277
|
+
const other = mkNet("other");
|
|
278
|
+
due.runDueReminders({ now: T0 - 30 * DAY }); // other 的基线
|
|
279
|
+
const mine = await card("dr-白名单内", "2026-10-11");
|
|
280
|
+
const theirs = await cardIn(other, "dr-白名单外", "2026-10-11");
|
|
281
|
+
process.env.COMMHUB_DUE_REMINDER_NETWORKS = ` ${NET} , `;
|
|
282
|
+
try {
|
|
283
|
+
const out = due.runDueReminders({ now: T0 });
|
|
284
|
+
expect(sentFor(out, mine)).toEqual(["due_soon"]);
|
|
285
|
+
expect(sentFor(out, theirs)).toEqual([]);
|
|
286
|
+
expect(db.get("SELECT COUNT(*) AS n FROM requirement_due_reminders WHERE requirement_id = ?1", theirs).n).toBe(0);
|
|
287
|
+
} finally { delete process.env.COMMHUB_DUE_REMINDER_NETWORKS; }
|
|
288
|
+
expect(sentFor(due.runDueReminders({ now: T0 }), theirs)).toEqual(["due_soon"]);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("节点提醒默认关:人照收,节点 inbox 没有", async () => {
|
|
292
|
+
delete process.env.COMMHUB_DUE_REMINDER_NODES;
|
|
293
|
+
try {
|
|
294
|
+
const id = await card("dr-节点默认关", "2026-10-11");
|
|
295
|
+
const out = due.runDueReminders({ now: T0 + 4 * 3600_000 });
|
|
296
|
+
expect(sentFor(out, id)).toEqual(["due_soon"]);
|
|
297
|
+
expect(out.find(x => x.requirement_id === id)?.node).toBeNull();
|
|
298
|
+
expect(nodeMessages(id)).toEqual([]);
|
|
299
|
+
expect(userNotices(id)[owen.id]).toHaveLength(1);
|
|
300
|
+
} finally { process.env.COMMHUB_DUE_REMINDER_NODES = "1"; }
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
test("开通时不补发:基线前就逾期的卡不发逾期提醒(第二天也不发);基线后才逾期的照发;今天 / 明天到期照发", async () => {
|
|
304
|
+
const fresh = mkNet("fresh");
|
|
305
|
+
const old = await cardIn(fresh, "dr-开通前逾期", "2026-10-07");
|
|
306
|
+
const oldTime = await cardIn(fresh, "dr-开通前逾期时刻", "2026-10-10T09:00:00+08:00");
|
|
307
|
+
const today = await cardIn(fresh, "dr-开通当天到期", "2026-10-10");
|
|
308
|
+
const tomorrow = await cardIn(fresh, "dr-开通次日到期", "2026-10-11");
|
|
309
|
+
// fresh 网络第一次被扫到 = T0(10:00 东八)
|
|
310
|
+
const first = due.runDueReminders({ now: T0 });
|
|
311
|
+
expect(sentFor(first, old)).toEqual([]);
|
|
312
|
+
expect(sentFor(first, oldTime)).toEqual([]);
|
|
313
|
+
expect(sentFor(first, today)).toEqual(["due_today"]);
|
|
314
|
+
const base = db.get("SELECT sent_at FROM requirement_due_reminders WHERE kind = 'baseline' AND network_id = ?1", fresh.net);
|
|
315
|
+
expect(base.sent_at).toBe(new Date(T0).toISOString());
|
|
316
|
+
// 第二天:旧卡仍不发;「开通当天到期」是在基线之后才逾期的 → 发「已逾期 1 天」;明天到期 → 今天到期
|
|
317
|
+
const next = due.runDueReminders({ now: T0 + DAY });
|
|
318
|
+
expect(sentFor(next, old)).toEqual([]);
|
|
319
|
+
expect(sentFor(next, oldTime)).toEqual([]);
|
|
320
|
+
expect(sentFor(next, today)).toEqual(["overdue"]);
|
|
321
|
+
expect(sentFor(next, tomorrow)).toEqual(["due_today"]);
|
|
322
|
+
// 基线行不被保留期清掉
|
|
323
|
+
due.runDueReminders({ now: T0 + 90 * DAY });
|
|
324
|
+
expect(db.get("SELECT COUNT(*) AS n FROM requirement_due_reminders WHERE kind = 'baseline' AND network_id = ?1", fresh.net).n).toBe(1);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("白名单后加进来的网络:以加进来那一刻为基线,不补发", async () => {
|
|
328
|
+
const late = mkNet("late");
|
|
329
|
+
process.env.COMMHUB_DUE_REMINDER_NETWORKS = NET;
|
|
330
|
+
try {
|
|
331
|
+
const old = await cardIn(late, "dr-后加网络旧卡", "2026-10-08");
|
|
332
|
+
due.runDueReminders({ now: T0 });
|
|
333
|
+
expect(db.get("SELECT COUNT(*) AS n FROM requirement_due_reminders WHERE kind = 'baseline' AND network_id = ?1", late.net).n).toBe(0);
|
|
334
|
+
process.env.COMMHUB_DUE_REMINDER_NETWORKS = `${NET},${late.net}`;
|
|
335
|
+
expect(sentFor(due.runDueReminders({ now: T0 + 3600_000 }), old)).toEqual([]);
|
|
336
|
+
} finally { delete process.env.COMMHUB_DUE_REMINDER_NETWORKS; }
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe("#494 列表筛选", () => {
|
|
341
|
+
// 筛选用真时钟:due 按此刻算出来。
|
|
342
|
+
const tz = "Asia/Shanghai";
|
|
343
|
+
let today = "", ids: Record<string, string> = {};
|
|
344
|
+
beforeAll(async () => {
|
|
345
|
+
const now = Date.now();
|
|
346
|
+
today = due.localDate(now, tz);
|
|
347
|
+
ids.over = await card("df-逾期", due.addDays(today, -2));
|
|
348
|
+
ids.overTime = await card("df-逾期时刻", new Date(now - 3600_000).toISOString());
|
|
349
|
+
ids.today = await card("df-今天", today);
|
|
350
|
+
ids.in3 = await card("df-三天后", due.addDays(today, 3));
|
|
351
|
+
ids.in10 = await card("df-十天后", due.addDays(today, 10));
|
|
352
|
+
ids.doneOver = await card("df-完成逾期", due.addDays(today, -2), { column: "done" });
|
|
353
|
+
ids.none = await card("df-无due", "");
|
|
354
|
+
});
|
|
355
|
+
const names = (rows: any[]) => rows.map((r: any) => r.name).filter((n: string) => n.startsWith("df-")).sort();
|
|
356
|
+
|
|
357
|
+
test("MCP overdue=true:只要没完成且过期的(日期型 + 时刻型)", async () => {
|
|
358
|
+
const r = await mcp(owen.token, "requirements_list", { network_id: NET, overdue: true, limit: 200 });
|
|
359
|
+
expect(r.ok).toBe(true);
|
|
360
|
+
expect(names(r.requirements)).toEqual(["df-逾期", "df-逾期时刻"].sort());
|
|
361
|
+
});
|
|
362
|
+
test("MCP overdue=false:其余的", async () => {
|
|
363
|
+
const r = await mcp(owen.token, "requirements_list", { network_id: NET, overdue: false, limit: 500 });
|
|
364
|
+
expect(names(r.requirements)).toEqual(["df-三天后", "df-今天", "df-十天后", "df-完成逾期", "df-无due"].sort());
|
|
365
|
+
});
|
|
366
|
+
test("MCP due_within_days:0 = 今天;3 = 到三天后;不含逾期 / 完成 / 无 due", async () => {
|
|
367
|
+
expect(names((await mcp(owen.token, "requirements_list", { network_id: NET, due_within_days: 0, limit: 200 })).requirements)).toEqual(["df-今天"]);
|
|
368
|
+
expect(names((await mcp(owen.token, "requirements_list", { network_id: NET, due_within_days: 3, limit: 200 })).requirements)).toEqual(["df-三天后", "df-今天"].sort());
|
|
369
|
+
});
|
|
370
|
+
test("MCP 严格参数:负数 / 非整数 / 字符串被拒", async () => {
|
|
371
|
+
for (const bad of [{ due_within_days: -1 }, { due_within_days: 1.5 }, { overdue: "yes" }]) {
|
|
372
|
+
const r = await mcp(owen.token, "requirements_list", { network_id: NET, ...bad });
|
|
373
|
+
expect(r.ok).not.toBe(true);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
test("REST 同一组参数;不合法 → 400 + field", async () => {
|
|
377
|
+
const ok = await send(owen.token, "GET", `/api/requirements?network_id=${NET}&overdue=1`);
|
|
378
|
+
expect(ok.status).toBe(200);
|
|
379
|
+
expect(names(ok.body.requirements)).toEqual(["df-逾期", "df-逾期时刻"].sort());
|
|
380
|
+
const w = await send(owen.token, "GET", `/api/requirements?network_id=${NET}&due_within_days=10`);
|
|
381
|
+
expect(names(w.body.requirements)).toEqual(["df-三天后", "df-今天", "df-十天后"].sort());
|
|
382
|
+
const bad = await send(owen.token, "GET", `/api/requirements?network_id=${NET}&overdue=maybe`);
|
|
383
|
+
expect(bad.status).toBe(400);
|
|
384
|
+
expect(bad.body.error).toBe("invalid_overdue");
|
|
385
|
+
expect(bad.body.field).toBe("overdue");
|
|
386
|
+
const bad2 = await send(owen.token, "GET", `/api/requirements?network_id=${NET}&due_within_days=999`);
|
|
387
|
+
expect(bad2.status).toBe(400);
|
|
388
|
+
expect(bad2.body.error).toBe("invalid_due_within_days");
|
|
389
|
+
});
|
|
390
|
+
});
|