wip: snapshot das alterações não commitadas (2026-10-20)
Validate / validate (push) Failing after 4m58s
Validate / validate (push) Failing after 4m58s
Origem: D:/Documents/Github/oh-my-pi-telegram Arquivos omitidos (segredos, bancos de dados, logs, >50 MB): 0
This commit is contained in:
1 parent
a56408711e
commit
97f2b723bb
5 files changed
+916
No files matched your search
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Regression tests for the Telegram operator notices
|
||||
* Covers the toast wording, severity, throttling and failure isolation.
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
createTelegramNoticeRuntime,
|
||||
TELEGRAM_DELIVERY_FAILED_NOTICE_COOLDOWN_MS,
|
||||
} from "../lib/notices.ts";
|
||||
|
||||
function createNoticeHarness(options: { notifyThrows?: boolean } = {}) {
|
||||
const notices: string[] = [];
|
||||
const recorded: string[] = [];
|
||||
let nowMs = 1_000_000;
|
||||
let context: string | undefined = "session";
|
||||
const runtime = createTelegramNoticeRuntime({
|
||||
getContext: () => context,
|
||||
notify: (ctx: string, message, level) => {
|
||||
if (options.notifyThrows) throw new Error("stale ctx");
|
||||
notices.push(`${level}|${ctx}|${message}`);
|
||||
},
|
||||
recordRuntimeEvent: (category, error, details) => {
|
||||
recorded.push(`${category}:${(error as Error).message}:${details?.phase}`);
|
||||
},
|
||||
now: () => nowMs,
|
||||
});
|
||||
return {
|
||||
runtime,
|
||||
notices,
|
||||
recorded,
|
||||
advance: (ms: number) => {
|
||||
nowMs += ms;
|
||||
},
|
||||
dropContext: () => {
|
||||
context = undefined;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test("Notices announce pairing and recovery as info toasts", () => {
|
||||
const { runtime, notices } = createNoticeHarness();
|
||||
runtime.paired(777, "ctx-a");
|
||||
runtime.recovered("ctx-b");
|
||||
assert.deepEqual(notices, [
|
||||
"info|ctx-a|Telegram paired with user 777",
|
||||
"info|ctx-b|Telegram reconnected",
|
||||
]);
|
||||
});
|
||||
|
||||
test("Delivery failures explain the reason and pick a severity from it", () => {
|
||||
const harness = createNoticeHarness();
|
||||
harness.runtime.deliveryFailed(
|
||||
new Error("Telegram API sendMessage failed: HTTP 429: Too Many Requests: retry after 12"),
|
||||
);
|
||||
harness.advance(TELEGRAM_DELIVERY_FAILED_NOTICE_COOLDOWN_MS);
|
||||
harness.runtime.deliveryFailed(new Error("Unable to connect. Is the computer able to access the url?"));
|
||||
harness.advance(TELEGRAM_DELIVERY_FAILED_NOTICE_COOLDOWN_MS);
|
||||
harness.runtime.deliveryFailed(new Error("Telegram API sendMessage failed: HTTP 401: Unauthorized"));
|
||||
harness.advance(TELEGRAM_DELIVERY_FAILED_NOTICE_COOLDOWN_MS);
|
||||
harness.runtime.deliveryFailed(new Error("message is too long"));
|
||||
harness.advance(TELEGRAM_DELIVERY_FAILED_NOTICE_COOLDOWN_MS);
|
||||
harness.runtime.deliveryFailed(
|
||||
new Error("Telegram API sendRichMessage may have committed before transport failed."),
|
||||
);
|
||||
assert.deepEqual(harness.notices, [
|
||||
"warning|session|Telegram: reply not delivered (rate limited 12s)",
|
||||
"warning|session|Telegram: reply not delivered (offline)",
|
||||
"error|session|Telegram: reply not delivered (invalid token)",
|
||||
"error|session|Telegram: reply not delivered (message is too long)",
|
||||
"warning|session|Telegram: reply delivery unconfirmed (check the chat)",
|
||||
]);
|
||||
});
|
||||
|
||||
test("Delivery failure notices are throttled and need a live context", () => {
|
||||
const harness = createNoticeHarness();
|
||||
harness.runtime.deliveryFailed(new Error("fetch failed"));
|
||||
harness.advance(TELEGRAM_DELIVERY_FAILED_NOTICE_COOLDOWN_MS - 1);
|
||||
harness.runtime.deliveryFailed(new Error("fetch failed"));
|
||||
assert.equal(harness.notices.length, 1);
|
||||
harness.advance(1);
|
||||
harness.runtime.deliveryFailed(new Error("fetch failed"));
|
||||
assert.equal(harness.notices.length, 2);
|
||||
harness.advance(TELEGRAM_DELIVERY_FAILED_NOTICE_COOLDOWN_MS);
|
||||
harness.dropContext();
|
||||
harness.runtime.deliveryFailed(new Error("fetch failed"));
|
||||
assert.equal(harness.notices.length, 2);
|
||||
});
|
||||
|
||||
test("A failing toast is recorded and never reaches the caller", () => {
|
||||
const harness = createNoticeHarness({ notifyThrows: true });
|
||||
assert.doesNotThrow(() => {
|
||||
harness.runtime.paired(777, "ctx");
|
||||
harness.runtime.recovered("ctx");
|
||||
harness.runtime.deliveryFailed(new Error("fetch failed"));
|
||||
});
|
||||
assert.deepEqual(harness.recorded, [
|
||||
"status:stale ctx:notice",
|
||||
"status:stale ctx:notice",
|
||||
"status:stale ctx:notice",
|
||||
]);
|
||||
});
|
||||
|
||||
test("Delivery failure notices carry no control sequences from raw error text", () => {
|
||||
const harness = createNoticeHarness();
|
||||
harness.runtime.deliveryFailed(new Error("bad\u001b]0;pwned\u0007\u001b[31m text"));
|
||||
assert.equal(harness.notices.length, 1);
|
||||
assert.doesNotMatch(harness.notices[0], /[\u0000-\u001f\u007f-\u009f]/);
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* Regression tests for the Telegram traffic counters
|
||||
* Covers what counts as a message, last-activity tracking, failed deliveries, and the /telegram-status line.
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import { buildTelegramBridgeStatusLines } from "../lib/status.ts";
|
||||
import {
|
||||
createTelegramTrafficCounters,
|
||||
createTelegramTrafficStatusBridge,
|
||||
isTelegramOutboundMessageMethod,
|
||||
} from "../lib/traffic.ts";
|
||||
|
||||
test("Traffic counters count every direction and remember the latest message", () => {
|
||||
let nowMs = 1_000;
|
||||
const counters = createTelegramTrafficCounters(() => nowMs);
|
||||
assert.deepEqual(counters.snapshot(), { received: 0, sent: 0, failed: 0 });
|
||||
counters.recordReceived();
|
||||
nowMs = 2_000;
|
||||
counters.recordSent();
|
||||
counters.recordSent();
|
||||
nowMs = 3_000;
|
||||
counters.recordFailed();
|
||||
assert.deepEqual(counters.snapshot(), {
|
||||
received: 1,
|
||||
sent: 2,
|
||||
failed: 1,
|
||||
lastAtMs: 2_000,
|
||||
});
|
||||
});
|
||||
|
||||
test("Only calls that deliver a readable message count as sent", () => {
|
||||
for (const method of ["sendMessage", "sendRichMessage", "sendPhoto", "sendDocument", "sendVoice"]) {
|
||||
assert.equal(isTelegramOutboundMessageMethod(method), true, method);
|
||||
}
|
||||
for (const method of [
|
||||
"sendChatAction",
|
||||
"sendMessageDraft",
|
||||
"editMessageText",
|
||||
"setMessageReaction",
|
||||
"answerCallbackQuery",
|
||||
"getUpdates",
|
||||
"toString",
|
||||
]) {
|
||||
assert.equal(isTelegramOutboundMessageMethod(method), false, method);
|
||||
}
|
||||
});
|
||||
|
||||
test("Status lines report the message counters and the time of the last one", () => {
|
||||
const baseState = {
|
||||
botUsername: "demo_bot",
|
||||
allowedUserId: 7,
|
||||
pollingActive: true,
|
||||
pendingDispatch: false,
|
||||
compactionInProgress: false,
|
||||
activeToolExecutions: 0,
|
||||
pendingModelSwitch: false,
|
||||
queuedItems: [],
|
||||
recentRuntimeEvents: [],
|
||||
};
|
||||
const lastAtMs = new Date(2026, 9, 1, 14, 32).getTime();
|
||||
const clean = buildTelegramBridgeStatusLines({
|
||||
...baseState,
|
||||
traffic: { received: 12, sent: 11, failed: 0, lastAtMs },
|
||||
});
|
||||
assert.ok(
|
||||
clean.includes("- messages: 12 received, 11 sent, last 14:32"),
|
||||
clean.join("\n"),
|
||||
);
|
||||
const withFailures = buildTelegramBridgeStatusLines({
|
||||
...baseState,
|
||||
traffic: { received: 12, sent: 11, failed: 2, lastAtMs },
|
||||
});
|
||||
assert.ok(
|
||||
withFailures.includes("- messages: 12 received, 11 sent, 2 failed, last 14:32"),
|
||||
withFailures.join("\n"),
|
||||
);
|
||||
const withoutTraffic = buildTelegramBridgeStatusLines(baseState);
|
||||
assert.equal(withoutTraffic.some((line) => line.startsWith("- messages:")), false);
|
||||
});
|
||||
|
||||
test("Traffic bridge counts delivered messages and refreshes the status with the current context", () => {
|
||||
const counters = createTelegramTrafficCounters();
|
||||
const refreshed: string[] = [];
|
||||
let context: string | undefined = "session-a";
|
||||
const bridge = createTelegramTrafficStatusBridge({
|
||||
counters,
|
||||
getContext: () => context,
|
||||
updateStatus: (ctx: string) => {
|
||||
refreshed.push(ctx);
|
||||
},
|
||||
recordRuntimeEvent: () => {},
|
||||
});
|
||||
bridge.onCallSucceeded("sendChatAction");
|
||||
bridge.onCallSucceeded("editMessageText");
|
||||
assert.deepEqual(counters.snapshot(), { received: 0, sent: 0, failed: 0 });
|
||||
assert.deepEqual(refreshed, []);
|
||||
bridge.onCallSucceeded("sendRichMessage");
|
||||
assert.equal(counters.snapshot().sent, 1);
|
||||
assert.deepEqual(refreshed, ["session-a"]);
|
||||
context = undefined;
|
||||
bridge.onCallSucceeded("sendMessage");
|
||||
assert.equal(counters.snapshot().sent, 2);
|
||||
assert.deepEqual(refreshed, ["session-a"]);
|
||||
bridge.onAuthorizedMessage("session-b");
|
||||
assert.equal(counters.snapshot().received, 1);
|
||||
assert.deepEqual(refreshed, ["session-a", "session-b"]);
|
||||
});
|
||||
|
||||
test("Traffic bridge counts undelivered messages, reports them and refreshes the status", () => {
|
||||
const counters = createTelegramTrafficCounters();
|
||||
const refreshed: string[] = [];
|
||||
const failures: string[] = [];
|
||||
const bridge = createTelegramTrafficStatusBridge({
|
||||
counters,
|
||||
getContext: () => "session",
|
||||
updateStatus: (ctx: string) => {
|
||||
refreshed.push(ctx);
|
||||
},
|
||||
recordRuntimeEvent: () => {},
|
||||
onDeliveryFailed: (error) => {
|
||||
failures.push((error as Error).message);
|
||||
},
|
||||
});
|
||||
bridge.onCallFailed("getUpdates", new Error("poll failed"));
|
||||
bridge.onCallFailed("sendChatAction", new Error("typing failed"));
|
||||
assert.equal(counters.snapshot().failed, 0);
|
||||
assert.deepEqual(failures, []);
|
||||
assert.deepEqual(refreshed, []);
|
||||
bridge.onCallFailed("sendMessage", new Error("HTTP 500"));
|
||||
assert.equal(counters.snapshot().failed, 1);
|
||||
assert.equal(counters.snapshot().sent, 0);
|
||||
assert.deepEqual(failures, ["HTTP 500"]);
|
||||
assert.deepEqual(refreshed, ["session"]);
|
||||
});
|
||||
|
||||
test("Traffic bridge records a failed status refresh instead of breaking the message path", () => {
|
||||
const counters = createTelegramTrafficCounters();
|
||||
const recorded: string[] = [];
|
||||
const bridge = createTelegramTrafficStatusBridge({
|
||||
counters,
|
||||
getContext: () => "session",
|
||||
updateStatus: () => {
|
||||
throw new Error("stale ctx");
|
||||
},
|
||||
recordRuntimeEvent: (category, error, details) => {
|
||||
recorded.push(`${category}:${(error as Error).message}:${details?.phase}`);
|
||||
},
|
||||
});
|
||||
bridge.onCallSucceeded("sendMessage");
|
||||
bridge.onAuthorizedMessage("session");
|
||||
bridge.onCallFailed("sendMessage", new Error("HTTP 500"));
|
||||
assert.equal(counters.snapshot().received, 1);
|
||||
assert.equal(counters.snapshot().sent, 1);
|
||||
assert.equal(counters.snapshot().failed, 1);
|
||||
assert.deepEqual(recorded, [
|
||||
"status:stale ctx:traffic-refresh",
|
||||
"status:stale ctx:traffic-refresh",
|
||||
"status:stale ctx:traffic-refresh",
|
||||
]);
|
||||
});
|
||||
Reference in new issue
Block a user