@cursor/july 0.1.23 → 0.1.24

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.
Files changed (174) hide show
  1. package/dist/bin/agent-serve.js +0 -0
  2. package/dist/docs/404.html +1 -1
  3. package/dist/docs/ab.html +2 -2
  4. package/dist/docs/assets/{app.DYcC9FY-.js → app.CvSGaAxk.js} +1 -1
  5. package/dist/docs/assets/chunks/@localSearchIndexroot.CL2Y0Zmh.js +1 -0
  6. package/dist/docs/assets/chunks/{VPLocalSearchBox.o4N_knTV.js → VPLocalSearchBox.J1jJbCvs.js} +1 -1
  7. package/dist/docs/assets/chunks/{theme.DQ-njyo0.js → theme.C7vfzr7h.js} +2 -2
  8. package/dist/docs/building-with-agents.html +2 -2
  9. package/dist/docs/concepts.html +2 -2
  10. package/dist/docs/deployment.html +2 -2
  11. package/dist/docs/evals.html +2 -2
  12. package/dist/docs/example-agents/approval-buddy.html +2 -2
  13. package/dist/docs/example-agents/benny.html +2 -2
  14. package/dist/docs/example-agents/bugbot.html +2 -2
  15. package/dist/docs/example-agents/codebase-wiki.html +2 -2
  16. package/dist/docs/example-agents/codeowners-review.html +2 -2
  17. package/dist/docs/example-agents/concierge.html +2 -2
  18. package/dist/docs/example-agents/fsd.html +2 -2
  19. package/dist/docs/example-agents/index.html +2 -2
  20. package/dist/docs/example-agents/knowledge-base.html +2 -2
  21. package/dist/docs/example-agents/oncall.html +2 -2
  22. package/dist/docs/example-agents/security-reviewer.html +2 -2
  23. package/dist/docs/example-agents/slack-agent.html +2 -2
  24. package/dist/docs/example-agents/weather-agent.html +2 -2
  25. package/dist/docs/guides/agent-to-agent.html +2 -2
  26. package/dist/docs/guides/cloud-runtime.html +2 -2
  27. package/dist/docs/guides/github.html +2 -2
  28. package/dist/docs/guides/human-in-the-loop.html +2 -2
  29. package/dist/docs/guides/mcp-oauth.html +2 -2
  30. package/dist/docs/guides/slack.html +2 -2
  31. package/dist/docs/guides/webhooks.html +2 -2
  32. package/dist/docs/hillclimbing.html +2 -2
  33. package/dist/docs/index.html +2 -2
  34. package/dist/docs/quickstart.html +2 -2
  35. package/dist/docs/reference/agent-config.html +2 -2
  36. package/dist/docs/reference/channels.html +2 -2
  37. package/dist/docs/reference/cli.html +2 -2
  38. package/dist/docs/reference/connections.html +2 -2
  39. package/dist/docs/reference/hooks.html +2 -2
  40. package/dist/docs/reference/http-api.html +2 -2
  41. package/dist/docs/reference/instructions.html +2 -2
  42. package/dist/docs/reference/playground.html +2 -2
  43. package/dist/docs/reference/project-layout.html +2 -2
  44. package/dist/docs/reference/prompt.html +2 -2
  45. package/dist/docs/reference/schedules.html +2 -2
  46. package/dist/docs/reference/sessions.html +2 -2
  47. package/dist/docs/reference/skills.html +2 -2
  48. package/dist/docs/reference/subagents.html +2 -2
  49. package/dist/docs/reference/tools.html +2 -2
  50. package/dist/docs/scaffolding-agents.html +2 -2
  51. package/dist/docs/storage.html +2 -2
  52. package/dist/docs/troubleshooting.html +2 -2
  53. package/dist/playground/assets/index-CidizGZv.css +1 -0
  54. package/dist/playground/assets/{index-DqXdAFGa.js → index-DTG9OsPV.js} +41 -41
  55. package/dist/playground/index.html +2 -2
  56. package/package.json +24 -24
  57. package/dist/channels/github/instrument.d.ts +0 -20
  58. package/dist/channels/github/instrument.d.ts.map +0 -1
  59. package/dist/docs/assets/chunks/@localSearchIndexroot.BQTzJjR_.js +0 -1
  60. package/dist/internal/json-dir-store.d.ts +0 -32
  61. package/dist/internal/json-dir-store.d.ts.map +0 -1
  62. package/dist/internal/json-dir-store.js +0 -100
  63. package/dist/playground/assets/index-CjOQ4hN9.css +0 -1
  64. package/src/bin/agent-serve.version.test.ts +0 -64
  65. package/src/channels/github/api.test.ts +0 -64
  66. package/src/channels/github/auth.test.ts +0 -105
  67. package/src/channels/github/cursor-account.test.ts +0 -204
  68. package/src/channels/github/forward.test.ts +0 -457
  69. package/src/channels/github/github.test.ts +0 -937
  70. package/src/channels/github/replay.test.ts +0 -179
  71. package/src/channels/slack/api.post-message.test.ts +0 -148
  72. package/src/channels/slack/approvals.test.ts +0 -328
  73. package/src/channels/slack/block-actions.test.ts +0 -452
  74. package/src/channels/slack/bot-mentions.test.ts +0 -267
  75. package/src/channels/slack/channel-watch.test.ts +0 -363
  76. package/src/channels/slack/cursor-account.test.ts +0 -253
  77. package/src/channels/slack/defaults.final-post.test.ts +0 -182
  78. package/src/channels/slack/dispatch.test.ts +0 -795
  79. package/src/channels/slack/eval-directive.test.ts +0 -273
  80. package/src/channels/slack/message-body.test.ts +0 -54
  81. package/src/channels/slack/nudge-store.test.ts +0 -143
  82. package/src/channels/slack/slack.test.ts +0 -391
  83. package/src/channels/slack/stop.test.ts +0 -23
  84. package/src/channels/slack/thread-context.test.ts +0 -202
  85. package/src/evals/assertions.test.ts +0 -580
  86. package/src/evals/expect.test.ts +0 -144
  87. package/src/evals/judge.test.ts +0 -181
  88. package/src/evals/loaders.test.ts +0 -132
  89. package/src/evals/matchers.test.ts +0 -95
  90. package/src/evals/reporters.test.ts +0 -303
  91. package/src/evals/run-facts.test.ts +0 -259
  92. package/src/internal/ab-snapshot.test.ts +0 -325
  93. package/src/internal/approval-gate.test.ts +0 -49
  94. package/src/internal/approvals.integration.test.ts +0 -383
  95. package/src/internal/authored-loaders.test.ts +0 -31
  96. package/src/internal/builtin-tools/reminders.test.ts +0 -201
  97. package/src/internal/channel-route-schema.test.ts +0 -294
  98. package/src/internal/chat-attach.test.ts +0 -262
  99. package/src/internal/cli-deploy.test.ts +0 -1991
  100. package/src/internal/cli-docs.test.ts +0 -161
  101. package/src/internal/cli-mcp.test.ts +0 -789
  102. package/src/internal/cli-skills.test.ts +0 -133
  103. package/src/internal/cli-slack.test.ts +0 -1647
  104. package/src/internal/cloud-merge.test.ts +0 -74
  105. package/src/internal/cron.test.ts +0 -22
  106. package/src/internal/cursor/account-mcp.test.ts +0 -807
  107. package/src/internal/cursor/backend-client.test.ts +0 -591
  108. package/src/internal/cursor/credentials.test.ts +0 -351
  109. package/src/internal/cursor/github-credentials.test.ts +0 -136
  110. package/src/internal/cursor-account-mcp-auth.test.ts +0 -310
  111. package/src/internal/cursor-account.integration.test.ts +0 -441
  112. package/src/internal/cursor-event-relay.test.ts +0 -746
  113. package/src/internal/cursor-github-credentials.integration.test.ts +0 -271
  114. package/src/internal/cursor-slack-relay.test.ts +0 -525
  115. package/src/internal/deploy-source.test.ts +0 -111
  116. package/src/internal/discovery.builtin-tools.test.ts +0 -94
  117. package/src/internal/discovery.concurrency.test.ts +0 -60
  118. package/src/internal/discovery.cursor-account.test.ts +0 -133
  119. package/src/internal/discovery.cwd.test.ts +0 -83
  120. package/src/internal/discovery.hosting.test.ts +0 -80
  121. package/src/internal/discovery.identity.test.ts +0 -44
  122. package/src/internal/docs-site.test.ts +0 -66
  123. package/src/internal/duration.test.ts +0 -29
  124. package/src/internal/eval-judge-model.test.ts +0 -187
  125. package/src/internal/eval-run-store.cancel.test.ts +0 -142
  126. package/src/internal/eval-run-store.storage.test.ts +0 -211
  127. package/src/internal/eval-runner.http.test.ts +0 -403
  128. package/src/internal/eval-runner.run.test.ts +0 -928
  129. package/src/internal/evals-client.test.ts +0 -307
  130. package/src/internal/event-mapper.test.ts +0 -243
  131. package/src/internal/github-fanout.test.ts +0 -213
  132. package/src/internal/handleAgentServeTrigger.test.ts +0 -179
  133. package/src/internal/host-kv.test.ts +0 -82
  134. package/src/internal/host-platforms.test.ts +0 -126
  135. package/src/internal/http-channel.test.ts +0 -402
  136. package/src/internal/init-project.test.ts +0 -270
  137. package/src/internal/install-cursor-skills.test.ts +0 -262
  138. package/src/internal/local-env.test.ts +0 -120
  139. package/src/internal/log-ring.test.ts +0 -31
  140. package/src/internal/logs-client.test.ts +0 -350
  141. package/src/internal/mcp-endpoint.test.ts +0 -436
  142. package/src/internal/mcp-host.test.ts +0 -298
  143. package/src/internal/mcp-oauth.test.ts +0 -148
  144. package/src/internal/net.test.ts +0 -17
  145. package/src/internal/peer-connections.test.ts +0 -128
  146. package/src/internal/peer-mcp.integration.test.ts +0 -289
  147. package/src/internal/playground/toolchain.test.ts +0 -53
  148. package/src/internal/playground-cli.test.ts +0 -187
  149. package/src/internal/playground-proxy.test.ts +0 -376
  150. package/src/internal/prompt-context.integration.test.ts +0 -232
  151. package/src/internal/prompt-context.test.ts +0 -127
  152. package/src/internal/reminder-runner.test.ts +0 -390
  153. package/src/internal/reminder-store.test.ts +0 -53
  154. package/src/internal/request-headers.test.ts +0 -27
  155. package/src/internal/resolve-prod-target.test.ts +0 -787
  156. package/src/internal/resolved-connections.test.ts +0 -295
  157. package/src/internal/router.test.ts +0 -57
  158. package/src/internal/sdk-runner.test.ts +0 -290
  159. package/src/internal/session-engine.coalesce.test.ts +0 -169
  160. package/src/internal/session-engine.concurrency.test.ts +0 -250
  161. package/src/internal/session-engine.host-oauth-mcp.test.ts +0 -110
  162. package/src/internal/session-engine.interrupt.test.ts +0 -577
  163. package/src/internal/session-engine.storage.test.ts +0 -547
  164. package/src/internal/session-urls.test.ts +0 -28
  165. package/src/internal/sessions-client.test.ts +0 -518
  166. package/src/internal/storage-coordinator.test.ts +0 -517
  167. package/src/internal/tool-call.test.ts +0 -458
  168. package/src/internal/tool-result.test.ts +0 -52
  169. package/src/internal/trajectory.approvals.test.ts +0 -83
  170. package/src/internal/trajectory.subagents.test.ts +0 -198
  171. package/src/internal/turn-governor.test.ts +0 -137
  172. package/src/internal/update-check.test.ts +0 -485
  173. package/src/internal/workspace.test.ts +0 -207
  174. package/src/storage-backends/cursor-hosted.test.ts +0 -121
@@ -1,267 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- botMentionsFromEnv,
4
- buildDelegationMetadata,
5
- normalizeBotMentionAllowlist,
6
- parseDelegationMetadata,
7
- SLACK_DELEGATION_METADATA_EVENT_TYPE,
8
- SlackBotMentionGate,
9
- } from "./bot-mentions.js";
10
- import { parseAppMentionEvent } from "./inbound.js";
11
- import type { SlackMessage } from "./types.js";
12
-
13
- function botMention(patch: Partial<SlackMessage> = {}): SlackMessage {
14
- return {
15
- text: "<@ULASSO> please deploy #123",
16
- body: "<@ULASSO> please deploy #123",
17
- markdown: "@ULASSO please deploy #123",
18
- ts: "2.0",
19
- threadTs: "1.0",
20
- channelId: "C9",
21
- author: { userId: "ULASSO", isBot: true, botId: "BLASSO" },
22
- ...patch,
23
- };
24
- }
25
-
26
- function gate(
27
- options: Partial<ConstructorParameters<typeof SlackBotMentionGate>[0]> = {},
28
- identity: { botId?: string; botUserId?: string } = {
29
- botId: "BSELF",
30
- botUserId: "USELF",
31
- }
32
- ): SlackBotMentionGate {
33
- return new SlackBotMentionGate(
34
- { allowFrom: ["BLASSO"], ...options },
35
- identity,
36
- { log: { info() {}, warn() {}, error() {}, debug() {}, write() {} } }
37
- );
38
- }
39
-
40
- describe("delegation metadata", () => {
41
- it("round-trips build → parse", () => {
42
- const metadata = buildDelegationMetadata({
43
- depth: 2,
44
- fromAgent: "lasso",
45
- originSessionId: "sess-1",
46
- });
47
- expect(metadata.event_type).toBe(SLACK_DELEGATION_METADATA_EVENT_TYPE);
48
- const parsed = parseDelegationMetadata({ metadata });
49
- expect(parsed).toEqual({
50
- depth: 2,
51
- fromAgent: "lasso",
52
- originSessionId: "sess-1",
53
- });
54
- });
55
-
56
- it("ignores foreign and malformed metadata", () => {
57
- expect(parseDelegationMetadata({})).toBeUndefined();
58
- expect(
59
- parseDelegationMetadata({
60
- metadata: { event_type: "other", event_payload: { depth: 1 } },
61
- })
62
- ).toBeUndefined();
63
- expect(
64
- parseDelegationMetadata({
65
- metadata: {
66
- event_type: SLACK_DELEGATION_METADATA_EVENT_TYPE,
67
- event_payload: { depth: 0 },
68
- },
69
- })
70
- ).toBeUndefined();
71
- expect(
72
- parseDelegationMetadata({
73
- metadata: {
74
- event_type: SLACK_DELEGATION_METADATA_EVENT_TYPE,
75
- event_payload: { depth: "two" },
76
- },
77
- })
78
- ).toBeUndefined();
79
- });
80
- });
81
-
82
- describe("botMentionsFromEnv", () => {
83
- it("returns undefined when unset and options when set", () => {
84
- const envVar = "BOT_MENTIONS_TEST_VAR";
85
- delete process.env[envVar];
86
- expect(botMentionsFromEnv(envVar)).toBeUndefined();
87
- process.env[envVar] = " B1 ,,U2 ";
88
- expect(botMentionsFromEnv(envVar)).toEqual({ allowFrom: ["B1", "U2"] });
89
- delete process.env[envVar];
90
- });
91
- });
92
-
93
- describe("parseAppMentionEvent bot-authored", () => {
94
- const body = {
95
- type: "event_callback",
96
- team_id: "T1",
97
- event: {
98
- type: "app_mention",
99
- user: "ULASSO",
100
- bot_id: "BLASSO",
101
- text: "<@UTARGET> look into this",
102
- ts: "2.0",
103
- thread_ts: "1.0",
104
- channel: "C9",
105
- metadata: buildDelegationMetadata({ depth: 1, fromAgent: "lasso" }),
106
- },
107
- };
108
-
109
- it("drops bot mentions by default", () => {
110
- expect(parseAppMentionEvent(body)).toBeNull();
111
- });
112
-
113
- it("keeps bot mentions with delegation info when opted in", () => {
114
- const message = parseAppMentionEvent(body, { includeBotAuthored: true });
115
- expect(message).not.toBeNull();
116
- expect(message?.author?.isBot).toBe(true);
117
- expect(message?.author?.botId).toBe("BLASSO");
118
- expect(message?.delegation).toEqual({ depth: 1, fromAgent: "lasso" });
119
- });
120
-
121
- it("still parses human mentions when opted in", () => {
122
- const message = parseAppMentionEvent(
123
- {
124
- type: "event_callback",
125
- event: {
126
- type: "app_mention",
127
- user: "U1",
128
- text: "<@UTARGET> hi",
129
- ts: "3.0",
130
- channel: "C9",
131
- },
132
- },
133
- { includeBotAuthored: true }
134
- );
135
- expect(message?.author?.isBot).toBe(false);
136
- expect(message?.delegation).toBeUndefined();
137
- });
138
- });
139
-
140
- describe("normalizeBotMentionAllowlist", () => {
141
- it("trims entries and rejects empty lists", () => {
142
- expect(normalizeBotMentionAllowlist([" B1 ", "U2"])).toEqual(["B1", "U2"]);
143
- expect(() => normalizeBotMentionAllowlist([" "])).toThrow(/allowFrom/);
144
- });
145
- });
146
-
147
- describe("SlackBotMentionGate", () => {
148
- it("requires its own bot identity", () => {
149
- expect(() => gate({}, {})).toThrow(/identity/);
150
- });
151
-
152
- it("admits an allowlisted bot mention", () => {
153
- const verdict = gate().evaluate(botMention());
154
- expect(verdict).toEqual({ allow: true, depth: 1 });
155
- });
156
-
157
- it("matches on bot user id as well as bot_id", () => {
158
- const verdict = gate({ allowFrom: ["ULASSO"] }).evaluate(botMention());
159
- expect(verdict.allow).toBe(true);
160
- });
161
-
162
- it("matches compound botId:username entries case-insensitively", () => {
163
- const g = gate({ allowFrom: ["BCURSOR:Lasso"] });
164
- const fromCursorAsLasso = botMention({
165
- author: {
166
- userId: "UCURSOR",
167
- isBot: true,
168
- botId: "BCURSOR",
169
- userName: "lasso",
170
- },
171
- });
172
- expect(g.evaluate(fromCursorAsLasso).allow).toBe(true);
173
- });
174
-
175
- it("compound entries deny other usernames and missing usernames", () => {
176
- const g = gate({ allowFrom: ["BCURSOR:Lasso"] });
177
- expect(
178
- g.evaluate(
179
- botMention({
180
- author: {
181
- userId: "UCURSOR",
182
- isBot: true,
183
- botId: "BCURSOR",
184
- userName: "SomeOtherAgent",
185
- },
186
- })
187
- )
188
- ).toMatchObject({ allow: false });
189
- // No username on the event → fail closed.
190
- expect(
191
- g.evaluate(
192
- botMention({
193
- author: { userId: "UCURSOR", isBot: true, botId: "BCURSOR" },
194
- })
195
- )
196
- ).toMatchObject({ allow: false });
197
- // Bare bot_id was not allowlisted, only the compound form.
198
- expect(
199
- g.evaluate(
200
- botMention({
201
- author: {
202
- userId: "UCURSOR",
203
- isBot: true,
204
- botId: "BCURSOR",
205
- userName: undefined,
206
- },
207
- })
208
- )
209
- ).toMatchObject({ allow: false });
210
- });
211
-
212
- it("denies bots not in the allowlist", () => {
213
- const verdict = gate().evaluate(
214
- botMention({ author: { userId: "UOTHER", isBot: true, botId: "BOTHER" } })
215
- );
216
- expect(verdict).toMatchObject({ allow: false });
217
- });
218
-
219
- it("denies its own posts even when allowlisted", () => {
220
- const verdict = gate({ allowFrom: ["BSELF"] }).evaluate(
221
- botMention({ author: { userId: "USELF", isBot: true, botId: "BSELF" } })
222
- );
223
- expect(verdict).toMatchObject({ allow: false, reason: /own post/ });
224
- });
225
-
226
- it("enforces the depth budget", () => {
227
- const g = gate();
228
- expect(g.evaluate(botMention({ delegation: { depth: 2 } })).allow).toBe(
229
- true
230
- );
231
- expect(
232
- g.evaluate(botMention({ ts: "3.0", delegation: { depth: 3 } }))
233
- ).toMatchObject({ allow: false, reason: /depth/ });
234
- });
235
-
236
- it("enforces the rate budget with a sliding window", () => {
237
- const g = gate({ maxPerThreadPerHour: 2 });
238
- expect(g.evaluate(botMention(), 1000).allow).toBe(true);
239
- expect(g.evaluate(botMention(), 2000).allow).toBe(true);
240
- expect(g.evaluate(botMention(), 3000)).toMatchObject({
241
- allow: false,
242
- reason: /rate budget/,
243
- });
244
- // Window slides: an hour later the thread has rate budget again.
245
- const later = 3000 + 60 * 60 * 1000;
246
- expect(g.evaluate(botMention(), later).allow).toBe(true);
247
- // Other threads are unaffected.
248
- expect(g.evaluate(botMention({ threadTs: "9.0" }), 3000).allow).toBe(true);
249
- });
250
-
251
- it("terminates ping-pong with the lifetime thread cap", () => {
252
- const g = gate({ maxPerThreadPerHour: 100, maxPerThreadTotal: 3 });
253
- expect(g.evaluate(botMention(), 1000).allow).toBe(true);
254
- expect(g.evaluate(botMention(), 2000).allow).toBe(true);
255
- expect(g.evaluate(botMention(), 3000).allow).toBe(true);
256
- expect(g.evaluate(botMention(), 4000)).toMatchObject({
257
- allow: false,
258
- reason: /lifetime/,
259
- });
260
- // Sliding the window does NOT refill the lifetime cap.
261
- const muchLater = 4000 + 5 * 60 * 60 * 1000;
262
- expect(g.evaluate(botMention(), muchLater)).toMatchObject({
263
- allow: false,
264
- reason: /lifetime/,
265
- });
266
- });
267
- });
@@ -1,363 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import {
3
- createChannelPostWatcher,
4
- SlackChannelAllowlist,
5
- } from "./channel-watch.js";
6
- import {
7
- parseChannelMessageEvent,
8
- type SlackChannelMessageEvent,
9
- } from "./inbound.js";
10
- import type { SlackMessage } from "./types.js";
11
-
12
- function channelPostBody(args: {
13
- channel?: string;
14
- ts?: string;
15
- threadTs?: string;
16
- text?: string;
17
- subtype?: string;
18
- botId?: string;
19
- channelType?: string;
20
- user?: string | null;
21
- }): unknown {
22
- return {
23
- type: "event_callback",
24
- event_id: "Ev1",
25
- team_id: "T1",
26
- event: {
27
- type: "message",
28
- channel: args.channel ?? "C123",
29
- channel_type: args.channelType ?? "channel",
30
- ts: args.ts ?? "100.1",
31
- ...(args.threadTs === undefined ? {} : { thread_ts: args.threadTs }),
32
- text: args.text ?? "new bug report",
33
- ...(args.user === null ? {} : { user: args.user ?? "U1" }),
34
- ...(args.subtype === undefined ? {} : { subtype: args.subtype }),
35
- ...(args.botId === undefined ? {} : { bot_id: args.botId }),
36
- },
37
- };
38
- }
39
-
40
- describe("parseChannelMessageEvent", () => {
41
- it("parses a top-level channel post", () => {
42
- const parsed = parseChannelMessageEvent(channelPostBody({}));
43
- expect(parsed).not.toBeNull();
44
- expect(parsed?.kind).toBe("post");
45
- if (parsed?.kind === "post") {
46
- expect(parsed.message.channelId).toBe("C123");
47
- expect(parsed.message.ts).toBe("100.1");
48
- expect(parsed.message.threadTs).toBe("100.1");
49
- }
50
- });
51
-
52
- it("parses private-channel (group) posts", () => {
53
- const parsed = parseChannelMessageEvent(
54
- channelPostBody({ channel: "G9", channelType: "group" })
55
- );
56
- expect(parsed?.kind).toBe("post");
57
- });
58
-
59
- it("drops bot-authored posts", () => {
60
- expect(
61
- parseChannelMessageEvent(channelPostBody({ botId: "B77" }))
62
- ).toBeNull();
63
- });
64
-
65
- it("keeps bot-authored posts when includeBotAuthored is set", () => {
66
- const parsed = parseChannelMessageEvent(channelPostBody({ botId: "B77" }), {
67
- includeBotAuthored: true,
68
- });
69
- expect(parsed?.kind).toBe("post");
70
- if (parsed?.kind === "post") {
71
- expect(parsed.message.author?.isBot).toBe(true);
72
- expect(parsed.message.author?.botId).toBe("B77");
73
- }
74
- });
75
-
76
- it("keeps userless bot_message webhook posts when includeBotAuthored is set", () => {
77
- const body = channelPostBody({
78
- botId: "B77",
79
- subtype: "bot_message",
80
- user: null,
81
- text: "PagerDuty: high error rate on api",
82
- });
83
- expect(parseChannelMessageEvent(body)).toBeNull();
84
- const parsed = parseChannelMessageEvent(body, {
85
- includeBotAuthored: true,
86
- });
87
- expect(parsed?.kind).toBe("post");
88
- if (parsed?.kind === "post") {
89
- expect(parsed.message.author).toEqual({
90
- userId: "",
91
- isBot: true,
92
- botId: "B77",
93
- });
94
- }
95
- });
96
-
97
- it("recovers alert content from attachments when top-level text is empty", () => {
98
- const parsed = parseChannelMessageEvent(
99
- {
100
- type: "event_callback",
101
- team_id: "T1",
102
- event: {
103
- type: "message",
104
- subtype: "bot_message",
105
- channel: "C123",
106
- channel_type: "channel",
107
- ts: "100.1",
108
- bot_id: "B77",
109
- username: "Datadog",
110
- text: "",
111
- attachments: [
112
- {
113
- title:
114
- "Triggered: [service-monitor prod:temporal-bugbot] High cache error rate",
115
- title_link: "https://us5.datadoghq.com/monitors/19610160",
116
- text: "Service temporal-bugbot has a high cache error rate.",
117
- },
118
- ],
119
- },
120
- },
121
- { includeBotAuthored: true }
122
- );
123
- expect(parsed?.kind).toBe("post");
124
- if (parsed?.kind === "post") {
125
- expect(parsed.message.text).toBe("");
126
- expect(parsed.message.body).toContain("temporal-bugbot");
127
- // Raw body keeps the monitor link; markdown is the model-facing view.
128
- expect(parsed.message.body).toContain(
129
- "https://us5.datadoghq.com/monitors/19610160"
130
- );
131
- expect(parsed.message.markdown).toContain("High cache error rate");
132
- }
133
- });
134
-
135
- it("drops DMs and unknown subtypes", () => {
136
- expect(
137
- parseChannelMessageEvent(
138
- channelPostBody({ channel: "D5", channelType: "im" })
139
- )
140
- ).toBeNull();
141
- expect(
142
- parseChannelMessageEvent(channelPostBody({ subtype: "channel_join" }))
143
- ).toBeNull();
144
- });
145
-
146
- it("parses message_changed into an edit with the new text", () => {
147
- const parsed = parseChannelMessageEvent({
148
- type: "event_callback",
149
- event: {
150
- type: "message",
151
- subtype: "message_changed",
152
- channel: "C123",
153
- channel_type: "channel",
154
- ts: "101.0",
155
- message: { type: "message", ts: "100.1", text: "edited!", user: "U1" },
156
- },
157
- });
158
- expect(parsed?.kind).toBe("edited");
159
- if (parsed?.kind === "edited") {
160
- expect(parsed.message.ts).toBe("100.1");
161
- expect(parsed.message.text).toBe("edited!");
162
- }
163
- });
164
-
165
- it("parses message_deleted into a deletion", () => {
166
- const parsed = parseChannelMessageEvent({
167
- type: "event_callback",
168
- event: {
169
- type: "message",
170
- subtype: "message_deleted",
171
- channel: "C123",
172
- channel_type: "channel",
173
- ts: "101.0",
174
- deleted_ts: "100.1",
175
- },
176
- });
177
- expect(parsed).toEqual({ kind: "deleted", channelId: "C123", ts: "100.1" });
178
- });
179
- });
180
-
181
- describe("SlackChannelAllowlist", () => {
182
- it("matches ids directly and names after resolution", () => {
183
- const allowlist = new SlackChannelAllowlist(["C123", "#issues-glass"]);
184
- expect(allowlist.matches("C123")).toBe(true);
185
- expect(allowlist.matches("C999")).toBe(false);
186
- expect(allowlist.unresolvedNames()).toEqual(["issues-glass"]);
187
- allowlist.applyResolvedNames(new Map([["issues-glass", "C999"]]));
188
- expect(allowlist.matches("C999")).toBe(true);
189
- });
190
-
191
- it("treats bare names as names, not ids", () => {
192
- const allowlist = new SlackChannelAllowlist(["issues-glass"]);
193
- expect(allowlist.unresolvedNames()).toEqual(["issues-glass"]);
194
- });
195
- });
196
-
197
- describe("createChannelPostWatcher", () => {
198
- const dispatched: Array<{ message: SlackMessage; eventId?: string }> = [];
199
-
200
- function watcher(args?: {
201
- posts?: "top-level" | "all";
202
- debounceMs?: number;
203
- includeBotPosts?: boolean;
204
- botUserId?: string;
205
- botId?: string;
206
- allow?: string[];
207
- }) {
208
- return createChannelPostWatcher({
209
- options: {
210
- allow: args?.allow ?? ["C123"],
211
- ...(args?.posts === undefined ? {} : { posts: args.posts }),
212
- ...(args?.debounceMs === undefined
213
- ? {}
214
- : { debounceMs: args.debounceMs }),
215
- ...(args?.includeBotPosts === undefined
216
- ? {}
217
- : { includeBotPosts: args.includeBotPosts }),
218
- },
219
- allowlist: new SlackChannelAllowlist(args?.allow ?? ["C123"]),
220
- getBotUserId: () => args?.botUserId,
221
- getBotId: () => args?.botId,
222
- dispatch: (message, eventId) =>
223
- dispatched.push(
224
- eventId === undefined ? { message } : { message, eventId }
225
- ),
226
- });
227
- }
228
-
229
- function post(args: {
230
- channel?: string;
231
- ts?: string;
232
- threadTs?: string;
233
- text?: string;
234
- subtype?: string;
235
- botId?: string;
236
- user?: string | null;
237
- }): SlackChannelMessageEvent {
238
- const parsed = parseChannelMessageEvent(channelPostBody(args), {
239
- includeBotAuthored: true,
240
- });
241
- if (parsed === null) {
242
- throw new Error("fixture did not parse");
243
- }
244
- return parsed;
245
- }
246
-
247
- beforeEach(() => {
248
- dispatched.length = 0;
249
- });
250
- afterEach(() => {
251
- vi.useRealTimers();
252
- });
253
-
254
- it("dispatches allowlisted top-level posts immediately by default", () => {
255
- const w = watcher();
256
- expect(w.handle(post({}), "Ev1")).toBe(true);
257
- expect(dispatched).toHaveLength(1);
258
- expect(dispatched[0]?.eventId).toBe("Ev1");
259
- });
260
-
261
- it("skips channels outside the allowlist", () => {
262
- const w = watcher();
263
- w.handle(post({ channel: "C999" }));
264
- expect(dispatched).toHaveLength(0);
265
- });
266
-
267
- it("skips thread replies unless posts is 'all'", () => {
268
- const w = watcher();
269
- w.handle(post({ ts: "100.2", threadTs: "100.1" }));
270
- expect(dispatched).toHaveLength(0);
271
-
272
- const all = watcher({ posts: "all" });
273
- all.handle(post({ ts: "100.2", threadTs: "100.1" }));
274
- expect(dispatched).toHaveLength(1);
275
- });
276
-
277
- it("skips posts that mention the bot (app_mention path owns them)", () => {
278
- const w = watcher({ botUserId: "UBOT" });
279
- w.handle(post({ text: "<@UBOT> please triage" }));
280
- expect(dispatched).toHaveLength(0);
281
- });
282
-
283
- it("skips bot-authored posts unless includeBotPosts is set", () => {
284
- const w = watcher();
285
- w.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
286
- expect(dispatched).toHaveLength(0);
287
-
288
- const withBots = watcher({
289
- includeBotPosts: true,
290
- botId: "BSELF",
291
- botUserId: "UBOT",
292
- });
293
- withBots.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
294
- expect(dispatched).toHaveLength(1);
295
- expect(dispatched[0]?.message.author?.botId).toBe("B77");
296
- });
297
-
298
- it("skips bot-authored posts when includeBotPosts is on but bot identity is unknown", () => {
299
- const w = watcher({ includeBotPosts: true });
300
- w.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
301
- expect(dispatched).toHaveLength(0);
302
- });
303
-
304
- it("always skips the watching bot's own posts (loop safety)", () => {
305
- const w = watcher({
306
- includeBotPosts: true,
307
- botUserId: "UBOT",
308
- botId: "BSELF",
309
- });
310
- w.handle(post({ botId: "BSELF", user: null, subtype: "bot_message" }));
311
- expect(dispatched).toHaveLength(0);
312
-
313
- // Same app posting with its bot user id attached.
314
- w.handle(post({ botId: "B99", user: "UBOT" }));
315
- expect(dispatched).toHaveLength(0);
316
-
317
- // A different bot still dispatches.
318
- w.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
319
- expect(dispatched).toHaveLength(1);
320
- });
321
-
322
- it("debounces: edits update the pending text, deletes cancel", () => {
323
- vi.useFakeTimers();
324
- const w = watcher({ debounceMs: 1_000 });
325
-
326
- w.handle(post({ ts: "100.1", text: "first" }));
327
- expect(dispatched).toHaveLength(0);
328
- const edit = parseChannelMessageEvent({
329
- type: "event_callback",
330
- event: {
331
- type: "message",
332
- subtype: "message_changed",
333
- channel: "C123",
334
- channel_type: "channel",
335
- ts: "101.0",
336
- message: { type: "message", ts: "100.1", text: "edited", user: "U1" },
337
- },
338
- });
339
- if (edit === null) {
340
- throw new Error("edit fixture did not parse");
341
- }
342
- w.handle(edit);
343
- vi.advanceTimersByTime(1_000);
344
- expect(dispatched).toHaveLength(1);
345
- expect(dispatched[0]?.message.text).toBe("edited");
346
-
347
- w.handle(post({ ts: "200.1", text: "will be deleted" }));
348
- w.handle({ kind: "deleted", channelId: "C123", ts: "200.1" });
349
- vi.advanceTimersByTime(5_000);
350
- expect(dispatched).toHaveLength(1);
351
-
352
- w.stop();
353
- });
354
-
355
- it("stop() cancels pending dispatches", () => {
356
- vi.useFakeTimers();
357
- const w = watcher({ debounceMs: 1_000 });
358
- w.handle(post({}));
359
- w.stop();
360
- vi.advanceTimersByTime(5_000);
361
- expect(dispatched).toHaveLength(0);
362
- });
363
- });