


























@@ -0,0 +1,159 @@
1+import { beforeEach, describe, expect, it } from "vitest";
2+import {
3+resetGroupAllowlistWarningsForTesting,
4+warnGroupAllowlistDropPerChatOnce,
5+warnGroupAllowlistMisconfigOnce,
6+} from "./group-allowlist-warnings.js";
7+8+beforeEach(() => {
9+resetGroupAllowlistWarningsForTesting();
10+});
11+12+describe("warnGroupAllowlistMisconfigOnce", () => {
13+it("fires when groupPolicy=allowlist and groups is undefined", () => {
14+const messages: string[] = [];
15+const fired = warnGroupAllowlistMisconfigOnce({
16+groupPolicy: "allowlist",
17+groups: undefined,
18+accountId: "default",
19+log: (m) => messages.push(m),
20+});
21+expect(fired).toBe(true);
22+expect(messages).toHaveLength(1);
23+expect(messages[0]).toContain('groupPolicy="allowlist"');
24+expect(messages[0]).toContain("channels.imessage.groups is empty");
25+expect(messages[0]).toContain("default");
26+});
27+28+it("fires when groupPolicy=allowlist and groups is empty object", () => {
29+const messages: string[] = [];
30+const fired = warnGroupAllowlistMisconfigOnce({
31+groupPolicy: "allowlist",
32+groups: {},
33+accountId: "default",
34+log: (m) => messages.push(m),
35+});
36+expect(fired).toBe(true);
37+expect(messages).toHaveLength(1);
38+});
39+40+it("does not fire when groupPolicy is not allowlist", () => {
41+const messages: string[] = [];
42+const fired = warnGroupAllowlistMisconfigOnce({
43+groupPolicy: "open",
44+groups: undefined,
45+accountId: "default",
46+log: (m) => messages.push(m),
47+});
48+expect(fired).toBe(false);
49+expect(messages).toHaveLength(0);
50+});
51+52+it("does not fire when groups has a wildcard entry", () => {
53+const messages: string[] = [];
54+const fired = warnGroupAllowlistMisconfigOnce({
55+groupPolicy: "allowlist",
56+groups: { "*": { requireMention: true } },
57+accountId: "default",
58+log: (m) => messages.push(m),
59+});
60+expect(fired).toBe(false);
61+expect(messages).toHaveLength(0);
62+});
63+64+it("does not fire when groups has explicit chat_id entries", () => {
65+const messages: string[] = [];
66+const fired = warnGroupAllowlistMisconfigOnce({
67+groupPolicy: "allowlist",
68+groups: { "12345": {} },
69+accountId: "default",
70+log: (m) => messages.push(m),
71+});
72+expect(fired).toBe(false);
73+expect(messages).toHaveLength(0);
74+});
75+76+it("only fires once per accountId", () => {
77+const messages: string[] = [];
78+const log = (m: string) => messages.push(m);
79+expect(
80+warnGroupAllowlistMisconfigOnce({
81+groupPolicy: "allowlist",
82+groups: undefined,
83+accountId: "default",
84+ log,
85+}),
86+).toBe(true);
87+expect(
88+warnGroupAllowlistMisconfigOnce({
89+groupPolicy: "allowlist",
90+groups: undefined,
91+accountId: "default",
92+ log,
93+}),
94+).toBe(false);
95+expect(messages).toHaveLength(1);
96+});
97+98+it("fires separately for distinct accountIds", () => {
99+const messages: string[] = [];
100+const log = (m: string) => messages.push(m);
101+warnGroupAllowlistMisconfigOnce({
102+groupPolicy: "allowlist",
103+groups: undefined,
104+accountId: "primary",
105+ log,
106+});
107+warnGroupAllowlistMisconfigOnce({
108+groupPolicy: "allowlist",
109+groups: undefined,
110+accountId: "secondary",
111+ log,
112+});
113+expect(messages).toHaveLength(2);
114+});
115+});
116+117+describe("warnGroupAllowlistDropPerChatOnce", () => {
118+it("fires once per accountId:chat_id pair", () => {
119+const messages: string[] = [];
120+const log = (m: string) => messages.push(m);
121+expect(warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: 42, log })).toBe(true);
122+expect(warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: 42, log })).toBe(
123+false,
124+);
125+expect(messages).toHaveLength(1);
126+expect(messages[0]).toContain("chat_id=42");
127+expect(messages[0]).toContain("default");
128+expect(messages[0]).toContain('channels.imessage.groups["42"]');
129+});
130+131+it("fires separately for distinct chat_ids on the same account", () => {
132+const messages: string[] = [];
133+const log = (m: string) => messages.push(m);
134+warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: 1, log });
135+warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: 2, log });
136+warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: 2, log });
137+expect(messages).toHaveLength(2);
138+});
139+140+it("treats numeric and string chat_ids as the same key", () => {
141+const messages: string[] = [];
142+const log = (m: string) => messages.push(m);
143+warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: 42, log });
144+warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: "42", log });
145+expect(messages).toHaveLength(1);
146+});
147+148+it("skips when chat_id is undefined or empty", () => {
149+const messages: string[] = [];
150+const log = (m: string) => messages.push(m);
151+expect(
152+warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: undefined, log }),
153+).toBe(false);
154+expect(warnGroupAllowlistDropPerChatOnce({ accountId: "default", chatId: "", log })).toBe(
155+false,
156+);
157+expect(messages).toHaveLength(0);
158+});
159+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。