| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { spawn } from "node:child_process"; |
| 4 | import fs from "node:fs/promises"; |
| 5 | import http from "node:http"; |
| 6 | import os from "node:os"; |
| 7 | import path from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | |
| 10 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | |
| 12 | function message(updateId, chatId, text) { |
| 13 | return { |
| 14 | update_id: updateId, |
| 15 | message: { |
| 16 | message_id: updateId, |
| 17 | date: 0, |
| 18 | chat: { id: chatId, type: "private" }, |
| 19 | from: { id: chatId, is_bot: false, first_name: "user" }, |
| 20 | text |
| 21 | } |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | test("an update whose reply is permanently refused does not block later updates", async (t) => { |
| 26 | const pending = [message(500, 999, "hi"), message(501, 111, "/help")]; |
| 27 | const stats = { getUpdates: 0, sendTo999: 0, sendTo111: 0 }; |
| 28 | const server = http.createServer((req, res) => { |
| 29 | let raw = ""; |
| 30 | req.on("data", (chunk) => (raw += chunk)); |
| 31 | req.on("end", () => { |
| 32 | const body = raw ? JSON.parse(raw) : {}; |
| 33 | const method = req.url.split("/").pop(); |
| 34 | const reply = (status, payload) => { |
| 35 | res.writeHead(status, { "content-type": "application/json" }); |
| 36 | res.end(JSON.stringify(payload)); |
| 37 | }; |
| 38 | if (method === "getUpdates") { |
| 39 | stats.getUpdates += 1; |
| 40 | const result = pending.filter((update) => update.update_id >= (body.offset || 0)); |
| 41 | if (result.length) return reply(200, { ok: true, result }); |
| 42 | return setTimeout(() => reply(200, { ok: true, result: [] }), 200); |
| 43 | } |
| 44 | if (method === "sendMessage" && Number(body.chat_id) === 999) { |
| 45 | stats.sendTo999 += 1; |
| 46 | return reply(403, { ok: false, error_code: 403, description: "Forbidden: bot was blocked by the user" }); |
| 47 | } |
| 48 | if (method === "sendMessage") { |
| 49 | stats.sendTo111 += 1; |
| 50 | return reply(200, { ok: true, result: { message_id: 1, chat: { id: body.chat_id } } }); |
| 51 | } |
| 52 | return reply(200, { ok: true, result: true }); |
| 53 | }); |
| 54 | }); |
| 55 | await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 56 | const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "telegram-poll-loop-")); |
| 57 | const bridge = spawn(process.execPath, [path.join(__dirname, "../src/index.mjs")], { |
| 58 | env: { |
| 59 | ...process.env, |
| 60 | TELEGRAM_BOT_TOKEN: "123:test", |
| 61 | TELEGRAM_API_BASE: `http://127.0.0.1:${server.address().port}`, |
| 62 | TELEGRAM_CHAT_ALLOWLIST: "111", |
| 63 | TELEGRAM_THREAD_MAP_PATH: path.join(stateDir, "thread-map.json"), |
| 64 | TELEGRAM_POLL_TIMEOUT_SECONDS: "1", |
| 65 | CODEWHALE_RUNTIME_URL: "http://127.0.0.1:9", |
| 66 | CODEWHALE_RUNTIME_TOKEN: "test" |
| 67 | }, |
| 68 | stdio: "ignore" |
| 69 | }); |
| 70 | t.after(async () => { |
| 71 | bridge.kill("SIGKILL"); |
| 72 | server.close(); |
| 73 | await fs.rm(stateDir, { recursive: true, force: true }); |
| 74 | }); |
| 75 | |
| 76 | const deadline = Date.now() + 15000; |
| 77 | while (stats.sendTo111 === 0 && Date.now() < deadline) { |
| 78 | await new Promise((resolve) => setTimeout(resolve, 50)); |
| 79 | } |
| 80 | |
| 81 | assert.equal(stats.sendTo111, 1, `later update was never handled: ${JSON.stringify(stats)}`); |
| 82 | assert.equal(stats.sendTo999, 1, `refused update was retried: ${JSON.stringify(stats)}`); |
| 83 | }); |
| 84 | |
| 85 | test("a 200 sendMessage response without ok=true is not treated as delivered", async (t) => { |
| 86 | const pending = [message(600, 999, "hi")]; |
| 87 | const stats = { attempts: 0, offsets: [] }; |
| 88 | const server = http.createServer((req, res) => { |
| 89 | let raw = ""; |
| 90 | req.on("data", (chunk) => (raw += chunk)); |
| 91 | req.on("end", () => { |
| 92 | const body = raw ? JSON.parse(raw) : {}; |
| 93 | const method = req.url.split("/").pop(); |
| 94 | const reply = (status, payload) => { |
| 95 | res.writeHead(status, { "content-type": "application/json" }); |
| 96 | res.end(JSON.stringify(payload)); |
| 97 | }; |
| 98 | if (method === "getUpdates") { |
| 99 | stats.offsets.push(body.offset ?? null); |
| 100 | const result = pending.filter((update) => update.update_id >= (body.offset || 0)); |
| 101 | if (result.length) return reply(200, { ok: true, result }); |
| 102 | return setTimeout(() => reply(200, { ok: true, result: [] }), 200); |
| 103 | } |
| 104 | if (method === "sendMessage") { |
| 105 | stats.attempts += 1; |
| 106 | if (stats.attempts === 1) { |
| 107 | res.writeHead(200, { "content-type": "text/plain" }); |
| 108 | res.end(""); |
| 109 | return; |
| 110 | } |
| 111 | return reply(200, { ok: true, result: { message_id: 2, chat: { id: body.chat_id } } }); |
| 112 | } |
| 113 | return reply(200, { ok: true, result: true }); |
| 114 | }); |
| 115 | }); |
| 116 | await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 117 | const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "telegram-ack-")); |
| 118 | const bridge = spawn(process.execPath, [path.join(__dirname, "../src/index.mjs")], { |
| 119 | env: { |
| 120 | ...process.env, |
| 121 | TELEGRAM_BOT_TOKEN: "123:test", |
| 122 | TELEGRAM_API_BASE: `http://127.0.0.1:${server.address().port}`, |
| 123 | TELEGRAM_CHAT_ALLOWLIST: "111", |
| 124 | TELEGRAM_THREAD_MAP_PATH: path.join(stateDir, "thread-map.json"), |
| 125 | TELEGRAM_POLL_TIMEOUT_SECONDS: "1", |
| 126 | CODEWHALE_RUNTIME_URL: "http://127.0.0.1:9", |
| 127 | CODEWHALE_RUNTIME_TOKEN: "test" |
| 128 | }, |
| 129 | stdio: "ignore" |
| 130 | }); |
| 131 | t.after(async () => { |
| 132 | bridge.kill("SIGKILL"); |
| 133 | server.close(); |
| 134 | await fs.rm(stateDir, { recursive: true, force: true }); |
| 135 | }); |
| 136 | |
| 137 | const deadline = Date.now() + 15000; |
| 138 | while ((stats.attempts < 2 || !stats.offsets.includes(601)) && Date.now() < deadline) { |
| 139 | await new Promise((resolve) => setTimeout(resolve, 50)); |
| 140 | } |
| 141 | |
| 142 | assert.equal(stats.attempts, 2, `unconfirmed 200 response was not retried: ${JSON.stringify(stats)}`); |
| 143 | assert.ok(stats.offsets.includes(601), `update offset did not advance after ok=true: ${JSON.stringify(stats)}`); |
| 144 | }); |
| 145 |