@yaebal/test 0.0.1 → 0.2.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/README.md +400 -15
- package/lib/api.d.ts +83 -0
- package/lib/api.d.ts.map +1 -0
- package/lib/api.js +186 -0
- package/lib/api.js.map +1 -0
- package/lib/api.test.d.ts +2 -0
- package/lib/api.test.d.ts.map +1 -0
- package/lib/api.test.js +131 -0
- package/lib/api.test.js.map +1 -0
- package/lib/bot-messages.d.ts +42 -0
- package/lib/bot-messages.d.ts.map +1 -0
- package/lib/bot-messages.js +72 -0
- package/lib/bot-messages.js.map +1 -0
- package/lib/chat-actor.d.ts +45 -0
- package/lib/chat-actor.d.ts.map +1 -0
- package/lib/chat-actor.js +72 -0
- package/lib/chat-actor.js.map +1 -0
- package/lib/clock.d.ts +22 -0
- package/lib/clock.d.ts.map +1 -0
- package/lib/clock.js +72 -0
- package/lib/clock.js.map +1 -0
- package/lib/clock.test.d.ts +2 -0
- package/lib/clock.test.d.ts.map +1 -0
- package/lib/clock.test.js +69 -0
- package/lib/clock.test.js.map +1 -0
- package/lib/env.d.ts +100 -0
- package/lib/env.d.ts.map +1 -0
- package/lib/env.js +164 -0
- package/lib/env.js.map +1 -0
- package/lib/env.test.d.ts +2 -0
- package/lib/env.test.d.ts.map +1 -0
- package/lib/env.test.js +302 -0
- package/lib/env.test.js.map +1 -0
- package/lib/fetch.d.ts +3 -0
- package/lib/fetch.d.ts.map +1 -0
- package/lib/fetch.js +12 -0
- package/lib/fetch.js.map +1 -0
- package/lib/index.d.ts +19 -52
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +19 -115
- package/lib/index.js.map +1 -1
- package/lib/internal.d.ts +25 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +19 -0
- package/lib/internal.js.map +1 -0
- package/lib/keyboard.d.ts +15 -0
- package/lib/keyboard.d.ts.map +1 -0
- package/lib/keyboard.js +25 -0
- package/lib/keyboard.js.map +1 -0
- package/lib/keyboard.test.d.ts +2 -0
- package/lib/keyboard.test.d.ts.map +1 -0
- package/lib/keyboard.test.js +31 -0
- package/lib/keyboard.test.js.map +1 -0
- package/lib/normalize.d.ts +10 -0
- package/lib/normalize.d.ts.map +1 -0
- package/lib/normalize.js +27 -0
- package/lib/normalize.js.map +1 -0
- package/lib/reactions.d.ts +3 -0
- package/lib/reactions.d.ts.map +1 -0
- package/lib/reactions.js +17 -0
- package/lib/reactions.js.map +1 -0
- package/lib/updates.d.ts +126 -0
- package/lib/updates.d.ts.map +1 -0
- package/lib/updates.js +200 -0
- package/lib/updates.js.map +1 -0
- package/lib/updates.test.d.ts +2 -0
- package/lib/updates.test.d.ts.map +1 -0
- package/lib/updates.test.js +72 -0
- package/lib/updates.test.js.map +1 -0
- package/lib/user-actor.d.ts +188 -0
- package/lib/user-actor.d.ts.map +1 -0
- package/lib/user-actor.js +465 -0
- package/lib/user-actor.js.map +1 -0
- package/lib/webhook.d.ts +18 -0
- package/lib/webhook.d.ts.map +1 -0
- package/lib/webhook.js +25 -0
- package/lib/webhook.js.map +1 -0
- package/lib/webhook.test.d.ts +2 -0
- package/lib/webhook.test.d.ts.map +1 -0
- package/lib/webhook.test.js +36 -0
- package/lib/webhook.test.js.map +1 -0
- package/package.json +7 -5
- package/src/api.test.ts +180 -0
- package/src/api.ts +289 -0
- package/src/bot-messages.ts +117 -0
- package/src/chat-actor.ts +101 -0
- package/src/clock.test.ts +80 -0
- package/src/clock.ts +118 -0
- package/src/env.test.ts +370 -0
- package/src/env.ts +235 -0
- package/src/fetch.ts +11 -0
- package/src/index.ts +79 -169
- package/src/internal.ts +43 -0
- package/src/keyboard.test.ts +35 -0
- package/src/keyboard.ts +38 -0
- package/src/normalize.ts +34 -0
- package/src/reactions.ts +18 -0
- package/src/updates.test.ts +107 -0
- package/src/updates.ts +354 -0
- package/src/user-actor.ts +702 -0
- package/src/webhook.test.ts +54 -0
- package/src/webhook.ts +48 -0
- package/lib/index.test.d.ts +0 -2
- package/lib/index.test.d.ts.map +0 -1
- package/lib/index.test.js +0 -66
- package/lib/index.test.js.map +0 -1
- package/src/index.test.ts +0 -101
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { withFetch } from "./fetch.js";
|
|
4
|
+
import { messageUpdate } from "./updates.js";
|
|
5
|
+
import { collectUpdates, webhookRequest } from "./webhook.js";
|
|
6
|
+
|
|
7
|
+
test("collectUpdates records every update handed to the sink", async () => {
|
|
8
|
+
const { sink, updates } = collectUpdates();
|
|
9
|
+
|
|
10
|
+
await sink.handleUpdate(messageUpdate({ text: "one" }));
|
|
11
|
+
await sink.handleUpdate(messageUpdate({ text: "two" }));
|
|
12
|
+
|
|
13
|
+
assert.equal(updates.length, 2);
|
|
14
|
+
assert.equal(updates[0]?.message?.text, "one");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("webhookRequest builds a POST carrying the update as JSON, with an optional secret header", async () => {
|
|
18
|
+
const update = messageUpdate({ text: "hi" });
|
|
19
|
+
const req = webhookRequest(update, { secretToken: "s3cret" });
|
|
20
|
+
|
|
21
|
+
assert.equal(req.method, "POST");
|
|
22
|
+
assert.equal(req.headers.get("x-telegram-bot-api-secret-token"), "s3cret");
|
|
23
|
+
assert.deepEqual(await req.json(), update);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("withFetch stubs globalThis.fetch for the duration of fn, then restores it", async () => {
|
|
27
|
+
const realFetch = globalThis.fetch;
|
|
28
|
+
|
|
29
|
+
const result = await withFetch(
|
|
30
|
+
async () => new Response("stubbed"),
|
|
31
|
+
async () => {
|
|
32
|
+
const res = await fetch("https://example.invalid/");
|
|
33
|
+
return res.text();
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
assert.equal(result, "stubbed");
|
|
38
|
+
assert.equal(globalThis.fetch, realFetch);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("withFetch restores the original fetch even if fn throws", async () => {
|
|
42
|
+
const realFetch = globalThis.fetch;
|
|
43
|
+
|
|
44
|
+
await assert.rejects(
|
|
45
|
+
withFetch(
|
|
46
|
+
async () => new Response("x"),
|
|
47
|
+
() => {
|
|
48
|
+
throw new Error("boom");
|
|
49
|
+
},
|
|
50
|
+
),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
assert.equal(globalThis.fetch, realFetch);
|
|
54
|
+
});
|
package/src/webhook.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Update, UpdateSink } from "@yaebal/core";
|
|
2
|
+
|
|
3
|
+
/** result of {@link collectUpdates}: an {@link UpdateSink} plus the updates it received, in order. */
|
|
4
|
+
export interface UpdateCollector {
|
|
5
|
+
sink: UpdateSink;
|
|
6
|
+
updates: Update[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** a minimal {@link UpdateSink} (the `{ handleUpdate }` shape `webhookCallback`/runners expect) that just records. */
|
|
10
|
+
export function collectUpdates(): UpdateCollector {
|
|
11
|
+
const updates: Update[] = [];
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
sink: {
|
|
15
|
+
handleUpdate: async (update) => {
|
|
16
|
+
updates.push(update);
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
updates,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** options for {@link webhookRequest}. */
|
|
24
|
+
export interface WebhookRequestOptions {
|
|
25
|
+
url?: string;
|
|
26
|
+
method?: string;
|
|
27
|
+
secretToken?: string;
|
|
28
|
+
headers?: Record<string, string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** build a `Request` carrying `update` as JSON, as telegram would POST it to a webhook handler. */
|
|
32
|
+
export function webhookRequest(update: Update, options: WebhookRequestOptions = {}): Request {
|
|
33
|
+
const {
|
|
34
|
+
url = "https://example.invalid/webhook",
|
|
35
|
+
method = "POST",
|
|
36
|
+
secretToken,
|
|
37
|
+
headers = {},
|
|
38
|
+
} = options;
|
|
39
|
+
|
|
40
|
+
const finalHeaders: Record<string, string> = { "content-type": "application/json", ...headers };
|
|
41
|
+
if (secretToken) finalHeaders["x-telegram-bot-api-secret-token"] = secretToken;
|
|
42
|
+
|
|
43
|
+
return new Request(url, {
|
|
44
|
+
method,
|
|
45
|
+
headers: finalHeaders,
|
|
46
|
+
body: method === "GET" || method === "HEAD" ? undefined : JSON.stringify(update),
|
|
47
|
+
});
|
|
48
|
+
}
|
package/lib/index.test.d.ts
DELETED
package/lib/index.test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
|
package/lib/index.test.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
import { Composer, Context } from "@yaebal/core";
|
|
4
|
-
import { callbackUpdate, createContext, createUpdate, messageUpdate, mockApi, runMiddleware, } from "./index.js";
|
|
5
|
-
test("mockApi records calls and resolves sensible defaults", async () => {
|
|
6
|
-
const { api, calls } = mockApi();
|
|
7
|
-
const sent = await api.sendMessage({ chat_id: 1, text: "hi" });
|
|
8
|
-
assert.deepEqual(sent, { message_id: 1 });
|
|
9
|
-
const answered = await api.answerCallbackQuery({ callback_query_id: "1" });
|
|
10
|
-
assert.equal(answered, true);
|
|
11
|
-
const viaCall = await api.call("setMyCommands", { commands: [] });
|
|
12
|
-
assert.deepEqual(viaCall, {});
|
|
13
|
-
assert.deepEqual(calls, [
|
|
14
|
-
{ method: "sendMessage", params: { chat_id: 1, text: "hi" } },
|
|
15
|
-
{ method: "answerCallbackQuery", params: { callback_query_id: "1" } },
|
|
16
|
-
{ method: "setMyCommands", params: { commands: [] } },
|
|
17
|
-
]);
|
|
18
|
-
});
|
|
19
|
-
test("mockApi hook registrars are chainable no-ops", () => {
|
|
20
|
-
const { api } = mockApi();
|
|
21
|
-
assert.equal(api.before(() => undefined), api);
|
|
22
|
-
assert.equal(api.after((_m, r) => r), api);
|
|
23
|
-
});
|
|
24
|
-
test("messageUpdate produces a valid message update", () => {
|
|
25
|
-
const update = messageUpdate({ text: "hello", chatId: 42, chatType: "group" });
|
|
26
|
-
assert.ok(update.update_id > 0);
|
|
27
|
-
assert.equal(update.message?.text, "hello");
|
|
28
|
-
assert.equal(update.message?.chat.id, 42);
|
|
29
|
-
assert.equal(update.message?.chat.type, "group");
|
|
30
|
-
assert.equal(update.message?.from?.id, 42);
|
|
31
|
-
});
|
|
32
|
-
test("callbackUpdate produces a valid callback_query update", () => {
|
|
33
|
-
const update = callbackUpdate({ data: "click", chatId: 7, fromId: 9 });
|
|
34
|
-
assert.equal(update.callback_query?.data, "click");
|
|
35
|
-
assert.equal(update.callback_query?.from.id, 9);
|
|
36
|
-
assert.equal(update.callback_query?.message?.chat.id, 7);
|
|
37
|
-
});
|
|
38
|
-
test("createUpdate fills a fresh update_id", () => {
|
|
39
|
-
const a = createUpdate();
|
|
40
|
-
const b = createUpdate();
|
|
41
|
-
assert.notEqual(a.update_id, b.update_id);
|
|
42
|
-
});
|
|
43
|
-
test("createContext yields a Context whose getters work", () => {
|
|
44
|
-
const ctx = createContext(messageUpdate({ text: "yo", chatId: 5 }));
|
|
45
|
-
assert.ok(ctx instanceof Context);
|
|
46
|
-
assert.equal(ctx.updateType, "message");
|
|
47
|
-
assert.equal(ctx.text, "yo");
|
|
48
|
-
assert.equal(ctx.chat?.id, 5);
|
|
49
|
-
});
|
|
50
|
-
test("createContext detects callback_query updates", () => {
|
|
51
|
-
const ctx = createContext(callbackUpdate({ data: "x" }));
|
|
52
|
-
assert.equal(ctx.updateType, "callback_query");
|
|
53
|
-
assert.equal(ctx.callbackQuery?.data, "x");
|
|
54
|
-
});
|
|
55
|
-
test("a handler calling ctx.reply records a sendMessage call", async () => {
|
|
56
|
-
const { api, calls } = mockApi();
|
|
57
|
-
const composer = new Composer().on("message:text", (ctx) => ctx.reply("pong"));
|
|
58
|
-
await runMiddleware(composer, createContext(messageUpdate({ text: "ping", chatId: 3 }), api));
|
|
59
|
-
assert.equal(calls.length, 1);
|
|
60
|
-
const call = calls[0];
|
|
61
|
-
assert.equal(call?.method, "sendMessage");
|
|
62
|
-
assert.equal(call?.params?.chat_id, 3);
|
|
63
|
-
assert.equal(call?.params?.text, "pong");
|
|
64
|
-
assert.deepEqual(call?.params?.reply_parameters, { message_id: 1 });
|
|
65
|
-
});
|
|
66
|
-
//# sourceMappingURL=index.test.js.map
|
package/lib/index.test.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EACN,cAAc,EACd,aAAa,EACb,YAAY,EACZ,aAAa,EACb,OAAO,EACP,aAAa,GACb,MAAM,YAAY,CAAC;AAEpB,IAAI,CAAC,sDAAsD,EAAE,KAAK,IAAI,EAAE;IACvE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC;IAEjC,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,WAAW,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/D,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;IAE1C,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,mBAAmB,CAAC,EAAE,iBAAiB,EAAE,GAAG,EAAE,CAAC,CAAC;IAC3E,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAE7B,MAAM,OAA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C,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IAEzC,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;AACrE,CAAC,CAAC,CAAC"}
|
package/src/index.test.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
import { Composer, Context } from "@yaebal/core";
|
|
4
|
-
import {
|
|
5
|
-
callbackUpdate,
|
|
6
|
-
createContext,
|
|
7
|
-
createUpdate,
|
|
8
|
-
messageUpdate,
|
|
9
|
-
mockApi,
|
|
10
|
-
runMiddleware,
|
|
11
|
-
} from "./index.js";
|
|
12
|
-
|
|
13
|
-
test("mockApi records calls and resolves sensible defaults", async () => {
|
|
14
|
-
const { api, calls } = mockApi();
|
|
15
|
-
|
|
16
|
-
const sent = await api.sendMessage({ chat_id: 1, text: "hi" });
|
|
17
|
-
assert.deepEqual(sent, { message_id: 1 });
|
|
18
|
-
|
|
19
|
-
const answered = await api.answerCallbackQuery({ callback_query_id: "1" });
|
|
20
|
-
assert.equal(answered, true);
|
|
21
|
-
|
|
22
|
-
const viaCall = await api.call("setMyCommands", { commands: [] });
|
|
23
|
-
assert.deepEqual(viaCall, {});
|
|
24
|
-
|
|
25
|
-
assert.deepEqual(calls, [
|
|
26
|
-
{ method: "sendMessage", params: { chat_id: 1, text: "hi" } },
|
|
27
|
-
{ method: "answerCallbackQuery", params: { callback_query_id: "1" } },
|
|
28
|
-
{ method: "setMyCommands", params: { commands: [] } },
|
|
29
|
-
]);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("mockApi hook registrars are chainable no-ops", () => {
|
|
33
|
-
const { api } = mockApi();
|
|
34
|
-
assert.equal(
|
|
35
|
-
api.before(() => undefined),
|
|
36
|
-
api,
|
|
37
|
-
);
|
|
38
|
-
assert.equal(
|
|
39
|
-
api.after((_m, r) => r),
|
|
40
|
-
api,
|
|
41
|
-
);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("messageUpdate produces a valid message update", () => {
|
|
45
|
-
const update = messageUpdate({ text: "hello", chatId: 42, chatType: "group" });
|
|
46
|
-
|
|
47
|
-
assert.ok(update.update_id > 0);
|
|
48
|
-
|
|
49
|
-
assert.equal(update.message?.text, "hello");
|
|
50
|
-
assert.equal(update.message?.chat.id, 42);
|
|
51
|
-
assert.equal(update.message?.chat.type, "group");
|
|
52
|
-
assert.equal(update.message?.from?.id, 42);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("callbackUpdate produces a valid callback_query update", () => {
|
|
56
|
-
const update = callbackUpdate({ data: "click", chatId: 7, fromId: 9 });
|
|
57
|
-
|
|
58
|
-
assert.equal(update.callback_query?.data, "click");
|
|
59
|
-
assert.equal(update.callback_query?.from.id, 9);
|
|
60
|
-
assert.equal(update.callback_query?.message?.chat.id, 7);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test("createUpdate fills a fresh update_id", () => {
|
|
64
|
-
const a = createUpdate();
|
|
65
|
-
const b = createUpdate();
|
|
66
|
-
|
|
67
|
-
assert.notEqual(a.update_id, b.update_id);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test("createContext yields a Context whose getters work", () => {
|
|
71
|
-
const ctx = createContext(messageUpdate({ text: "yo", chatId: 5 }));
|
|
72
|
-
|
|
73
|
-
assert.ok(ctx instanceof Context);
|
|
74
|
-
|
|
75
|
-
assert.equal(ctx.updateType, "message");
|
|
76
|
-
assert.equal(ctx.text, "yo");
|
|
77
|
-
assert.equal(ctx.chat?.id, 5);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("createContext detects callback_query updates", () => {
|
|
81
|
-
const ctx = createContext(callbackUpdate({ data: "x" }));
|
|
82
|
-
|
|
83
|
-
assert.equal(ctx.updateType, "callback_query");
|
|
84
|
-
assert.equal(ctx.callbackQuery?.data, "x");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("a handler calling ctx.reply records a sendMessage call", async () => {
|
|
88
|
-
const { api, calls } = mockApi();
|
|
89
|
-
|
|
90
|
-
const composer = new Composer().on("message:text", (ctx) => ctx.reply("pong"));
|
|
91
|
-
await runMiddleware(composer, createContext(messageUpdate({ text: "ping", chatId: 3 }), api));
|
|
92
|
-
|
|
93
|
-
assert.equal(calls.length, 1);
|
|
94
|
-
|
|
95
|
-
const call = calls[0];
|
|
96
|
-
assert.equal(call?.method, "sendMessage");
|
|
97
|
-
assert.equal(call?.params?.chat_id, 3);
|
|
98
|
-
assert.equal(call?.params?.text, "pong");
|
|
99
|
-
|
|
100
|
-
assert.deepEqual(call?.params?.reply_parameters, { message_id: 1 });
|
|
101
|
-
});
|