@niwa-labs/telegram 0.0.0-stage → 0.0.0
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/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +18 -4
- package/src/bot.ts +212 -0
- package/src/index.ts +964 -0
- package/test/bot.test.ts +101 -0
- package/test/telegram.test.ts +1371 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,1371 @@
|
|
|
1
|
+
// The checks of the Telegram surface, against a fake Bot API.
|
|
2
|
+
//
|
|
3
|
+
// 1. the owner's message is one record, a foreign chat is only noted, and the offset sent to
|
|
4
|
+
// Telegram moves past what was handled;
|
|
5
|
+
// 2. after a restart the offset comes from the journal: nothing is lost, nothing is written twice;
|
|
6
|
+
// 3. the action telegram.send posts to the owner's chat, once;
|
|
7
|
+
// 4. a request to the owner is a message with its choices, a press answers it, and the one who
|
|
8
|
+
// asked goes on;
|
|
9
|
+
// 5. a request to somebody else is not shown to the owner;
|
|
10
|
+
// 6. the polling stops with the host, and the token is in no record.
|
|
11
|
+
|
|
12
|
+
import { expect, test } from "bun:test";
|
|
13
|
+
|
|
14
|
+
import { Address, NotSent, action, record, seq } from "@niwa-labs/core";
|
|
15
|
+
import type { Caller, HandlerId, InvocationId, StoredRecord } from "@niwa-labs/core";
|
|
16
|
+
import { messageSend, messages } from "@niwa-labs/messages";
|
|
17
|
+
import { messageSendRef } from "@niwa-labs/messages/records";
|
|
18
|
+
import type { CallerBinding } from "@niwa-labs/plugin";
|
|
19
|
+
import { answeredBy, RequestAnswered, RequestCreated, requestAsk, question, requests } from "@niwa-labs/requests";
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
Pressed,
|
|
24
|
+
Ignored,
|
|
25
|
+
Posted,
|
|
26
|
+
Received,
|
|
27
|
+
botIdOf,
|
|
28
|
+
keyboardFor,
|
|
29
|
+
parseCallback,
|
|
30
|
+
receivedId,
|
|
31
|
+
chatId,
|
|
32
|
+
telegram,
|
|
33
|
+
telegramSend,
|
|
34
|
+
} from "../src/index.ts";
|
|
35
|
+
import type { UpdateId } from "../src/index.ts";
|
|
36
|
+
import { createNiwa, eventually, tempHome, connectAsOwner, opt, plugin } from "@niwa-labs/testing";
|
|
37
|
+
|
|
38
|
+
/** The chat of the owner in the test. */
|
|
39
|
+
const OWNER = chatId(4242);
|
|
40
|
+
|
|
41
|
+
/** The chat of somebody else in the test. */
|
|
42
|
+
const FOREIGN = chatId(777);
|
|
43
|
+
|
|
44
|
+
/** The question the owner is asked in the test, and the choices it offers. */
|
|
45
|
+
const Choice = question("test.choice", {
|
|
46
|
+
kind: "choice",
|
|
47
|
+
title: "Ship the release?",
|
|
48
|
+
detail: "the release",
|
|
49
|
+
options: [opt("yes", "Yes, ship it"), opt("no", "No, hold it")],
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/** Wakes the one who asks a question. */
|
|
53
|
+
const Start = record(
|
|
54
|
+
"test.start",
|
|
55
|
+
z.object({
|
|
56
|
+
to: Address,
|
|
57
|
+
detail: z.string().optional(),
|
|
58
|
+
/** The kind of question to ask; a choice of release by default. */
|
|
59
|
+
kind: z.enum(["multi", "text", "form", "other"]).optional(),
|
|
60
|
+
}),
|
|
61
|
+
{ open: true },
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
/** The questions of the other kinds. */
|
|
65
|
+
const Others = {
|
|
66
|
+
multi: question("test.multi", { kind: "multi", title: "Which environments?", options: [opt("a", "Alpha"), opt("b", "Beta"), opt("c", "Gamma")] }),
|
|
67
|
+
text: question("test.text", { kind: "text", title: "Why?" }),
|
|
68
|
+
form: question("test.form", { kind: "form", title: "Fill in", schema: { type: "object", properties: { x: { type: "string" } } } }),
|
|
69
|
+
other: question("test.other", { kind: "choice", title: "Which one?", options: [opt("yes", "Yes"), opt("no", "No")], allowOther: true }),
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** The answer of the owner, as the one who asked sees it. */
|
|
73
|
+
const Continued = record("test.continued", z.object({ answer: z.string() }));
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The one who asks a question and goes on when the answer arrives.
|
|
77
|
+
*
|
|
78
|
+
* The question is asked from inside the host: a question holds its schemas, so it cannot travel
|
|
79
|
+
* over the wire.
|
|
80
|
+
*/
|
|
81
|
+
function asker(): ReturnType<typeof plugin> {
|
|
82
|
+
return plugin("test-asker", (api) => {
|
|
83
|
+
api.declare(Start, Continued);
|
|
84
|
+
// Each question is asked on its own: one that waits does not hold up the next.
|
|
85
|
+
api.on(Start, { concurrency: Number.POSITIVE_INFINITY }, async (data, step) => {
|
|
86
|
+
const base = data.kind === undefined ? Choice : Others[data.kind];
|
|
87
|
+
const asked = data.detail === undefined ? base : { ...base, input: { ...base.input, detail: data.detail } };
|
|
88
|
+
const { answer } = answeredBy(await step(requestAsk, { to: data.to, ...asked }));
|
|
89
|
+
await step.emit(Continued, { answer: JSON.stringify(answer) });
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The fake Bot API: like Telegram, it keeps an update until a call asks from an offset past it.
|
|
96
|
+
*/
|
|
97
|
+
function fakeTelegram(): {
|
|
98
|
+
base: string;
|
|
99
|
+
sent: Array<{
|
|
100
|
+
chat_id: number;
|
|
101
|
+
text: string;
|
|
102
|
+
reply_markup?: { inline_keyboard: Array<Array<{ text: string; callback_data: string }>> };
|
|
103
|
+
}>;
|
|
104
|
+
/** The offsets the polls asked from. */
|
|
105
|
+
offsets: number[];
|
|
106
|
+
/** What the surface told Telegram of the presses it saw. */
|
|
107
|
+
acks: Array<{ callback_query_id: string; text?: string }>;
|
|
108
|
+
/** Refuse this many of the next sends, as Telegram does when it will not take a message. */
|
|
109
|
+
refuse(count: number): void;
|
|
110
|
+
push(update: unknown): void;
|
|
111
|
+
stop(): void;
|
|
112
|
+
} {
|
|
113
|
+
const sent: ReturnType<typeof fakeTelegram>["sent"] = [];
|
|
114
|
+
const offsets: number[] = [];
|
|
115
|
+
const acks: ReturnType<typeof fakeTelegram>["acks"] = [];
|
|
116
|
+
let pending: Array<{ update_id: number }> = [];
|
|
117
|
+
let refusing = 0;
|
|
118
|
+
|
|
119
|
+
const server = Bun.serve({
|
|
120
|
+
port: 0,
|
|
121
|
+
async fetch(request) {
|
|
122
|
+
const method = new URL(request.url).pathname.split("/").pop() ?? "";
|
|
123
|
+
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
|
124
|
+
|
|
125
|
+
if (method === "getUpdates") {
|
|
126
|
+
const offset = Number(body["offset"]);
|
|
127
|
+
offsets.push(offset);
|
|
128
|
+
pending = pending.filter((update) => update.update_id >= offset);
|
|
129
|
+
const updates = [...pending];
|
|
130
|
+
if (updates.length === 0) await Bun.sleep(30);
|
|
131
|
+
return Response.json({ ok: true, result: updates });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (method === "sendMessage") {
|
|
135
|
+
// What Telegram does with a message it will not take, or one that is too long.
|
|
136
|
+
if (refusing > 0 || String(body["text"]).length > 4096) {
|
|
137
|
+
refusing = Math.max(0, refusing - 1);
|
|
138
|
+
return Response.json({ ok: false, error_code: 400, description: "Bad Request: message is too long" }, { status: 400 });
|
|
139
|
+
}
|
|
140
|
+
sent.push(body as (typeof sent)[number]);
|
|
141
|
+
return Response.json({
|
|
142
|
+
ok: true,
|
|
143
|
+
result: { message_id: sent.length, chat: { id: Number(body["chat_id"]) }, text: body["text"] },
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (method === "answerCallbackQuery") {
|
|
148
|
+
acks.push(body as (typeof acks)[number]);
|
|
149
|
+
return Response.json({ ok: true, result: true });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return Response.json({ ok: false, description: `unknown method ${method}` }, { status: 404 });
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
base: server.url.toString().replace(/\/$/, ""),
|
|
158
|
+
sent,
|
|
159
|
+
offsets,
|
|
160
|
+
acks,
|
|
161
|
+
refuse(count: number) {
|
|
162
|
+
refusing = count;
|
|
163
|
+
},
|
|
164
|
+
push(update: unknown) {
|
|
165
|
+
pending.push(update as { update_id: number });
|
|
166
|
+
},
|
|
167
|
+
stop() {
|
|
168
|
+
server.stop(true);
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A message update from a chat. */
|
|
174
|
+
function messageUpdate(updateId: number, chat: number, text: string): unknown {
|
|
175
|
+
return {
|
|
176
|
+
update_id: updateId,
|
|
177
|
+
message: {
|
|
178
|
+
message_id: updateId * 10,
|
|
179
|
+
chat: { id: chat, type: "private" },
|
|
180
|
+
date: 1_700_000_000,
|
|
181
|
+
text,
|
|
182
|
+
from: { id: chat, username: chat === OWNER ? "owner" : "stranger" },
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** A reply of the owner to a message of the surface. */
|
|
188
|
+
function replyUpdate(updateId: number, replyTo: number, text: string): unknown {
|
|
189
|
+
const update = messageUpdate(updateId, OWNER, text) as { message: Record<string, unknown> };
|
|
190
|
+
update.message["reply_to_message"] = { message_id: replyTo };
|
|
191
|
+
return update;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** A button press on a message. */
|
|
195
|
+
function callbackUpdate(updateId: number, messageId: number, data: string, chat: number = OWNER): unknown {
|
|
196
|
+
return {
|
|
197
|
+
update_id: updateId,
|
|
198
|
+
callback_query: {
|
|
199
|
+
id: `cb${String(updateId)}`,
|
|
200
|
+
data,
|
|
201
|
+
message: { message_id: messageId, chat: { id: chat, type: "private" } },
|
|
202
|
+
from: { id: chat, username: chat === OWNER ? "owner" : "stranger" },
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The records of the journal, read through the public API.
|
|
209
|
+
*
|
|
210
|
+
* A subscription never ends by itself, so the read stops itself: it waits for the journal to
|
|
211
|
+
* settle and then stops.
|
|
212
|
+
*/
|
|
213
|
+
async function recordsOf(home: string, settleMs = 500): Promise<StoredRecord[]> {
|
|
214
|
+
const client = await connectAsOwner(home);
|
|
215
|
+
const records: StoredRecord[] = [];
|
|
216
|
+
const reading = (async () => {
|
|
217
|
+
for await (const record of client.records.since()) {
|
|
218
|
+
records.push(record);
|
|
219
|
+
}
|
|
220
|
+
})().catch(() => undefined);
|
|
221
|
+
await Bun.sleep(settleMs);
|
|
222
|
+
await client.close().catch(() => undefined);
|
|
223
|
+
await reading;
|
|
224
|
+
return records;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const options = (base: string) =>
|
|
228
|
+
({ token: "test-token", owner: OWNER, speaksFor: "owner", apiBase: base, retryDelay: 20 }) as const;
|
|
229
|
+
|
|
230
|
+
/** The same surface without `speaksFor`: a plain client. */
|
|
231
|
+
const plainOptions = (base: string) =>
|
|
232
|
+
({ token: "test-token", owner: OWNER, apiBase: base, retryDelay: 20 }) as const;
|
|
233
|
+
|
|
234
|
+
test("telegram: the owner's message is one record, a foreign chat is only noted", async () => {
|
|
235
|
+
const home = await tempHome();
|
|
236
|
+
const fake = fakeTelegram();
|
|
237
|
+
const host = createNiwa({ home: home.path });
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
host.use(telegram(options(fake.base)));
|
|
241
|
+
await host.serve();
|
|
242
|
+
|
|
243
|
+
fake.push(messageUpdate(100, OWNER, "hello Rin"));
|
|
244
|
+
fake.push(messageUpdate(100, OWNER, "hello Rin"));
|
|
245
|
+
fake.push(messageUpdate(101, FOREIGN, "let me in"));
|
|
246
|
+
await eventually(() => fake.offsets.includes(102), 5_000, "the offset should pass the updates");
|
|
247
|
+
|
|
248
|
+
const records = await recordsOf(home.path);
|
|
249
|
+
const received = records.filter((record) => record.type === Received.name);
|
|
250
|
+
const ignored = records.filter((record) => record.type === Ignored.name);
|
|
251
|
+
|
|
252
|
+
expect(received).toHaveLength(1);
|
|
253
|
+
const { epoch } = received[0]?.data as { epoch: number };
|
|
254
|
+
expect(received[0]?.id).toBe(receivedId({ bot: botIdOf("test-token"), epoch }, 100 as UpdateId) as never);
|
|
255
|
+
expect(received[0]?.data).toMatchObject({ text: "hello Rin", chat: OWNER });
|
|
256
|
+
|
|
257
|
+
expect(ignored).toHaveLength(1);
|
|
258
|
+
expect(ignored[0]?.data).toEqual({ bot: botIdOf("test-token"), epoch, update: 101, chat: FOREIGN, message: 1010 });
|
|
259
|
+
expect(JSON.stringify(records)).not.toContain("let me in");
|
|
260
|
+
expect(JSON.stringify(records)).not.toContain("test-token");
|
|
261
|
+
} finally {
|
|
262
|
+
await host.close();
|
|
263
|
+
fake.stop();
|
|
264
|
+
await home.cleanup();
|
|
265
|
+
}
|
|
266
|
+
}, 30_000);
|
|
267
|
+
|
|
268
|
+
test("telegram: only the owner's private chat is the owner, a group of the same id is not", async () => {
|
|
269
|
+
const home = await tempHome();
|
|
270
|
+
const fake = fakeTelegram();
|
|
271
|
+
const host = createNiwa({ home: home.path });
|
|
272
|
+
|
|
273
|
+
try {
|
|
274
|
+
host.use(telegram(options(fake.base)));
|
|
275
|
+
await host.serve();
|
|
276
|
+
|
|
277
|
+
// A group with the configured id, a private chat written to by somebody else, and a message
|
|
278
|
+
// that names no sender: none of them is the owner.
|
|
279
|
+
const inGroup = messageUpdate(110, OWNER, "from a group") as { message: { chat: { type: string } } };
|
|
280
|
+
inGroup.message.chat.type = "supergroup";
|
|
281
|
+
const bySomebodyElse = messageUpdate(111, OWNER, "from a stranger") as { message: { from: { id: number } } };
|
|
282
|
+
bySomebodyElse.message.from.id = FOREIGN;
|
|
283
|
+
const anonymous = messageUpdate(112, OWNER, "from nobody") as { message: { from?: unknown } };
|
|
284
|
+
delete anonymous.message.from;
|
|
285
|
+
for (const update of [inGroup, bySomebodyElse, anonymous]) fake.push(update);
|
|
286
|
+
fake.push(messageUpdate(113, OWNER, "from the owner"));
|
|
287
|
+
await eventually(() => fake.offsets.includes(114), 5_000, "the offset should pass the updates");
|
|
288
|
+
|
|
289
|
+
const records = await recordsOf(home.path);
|
|
290
|
+
expect(records.filter((record) => record.type === Received.name).map((record) => (record.data as { text: string }).text)).toEqual([
|
|
291
|
+
"from the owner",
|
|
292
|
+
]);
|
|
293
|
+
expect(records.filter((record) => record.type === Ignored.name)).toHaveLength(3);
|
|
294
|
+
} finally {
|
|
295
|
+
await host.close();
|
|
296
|
+
fake.stop();
|
|
297
|
+
await home.cleanup();
|
|
298
|
+
}
|
|
299
|
+
}, 30_000);
|
|
300
|
+
|
|
301
|
+
test("telegram: a button pressed by somebody else in the owner's chat is not the owner's answer", async () => {
|
|
302
|
+
const home = await tempHome();
|
|
303
|
+
const fake = fakeTelegram();
|
|
304
|
+
const host = createNiwa({ home: home.path });
|
|
305
|
+
|
|
306
|
+
try {
|
|
307
|
+
const surface = telegram(options(fake.base));
|
|
308
|
+
host.use(requests({ bind: surface.bind() }));
|
|
309
|
+
host.use(asker());
|
|
310
|
+
host.use(surface);
|
|
311
|
+
await host.serve();
|
|
312
|
+
|
|
313
|
+
const client = await connectAsOwner(home.path);
|
|
314
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
315
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
316
|
+
const data = fake.sent[0]?.reply_markup?.inline_keyboard[0]?.[0]?.callback_data ?? "";
|
|
317
|
+
|
|
318
|
+
const press = callbackUpdate(520, 1, data) as { callback_query: { from: { id: number }; message: { chat: { type: string } } } };
|
|
319
|
+
press.callback_query.from.id = FOREIGN;
|
|
320
|
+
fake.push(press);
|
|
321
|
+
const inGroup = callbackUpdate(521, 1, data) as { callback_query: { message: { chat: { type: string } } } };
|
|
322
|
+
inGroup.callback_query.message.chat.type = "group";
|
|
323
|
+
fake.push(inGroup);
|
|
324
|
+
await eventually(() => fake.offsets.includes(522), 5_000, "the presses should be handled");
|
|
325
|
+
|
|
326
|
+
const records = await recordsOf(home.path);
|
|
327
|
+
expect(records.filter((record) => record.type === Ignored.name)).toHaveLength(2);
|
|
328
|
+
expect(records.filter((record) => record.type === RequestAnswered.name)).toHaveLength(0);
|
|
329
|
+
await client.close();
|
|
330
|
+
} finally {
|
|
331
|
+
await host.close();
|
|
332
|
+
fake.stop();
|
|
333
|
+
await home.cleanup();
|
|
334
|
+
}
|
|
335
|
+
}, 30_000);
|
|
336
|
+
|
|
337
|
+
test("telegram: a surface for the owner is refused when its chat is not a private one", () => {
|
|
338
|
+
const options = { token: "test-token", owner: chatId(-1001234567890), speaksFor: "owner" } as const;
|
|
339
|
+
expect(() => telegram(options)).toThrow("private chat");
|
|
340
|
+
expect(() => telegram({ ...options, owner: chatId(0) })).toThrow("private chat");
|
|
341
|
+
expect(() => telegram({ ...options, owner: chatId(1.5) })).toThrow("private chat");
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test("telegram: the action that hands a message to the agent exists only when there is a hook for it", async () => {
|
|
345
|
+
const fake = fakeTelegram();
|
|
346
|
+
const served = async (hooks: Parameters<typeof telegram>[1]): Promise<boolean> => {
|
|
347
|
+
const home = await tempHome();
|
|
348
|
+
const host = createNiwa({ home: home.path });
|
|
349
|
+
try {
|
|
350
|
+
host.use(telegram(options(fake.base), hooks));
|
|
351
|
+
await host.serve();
|
|
352
|
+
return await host.call("telegram.to-agent", { text: "hello" }).then(
|
|
353
|
+
() => true,
|
|
354
|
+
() => false,
|
|
355
|
+
);
|
|
356
|
+
} finally {
|
|
357
|
+
await host.close();
|
|
358
|
+
await home.cleanup();
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
try {
|
|
362
|
+
expect(await served({})).toBe(false);
|
|
363
|
+
expect(await served({ toAgent: async () => undefined })).toBe(true);
|
|
364
|
+
} finally {
|
|
365
|
+
fake.stop();
|
|
366
|
+
}
|
|
367
|
+
}, 30_000);
|
|
368
|
+
|
|
369
|
+
test("telegram: after a restart the offset comes from the journal", async () => {
|
|
370
|
+
const home = await tempHome();
|
|
371
|
+
const fake = fakeTelegram();
|
|
372
|
+
const toAgent: string[] = [];
|
|
373
|
+
|
|
374
|
+
try {
|
|
375
|
+
const first = createNiwa({ home: home.path });
|
|
376
|
+
first.use(telegram(options(fake.base), { toAgent: async (text) => void toAgent.push(text) }));
|
|
377
|
+
await first.serve();
|
|
378
|
+
fake.push(messageUpdate(200, OWNER, "one"));
|
|
379
|
+
await eventually(() => fake.offsets.includes(201), 5_000, "the first update should be handled");
|
|
380
|
+
await first.close();
|
|
381
|
+
|
|
382
|
+
// Telegram still holds update 200 as far as the second host knows: the fake is not told.
|
|
383
|
+
// The second host must ask from the journal's offset, not from zero.
|
|
384
|
+
fake.offsets.length = 0;
|
|
385
|
+
const second = createNiwa({ home: home.path });
|
|
386
|
+
second.use(telegram(options(fake.base), { toAgent: async (text) => void toAgent.push(text) }));
|
|
387
|
+
await second.serve();
|
|
388
|
+
fake.push(messageUpdate(201, OWNER, "two"));
|
|
389
|
+
await eventually(() => fake.offsets.includes(202), 5_000, "the second update should be handled");
|
|
390
|
+
const received = (await recordsOf(home.path)).filter((record) => record.type === Received.name);
|
|
391
|
+
await second.close();
|
|
392
|
+
|
|
393
|
+
expect(fake.offsets[0]).toBe(201);
|
|
394
|
+
expect(received.map((record) => (record.data as { text: string }).text)).toEqual(["one", "two"]);
|
|
395
|
+
expect(toAgent).toEqual(["one", "two"]);
|
|
396
|
+
} finally {
|
|
397
|
+
fake.stop();
|
|
398
|
+
await home.cleanup();
|
|
399
|
+
}
|
|
400
|
+
}, 30_000);
|
|
401
|
+
|
|
402
|
+
/** The texts of the messages the owner has written, as the journal holds them. */
|
|
403
|
+
async function receivedTexts(home: string): Promise<string[]> {
|
|
404
|
+
return (await recordsOf(home, 200))
|
|
405
|
+
.filter((record) => record.type === Received.name)
|
|
406
|
+
.map((record) => (record.data as { text: string }).text);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
test("telegram: past the time Telegram keeps updates, numbers it hands out again are new messages", async () => {
|
|
410
|
+
const home = await tempHome();
|
|
411
|
+
const fake = fakeTelegram();
|
|
412
|
+
const quick = { ...options(fake.base), token: "111:first", offsetTtl: 400 };
|
|
413
|
+
|
|
414
|
+
try {
|
|
415
|
+
const first = createNiwa({ home: home.path });
|
|
416
|
+
first.use(telegram(quick));
|
|
417
|
+
await first.serve();
|
|
418
|
+
fake.push(messageUpdate(5000, OWNER, "before the silence"));
|
|
419
|
+
await eventually(() => fake.offsets.includes(5001), 5_000, "the first update should be handled");
|
|
420
|
+
await first.close();
|
|
421
|
+
|
|
422
|
+
// After a week of silence Telegram numbers anew, from any number: here a lower one, and one
|
|
423
|
+
// that is the same as a number already seen.
|
|
424
|
+
await Bun.sleep(600);
|
|
425
|
+
fake.offsets.length = 0;
|
|
426
|
+
const second = createNiwa({ home: home.path });
|
|
427
|
+
second.use(telegram(quick));
|
|
428
|
+
await second.serve();
|
|
429
|
+
fake.push(messageUpdate(10, OWNER, "after the silence"));
|
|
430
|
+
fake.push(messageUpdate(5000, OWNER, "same number, other message"));
|
|
431
|
+
await eventually(() => fake.offsets.includes(5001), 5_000, "the new updates should be handled");
|
|
432
|
+
const texts = await receivedTexts(home.path);
|
|
433
|
+
await second.close();
|
|
434
|
+
|
|
435
|
+
expect(fake.offsets[0]).toBe(0);
|
|
436
|
+
expect(texts).toEqual(["before the silence", "after the silence", "same number, other message"]);
|
|
437
|
+
} finally {
|
|
438
|
+
fake.stop();
|
|
439
|
+
await home.cleanup();
|
|
440
|
+
}
|
|
441
|
+
}, 30_000);
|
|
442
|
+
|
|
443
|
+
test("telegram: a host that is up through the silence also starts a new run of numbers", async () => {
|
|
444
|
+
const home = await tempHome();
|
|
445
|
+
const fake = fakeTelegram();
|
|
446
|
+
const host = createNiwa({ home: home.path });
|
|
447
|
+
|
|
448
|
+
try {
|
|
449
|
+
host.use(telegram({ ...options(fake.base), offsetTtl: 400 }));
|
|
450
|
+
await host.serve();
|
|
451
|
+
fake.push(messageUpdate(5000, OWNER, "before the silence"));
|
|
452
|
+
await eventually(() => fake.offsets.includes(5001), 5_000, "the first update should be handled");
|
|
453
|
+
|
|
454
|
+
await Bun.sleep(700);
|
|
455
|
+
fake.push(messageUpdate(10, OWNER, "after the silence"));
|
|
456
|
+
await eventually(() => fake.offsets.includes(11), 5_000, "the new update should be handled");
|
|
457
|
+
expect(await receivedTexts(home.path)).toEqual(["before the silence", "after the silence"]);
|
|
458
|
+
} finally {
|
|
459
|
+
await host.close();
|
|
460
|
+
fake.stop();
|
|
461
|
+
await home.cleanup();
|
|
462
|
+
}
|
|
463
|
+
}, 30_000);
|
|
464
|
+
|
|
465
|
+
test("telegram: another bot's numbers do not continue the offset of the one before it", async () => {
|
|
466
|
+
const home = await tempHome();
|
|
467
|
+
const fake = fakeTelegram();
|
|
468
|
+
|
|
469
|
+
try {
|
|
470
|
+
const first = createNiwa({ home: home.path });
|
|
471
|
+
first.use(telegram({ ...options(fake.base), token: "111:first" }));
|
|
472
|
+
await first.serve();
|
|
473
|
+
fake.push(messageUpdate(5000, OWNER, "from the first bot"));
|
|
474
|
+
await eventually(() => fake.offsets.includes(5001), 5_000, "the first update should be handled");
|
|
475
|
+
await first.close();
|
|
476
|
+
|
|
477
|
+
fake.offsets.length = 0;
|
|
478
|
+
const second = createNiwa({ home: home.path });
|
|
479
|
+
second.use(telegram({ ...options(fake.base), token: "222:second" }));
|
|
480
|
+
await second.serve();
|
|
481
|
+
fake.push(messageUpdate(5000, OWNER, "from the second bot"));
|
|
482
|
+
await eventually(() => fake.offsets.includes(5001), 5_000, "the second bot's update should be handled");
|
|
483
|
+
const texts = await receivedTexts(home.path);
|
|
484
|
+
await second.close();
|
|
485
|
+
|
|
486
|
+
expect(fake.offsets[0]).toBe(0);
|
|
487
|
+
expect(texts).toEqual(["from the first bot", "from the second bot"]);
|
|
488
|
+
} finally {
|
|
489
|
+
fake.stop();
|
|
490
|
+
await home.cleanup();
|
|
491
|
+
}
|
|
492
|
+
}, 30_000);
|
|
493
|
+
|
|
494
|
+
test("telegram: a failing delivery to the agent does not hold up the next message", async () => {
|
|
495
|
+
const home = await tempHome();
|
|
496
|
+
const fake = fakeTelegram();
|
|
497
|
+
const delivered: string[] = [];
|
|
498
|
+
const host = createNiwa({ home: home.path });
|
|
499
|
+
|
|
500
|
+
try {
|
|
501
|
+
host.use(
|
|
502
|
+
telegram(options(fake.base), {
|
|
503
|
+
toAgent: async (text) => {
|
|
504
|
+
if (text === "bad") throw new Error("the agent is down");
|
|
505
|
+
delivered.push(text);
|
|
506
|
+
},
|
|
507
|
+
}),
|
|
508
|
+
);
|
|
509
|
+
await host.serve();
|
|
510
|
+
fake.push(messageUpdate(500, OWNER, "bad"));
|
|
511
|
+
fake.push(messageUpdate(501, OWNER, "good"));
|
|
512
|
+
await eventually(() => delivered.includes("good"), 5_000, "the next message should be delivered");
|
|
513
|
+
expect(fake.offsets.includes(502)).toBe(true);
|
|
514
|
+
const received = (await recordsOf(home.path)).filter((record) => record.type === Received.name);
|
|
515
|
+
expect(received.map((record) => (record.data as { text: string }).text)).toEqual(["bad", "good"]);
|
|
516
|
+
} finally {
|
|
517
|
+
await host.close();
|
|
518
|
+
fake.stop();
|
|
519
|
+
await home.cleanup();
|
|
520
|
+
}
|
|
521
|
+
}, 30_000);
|
|
522
|
+
|
|
523
|
+
test("telegram: a delivery cut off by a restart is not repeated, and does not block the next message", async () => {
|
|
524
|
+
const home = await tempHome();
|
|
525
|
+
const fake = fakeTelegram();
|
|
526
|
+
const delivered: string[] = [];
|
|
527
|
+
let started = false;
|
|
528
|
+
|
|
529
|
+
try {
|
|
530
|
+
// The first host is stopped while the agent is taking the message.
|
|
531
|
+
const first = createNiwa({ home: home.path });
|
|
532
|
+
first.use(
|
|
533
|
+
telegram(options(fake.base), {
|
|
534
|
+
toAgent: async () => {
|
|
535
|
+
started = true;
|
|
536
|
+
await new Promise(() => undefined);
|
|
537
|
+
},
|
|
538
|
+
}),
|
|
539
|
+
);
|
|
540
|
+
await first.serve();
|
|
541
|
+
fake.push(messageUpdate(300, OWNER, "old"));
|
|
542
|
+
await eventually(() => started, 5_000, "the delivery should have begun");
|
|
543
|
+
await first.close();
|
|
544
|
+
|
|
545
|
+
const second = createNiwa({ home: home.path });
|
|
546
|
+
second.use(telegram(options(fake.base), { toAgent: async (text) => void delivered.push(text) }));
|
|
547
|
+
await second.serve();
|
|
548
|
+
fake.push(messageUpdate(301, OWNER, "new"));
|
|
549
|
+
await eventually(() => delivered.includes("new"), 5_000, "the new message should be delivered");
|
|
550
|
+
await second.close();
|
|
551
|
+
|
|
552
|
+
// "old" may have reached the agent before the cut: it is never sent a second time.
|
|
553
|
+
expect(delivered).toEqual(["new"]);
|
|
554
|
+
} finally {
|
|
555
|
+
fake.stop();
|
|
556
|
+
await home.cleanup();
|
|
557
|
+
}
|
|
558
|
+
}, 30_000);
|
|
559
|
+
|
|
560
|
+
test("telegram: the message reaches the agent, and telegram.send posts once to the owner", async () => {
|
|
561
|
+
const home = await tempHome();
|
|
562
|
+
const fake = fakeTelegram();
|
|
563
|
+
const host = createNiwa({ home: home.path });
|
|
564
|
+
|
|
565
|
+
try {
|
|
566
|
+
host.use(messages());
|
|
567
|
+
const plugin = telegram(options(fake.base), {
|
|
568
|
+
toAgent: async (text) => {
|
|
569
|
+
await host.call(messageSend(), {
|
|
570
|
+
from: { kind: "owner" },
|
|
571
|
+
to: { kind: "agent", id: "rin" },
|
|
572
|
+
body: { type: "assignment", text },
|
|
573
|
+
} as never);
|
|
574
|
+
},
|
|
575
|
+
});
|
|
576
|
+
host.use(plugin);
|
|
577
|
+
await host.serve();
|
|
578
|
+
|
|
579
|
+
fake.push(messageUpdate(300, OWNER, "do the thing"));
|
|
580
|
+
await eventually(() => fake.offsets.includes(301), 5_000, "the update should be handled");
|
|
581
|
+
|
|
582
|
+
const sent = (await recordsOf(home.path)).filter((record) => record.type === "message.sent");
|
|
583
|
+
expect(sent).toHaveLength(1);
|
|
584
|
+
expect(sent[0]?.data).toMatchObject({
|
|
585
|
+
to: { kind: "agent", id: "rin" },
|
|
586
|
+
body: { type: "assignment", text: "do the thing" },
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
const posted = await host.call(telegramSend({ bot: plugin.service.client, owner: OWNER }), {
|
|
590
|
+
text: "done",
|
|
591
|
+
});
|
|
592
|
+
expect(posted.chat).toBe(OWNER);
|
|
593
|
+
expect(fake.sent).toHaveLength(1);
|
|
594
|
+
expect(fake.sent[0]).toMatchObject({ chat_id: OWNER, text: "done" });
|
|
595
|
+
} finally {
|
|
596
|
+
await host.close();
|
|
597
|
+
fake.stop();
|
|
598
|
+
await home.cleanup();
|
|
599
|
+
}
|
|
600
|
+
}, 30_000);
|
|
601
|
+
|
|
602
|
+
test("telegram: the hook steps an action as the owner, so what it sends is the owner's word", async () => {
|
|
603
|
+
const home = await tempHome();
|
|
604
|
+
const fake = fakeTelegram();
|
|
605
|
+
const host = createNiwa({ home: home.path });
|
|
606
|
+
|
|
607
|
+
try {
|
|
608
|
+
const surface = telegram(options(fake.base), {
|
|
609
|
+
toAgent: async (text, ctx) => {
|
|
610
|
+
return ctx.step.outcome(messageSendRef, { to: { kind: "agent", id: "rin" }, body: { type: "update", text } });
|
|
611
|
+
},
|
|
612
|
+
});
|
|
613
|
+
host.use(messages({ bind: surface.bind() }));
|
|
614
|
+
host.use(surface);
|
|
615
|
+
await host.serve();
|
|
616
|
+
|
|
617
|
+
fake.push(messageUpdate(310, OWNER, "good morning"));
|
|
618
|
+
await eventually(() => fake.offsets.includes(311), 5_000, "the update should be handled");
|
|
619
|
+
|
|
620
|
+
const sent = (await recordsOf(home.path)).filter((record) => record.type === "message.sent");
|
|
621
|
+
expect(sent).toHaveLength(1);
|
|
622
|
+
expect(sent[0]?.data).toMatchObject({
|
|
623
|
+
from: { kind: "owner" },
|
|
624
|
+
to: { kind: "agent", id: "rin" },
|
|
625
|
+
body: { type: "update", text: "good morning" },
|
|
626
|
+
});
|
|
627
|
+
} finally {
|
|
628
|
+
await host.close();
|
|
629
|
+
fake.stop();
|
|
630
|
+
await home.cleanup();
|
|
631
|
+
}
|
|
632
|
+
}, 30_000);
|
|
633
|
+
|
|
634
|
+
test("telegram: a request to the owner is a message, and a press answers it", async () => {
|
|
635
|
+
const home = await tempHome();
|
|
636
|
+
const fake = fakeTelegram();
|
|
637
|
+
const host = createNiwa({ home: home.path });
|
|
638
|
+
|
|
639
|
+
try {
|
|
640
|
+
const surface = telegram(options(fake.base));
|
|
641
|
+
host.use(requests({ bind: surface.bind() }));
|
|
642
|
+
host.use(asker());
|
|
643
|
+
host.use(surface);
|
|
644
|
+
await host.serve();
|
|
645
|
+
|
|
646
|
+
// The question is asked of the owner: the one who asked waits for the answer.
|
|
647
|
+
const client = await connectAsOwner(home.path);
|
|
648
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
649
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
650
|
+
|
|
651
|
+
// The owner sees the request and its choices as buttons.
|
|
652
|
+
const posted = fake.sent[0];
|
|
653
|
+
expect(posted?.chat_id).toBe(OWNER);
|
|
654
|
+
expect(posted?.text).toContain("test.choice");
|
|
655
|
+
expect(posted?.text).toContain("the release");
|
|
656
|
+
const buttons = posted?.reply_markup?.inline_keyboard ?? [];
|
|
657
|
+
// The buttons show the labels; a press answers with the id of the option.
|
|
658
|
+
expect(buttons.flat().map((button) => button.text)).toEqual(["Yes, ship it", "No, hold it"]);
|
|
659
|
+
|
|
660
|
+
const data = buttons[0]?.[0]?.callback_data;
|
|
661
|
+
expect(parseCallback(data)?.index).toBe(0);
|
|
662
|
+
|
|
663
|
+
// The press is the owner's answer: the request is answered, and the one who asked goes on.
|
|
664
|
+
fake.push(callbackUpdate(400, 1, data ?? ""));
|
|
665
|
+
await eventually(
|
|
666
|
+
async () => (await recordsOf(home.path, 200)).some((record) => record.type === Continued.name),
|
|
667
|
+
5_000,
|
|
668
|
+
"the one who asked should go on",
|
|
669
|
+
);
|
|
670
|
+
|
|
671
|
+
const records = await recordsOf(home.path);
|
|
672
|
+
expect(records.some((record) => record.type === Posted.name)).toBe(true);
|
|
673
|
+
const presses = records.filter((record) => record.type === Pressed.name);
|
|
674
|
+
expect(presses).toHaveLength(1);
|
|
675
|
+
expect(presses[0]?.data).toMatchObject({ message: 1, query: "cb400", data: "answer:0" });
|
|
676
|
+
expect(fake.acks).toEqual([{ callback_query_id: "cb400", text: "Answer sent" }]);
|
|
677
|
+
|
|
678
|
+
// The answer is the request's: it is written by the requests module, and it is the owner's.
|
|
679
|
+
const answered = records.filter((record) => record.type === RequestAnswered.name);
|
|
680
|
+
expect(answered).toHaveLength(1);
|
|
681
|
+
expect(answered[0]?.data).toMatchObject({ answer: { ids: ["yes"] }, by: { kind: "owner" } });
|
|
682
|
+
const continued = records.filter((record) => record.type === Continued.name);
|
|
683
|
+
expect(continued[0]?.data).toMatchObject({ answer: JSON.stringify({ ids: ["yes"] }) });
|
|
684
|
+
|
|
685
|
+
// A second press on the same message is late: it changes nothing and the handler ends.
|
|
686
|
+
fake.push(callbackUpdate(401, 1, buttons[1]?.[0]?.callback_data ?? ""));
|
|
687
|
+
await eventually(
|
|
688
|
+
async () => (await recordsOf(home.path, 300)).filter((r) => r.type === Pressed.name).length === 2,
|
|
689
|
+
5_000,
|
|
690
|
+
"the late press should be written down",
|
|
691
|
+
);
|
|
692
|
+
const after = await recordsOf(home.path, 300);
|
|
693
|
+
expect(after.filter((record) => record.type === RequestAnswered.name)).toHaveLength(1);
|
|
694
|
+
// The owner is told the late press was not taken, on the button and in the chat.
|
|
695
|
+
expect(fake.acks.map((ack) => ack.text)).toEqual(["Answer sent", "Not accepted"]);
|
|
696
|
+
expect(fake.sent.at(-1)?.text).toContain("was not accepted");
|
|
697
|
+
|
|
698
|
+
// Every button of a long Cyrillic choice stays inside the 64 bytes Telegram allows.
|
|
699
|
+
const long = keyboardFor(["Разрешить всё до конца сессии", "Отклонить и объяснить причину"]);
|
|
700
|
+
for (const row of long.inline_keyboard) {
|
|
701
|
+
for (const button of row) {
|
|
702
|
+
expect(new TextEncoder().encode(button.callback_data).length).toBeLessThanOrEqual(64);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
await client.close();
|
|
707
|
+
} finally {
|
|
708
|
+
await host.close();
|
|
709
|
+
fake.stop();
|
|
710
|
+
await home.cleanup();
|
|
711
|
+
}
|
|
712
|
+
}, 30_000);
|
|
713
|
+
|
|
714
|
+
test("telegram: a request that cannot be posted does not hold up the requests after it", async () => {
|
|
715
|
+
const home = await tempHome();
|
|
716
|
+
const fake = fakeTelegram();
|
|
717
|
+
const host = createNiwa({ home: home.path });
|
|
718
|
+
|
|
719
|
+
try {
|
|
720
|
+
const surface = telegram(options(fake.base));
|
|
721
|
+
host.use(requests({ bind: surface.bind() }));
|
|
722
|
+
host.use(asker());
|
|
723
|
+
host.use(surface);
|
|
724
|
+
await host.serve();
|
|
725
|
+
fake.refuse(1);
|
|
726
|
+
|
|
727
|
+
const client = await connectAsOwner(home.path);
|
|
728
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
729
|
+
await Bun.sleep(300);
|
|
730
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
731
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the second request should be posted");
|
|
732
|
+
await client.close();
|
|
733
|
+
} finally {
|
|
734
|
+
await host.close();
|
|
735
|
+
fake.stop();
|
|
736
|
+
await home.cleanup();
|
|
737
|
+
}
|
|
738
|
+
}, 30_000);
|
|
739
|
+
|
|
740
|
+
test("telegram: a question longer than a message is cut to fit and still posted with its buttons", async () => {
|
|
741
|
+
const home = await tempHome();
|
|
742
|
+
const fake = fakeTelegram();
|
|
743
|
+
const host = createNiwa({ home: home.path });
|
|
744
|
+
|
|
745
|
+
try {
|
|
746
|
+
const surface = telegram(options(fake.base));
|
|
747
|
+
host.use(requests({ bind: surface.bind() }));
|
|
748
|
+
host.use(asker());
|
|
749
|
+
host.use(surface);
|
|
750
|
+
await host.serve();
|
|
751
|
+
|
|
752
|
+
const client = await connectAsOwner(home.path);
|
|
753
|
+
await client.emit(Start, { to: { kind: "owner" }, detail: "x\u{1F600}".repeat(3_000) });
|
|
754
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
755
|
+
expect(fake.sent[0]?.text.length).toBeLessThanOrEqual(4096);
|
|
756
|
+
expect(fake.sent[0]?.text.isWellFormed()).toBe(true);
|
|
757
|
+
expect(fake.sent[0]?.reply_markup?.inline_keyboard.flat().map((button) => button.text)).toEqual(["Yes, ship it", "No, hold it"]);
|
|
758
|
+
await client.close();
|
|
759
|
+
} finally {
|
|
760
|
+
await host.close();
|
|
761
|
+
fake.stop();
|
|
762
|
+
await home.cleanup();
|
|
763
|
+
}
|
|
764
|
+
}, 30_000);
|
|
765
|
+
|
|
766
|
+
test("telegram: a request that was open before the surface was installed is posted when it is", async () => {
|
|
767
|
+
const home = await tempHome();
|
|
768
|
+
const fake = fakeTelegram();
|
|
769
|
+
|
|
770
|
+
try {
|
|
771
|
+
const before = createNiwa({ home: home.path });
|
|
772
|
+
before.use(requests());
|
|
773
|
+
before.use(asker());
|
|
774
|
+
await before.serve();
|
|
775
|
+
const client = await connectAsOwner(home.path);
|
|
776
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
777
|
+
await eventually(
|
|
778
|
+
async () => (await recordsOf(home.path, 100)).some((record) => record.type === RequestCreated.name),
|
|
779
|
+
5_000,
|
|
780
|
+
"the request should be open",
|
|
781
|
+
);
|
|
782
|
+
await client.close();
|
|
783
|
+
await before.close();
|
|
784
|
+
|
|
785
|
+
const after = createNiwa({ home: home.path });
|
|
786
|
+
const surface = telegram(options(fake.base));
|
|
787
|
+
after.use(requests({ bind: surface.bind() }));
|
|
788
|
+
after.use(asker());
|
|
789
|
+
after.use(surface);
|
|
790
|
+
await after.serve();
|
|
791
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the open request should be posted");
|
|
792
|
+
await Bun.sleep(300);
|
|
793
|
+
expect(fake.sent).toHaveLength(1);
|
|
794
|
+
await after.close();
|
|
795
|
+
} finally {
|
|
796
|
+
fake.stop();
|
|
797
|
+
await home.cleanup();
|
|
798
|
+
}
|
|
799
|
+
}, 30_000);
|
|
800
|
+
|
|
801
|
+
test("telegram: telegram.send is plain text, the buttons belong to requests", async () => {
|
|
802
|
+
const home = await tempHome();
|
|
803
|
+
const fake = fakeTelegram();
|
|
804
|
+
const host = createNiwa({ home: home.path });
|
|
805
|
+
|
|
806
|
+
try {
|
|
807
|
+
host.use(telegram(options(fake.base)));
|
|
808
|
+
await host.serve();
|
|
809
|
+
await host.call("telegram.send", { text: "pick one", choices: ["a", "b"] });
|
|
810
|
+
expect(fake.sent).toHaveLength(1);
|
|
811
|
+
expect(fake.sent[0]?.reply_markup).toBeUndefined();
|
|
812
|
+
} finally {
|
|
813
|
+
await host.close();
|
|
814
|
+
fake.stop();
|
|
815
|
+
await home.cleanup();
|
|
816
|
+
}
|
|
817
|
+
}, 30_000);
|
|
818
|
+
|
|
819
|
+
test("telegram: a press from another chat is only noted, and the owner's request stays open", async () => {
|
|
820
|
+
const home = await tempHome();
|
|
821
|
+
const fake = fakeTelegram();
|
|
822
|
+
const host = createNiwa({ home: home.path });
|
|
823
|
+
|
|
824
|
+
try {
|
|
825
|
+
const surface = telegram(options(fake.base));
|
|
826
|
+
host.use(requests({ bind: surface.bind() }));
|
|
827
|
+
host.use(asker());
|
|
828
|
+
host.use(surface);
|
|
829
|
+
await host.serve();
|
|
830
|
+
|
|
831
|
+
const client = await connectAsOwner(home.path);
|
|
832
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
833
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
834
|
+
const data = fake.sent[0]?.reply_markup?.inline_keyboard[0]?.[0]?.callback_data ?? "";
|
|
835
|
+
|
|
836
|
+
// The stranger presses the same button of the same message number: message numbers are per
|
|
837
|
+
// chat, so only the chat tells whose press it is.
|
|
838
|
+
fake.push(callbackUpdate(500, 1, data, FOREIGN));
|
|
839
|
+
await eventually(() => fake.offsets.includes(501), 5_000, "the press should be handled");
|
|
840
|
+
const records = await recordsOf(home.path);
|
|
841
|
+
expect(records.filter((record) => record.type === Ignored.name)).toHaveLength(1);
|
|
842
|
+
expect(records.filter((record) => record.type === Pressed.name)).toHaveLength(0);
|
|
843
|
+
expect(records.filter((record) => record.type === RequestAnswered.name)).toHaveLength(0);
|
|
844
|
+
expect(records.filter((record) => record.type === Continued.name)).toHaveLength(0);
|
|
845
|
+
|
|
846
|
+
// The request is still open: the owner's own press answers it.
|
|
847
|
+
fake.push(callbackUpdate(501, 1, data));
|
|
848
|
+
await eventually(
|
|
849
|
+
async () => (await recordsOf(home.path, 200)).some((record) => record.type === RequestAnswered.name),
|
|
850
|
+
5_000,
|
|
851
|
+
"the owner's press should answer",
|
|
852
|
+
);
|
|
853
|
+
|
|
854
|
+
await client.close();
|
|
855
|
+
} finally {
|
|
856
|
+
await host.close();
|
|
857
|
+
fake.stop();
|
|
858
|
+
await home.cleanup();
|
|
859
|
+
}
|
|
860
|
+
}, 30_000);
|
|
861
|
+
|
|
862
|
+
test("telegram: a press that answers nothing is written down and the owner is told", async () => {
|
|
863
|
+
const home = await tempHome();
|
|
864
|
+
const fake = fakeTelegram();
|
|
865
|
+
const host = createNiwa({ home: home.path });
|
|
866
|
+
|
|
867
|
+
try {
|
|
868
|
+
host.use(requests());
|
|
869
|
+
host.use(telegram(options(fake.base)));
|
|
870
|
+
await host.serve();
|
|
871
|
+
fake.push(callbackUpdate(530, 999, "answer:0"));
|
|
872
|
+
await eventually(() => fake.sent.length === 1, 15_000, "the owner should be told");
|
|
873
|
+
expect(fake.sent[0]?.text).toContain("does not belong to a request");
|
|
874
|
+
expect(fake.acks).toEqual([{ callback_query_id: "cb530", text: "Not accepted" }]);
|
|
875
|
+
const presses = (await recordsOf(home.path, 200)).filter((record) => record.type === Pressed.name);
|
|
876
|
+
expect(presses).toHaveLength(1);
|
|
877
|
+
} finally {
|
|
878
|
+
await host.close();
|
|
879
|
+
fake.stop();
|
|
880
|
+
await home.cleanup();
|
|
881
|
+
}
|
|
882
|
+
}, 30_000);
|
|
883
|
+
|
|
884
|
+
test("telegram: a press is resolved by the chat as well as the message, so a former chat's post is not found", async () => {
|
|
885
|
+
const home = await tempHome();
|
|
886
|
+
const fake = fakeTelegram();
|
|
887
|
+
const FORMER = chatId(5555);
|
|
888
|
+
|
|
889
|
+
try {
|
|
890
|
+
const first = createNiwa({ home: home.path });
|
|
891
|
+
const surface = telegram(options(fake.base));
|
|
892
|
+
first.use(requests({ bind: surface.bind() }));
|
|
893
|
+
first.use(asker());
|
|
894
|
+
first.use(surface);
|
|
895
|
+
await first.serve();
|
|
896
|
+
const client = await connectAsOwner(home.path);
|
|
897
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
898
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
899
|
+
const data = fake.sent[0]?.reply_markup?.inline_keyboard[0]?.[0]?.callback_data ?? "";
|
|
900
|
+
await client.close();
|
|
901
|
+
await first.close();
|
|
902
|
+
|
|
903
|
+
// The owner's chat is another now: message 1 of it is not message 1 of the old chat.
|
|
904
|
+
const second = createNiwa({ home: home.path });
|
|
905
|
+
const moved = telegram({ ...options(fake.base), owner: FORMER });
|
|
906
|
+
second.use(requests({ bind: moved.bind() }));
|
|
907
|
+
second.use(asker());
|
|
908
|
+
second.use(moved);
|
|
909
|
+
await second.serve();
|
|
910
|
+
fake.push(callbackUpdate(540, 1, data, FORMER));
|
|
911
|
+
await eventually(() => fake.acks.length === 1, 15_000, "the press should be answered");
|
|
912
|
+
expect(fake.acks[0]?.text).toBe("Not accepted");
|
|
913
|
+
expect((await recordsOf(home.path, 200)).filter((record) => record.type === RequestAnswered.name)).toHaveLength(0);
|
|
914
|
+
await second.close();
|
|
915
|
+
} finally {
|
|
916
|
+
fake.stop();
|
|
917
|
+
await home.cleanup();
|
|
918
|
+
}
|
|
919
|
+
}, 40_000);
|
|
920
|
+
|
|
921
|
+
/** The answer the request of a kind gets from the replies, as the requests module records it. */
|
|
922
|
+
async function replyingTo(
|
|
923
|
+
kind: "multi" | "text" | "form" | "other",
|
|
924
|
+
replies: string[],
|
|
925
|
+
seen: (fake: ReturnType<typeof fakeTelegram>, answered: () => Promise<unknown[]>) => Promise<void>,
|
|
926
|
+
): Promise<void> {
|
|
927
|
+
const home = await tempHome();
|
|
928
|
+
const fake = fakeTelegram();
|
|
929
|
+
const host = createNiwa({ home: home.path });
|
|
930
|
+
const delivered: string[] = [];
|
|
931
|
+
|
|
932
|
+
try {
|
|
933
|
+
const surface = telegram(options(fake.base), { toAgent: async (text) => void delivered.push(text) });
|
|
934
|
+
host.use(requests({ bind: surface.bind() }));
|
|
935
|
+
host.use(asker());
|
|
936
|
+
host.use(surface);
|
|
937
|
+
await host.serve();
|
|
938
|
+
const client = await connectAsOwner(home.path);
|
|
939
|
+
await client.emit(Start, { to: { kind: "owner" }, kind });
|
|
940
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
941
|
+
replies.forEach((text, index) => fake.push(replyUpdate(900 + index, 1, text)));
|
|
942
|
+
await seen(fake, async () => {
|
|
943
|
+
const answered = (await recordsOf(home.path, 200)).filter((record) => record.type === RequestAnswered.name);
|
|
944
|
+
return answered.map((record) => (record.data as { answer: unknown }).answer);
|
|
945
|
+
});
|
|
946
|
+
expect(delivered).toEqual([]);
|
|
947
|
+
await client.close();
|
|
948
|
+
} finally {
|
|
949
|
+
await host.close();
|
|
950
|
+
fake.stop();
|
|
951
|
+
await home.cleanup();
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
test("telegram: a reply to a text question answers it, and is not said to the agent", async () => {
|
|
956
|
+
await replyingTo("text", ["because the users asked"], async (fake, answered) => {
|
|
957
|
+
expect(fake.sent[0]?.reply_markup).toBeUndefined();
|
|
958
|
+
expect(fake.sent[0]?.text).toContain("Reply to this message");
|
|
959
|
+
await eventually(async () => (await answered()).length === 1, 5_000, "the reply should answer the request");
|
|
960
|
+
expect(await answered()).toEqual([{ text: "because the users asked" }]);
|
|
961
|
+
});
|
|
962
|
+
}, 30_000);
|
|
963
|
+
|
|
964
|
+
test("telegram: a multi question lists its options, and a reply with their numbers answers it", async () => {
|
|
965
|
+
await replyingTo("multi", ["1, 3"], async (fake, answered) => {
|
|
966
|
+
expect(fake.sent[0]?.reply_markup).toBeUndefined();
|
|
967
|
+
expect(fake.sent[0]?.text).toContain("1. Alpha\n2. Beta\n3. Gamma");
|
|
968
|
+
await eventually(async () => (await answered()).length === 1, 5_000, "the reply should answer the request");
|
|
969
|
+
expect(await answered()).toEqual([{ ids: ["a", "c"] }]);
|
|
970
|
+
});
|
|
971
|
+
}, 30_000);
|
|
972
|
+
|
|
973
|
+
test("telegram: a reply that does not fit the question is explained and leaves it open", async () => {
|
|
974
|
+
await replyingTo("multi", ["7", "maybe"], async (fake, answered) => {
|
|
975
|
+
await eventually(() => fake.sent.length === 3, 5_000, "both replies should be explained");
|
|
976
|
+
expect(fake.sent[1]?.text).toContain("There is no option 7");
|
|
977
|
+
expect(fake.sent[2]?.text).toContain("Reply with the number of an option");
|
|
978
|
+
expect(await answered()).toEqual([]);
|
|
979
|
+
});
|
|
980
|
+
}, 30_000);
|
|
981
|
+
|
|
982
|
+
test("telegram: a free text answers a question that allows another answer", async () => {
|
|
983
|
+
await replyingTo("other", ["neither, see the ticket"], async (fake, answered) => {
|
|
984
|
+
expect(fake.sent[0]?.reply_markup?.inline_keyboard.flat().map((button) => button.text)).toEqual(["Yes", "No"]);
|
|
985
|
+
expect(fake.sent[0]?.text).toContain("your own answer");
|
|
986
|
+
await eventually(async () => (await answered()).length === 1, 5_000, "the reply should answer the request");
|
|
987
|
+
expect(await answered()).toEqual([{ text: "neither, see the ticket" }]);
|
|
988
|
+
});
|
|
989
|
+
}, 30_000);
|
|
990
|
+
|
|
991
|
+
test("telegram: a form says where it can be answered, and a reply to it explains the same", async () => {
|
|
992
|
+
await replyingTo("form", ["{}"], async (fake, answered) => {
|
|
993
|
+
expect(fake.sent[0]?.reply_markup).toBeUndefined();
|
|
994
|
+
expect(fake.sent[0]?.text).toMatch(/answer request req-[0-9a-f]+ in the cockpit/);
|
|
995
|
+
await eventually(() => fake.sent.length === 2, 5_000, "the reply should be explained");
|
|
996
|
+
expect(fake.sent[1]?.text).toContain("cannot be filled in here");
|
|
997
|
+
expect(await answered()).toEqual([]);
|
|
998
|
+
});
|
|
999
|
+
}, 30_000);
|
|
1000
|
+
|
|
1001
|
+
test("telegram: the way to answer stays in a long question when the question is cut", async () => {
|
|
1002
|
+
const home = await tempHome();
|
|
1003
|
+
const fake = fakeTelegram();
|
|
1004
|
+
const host = createNiwa({ home: home.path });
|
|
1005
|
+
|
|
1006
|
+
try {
|
|
1007
|
+
const surface = telegram(options(fake.base));
|
|
1008
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1009
|
+
host.use(asker());
|
|
1010
|
+
host.use(surface);
|
|
1011
|
+
await host.serve();
|
|
1012
|
+
const client = await connectAsOwner(home.path);
|
|
1013
|
+
await client.emit(Start, { to: { kind: "owner" }, kind: "text", detail: "y".repeat(6_000) });
|
|
1014
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
1015
|
+
expect(fake.sent[0]?.text.length).toBeLessThanOrEqual(4096);
|
|
1016
|
+
expect(fake.sent[0]?.text.endsWith("Reply to this message with your answer.")).toBe(true);
|
|
1017
|
+
await client.close();
|
|
1018
|
+
} finally {
|
|
1019
|
+
await host.close();
|
|
1020
|
+
fake.stop();
|
|
1021
|
+
await home.cleanup();
|
|
1022
|
+
}
|
|
1023
|
+
}, 30_000);
|
|
1024
|
+
|
|
1025
|
+
test("telegram: a reply to a message that shows no request goes to the agent as any message does", async () => {
|
|
1026
|
+
const home = await tempHome();
|
|
1027
|
+
const fake = fakeTelegram();
|
|
1028
|
+
const delivered: string[] = [];
|
|
1029
|
+
const host = createNiwa({ home: home.path });
|
|
1030
|
+
|
|
1031
|
+
try {
|
|
1032
|
+
host.use(requests());
|
|
1033
|
+
host.use(telegram(options(fake.base), { toAgent: async (text) => void delivered.push(text) }));
|
|
1034
|
+
await host.serve();
|
|
1035
|
+
fake.push(replyUpdate(950, 77, "about that earlier thing"));
|
|
1036
|
+
await eventually(() => delivered.length === 1, 5_000, "the reply should reach the agent");
|
|
1037
|
+
expect(delivered).toEqual(["about that earlier thing"]);
|
|
1038
|
+
} finally {
|
|
1039
|
+
await host.close();
|
|
1040
|
+
fake.stop();
|
|
1041
|
+
await home.cleanup();
|
|
1042
|
+
}
|
|
1043
|
+
}, 30_000);
|
|
1044
|
+
|
|
1045
|
+
test("telegram: without speaksFor the surface is a plain client, and its press is refused", async () => {
|
|
1046
|
+
const home = await tempHome();
|
|
1047
|
+
const fake = fakeTelegram();
|
|
1048
|
+
const host = createNiwa({ home: home.path });
|
|
1049
|
+
|
|
1050
|
+
try {
|
|
1051
|
+
const surface = telegram(plainOptions(fake.base));
|
|
1052
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1053
|
+
host.use(asker());
|
|
1054
|
+
host.use(surface);
|
|
1055
|
+
await host.serve();
|
|
1056
|
+
|
|
1057
|
+
const client = await connectAsOwner(home.path);
|
|
1058
|
+
await client.emit(Start, { to: { kind: "owner" } });
|
|
1059
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the request should be posted");
|
|
1060
|
+
const data = fake.sent[0]?.reply_markup?.inline_keyboard[0]?.[0]?.callback_data ?? "";
|
|
1061
|
+
|
|
1062
|
+
// The owner's chat pressed it, but this instance does not speak for the owner.
|
|
1063
|
+
fake.push(callbackUpdate(510, 1, data));
|
|
1064
|
+
await eventually(
|
|
1065
|
+
async () => (await recordsOf(home.path, 200)).some((record) => record.type === Pressed.name),
|
|
1066
|
+
5_000,
|
|
1067
|
+
"the press should be written down",
|
|
1068
|
+
);
|
|
1069
|
+
await Bun.sleep(300);
|
|
1070
|
+
const records = await recordsOf(home.path, 200);
|
|
1071
|
+
expect(records.filter((record) => record.type === RequestAnswered.name)).toHaveLength(0);
|
|
1072
|
+
expect(records.filter((record) => record.type === Continued.name)).toHaveLength(0);
|
|
1073
|
+
|
|
1074
|
+
await client.close();
|
|
1075
|
+
} finally {
|
|
1076
|
+
await host.close();
|
|
1077
|
+
fake.stop();
|
|
1078
|
+
await home.cleanup();
|
|
1079
|
+
}
|
|
1080
|
+
}, 30_000);
|
|
1081
|
+
|
|
1082
|
+
test("telegram: the binding is the owner with speaksFor and a plain client without it", () => {
|
|
1083
|
+
const handler: Caller = { kind: "handler", handler: "telegram/press" as HandlerId, invocation: "inv" as InvocationId };
|
|
1084
|
+
expect(telegram(options("http://localhost")).bind()(handler)).toEqual({ kind: "owner" });
|
|
1085
|
+
expect(telegram(options("http://localhost")).bind()({ ...handler, handler: "telegram/reply" as HandlerId })).toBeUndefined();
|
|
1086
|
+
expect(telegram(plainOptions("http://localhost")).bind()(handler)).toEqual({ kind: "client", id: "telegram" });
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
test("telegram: the binding of the surface composes with another binding", () => {
|
|
1090
|
+
const other: CallerBinding = () => ({ kind: "agent", id: "rin" });
|
|
1091
|
+
const surface = telegram(options("http://localhost"));
|
|
1092
|
+
expect(surface.bind()({ kind: "host" })).toBeUndefined();
|
|
1093
|
+
expect(surface.bind(other)({ kind: "host" })).toEqual({ kind: "agent", id: "rin" });
|
|
1094
|
+
});
|
|
1095
|
+
|
|
1096
|
+
test("telegram: a request to somebody else is not shown to the owner", async () => {
|
|
1097
|
+
const home = await tempHome();
|
|
1098
|
+
const fake = fakeTelegram();
|
|
1099
|
+
const host = createNiwa({ home: home.path });
|
|
1100
|
+
|
|
1101
|
+
try {
|
|
1102
|
+
host.use(asker());
|
|
1103
|
+
const surface = telegram(options(fake.base));
|
|
1104
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1105
|
+
host.use(surface);
|
|
1106
|
+
await host.serve();
|
|
1107
|
+
|
|
1108
|
+
const client = await connectAsOwner(home.path);
|
|
1109
|
+
await client.emit(Start, { to: { kind: "agent", id: "rin" } });
|
|
1110
|
+
await eventually(
|
|
1111
|
+
async () => (await recordsOf(home.path, 200)).some((record) => record.type === RequestCreated.name),
|
|
1112
|
+
5_000,
|
|
1113
|
+
"the request should be created",
|
|
1114
|
+
);
|
|
1115
|
+
|
|
1116
|
+
// Nothing was posted to the owner: the request is not his.
|
|
1117
|
+
expect(fake.sent).toHaveLength(0);
|
|
1118
|
+
|
|
1119
|
+
await client.close();
|
|
1120
|
+
} finally {
|
|
1121
|
+
await host.close();
|
|
1122
|
+
fake.stop();
|
|
1123
|
+
await home.cleanup();
|
|
1124
|
+
}
|
|
1125
|
+
}, 30_000);
|
|
1126
|
+
|
|
1127
|
+
test("telegram: closing the host stops the polling", async () => {
|
|
1128
|
+
const home = await tempHome();
|
|
1129
|
+
const fake = fakeTelegram();
|
|
1130
|
+
const host = createNiwa({ home: home.path });
|
|
1131
|
+
|
|
1132
|
+
try {
|
|
1133
|
+
host.use(telegram(options(fake.base)));
|
|
1134
|
+
await host.serve();
|
|
1135
|
+
await eventually(() => fake.offsets.length > 0, 5_000, "the service should poll");
|
|
1136
|
+
await host.close();
|
|
1137
|
+
await Bun.sleep(100);
|
|
1138
|
+
const polls = fake.offsets.length;
|
|
1139
|
+
await Bun.sleep(200);
|
|
1140
|
+
expect(fake.offsets.length).toBe(polls);
|
|
1141
|
+
} finally {
|
|
1142
|
+
fake.stop();
|
|
1143
|
+
await home.cleanup();
|
|
1144
|
+
}
|
|
1145
|
+
}, 30_000);
|
|
1146
|
+
|
|
1147
|
+
/** Presses the button with this text on the last message the owner was sent. */
|
|
1148
|
+
function press(fake: ReturnType<typeof fakeTelegram>, updateId: number, label: string): void {
|
|
1149
|
+
const message = fake.sent.length;
|
|
1150
|
+
const buttons = fake.sent[message - 1]?.reply_markup?.inline_keyboard.flat() ?? [];
|
|
1151
|
+
const data = buttons.find((button) => button.text === label)?.callback_data;
|
|
1152
|
+
fake.push(callbackUpdate(updateId, message, data ?? ""));
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
test("telegram: a delivery that throws asks the owner, and retry delivers exactly once", async () => {
|
|
1156
|
+
const home = await tempHome();
|
|
1157
|
+
const fake = fakeTelegram();
|
|
1158
|
+
const delivered: string[] = [];
|
|
1159
|
+
let calls = 0;
|
|
1160
|
+
const host = createNiwa({ home: home.path });
|
|
1161
|
+
|
|
1162
|
+
try {
|
|
1163
|
+
const surface = telegram(options(fake.base), {
|
|
1164
|
+
toAgent: async (text) => {
|
|
1165
|
+
calls += 1;
|
|
1166
|
+
if (calls === 1) throw new NotSent("the agent is down");
|
|
1167
|
+
delivered.push(text);
|
|
1168
|
+
},
|
|
1169
|
+
});
|
|
1170
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1171
|
+
host.use(surface);
|
|
1172
|
+
await host.serve();
|
|
1173
|
+
fake.push(messageUpdate(600, OWNER, "please deploy the release to production now"));
|
|
1174
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the owner should be asked");
|
|
1175
|
+
expect(fake.sent[0]?.text).toContain("NOT delivered");
|
|
1176
|
+
expect(fake.sent[0]?.text).toContain("please deploy the release");
|
|
1177
|
+
expect(fake.sent[0]?.reply_markup?.inline_keyboard.flat().map((b) => b.text)).toEqual(["retry", "cancel"]);
|
|
1178
|
+
expect(calls).toBe(1);
|
|
1179
|
+
expect(delivered).toEqual([]);
|
|
1180
|
+
|
|
1181
|
+
press(fake, 601, "retry");
|
|
1182
|
+
await eventually(() => delivered.length === 1, 5_000, "retry should deliver");
|
|
1183
|
+
await Bun.sleep(300);
|
|
1184
|
+
expect(delivered).toEqual(["please deploy the release to production now"]);
|
|
1185
|
+
expect(calls).toBe(2);
|
|
1186
|
+
} finally {
|
|
1187
|
+
await host.close();
|
|
1188
|
+
fake.stop();
|
|
1189
|
+
await home.cleanup();
|
|
1190
|
+
}
|
|
1191
|
+
}, 30_000);
|
|
1192
|
+
|
|
1193
|
+
test("telegram: a delivery cut off by a restart asks the owner and is not resent", async () => {
|
|
1194
|
+
const home = await tempHome();
|
|
1195
|
+
const fake = fakeTelegram();
|
|
1196
|
+
const delivered: string[] = [];
|
|
1197
|
+
let started = false;
|
|
1198
|
+
|
|
1199
|
+
try {
|
|
1200
|
+
const first = createNiwa({ home: home.path });
|
|
1201
|
+
const surface = telegram(options(fake.base), {
|
|
1202
|
+
toAgent: async () => {
|
|
1203
|
+
started = true;
|
|
1204
|
+
await new Promise(() => undefined);
|
|
1205
|
+
},
|
|
1206
|
+
});
|
|
1207
|
+
first.use(requests({ bind: surface.bind() }));
|
|
1208
|
+
first.use(surface);
|
|
1209
|
+
await first.serve();
|
|
1210
|
+
fake.push(messageUpdate(700, OWNER, "old message"));
|
|
1211
|
+
await eventually(() => started, 5_000, "the delivery should have begun");
|
|
1212
|
+
await first.close();
|
|
1213
|
+
|
|
1214
|
+
const second = createNiwa({ home: home.path });
|
|
1215
|
+
const resumed = telegram(options(fake.base), { toAgent: async (text) => void delivered.push(text) });
|
|
1216
|
+
second.use(requests({ bind: resumed.bind() }));
|
|
1217
|
+
second.use(resumed);
|
|
1218
|
+
await second.serve();
|
|
1219
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the owner should be asked");
|
|
1220
|
+
// The agent may have the message already: the owner is not told that it was not delivered.
|
|
1221
|
+
expect(fake.sent[0]?.text).not.toContain("NOT delivered");
|
|
1222
|
+
expect(fake.sent[0]?.text).toContain("may already have reached the agent");
|
|
1223
|
+
expect(fake.sent[0]?.text).toContain("old message");
|
|
1224
|
+
expect(fake.sent[0]?.text).toContain("repeats");
|
|
1225
|
+
await Bun.sleep(300);
|
|
1226
|
+
expect(delivered).toEqual([]);
|
|
1227
|
+
await second.close();
|
|
1228
|
+
} finally {
|
|
1229
|
+
fake.stop();
|
|
1230
|
+
await home.cleanup();
|
|
1231
|
+
}
|
|
1232
|
+
}, 30_000);
|
|
1233
|
+
|
|
1234
|
+
/** An action that fails the way the one a hook calls can: sure that nothing went, or not. */
|
|
1235
|
+
function failing(name: string, sure: boolean): ReturnType<typeof plugin> {
|
|
1236
|
+
return plugin(`test-${name}`, (api) => {
|
|
1237
|
+
api.action(
|
|
1238
|
+
action(`test.${name}`, {
|
|
1239
|
+
description: "Fails.",
|
|
1240
|
+
input: z.object({ text: z.string() }),
|
|
1241
|
+
output: z.object({}),
|
|
1242
|
+
recovery: "do-not-repeat",
|
|
1243
|
+
effect: "write",
|
|
1244
|
+
world: "local",
|
|
1245
|
+
run: async () => {
|
|
1246
|
+
throw sure ? new NotSent("the agent is down") : new Error("the connection broke");
|
|
1247
|
+
},
|
|
1248
|
+
}),
|
|
1249
|
+
);
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
test("telegram: a call of the hook that fails reaches the owner, who is told whether the agent may have it", async () => {
|
|
1254
|
+
for (const sure of [true, false]) {
|
|
1255
|
+
const home = await tempHome();
|
|
1256
|
+
const fake = fakeTelegram();
|
|
1257
|
+
const host = createNiwa({ home: home.path });
|
|
1258
|
+
const name = sure ? "tell-sure" : "tell-unsure";
|
|
1259
|
+
|
|
1260
|
+
try {
|
|
1261
|
+
const surface = telegram(options(fake.base), {
|
|
1262
|
+
toAgent: async (text, ctx) => ctx.step.outcome(`test.${name}`, { text }),
|
|
1263
|
+
});
|
|
1264
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1265
|
+
host.use(failing(name, sure));
|
|
1266
|
+
host.use(surface);
|
|
1267
|
+
await host.serve();
|
|
1268
|
+
fake.push(messageUpdate(850, OWNER, "tell Rin to deploy"));
|
|
1269
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the owner should be asked");
|
|
1270
|
+
const text = fake.sent[0]?.text ?? "";
|
|
1271
|
+
expect(text).toContain("tell Rin to deploy");
|
|
1272
|
+
if (sure) {
|
|
1273
|
+
expect(text).toContain("NOT delivered");
|
|
1274
|
+
expect(fake.sent[0]?.reply_markup?.inline_keyboard.flat().map((b) => b.text)).toEqual(["retry", "cancel"]);
|
|
1275
|
+
} else {
|
|
1276
|
+
expect(text).not.toContain("NOT delivered");
|
|
1277
|
+
expect(text).toContain("may already have reached the agent");
|
|
1278
|
+
expect(fake.sent[0]?.reply_markup?.inline_keyboard.flat().map((b) => b.text)).toEqual(["send again", "cancel"]);
|
|
1279
|
+
}
|
|
1280
|
+
} finally {
|
|
1281
|
+
await host.close();
|
|
1282
|
+
fake.stop();
|
|
1283
|
+
await home.cleanup();
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}, 30_000);
|
|
1287
|
+
|
|
1288
|
+
test("telegram: a hook that throws a plain error may have delivered, and the owner is told so", async () => {
|
|
1289
|
+
const home = await tempHome();
|
|
1290
|
+
const fake = fakeTelegram();
|
|
1291
|
+
const host = createNiwa({ home: home.path });
|
|
1292
|
+
|
|
1293
|
+
try {
|
|
1294
|
+
const surface = telegram(options(fake.base), {
|
|
1295
|
+
toAgent: async () => {
|
|
1296
|
+
throw new Error("the connection broke");
|
|
1297
|
+
},
|
|
1298
|
+
});
|
|
1299
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1300
|
+
host.use(surface);
|
|
1301
|
+
await host.serve();
|
|
1302
|
+
fake.push(messageUpdate(860, OWNER, "restart the server"));
|
|
1303
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the owner should be asked");
|
|
1304
|
+
expect(fake.sent[0]?.text).toContain("may already have reached the agent");
|
|
1305
|
+
expect(fake.sent[0]?.text).not.toContain("NOT delivered");
|
|
1306
|
+
} finally {
|
|
1307
|
+
await host.close();
|
|
1308
|
+
fake.stop();
|
|
1309
|
+
await home.cleanup();
|
|
1310
|
+
}
|
|
1311
|
+
}, 30_000);
|
|
1312
|
+
|
|
1313
|
+
test("telegram: cancel after a failed delivery delivers nothing", async () => {
|
|
1314
|
+
const home = await tempHome();
|
|
1315
|
+
const fake = fakeTelegram();
|
|
1316
|
+
let calls = 0;
|
|
1317
|
+
const host = createNiwa({ home: home.path });
|
|
1318
|
+
|
|
1319
|
+
try {
|
|
1320
|
+
const surface = telegram(options(fake.base), {
|
|
1321
|
+
toAgent: async () => {
|
|
1322
|
+
calls += 1;
|
|
1323
|
+
throw new Error("the agent is down");
|
|
1324
|
+
},
|
|
1325
|
+
});
|
|
1326
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1327
|
+
host.use(surface);
|
|
1328
|
+
await host.serve();
|
|
1329
|
+
fake.push(messageUpdate(800, OWNER, "never mind"));
|
|
1330
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the owner should be asked");
|
|
1331
|
+
press(fake, 801, "cancel");
|
|
1332
|
+
await eventually(
|
|
1333
|
+
async () => (await recordsOf(home.path, 200)).some((r) => r.type === RequestAnswered.name),
|
|
1334
|
+
5_000,
|
|
1335
|
+
"the cancel should answer the request",
|
|
1336
|
+
);
|
|
1337
|
+
await Bun.sleep(300);
|
|
1338
|
+
expect(calls).toBe(1);
|
|
1339
|
+
expect(fake.sent).toHaveLength(1);
|
|
1340
|
+
} finally {
|
|
1341
|
+
await host.close();
|
|
1342
|
+
fake.stop();
|
|
1343
|
+
await home.cleanup();
|
|
1344
|
+
}
|
|
1345
|
+
}, 30_000);
|
|
1346
|
+
|
|
1347
|
+
test("telegram: the quote of a long message never cuts an emoji in half", async () => {
|
|
1348
|
+
const home = await tempHome();
|
|
1349
|
+
const fake = fakeTelegram();
|
|
1350
|
+
const host = createNiwa({ home: home.path });
|
|
1351
|
+
|
|
1352
|
+
try {
|
|
1353
|
+
const surface = telegram(options(fake.base), {
|
|
1354
|
+
toAgent: async () => {
|
|
1355
|
+
throw new Error("the agent is down");
|
|
1356
|
+
},
|
|
1357
|
+
});
|
|
1358
|
+
host.use(requests({ bind: surface.bind() }));
|
|
1359
|
+
host.use(surface);
|
|
1360
|
+
await host.serve();
|
|
1361
|
+
fake.push(messageUpdate(900, OWNER, "\u{1F600}".repeat(81)));
|
|
1362
|
+
await eventually(() => fake.sent.length === 1, 5_000, "the owner should be asked");
|
|
1363
|
+
const text = fake.sent[0]?.text ?? "";
|
|
1364
|
+
expect(text).toContain(`"${"\u{1F600}".repeat(80)}..."`);
|
|
1365
|
+
expect(text.isWellFormed()).toBe(true);
|
|
1366
|
+
} finally {
|
|
1367
|
+
await host.close();
|
|
1368
|
+
fake.stop();
|
|
1369
|
+
await home.cleanup();
|
|
1370
|
+
}
|
|
1371
|
+
}, 30_000);
|