@llblab/pi-telegram 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.
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Regression tests for Telegram setup prompt defaults
3
+ * Covers token-prefill priority across stored config, environment variables, and placeholder fallback
4
+ */
5
+
6
+ import assert from "node:assert/strict";
7
+ import test from "node:test";
8
+
9
+ import { __telegramTestUtils } from "../index.ts";
10
+
11
+ test("Bot token input prefers stored config over env vars", () => {
12
+ const value = __telegramTestUtils.getTelegramBotTokenInputDefault(
13
+ {
14
+ TELEGRAM_KEY: "key-last",
15
+ TELEGRAM_TOKEN: "token-third",
16
+ TELEGRAM_BOT_KEY: "key-second",
17
+ TELEGRAM_BOT_TOKEN: "token-first",
18
+ },
19
+ "stored-token",
20
+ );
21
+ assert.equal(value, "stored-token");
22
+ });
23
+
24
+ test("Bot token input prefers the first configured Telegram env var when no config exists", () => {
25
+ const value = __telegramTestUtils.getTelegramBotTokenInputDefault({
26
+ TELEGRAM_KEY: "key-last",
27
+ TELEGRAM_TOKEN: "token-third",
28
+ TELEGRAM_BOT_KEY: "key-second",
29
+ TELEGRAM_BOT_TOKEN: "token-first",
30
+ });
31
+ assert.equal(value, "token-first");
32
+ });
33
+
34
+ test("Bot token prompt uses the editor when a real prefill exists", () => {
35
+ const prompt = __telegramTestUtils.getTelegramBotTokenPromptSpec({
36
+ TELEGRAM_BOT_TOKEN: "token-first",
37
+ });
38
+ assert.deepEqual(prompt, {
39
+ method: "editor",
40
+ value: "token-first",
41
+ });
42
+ });
43
+
44
+ test("Bot token prompt shows stored config before env values", () => {
45
+ const prompt = __telegramTestUtils.getTelegramBotTokenPromptSpec(
46
+ {
47
+ TELEGRAM_BOT_TOKEN: "token-first",
48
+ },
49
+ "stored-token",
50
+ );
51
+ assert.deepEqual(prompt, {
52
+ method: "editor",
53
+ value: "stored-token",
54
+ });
55
+ });
56
+
57
+ test("Bot token input skips blank env vars and falls back to config", () => {
58
+ const value = __telegramTestUtils.getTelegramBotTokenInputDefault(
59
+ {
60
+ TELEGRAM_BOT_TOKEN: " ",
61
+ TELEGRAM_BOT_KEY: "",
62
+ TELEGRAM_TOKEN: " ",
63
+ },
64
+ "stored-token",
65
+ );
66
+ assert.equal(value, "stored-token");
67
+ });
68
+
69
+ test("Bot token input falls back to placeholder when no value exists", () => {
70
+ const value = __telegramTestUtils.getTelegramBotTokenInputDefault({});
71
+ assert.equal(value, "123456:ABCDEF...");
72
+ });
73
+
74
+ test("Bot token prompt uses placeholder input when no prefill exists", () => {
75
+ const prompt = __telegramTestUtils.getTelegramBotTokenPromptSpec({});
76
+ assert.deepEqual(prompt, {
77
+ method: "input",
78
+ value: "123456:ABCDEF...",
79
+ });
80
+ });
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Regression tests for Telegram media and text extraction helpers
3
+ * Covers inbound file-info collection, text extraction, id collection, and history formatting
4
+ */
5
+
6
+ import assert from "node:assert/strict";
7
+ import test from "node:test";
8
+
9
+ import {
10
+ collectTelegramFileInfos,
11
+ collectTelegramMessageIds,
12
+ extractFirstTelegramMessageText,
13
+ extractTelegramMessagesText,
14
+ formatTelegramHistoryText,
15
+ guessMediaType,
16
+ } from "../lib/media.ts";
17
+
18
+ test("Media helpers collect file infos across Telegram message variants", () => {
19
+ const files = collectTelegramFileInfos([
20
+ {
21
+ message_id: 1,
22
+ text: "hello",
23
+ photo: [
24
+ { file_id: "small", file_size: 1 },
25
+ { file_id: "large", file_size: 10 },
26
+ ],
27
+ document: {
28
+ file_id: "doc",
29
+ file_name: "report.png",
30
+ mime_type: "image/png",
31
+ },
32
+ voice: {
33
+ file_id: "voice",
34
+ mime_type: "audio/ogg",
35
+ },
36
+ sticker: {
37
+ file_id: "sticker",
38
+ },
39
+ },
40
+ ]);
41
+ assert.deepEqual(
42
+ files.map((file) => ({
43
+ id: file.file_id,
44
+ name: file.fileName,
45
+ image: file.isImage,
46
+ })),
47
+ [
48
+ { id: "large", name: "photo-1.jpg", image: true },
49
+ { id: "doc", name: "report.png", image: true },
50
+ { id: "voice", name: "voice-1.ogg", image: false },
51
+ { id: "sticker", name: "sticker-1.webp", image: true },
52
+ ],
53
+ );
54
+ });
55
+
56
+ test("Media helpers extract text, ids, and history summaries", () => {
57
+ const messages = [
58
+ { message_id: 1, text: "first" },
59
+ { message_id: 2, caption: "second" },
60
+ { message_id: 2, text: "duplicate id" },
61
+ ];
62
+ assert.equal(
63
+ extractTelegramMessagesText(messages),
64
+ "first\n\nsecond\n\nduplicate id",
65
+ );
66
+ assert.equal(extractFirstTelegramMessageText(messages), "first");
67
+ assert.deepEqual(collectTelegramMessageIds(messages), [1, 2]);
68
+ assert.equal(
69
+ formatTelegramHistoryText("hello", [{ path: "/tmp/demo.txt" }]),
70
+ "hello\nAttachments:\n- /tmp/demo.txt",
71
+ );
72
+ });
73
+
74
+ test("Media helpers infer outgoing image media types from file paths", () => {
75
+ assert.equal(guessMediaType("/tmp/demo.png"), "image/png");
76
+ assert.equal(guessMediaType("/tmp/demo.txt"), undefined);
77
+ });