channelstack 0.0.0-stage → 0.1.1

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 (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -2
  3. package/bin/channelstack.mjs +31 -0
  4. package/dist/integrations/checks/checks.test.d.ts +1 -0
  5. package/dist/integrations/checks/checks.test.js +31 -0
  6. package/dist/integrations/checks/index.d.ts +69 -0
  7. package/dist/integrations/checks/index.js +578 -0
  8. package/dist/integrations/matchers-core/index.d.ts +138 -0
  9. package/dist/integrations/matchers-core/index.js +458 -0
  10. package/dist/integrations/matchers-core/matchers.test.d.ts +1 -0
  11. package/dist/integrations/matchers-core/matchers.test.js +159 -0
  12. package/dist/integrations/vitest/global-setup.d.ts +3 -0
  13. package/dist/integrations/vitest/global-setup.js +19 -0
  14. package/dist/integrations/vitest/index.d.ts +80 -0
  15. package/dist/integrations/vitest/index.js +42 -0
  16. package/dist/integrations/vitest/plugin.test.d.ts +1 -0
  17. package/dist/integrations/vitest/plugin.test.js +19 -0
  18. package/dist/integrations/vitest/refused.d.ts +22 -0
  19. package/dist/integrations/vitest/refused.js +49 -0
  20. package/dist/integrations/vitest/setup.d.ts +1 -0
  21. package/dist/integrations/vitest/setup.js +109 -0
  22. package/dist/sdks/typescript/client.d.ts +13 -0
  23. package/dist/sdks/typescript/client.js +11 -0
  24. package/dist/sdks/typescript/generated/channels.d.ts +24 -0
  25. package/dist/sdks/typescript/generated/channels.js +2 -0
  26. package/dist/sdks/typescript/generated/index.d.ts +2747 -0
  27. package/dist/sdks/typescript/generated/index.js +3039 -0
  28. package/dist/sdks/typescript/runtime/client.d.ts +47 -0
  29. package/dist/sdks/typescript/runtime/client.js +69 -0
  30. package/dist/sdks/typescript/runtime/credentials.d.ts +7 -0
  31. package/dist/sdks/typescript/runtime/credentials.js +20 -0
  32. package/dist/sdks/typescript/runtime/duration.d.ts +4 -0
  33. package/dist/sdks/typescript/runtime/duration.js +13 -0
  34. package/dist/sdks/typescript/runtime/files.d.ts +32 -0
  35. package/dist/sdks/typescript/runtime/files.js +45 -0
  36. package/dist/sdks/typescript/runtime/launcher.d.ts +24 -0
  37. package/dist/sdks/typescript/runtime/launcher.js +121 -0
  38. package/dist/sdks/typescript/runtime/transport.d.ts +43 -0
  39. package/dist/sdks/typescript/runtime/transport.js +95 -0
  40. package/dist/sdks/typescript/runtime/wait.d.ts +82 -0
  41. package/dist/sdks/typescript/runtime/wait.js +136 -0
  42. package/dist/sdks/typescript/types.d.ts +72 -0
  43. package/dist/sdks/typescript/types.js +1 -0
  44. package/package.json +80 -4
@@ -0,0 +1,578 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ /**
54
+ * `channelstack/checks`: the standard behaviour checks. Calling one registers ordinary tests in
55
+ * the suite that is running, on the fixtures and matchers of `channelstack/vitest`:
56
+ *
57
+ * import { slackChecks } from "channelstack/checks";
58
+ * slackChecks({ app: "my-agent" });
59
+ *
60
+ * Each check is something agents commonly get wrong, written against the public API only.
61
+ */
62
+ import { describe, expect, test } from "vitest";
63
+ /** The ids of the Slack checks, in the order they are registered. */
64
+ export const SLACK_CHECKS = [
65
+ "slack.dm-once",
66
+ "slack.mention-once",
67
+ "slack.in-thread",
68
+ "slack.retry",
69
+ "slack.redelivery",
70
+ "slack.ignores-bots",
71
+ "slack.bad-signature",
72
+ "slack.ack",
73
+ "slack.msg-too-long",
74
+ "slack.stream-stop",
75
+ "slack.stop",
76
+ "slack.click-unauthorized",
77
+ "slack.double-click",
78
+ ];
79
+ /** The ids of the GitHub checks, in the order they are registered. */
80
+ export const GITHUB_CHECKS = [
81
+ "github.ignores-own-comments",
82
+ "github.one-comment",
83
+ "github.review-thread",
84
+ "github.redelivery",
85
+ "github.check-run",
86
+ "github.bad-signature",
87
+ ];
88
+ /** The ids of the Linear checks, in the order they are registered. */
89
+ export const LINEAR_CHECKS = [
90
+ "linear.first-activity",
91
+ "linear.ends-turn",
92
+ "linear.stop",
93
+ "linear.redelivery",
94
+ "linear.stale",
95
+ "linear.bad-signature",
96
+ ];
97
+ /** The ids of the Notion checks, in the order they are registered. */
98
+ export const NOTION_CHECKS = [
99
+ "notion.reply-in-discussion",
100
+ "notion.ignores-own-comments",
101
+ "notion.redelivery",
102
+ "notion.bad-signature",
103
+ ];
104
+ /** The ids of the Resend checks, in the order they are registered. */
105
+ export const RESEND_CHECKS = [
106
+ "resend.reply-in-thread",
107
+ "resend.redelivery",
108
+ "resend.bad-signature",
109
+ "resend.stale",
110
+ ];
111
+ /** The ids of the Google checks, in the order they are registered. */
112
+ export const GOOGLE_CHECKS = [
113
+ "google.reply-in-thread",
114
+ "google.history",
115
+ "google.redelivery",
116
+ "google.bad-signature",
117
+ "google.wrong-audience",
118
+ "google.stale",
119
+ ];
120
+ /**
121
+ * The delivery a check retries, redelivers, forges or times: the one thing every delivery check
122
+ * needs, on every channel. Live, the platform delivers to the agent itself and channelstack has no
123
+ * delivery to show (reality-matrix.md), so the check skips with that reason, through the test's own
124
+ * `skip` as every live refusal does, before it waits for anything else.
125
+ */
126
+ const deliveryOf = (subject, { skip }, platform) => subject.delivery ?? skip(`live: ${platform} delivers to the agent itself, so delivery checks are fake-only`);
127
+ /**
128
+ * Registers one check as a test, with its id in the name. A check that was skipped, or whose
129
+ * prompt wasn't given, is left out. An id in `skip` that no check has is a mistake worth hearing about.
130
+ */
131
+ const registrar = (options, ids) => {
132
+ const unknown = (options.skip ?? []).filter((id) => !ids.includes(id));
133
+ if (unknown.length > 0) {
134
+ throw new Error(`No check is called ${unknown.map((id) => `"${id}"`).join(", ")}. The checks are: ${ids.join(", ")}`);
135
+ }
136
+ // `needs` is a rest argument: a default value would turn the `undefined` of a missing option into a value.
137
+ return (id, title, run, ...needs) => {
138
+ if (options.skip?.includes(id) || (needs.length > 0 && needs[0] === undefined))
139
+ return;
140
+ // Vitest reads which fixtures a test uses from how it destructures its context: name them all.
141
+ test(`${title} [${id}]`, ({ channelstack, slack, github, linear, notion, resend, google, task, expect, skip, signal, onTestFailed, onTestFinished, annotate, }) => run({
142
+ channelstack,
143
+ slack,
144
+ github,
145
+ linear,
146
+ notion,
147
+ resend,
148
+ google,
149
+ task,
150
+ expect,
151
+ skip,
152
+ signal,
153
+ onTestFailed,
154
+ onTestFinished,
155
+ annotate,
156
+ }));
157
+ };
158
+ };
159
+ /**
160
+ * Registers: replies once to a DM · once to a mention · replies in the thread · survives Slack's
161
+ * 3 s retry · handles redelivery idempotently · ignores other bots' messages · rejects a bad
162
+ * signature · acks within 3 s · handles `msg_too_long` · a stream starts and stops · honours
163
+ * Stop · refuses clicks from people who may not act · a double click acts once.
164
+ */
165
+ export function slackChecks(options) {
166
+ const { app } = options;
167
+ const prompt = options.prompt ?? `@${app} hello`;
168
+ const wait = options.timeout === undefined ? {} : { timeout: options.timeout };
169
+ const check = registrar(options, SLACK_CHECKS);
170
+ describe(`slack checks: ${app}`, () => {
171
+ check("slack.dm-once", "replies once to a DM", async ({ slack }) => {
172
+ const ana = await slack.createUser({ name: "Ana" });
173
+ const msg = await ana.say(prompt, { to: slack.app });
174
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
175
+ });
176
+ check("slack.mention-once", "replies once to a mention", async ({ slack }) => {
177
+ const ana = await slack.createUser({ name: "Ana" });
178
+ const channel = await slack.createChannel({ participants: [ana, slack.app] });
179
+ const msg = await ana.say(prompt, { channel });
180
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
181
+ });
182
+ check("slack.in-thread", "replies in the thread, not in the channel", async ({ slack }) => {
183
+ const ana = await slack.createUser({ name: "Ana" });
184
+ const channel = await slack.createChannel({ participants: [ana, slack.app] });
185
+ const msg = await ana.say(prompt, { channel });
186
+ await expect(msg.thread).toHaveReplyFrom(app, wait);
187
+ await expect(channel).not.toHaveChannelMessageFrom(app, wait);
188
+ });
189
+ check("slack.retry", "survives Slack's retry after 3 seconds", async (context) => {
190
+ const { slack } = context;
191
+ const ana = await slack.createUser({ name: "Ana" });
192
+ const channel = await slack.createChannel({ participants: [ana, slack.app] });
193
+ const msg = await ana.say(prompt, { channel });
194
+ // Slack retries what it thinks wasn't acknowledged in time, and says so in the retry headers.
195
+ await deliveryOf(msg, context, "Slack").retry({ after: "3s", reason: "http_timeout" });
196
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
197
+ });
198
+ check("slack.redelivery", "handles the same event delivered twice", async (context) => {
199
+ const { slack } = context;
200
+ const ana = await slack.createUser({ name: "Ana" });
201
+ const channel = await slack.createChannel({ participants: [ana, slack.app] });
202
+ const msg = await ana.say(prompt, { channel });
203
+ const delivery = deliveryOf(msg, context, "Slack");
204
+ await expect(msg.thread).toHaveReplyFrom(app, wait);
205
+ // The same event again, with nothing that marks it as a retry: only its event_id tells.
206
+ await delivery.resend();
207
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
208
+ });
209
+ check("slack.ignores-bots", "ignores what other bots write", async ({ slack }) => {
210
+ const bot = await slack.createUser({ name: "Deploy Bot", bot: true });
211
+ const channel = await slack.createChannel({ participants: [bot, slack.app] });
212
+ const msg = await bot.say(prompt, { channel });
213
+ await expect(msg.thread).not.toHaveReplyFrom(app, wait);
214
+ });
215
+ check("slack.bad-signature", "rejects a delivery with a bad signature", async (context) => {
216
+ const { slack } = context;
217
+ const ana = await slack.createUser({ name: "Ana" });
218
+ const msg = await ana.say(prompt, { to: slack.app });
219
+ const delivery = deliveryOf(msg, context, "Slack");
220
+ const first = await delivery.settled(wait);
221
+ // Over Socket Mode there is no signature to check: the connection is the app's own.
222
+ if (first.attempts[0]?.transport !== "http")
223
+ return;
224
+ await delivery.resend({ badSignature: true });
225
+ await expect(delivery).toBeRejected(wait);
226
+ });
227
+ check("slack.ack", "acknowledges within 3 seconds", async (context) => {
228
+ const { slack } = context;
229
+ const ana = await slack.createUser({ name: "Ana" });
230
+ const msg = await ana.say(prompt, { to: slack.app });
231
+ await expect(deliveryOf(msg, context, "Slack")).toBeAccepted({ within: "3s", ...wait });
232
+ });
233
+ check("slack.msg-too-long", "still answers when Slack refuses its message as too long", async ({ slack }) => {
234
+ const env_1 = { stack: [], error: void 0, hasError: false };
235
+ try {
236
+ const ana = await slack.createUser({ name: "Ana" });
237
+ const channel = await slack.createChannel({ participants: [ana, slack.app] });
238
+ // Slack refuses the agent's next message once; an agent that handles it says something shorter.
239
+ const _fault = __addDisposableResource(env_1, await slack.fault({
240
+ type: "error",
241
+ operation: "chat.postMessage",
242
+ error: "msg_too_long",
243
+ times: 1,
244
+ }), true);
245
+ const msg = await ana.say(prompt, { channel });
246
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
247
+ }
248
+ catch (e_1) {
249
+ env_1.error = e_1;
250
+ env_1.hasError = true;
251
+ }
252
+ finally {
253
+ const result_1 = __disposeResources(env_1);
254
+ if (result_1)
255
+ await result_1;
256
+ }
257
+ });
258
+ check("slack.stream-stop", "a stream it starts, it also stops", async ({ slack }) => {
259
+ const ana = await slack.createUser({ name: "Ana" });
260
+ const msg = await ana.say(options.streamPrompt ?? "", { to: slack.app });
261
+ await expect(msg.thread).toHaveStreamingReplyFrom(app, wait);
262
+ const reply = await msg.thread.latestReply({ from: app, ...wait });
263
+ const stream = await reply.stream?.ended(wait);
264
+ expect(stream?.state).toBe("completed");
265
+ await expect(msg.thread).toHaveStatus("idle", wait);
266
+ }, options.streamPrompt);
267
+ check("slack.stop", "honours Stop: nothing more is written after it", async ({ slack }) => {
268
+ const ana = await slack.createUser({ name: "Ana" });
269
+ const msg = await ana.say(options.streamPrompt ?? "", { to: slack.app });
270
+ await expect(msg.thread).toHaveStreamingReplyFrom(app, wait);
271
+ await ana.stop({ thread: msg });
272
+ await expect(msg.thread).toHaveStatus("stopped", wait);
273
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
274
+ }, options.streamPrompt);
275
+ check("slack.click-unauthorized", "refuses a click from someone who may not act", async ({ slack }) => {
276
+ const approval = options.approval ?? { prompt: "", button: "" };
277
+ const ana = await slack.createUser({ name: "Ana" });
278
+ const channel = await slack.createChannel({ participants: [ana, slack.app] });
279
+ const msg = await ana.say(approval.prompt, { channel });
280
+ await ana.click(approval.button, { thread: msg.thread });
281
+ // The ask is the only thing everyone sees: whatever the agent tells Ana, it tells her alone.
282
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
283
+ }, options.approval);
284
+ check("slack.double-click", "a double click acts once", async ({ slack }) => {
285
+ const approval = options.approval ?? { prompt: "", button: "" };
286
+ const ana = await slack.createUser({ name: "Ana" });
287
+ const admin = await slack.createUser({ name: "Ben", admin: true });
288
+ const channel = await slack.createChannel({ participants: [ana, admin, slack.app] });
289
+ const msg = await ana.say(approval.prompt, { channel });
290
+ const ask = await msg.thread.waitForReply({ from: app, ...wait });
291
+ // Both presses are on their way before the agent can answer the first. An agent that takes the
292
+ // button away once it has acted leaves nothing for the later press to find, and that is fine.
293
+ const press = () => admin.click(approval.button, { message: ask }).then(() => true, (error) => {
294
+ if (error.code !== "not_found")
295
+ throw error;
296
+ return false;
297
+ });
298
+ const pressed = await Promise.all([press(), press()]);
299
+ expect(pressed, "the button was there to press at least once").toContain(true);
300
+ // The approval shows up first; then the thread holds the ask and one message for the one approval.
301
+ await msg.thread.waitForReply({ from: app, ...wait });
302
+ await expect(msg.thread).toHaveRepliesFrom(app, { count: 2, ...wait });
303
+ }, options.approval);
304
+ });
305
+ }
306
+ /**
307
+ * Registers: ignores its own comments · one comment per trigger · review-thread replies trigger
308
+ * it · redelivery doesn't duplicate · check runs reach a conclusion · rejects a bad webhook
309
+ * signature.
310
+ */
311
+ export function githubChecks(options) {
312
+ const { app } = options;
313
+ const prompt = options.prompt ?? `@${app} hello`;
314
+ const wait = options.timeout === undefined ? {} : { timeout: options.timeout };
315
+ const check = registrar(options, GITHUB_CHECKS);
316
+ /** A repository the App is installed on, with Ana as a collaborator and an issue to talk in. */
317
+ const setup = async ({ github }) => {
318
+ const ana = await github.createUser({ login: "ana" });
319
+ const repo = await github.createRepo({ collaborators: [ana], apps: [github.app] });
320
+ return { ana, repo };
321
+ };
322
+ const pullRequest = async (context) => {
323
+ const { ana, repo } = await setup(context);
324
+ const pr = await ana.openPullRequest({ from: "change", title: "Greet people", changes: { "greeting.txt": "hello\n" } }, { repo });
325
+ return { ana, repo, pr };
326
+ };
327
+ describe(`github checks: ${app}`, () => {
328
+ check("github.ignores-own-comments", "its own comment doesn't set it off again", async (context) => {
329
+ const { ana, repo } = await setup(context);
330
+ const issue = await ana.openIssue({ title: "A question" }, { repo });
331
+ await ana.comment(prompt, { issue });
332
+ await expect(issue).toHaveCommentFrom(app, wait);
333
+ // An agent that answers its own answer keeps going; one that doesn't is quiet with one comment.
334
+ await expect(issue).toHaveCommentsFrom(app, { count: 1, ...wait });
335
+ });
336
+ check("github.one-comment", "one comment per trigger", async (context) => {
337
+ const { ana, repo } = await setup(context);
338
+ const issue = await ana.openIssue({ title: "Two questions" }, { repo });
339
+ await ana.comment(prompt, { issue });
340
+ await issue.waitForComment({ from: app, ...wait });
341
+ await ana.comment(prompt, { issue });
342
+ await issue.waitForComment({ from: app, ...wait });
343
+ await expect(issue).toHaveCommentsFrom(app, { count: 2, ...wait });
344
+ });
345
+ check("github.review-thread", "answers a mention in a thread on the diff, in that thread", async (context) => {
346
+ const { ana, pr } = await pullRequest(context);
347
+ const inline = await ana.reviewComment(prompt, { pr, path: "greeting.txt", line: 1 });
348
+ const replies = await inline.replies();
349
+ expect(replies.map((reply) => reply.author.name)).toEqual([app]);
350
+ });
351
+ check("github.redelivery", "a redelivered webhook doesn't produce a second answer", async (context) => {
352
+ const { ana, repo } = await setup(context);
353
+ const issue = await ana.openIssue({ title: "Delivered twice" }, { repo });
354
+ const comment = await ana.comment(prompt, { issue });
355
+ const delivery = deliveryOf(comment, context, "GitHub");
356
+ await expect(issue).toHaveCommentFrom(app, wait);
357
+ // GitHub redelivers with the same `X-GitHub-Delivery`, from its UI and its API.
358
+ await delivery.redeliver();
359
+ await expect(issue).toHaveCommentsFrom(app, { count: 1, ...wait });
360
+ });
361
+ check("github.check-run", "every check run it starts reaches a conclusion", async (context) => {
362
+ const { pr } = await pullRequest(context);
363
+ // Quiet means nothing is left open; a check run that never completes is, and the wait says so.
364
+ await context.channelstack.quiet(wait);
365
+ const runs = await pr.checkRuns();
366
+ expect(runs.filter((run) => run.status !== "completed").map((run) => run.name)).toEqual([]);
367
+ });
368
+ check("github.bad-signature", "rejects a webhook with a bad signature", async (context) => {
369
+ const { ana, repo } = await setup(context);
370
+ const issue = await ana.openIssue({ title: "Signed by someone else" }, { repo });
371
+ const delivery = deliveryOf(await ana.comment(prompt, { issue }), context, "GitHub");
372
+ await delivery.resend({ badSignature: true });
373
+ await expect(delivery).toBeRejected(wait);
374
+ });
375
+ });
376
+ }
377
+ /**
378
+ * The Linear checks: an agent delegated an issue. `prompt` is the issue's title (default "Look
379
+ * into this"); the agent should take the issue, work on it and end its turn.
380
+ */
381
+ export function linearChecks(options) {
382
+ const { app } = options;
383
+ const title = options.prompt ?? "Look into this";
384
+ const wait = options.timeout === undefined ? {} : { timeout: options.timeout };
385
+ const check = registrar(options, LINEAR_CHECKS);
386
+ /** Ana in a team, an issue, and a session from delegating it to the app. */
387
+ const delegated = async ({ linear }) => {
388
+ const ana = await linear.createUser({ name: "Ana" });
389
+ const team = await linear.createTeam({ members: [ana] });
390
+ const issue = await ana.createIssue({ title }, { team });
391
+ const session = await ana.delegate(linear.app, { issue });
392
+ return { ana, issue, session };
393
+ };
394
+ const ended = ["complete", "awaitingInput", "error"];
395
+ describe(`linear checks: ${app}`, () => {
396
+ check("linear.first-activity", "acknowledges a new session within 10 seconds", async (context) => {
397
+ const { session } = await delegated(context);
398
+ // Linear shows a session as unresponsive when nothing arrives in that time.
399
+ await expect(session).toHaveActivity({}, { timeout: "10s" });
400
+ });
401
+ check("linear.ends-turn", "ends its turn: the session doesn't stay active", async (context) => {
402
+ const { session } = await delegated(context);
403
+ await session.waitForState([...ended], wait);
404
+ });
405
+ check("linear.stop", "Stop ends the session", async (context) => {
406
+ const { ana, session } = await delegated(context);
407
+ await expect(session).toHaveActivity({}, wait);
408
+ await ana.stop({ session });
409
+ // The agent answers Stop with a response or an error; until then the session is stopping.
410
+ await session.waitForState(["complete", "error"], wait);
411
+ });
412
+ check("linear.redelivery", "a redelivered webhook doesn't start the work again", async (context) => {
413
+ const { session } = await delegated(context);
414
+ const delivery = deliveryOf(session, context, "Linear");
415
+ await session.waitForState([...ended], wait);
416
+ await delivery.redeliver();
417
+ await context.channelstack.quiet(wait);
418
+ const responses = (await session.activities()).filter((activity) => activity.type === "response");
419
+ expect(responses.length).toBeLessThanOrEqual(1);
420
+ });
421
+ check("linear.stale", "rejects a webhook whose timestamp is more than a minute old", async (context) => {
422
+ const { session } = await delegated(context);
423
+ const delivery = deliveryOf(session, context, "Linear");
424
+ await delivery.resend({ stale: true });
425
+ await expect(delivery).toBeRejected(wait);
426
+ });
427
+ check("linear.bad-signature", "rejects a webhook with a bad signature", async (context) => {
428
+ const { session } = await delegated(context);
429
+ const delivery = deliveryOf(session, context, "Linear");
430
+ await delivery.resend({ badSignature: true });
431
+ await expect(delivery).toBeRejected(wait);
432
+ });
433
+ });
434
+ }
435
+ /**
436
+ * The Notion checks: an integration mentioned in a page comment. `prompt` is the comment (default
437
+ * `@<app> hello`); the agent should answer once, in that discussion.
438
+ */
439
+ export function notionChecks(options) {
440
+ const { app } = options;
441
+ const prompt = options.prompt ?? `@${app} hello`;
442
+ const wait = options.timeout === undefined ? {} : { timeout: options.timeout };
443
+ const check = registrar(options, NOTION_CHECKS);
444
+ /** Ana, and her comment on a page the integration is connected to. */
445
+ const commented = async ({ notion }) => {
446
+ const ana = await notion.createUser({ name: "Ana" });
447
+ const page = await notion.createPage({ title: "Notes", content: "- a first note", connections: [notion.app] });
448
+ const comment = await ana.comment(prompt, { page });
449
+ return { ana, page, comment };
450
+ };
451
+ describe(`notion checks: ${app}`, () => {
452
+ check("notion.reply-in-discussion", "answers a comment in its discussion", async (context) => {
453
+ const { page, comment } = await commented(context);
454
+ await expect(comment.discussion).toHaveCommentFrom(app, wait);
455
+ // An answer in a discussion of its own is one the person doesn't see where they asked.
456
+ const elsewhere = (await page.discussions()).filter((discussion) => discussion.id !== comment.discussion.id);
457
+ expect(elsewhere).toEqual([]);
458
+ });
459
+ check("notion.ignores-own-comments", "its own comment doesn't set it off again", async (context) => {
460
+ const { comment } = await commented(context);
461
+ await expect(comment.discussion).toHaveCommentFrom(app, wait);
462
+ await expect(comment.discussion).toHaveCommentsFrom(app, { count: 1, ...wait });
463
+ });
464
+ check("notion.redelivery", "a webhook sent again doesn't produce a second answer", async (context) => {
465
+ const { comment } = await commented(context);
466
+ const delivery = deliveryOf(comment, context, "Notion");
467
+ await expect(comment.discussion).toHaveCommentFrom(app, wait);
468
+ // Notion retries with the same event id.
469
+ await delivery.redeliver();
470
+ await expect(comment.discussion).toHaveCommentsFrom(app, { count: 1, ...wait });
471
+ });
472
+ check("notion.bad-signature", "rejects a webhook with a bad signature", async (context) => {
473
+ const delivery = deliveryOf((await commented(context)).comment, context, "Notion");
474
+ await delivery.resend({ badSignature: true });
475
+ await expect(delivery).toBeRejected(wait);
476
+ });
477
+ });
478
+ }
479
+ /**
480
+ * The Resend checks: a person emails the agent. `prompt` is the email's text (default `hello`); the
481
+ * agent should answer once, in the email's thread.
482
+ */
483
+ export function resendChecks(options) {
484
+ const { app } = options;
485
+ const prompt = options.prompt ?? "hello";
486
+ const wait = options.timeout === undefined ? {} : { timeout: options.timeout };
487
+ const check = registrar(options, RESEND_CHECKS);
488
+ /** Ana, and her email to the agent. */
489
+ const emailed = async ({ resend }) => {
490
+ const ana = await resend.createUser({ name: "Ana" });
491
+ const email = await ana.send({ to: [resend.app], subject: "A question", text: prompt });
492
+ return { ana, email };
493
+ };
494
+ describe(`resend checks: ${app}`, () => {
495
+ check("resend.reply-in-thread", "answers an email in its thread", async (context) => {
496
+ const { ana, email } = await emailed(context);
497
+ // The answer reaches Ana, and in the thread she started: In-Reply-To and References are set.
498
+ const answer = await ana.waitForEmail({ from: app, ...wait });
499
+ expect(answer.thread.id, "the answer started a thread of its own: set In-Reply-To and References").toBe(email.thread.id);
500
+ });
501
+ check("resend.redelivery", "a webhook sent again doesn't produce a second answer", async (context) => {
502
+ const { email } = await emailed(context);
503
+ const delivery = deliveryOf(email, context, "Resend");
504
+ await expect(email.thread).toHaveReplyFrom(app, wait);
505
+ // Resend retries with the same svix-id and the same email_id.
506
+ await delivery.redeliver();
507
+ await expect(email.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
508
+ });
509
+ check("resend.bad-signature", "rejects a webhook with a bad signature", async (context) => {
510
+ const delivery = deliveryOf((await emailed(context)).email, context, "Resend");
511
+ await delivery.resend({ badSignature: true });
512
+ await expect(delivery).toBeRejected(wait);
513
+ });
514
+ check("resend.stale", "rejects a webhook signed more than five minutes ago", async (context) => {
515
+ const delivery = deliveryOf((await emailed(context)).email, context, "Resend");
516
+ await delivery.resend({ stale: true });
517
+ await expect(delivery).toBeRejected(wait);
518
+ });
519
+ });
520
+ }
521
+ /**
522
+ * The Google checks: a person emails the agent's Gmail address. `prompt` is the email's text
523
+ * (default `hello`); the agent should answer once, in the email's thread, whatever Gmail pushes.
524
+ */
525
+ export function googleChecks(options) {
526
+ const { app } = options;
527
+ const prompt = options.prompt ?? "hello";
528
+ const wait = options.timeout === undefined ? {} : { timeout: options.timeout };
529
+ const check = registrar(options, GOOGLE_CHECKS);
530
+ /** Ana, and her email to the agent. */
531
+ const emailed = async ({ google }) => {
532
+ const ana = await google.createUser({ name: "Ana" });
533
+ const email = await ana.send({ to: [google.app], subject: "A question", text: prompt });
534
+ return { ana, email };
535
+ };
536
+ describe(`google checks: ${app}`, () => {
537
+ check("google.reply-in-thread", "answers an email in its thread", async (context) => {
538
+ const { ana, email } = await emailed(context);
539
+ // The answer reaches Ana, and in the thread she started: In-Reply-To and References are set.
540
+ const answer = await ana.waitForEmail({ from: app, ...wait });
541
+ expect(answer.thread.id, "the answer started a thread of its own: set In-Reply-To and References").toBe(email.thread.id);
542
+ });
543
+ check("google.history", "answers each of two emails Gmail told of in one push, once", async ({ google }) => {
544
+ // Gmail pushes at most once a second. A first email is pushed at once; the two after it, within
545
+ // that second, wait for its end and come in one push, so the agent must read its history.
546
+ const ana = await google.createUser({ name: "Ana" });
547
+ const opener = await ana.send({ to: [google.app], subject: "Hello", text: prompt });
548
+ const first = await ana.send({ to: [google.app], subject: "First", text: prompt });
549
+ const second = await ana.send({ to: [google.app], subject: "Second", text: prompt });
550
+ await expect(opener.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
551
+ await expect(first.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
552
+ await expect(second.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
553
+ });
554
+ check("google.redelivery", "a push sent again doesn't produce a second answer", async (context) => {
555
+ const { email } = await emailed(context);
556
+ const push = deliveryOf(email, context, "Gmail");
557
+ await expect(email.thread).toHaveReplyFrom(app, wait);
558
+ // Pub/Sub delivers at least once: the same message id again.
559
+ await push.redeliver();
560
+ await expect(email.thread).toHaveRepliesFrom(app, { count: 1, ...wait });
561
+ });
562
+ check("google.bad-signature", "rejects a push whose token another key signed", async (context) => {
563
+ const push = deliveryOf((await emailed(context)).email, context, "Gmail");
564
+ await push.resend({ badSignature: true });
565
+ await expect(push).toBeRejected(wait);
566
+ });
567
+ check("google.wrong-audience", "rejects a push whose token is for another audience", async (context) => {
568
+ const push = deliveryOf((await emailed(context)).email, context, "Gmail");
569
+ await push.resend({ wrongAudience: true });
570
+ await expect(push).toBeRejected(wait);
571
+ });
572
+ check("google.stale", "rejects a push whose token has expired", async (context) => {
573
+ const push = deliveryOf((await emailed(context)).email, context, "Gmail");
574
+ await push.resend({ stale: true });
575
+ await expect(push).toBeRejected(wait);
576
+ });
577
+ });
578
+ }