Sitelet https://github.com/Dream-AI-4444/bot/commit/2b1941fed16f17a0a4b548453c828aede2a76727
Skip to content

Commit 2b1941f

Browse files
committed
test: align envelope timestamp expectations (openclaw#1329) (thanks @dlauer)
1 parent 1acfa92 commit 2b1941f

5 files changed

Lines changed: 36 additions & 25 deletions

‎src/telegram/bot.create-telegram-bot.accepts-group-messages-mentionpatterns-match-without-botusername.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
2-
import {
3-
escapeRegExp,
4-
formatLocalEnvelopeTimestamp,
5-
} from "../../test/helpers/envelope-timestamp.js";
2+
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
63
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
74
import { createTelegramBot } from "./bot.js";
85

@@ -180,7 +177,7 @@ describe("createTelegramBot", () => {
180177
expect(payload.WasMentioned).toBe(true);
181178
expect(payload.SenderName).toBe("Ada");
182179
expect(payload.SenderId).toBe("9");
183-
const expectedTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
180+
const expectedTimestamp = formatEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
184181
const timestampPattern = escapeRegExp(expectedTimestamp);
185182
expect(payload.Body).toMatch(
186183
new RegExp(`^\\[Telegram Test Group id:7 (\\+\\d+[smhd] )?${timestampPattern}\\]`),
@@ -225,7 +222,7 @@ describe("createTelegramBot", () => {
225222
expect(payload.SenderName).toBe("Ada Lovelace");
226223
expect(payload.SenderId).toBe("99");
227224
expect(payload.SenderUsername).toBe("ada");
228-
const expectedTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
225+
const expectedTimestamp = formatEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
229226
const timestampPattern = escapeRegExp(expectedTimestamp);
230227
expect(payload.Body).toMatch(
231228
new RegExp(`^\\[Telegram Ops id:42 (\\+\\d+[smhd] )?${timestampPattern}\\]`),

‎src/telegram/bot.create-telegram-bot.installs-grammy-throttler.test.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
2-
import {
3-
escapeRegExp,
4-
formatLocalEnvelopeTimestamp,
5-
} from "../../test/helpers/envelope-timestamp.js";
2+
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
63
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
74
import { createTelegramBot, getTelegramSequentialKey } from "./bot.js";
85
import { resolveTelegramFetch } from "./fetch.js";
@@ -332,7 +329,7 @@ describe("createTelegramBot", () => {
332329

333330
expect(replySpy).toHaveBeenCalledTimes(1);
334331
const payload = replySpy.mock.calls[0][0];
335-
const expectedTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
332+
const expectedTimestamp = formatEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
336333
const timestampPattern = escapeRegExp(expectedTimestamp);
337334
expect(payload.Body).toMatch(
338335
new RegExp(

‎src/telegram/bot.test.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import { listSkillCommandsForAgents } from "../auto-reply/skill-commands.js";
1010
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
1111
import * as replyModule from "../auto-reply/reply.js";
1212
import { expectInboundContextContract } from "../../test/helpers/inbound-contract.js";
13-
import {
14-
escapeRegExp,
15-
formatLocalEnvelopeTimestamp,
16-
} from "../../test/helpers/envelope-timestamp.js";
13+
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
1714
import { createTelegramBot, getTelegramSequentialKey } from "./bot.js";
1815
import { resolveTelegramFetch } from "./fetch.js";
1916

@@ -454,7 +451,7 @@ describe("createTelegramBot", () => {
454451

455452
expect(replySpy).toHaveBeenCalledTimes(1);
456453
const payload = replySpy.mock.calls[0][0];
457-
const expectedTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
454+
const expectedTimestamp = formatEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
458455
const timestampPattern = escapeRegExp(expectedTimestamp);
459456
expect(payload.Body).toMatch(
460457
new RegExp(
@@ -593,7 +590,7 @@ describe("createTelegramBot", () => {
593590
const payload = replySpy.mock.calls[0][0];
594591
expectInboundContextContract(payload);
595592
expect(payload.WasMentioned).toBe(true);
596-
const expectedTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
593+
const expectedTimestamp = formatEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
597594
const timestampPattern = escapeRegExp(expectedTimestamp);
598595
expect(payload.Body).toMatch(
599596
new RegExp(`^\\[Telegram Test Group id:7 (\\+\\d+[smhd] )?${timestampPattern}\\]`),
@@ -639,7 +636,7 @@ describe("createTelegramBot", () => {
639636
expect(replySpy).toHaveBeenCalledTimes(1);
640637
const payload = replySpy.mock.calls[0][0];
641638
expectInboundContextContract(payload);
642-
const expectedTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
639+
const expectedTimestamp = formatEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
643640
const timestampPattern = escapeRegExp(expectedTimestamp);
644641
expect(payload.Body).toMatch(
645642
new RegExp(`^\\[Telegram Ops id:42 (\\+\\d+[smhd] )?${timestampPattern}\\]`),

‎src/web/auto-reply.web-auto-reply.reconnects-after-connection-close.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import fs from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
55
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6-
import {
7-
escapeRegExp,
8-
formatLocalEnvelopeTimestamp,
9-
} from "../../test/helpers/envelope-timestamp.js";
6+
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
107

118
vi.mock("../agents/pi-embedded.js", () => ({
129
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
@@ -332,8 +329,8 @@ describe("web auto-reply", () => {
332329
expect(resolver).toHaveBeenCalledTimes(2);
333330
const firstArgs = resolver.mock.calls[0][0];
334331
const secondArgs = resolver.mock.calls[1][0];
335-
const firstTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-01T00:00:00Z"));
336-
const secondTimestamp = formatLocalEnvelopeTimestamp(new Date("2025-01-01T01:00:00Z"));
332+
const firstTimestamp = formatEnvelopeTimestamp(new Date("2025-01-01T00:00:00Z"));
333+
const secondTimestamp = formatEnvelopeTimestamp(new Date("2025-01-01T01:00:00Z"));
337334
const firstPattern = escapeRegExp(firstTimestamp);
338335
const secondPattern = escapeRegExp(secondTimestamp);
339336
expect(firstArgs.Body).toMatch(

‎test/helpers/envelope-timestamp.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
export function formatLocalEnvelopeTimestamp(date: Date): string {
1+
type EnvelopeTimestampZone = string;
2+
3+
function formatUtcTimestamp(date: Date): string {
4+
const yyyy = String(date.getUTCFullYear()).padStart(4, "0");
5+
const mm = String(date.getUTCMonth() + 1).padStart(2, "0");
6+
const dd = String(date.getUTCDate()).padStart(2, "0");
7+
const hh = String(date.getUTCHours()).padStart(2, "0");
8+
const min = String(date.getUTCMinutes()).padStart(2, "0");
9+
return `${yyyy}-${mm}-${dd}T${hh}:${min}Z`;
10+
}
11+
12+
function formatZonedTimestamp(date: Date, timeZone?: string): string {
213
const parts = new Intl.DateTimeFormat("en-US", {
14+
timeZone,
315
year: "numeric",
416
month: "2-digit",
517
day: "2-digit",
@@ -27,6 +39,17 @@ export function formatLocalEnvelopeTimestamp(date: Date): string {
2739
return `${yyyy}-${mm}-${dd} ${hh}:${min}${tz ? ` ${tz}` : ""}`;
2840
}
2941

42+
export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone = "utc"): string {
43+
const normalized = zone.trim().toLowerCase();
44+
if (normalized === "utc" || normalized === "gmt") return formatUtcTimestamp(date);
45+
if (normalized === "local" || normalized === "host") return formatZonedTimestamp(date);
46+
return formatZonedTimestamp(date, zone);
47+
}
48+
49+
export function formatLocalEnvelopeTimestamp(date: Date): string {
50+
return formatEnvelopeTimestamp(date, "local");
51+
}
52+
3053
export function escapeRegExp(value: string): string {
3154
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3255
}

0 commit comments

Comments
 (0)