fix: use local timestamps in agent envelope
This commit is contained in:
@@ -4,6 +4,9 @@ import { formatAgentEnvelope } from "./envelope.js";
|
|||||||
|
|
||||||
describe("formatAgentEnvelope", () => {
|
describe("formatAgentEnvelope", () => {
|
||||||
it("includes surface, from, ip, host, and timestamp", () => {
|
it("includes surface, from, ip, host, and timestamp", () => {
|
||||||
|
const originalTz = process.env.TZ;
|
||||||
|
process.env.TZ = "UTC";
|
||||||
|
|
||||||
const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z
|
const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z
|
||||||
const body = formatAgentEnvelope({
|
const body = formatAgentEnvelope({
|
||||||
surface: "WebChat",
|
surface: "WebChat",
|
||||||
@@ -13,8 +16,29 @@ describe("formatAgentEnvelope", () => {
|
|||||||
timestamp: ts,
|
timestamp: ts,
|
||||||
body: "hello",
|
body: "hello",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
process.env.TZ = originalTz;
|
||||||
|
|
||||||
|
expect(body).toMatch(
|
||||||
|
/^\[WebChat user1 mac-mini 10\.0\.0\.5 2025-01-02T03:04\+00:00\{.+\}\] hello$/,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("formats timestamps in local time (not UTC)", () => {
|
||||||
|
const originalTz = process.env.TZ;
|
||||||
|
process.env.TZ = "America/Los_Angeles";
|
||||||
|
|
||||||
|
const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z
|
||||||
|
const body = formatAgentEnvelope({
|
||||||
|
surface: "WebChat",
|
||||||
|
timestamp: ts,
|
||||||
|
body: "hello",
|
||||||
|
});
|
||||||
|
|
||||||
|
process.env.TZ = originalTz;
|
||||||
|
|
||||||
expect(body).toBe(
|
expect(body).toBe(
|
||||||
"[WebChat user1 mac-mini 10.0.0.5 2025-01-02 03:04] hello",
|
"[WebChat 2025-01-01T19:04-08:00{America/Los_Angeles}] hello",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,26 @@ function formatTimestamp(ts?: number | Date): string | undefined {
|
|||||||
if (!ts) return undefined;
|
if (!ts) return undefined;
|
||||||
const date = ts instanceof Date ? ts : new Date(ts);
|
const date = ts instanceof Date ? ts : new Date(ts);
|
||||||
if (Number.isNaN(date.getTime())) return undefined;
|
if (Number.isNaN(date.getTime())) return undefined;
|
||||||
// Compact ISO-like format with minutes precision.
|
|
||||||
return date.toISOString().slice(0, 16).replace("T", " ");
|
const yyyy = String(date.getFullYear()).padStart(4, "0");
|
||||||
|
const mm = String(date.getMonth() + 1).padStart(2, "0");
|
||||||
|
const dd = String(date.getDate()).padStart(2, "0");
|
||||||
|
const hh = String(date.getHours()).padStart(2, "0");
|
||||||
|
const min = String(date.getMinutes()).padStart(2, "0");
|
||||||
|
|
||||||
|
// getTimezoneOffset() is minutes *behind* UTC. Flip sign to get ISO offset.
|
||||||
|
const offsetMinutes = -date.getTimezoneOffset();
|
||||||
|
const sign = offsetMinutes >= 0 ? "+" : "-";
|
||||||
|
const absOffsetMinutes = Math.abs(offsetMinutes);
|
||||||
|
const offsetH = String(Math.floor(absOffsetMinutes / 60)).padStart(2, "0");
|
||||||
|
const offsetM = String(absOffsetMinutes % 60).padStart(2, "0");
|
||||||
|
|
||||||
|
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
const tzSuffix = tz ? `{${tz}}` : "";
|
||||||
|
|
||||||
|
// Compact ISO-like *local* timestamp with minutes precision.
|
||||||
|
// Example: 2025-01-02T03:04-08:00{America/Los_Angeles}
|
||||||
|
return `${yyyy}-${mm}-${dd}T${hh}:${min}${sign}${offsetH}:${offsetM}${tzSuffix}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatAgentEnvelope(params: AgentEnvelopeParams): string {
|
export function formatAgentEnvelope(params: AgentEnvelopeParams): string {
|
||||||
|
|||||||
@@ -48,40 +48,47 @@ describe("createTelegramBot", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("wraps inbound message with Telegram envelope", async () => {
|
it("wraps inbound message with Telegram envelope", async () => {
|
||||||
onSpy.mockReset();
|
const originalTz = process.env.TZ;
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<
|
process.env.TZ = "Europe/Vienna";
|
||||||
typeof vi.fn
|
|
||||||
>;
|
|
||||||
replySpy.mockReset();
|
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
try {
|
||||||
expect(onSpy).toHaveBeenCalledWith("message", expect.any(Function));
|
onSpy.mockReset();
|
||||||
const handler = onSpy.mock.calls[0][1] as (
|
const replySpy = replyModule.__replySpy as unknown as ReturnType<
|
||||||
ctx: Record<string, unknown>,
|
typeof vi.fn
|
||||||
) => Promise<void>;
|
>;
|
||||||
|
replySpy.mockReset();
|
||||||
|
|
||||||
const message = {
|
createTelegramBot({ token: "tok" });
|
||||||
chat: { id: 1234, type: "private" },
|
expect(onSpy).toHaveBeenCalledWith("message", expect.any(Function));
|
||||||
text: "hello world",
|
const handler = onSpy.mock.calls[0][1] as (
|
||||||
date: 1736380800, // 2025-01-09T00:00:00Z
|
ctx: Record<string, unknown>,
|
||||||
from: {
|
) => Promise<void>;
|
||||||
first_name: "Ada",
|
|
||||||
last_name: "Lovelace",
|
|
||||||
username: "ada_bot",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
await handler({
|
|
||||||
message,
|
|
||||||
me: { username: "clawdis_bot" },
|
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
const message = {
|
||||||
const payload = replySpy.mock.calls[0][0];
|
chat: { id: 1234, type: "private" },
|
||||||
expect(payload.Body).toMatch(
|
text: "hello world",
|
||||||
/^\[Telegram Ada Lovelace \(@ada_bot\) id:1234 2025-01-09 00:00]/,
|
date: 1736380800, // 2025-01-09T00:00:00Z
|
||||||
);
|
from: {
|
||||||
expect(payload.Body).toContain("hello world");
|
first_name: "Ada",
|
||||||
|
last_name: "Lovelace",
|
||||||
|
username: "ada_bot",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
await handler({
|
||||||
|
message,
|
||||||
|
me: { username: "clawdis_bot" },
|
||||||
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||||
|
const payload = replySpy.mock.calls[0][0];
|
||||||
|
expect(payload.Body).toMatch(
|
||||||
|
/^\[Telegram Ada Lovelace \(@ada_bot\) id:1234 2025-01-09T01:00\+01:00\{Europe\/Vienna\}\]/,
|
||||||
|
);
|
||||||
|
expect(payload.Body).toContain("hello world");
|
||||||
|
} finally {
|
||||||
|
process.env.TZ = originalTz;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("triggers typing cue via onReplyStart", async () => {
|
it("triggers typing cue via onReplyStart", async () => {
|
||||||
|
|||||||
@@ -868,6 +868,9 @@ describe("web auto-reply", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("processes inbound messages without batching and preserves timestamps", async () => {
|
it("processes inbound messages without batching and preserves timestamps", async () => {
|
||||||
|
const originalTz = process.env.TZ;
|
||||||
|
process.env.TZ = "Europe/Vienna";
|
||||||
|
|
||||||
const originalMax = process.getMaxListeners();
|
const originalMax = process.getMaxListeners();
|
||||||
process.setMaxListeners?.(1); // force low to confirm bump
|
process.setMaxListeners?.(1); // force low to confirm bump
|
||||||
|
|
||||||
@@ -875,72 +878,75 @@ describe("web auto-reply", () => {
|
|||||||
main: { sessionId: "sid", updatedAt: Date.now() },
|
main: { sessionId: "sid", updatedAt: Date.now() },
|
||||||
});
|
});
|
||||||
|
|
||||||
const sendMedia = vi.fn();
|
try {
|
||||||
const reply = vi.fn().mockResolvedValue(undefined);
|
const sendMedia = vi.fn();
|
||||||
const sendComposing = vi.fn();
|
const reply = vi.fn().mockResolvedValue(undefined);
|
||||||
const resolver = vi.fn().mockResolvedValue({ text: "ok" });
|
const sendComposing = vi.fn();
|
||||||
|
const resolver = vi.fn().mockResolvedValue({ text: "ok" });
|
||||||
|
|
||||||
let capturedOnMessage:
|
let capturedOnMessage:
|
||||||
| ((msg: import("./inbound.js").WebInboundMessage) => Promise<void>)
|
| ((msg: import("./inbound.js").WebInboundMessage) => Promise<void>)
|
||||||
| undefined;
|
| undefined;
|
||||||
const listenerFactory = async (opts: {
|
const listenerFactory = async (opts: {
|
||||||
onMessage: (
|
onMessage: (
|
||||||
msg: import("./inbound.js").WebInboundMessage,
|
msg: import("./inbound.js").WebInboundMessage,
|
||||||
) => Promise<void>;
|
) => Promise<void>;
|
||||||
}) => {
|
}) => {
|
||||||
capturedOnMessage = opts.onMessage;
|
capturedOnMessage = opts.onMessage;
|
||||||
return { close: vi.fn() };
|
return { close: vi.fn() };
|
||||||
};
|
};
|
||||||
|
|
||||||
setLoadConfigMock(() => ({
|
setLoadConfigMock(() => ({
|
||||||
inbound: {
|
inbound: {
|
||||||
timestampPrefix: "UTC",
|
timestampPrefix: "UTC",
|
||||||
session: { store: store.storePath },
|
session: { store: store.storePath },
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
await monitorWebProvider(false, listenerFactory, false, resolver);
|
await monitorWebProvider(false, listenerFactory, false, resolver);
|
||||||
expect(capturedOnMessage).toBeDefined();
|
expect(capturedOnMessage).toBeDefined();
|
||||||
|
|
||||||
// Two messages from the same sender with fixed timestamps
|
// Two messages from the same sender with fixed timestamps
|
||||||
await capturedOnMessage?.({
|
await capturedOnMessage?.({
|
||||||
body: "first",
|
body: "first",
|
||||||
from: "+1",
|
from: "+1",
|
||||||
to: "+2",
|
to: "+2",
|
||||||
id: "m1",
|
id: "m1",
|
||||||
timestamp: 1735689600000, // Jan 1 2025 00:00:00 UTC
|
timestamp: 1735689600000, // Jan 1 2025 00:00:00 UTC
|
||||||
sendComposing,
|
sendComposing,
|
||||||
reply,
|
reply,
|
||||||
sendMedia,
|
sendMedia,
|
||||||
});
|
});
|
||||||
await capturedOnMessage?.({
|
await capturedOnMessage?.({
|
||||||
body: "second",
|
body: "second",
|
||||||
from: "+1",
|
from: "+1",
|
||||||
to: "+2",
|
to: "+2",
|
||||||
id: "m2",
|
id: "m2",
|
||||||
timestamp: 1735693200000, // Jan 1 2025 01:00:00 UTC
|
timestamp: 1735693200000, // Jan 1 2025 01:00:00 UTC
|
||||||
sendComposing,
|
sendComposing,
|
||||||
reply,
|
reply,
|
||||||
sendMedia,
|
sendMedia,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(resolver).toHaveBeenCalledTimes(2);
|
expect(resolver).toHaveBeenCalledTimes(2);
|
||||||
const firstArgs = resolver.mock.calls[0][0];
|
const firstArgs = resolver.mock.calls[0][0];
|
||||||
const secondArgs = resolver.mock.calls[1][0];
|
const secondArgs = resolver.mock.calls[1][0];
|
||||||
expect(firstArgs.Body).toContain(
|
expect(firstArgs.Body).toContain(
|
||||||
"[WhatsApp +1 2025-01-01 00:00] [clawdis] first",
|
"[WhatsApp +1 2025-01-01T01:00+01:00{Europe/Vienna}] [clawdis] first",
|
||||||
);
|
);
|
||||||
expect(firstArgs.Body).not.toContain("second");
|
expect(firstArgs.Body).not.toContain("second");
|
||||||
expect(secondArgs.Body).toContain(
|
expect(secondArgs.Body).toContain(
|
||||||
"[WhatsApp +1 2025-01-01 01:00] [clawdis] second",
|
"[WhatsApp +1 2025-01-01T02:00+01:00{Europe/Vienna}] [clawdis] second",
|
||||||
);
|
);
|
||||||
expect(secondArgs.Body).not.toContain("first");
|
expect(secondArgs.Body).not.toContain("first");
|
||||||
|
|
||||||
// Max listeners bumped to avoid warnings in multi-instance test runs
|
// Max listeners bumped to avoid warnings in multi-instance test runs
|
||||||
expect(process.getMaxListeners?.()).toBeGreaterThanOrEqual(50);
|
expect(process.getMaxListeners?.()).toBeGreaterThanOrEqual(50);
|
||||||
|
} finally {
|
||||||
process.setMaxListeners?.(originalMax);
|
process.setMaxListeners?.(originalMax);
|
||||||
await store.cleanup();
|
process.env.TZ = originalTz;
|
||||||
|
await store.cleanup();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to text when media send fails", async () => {
|
it("falls back to text when media send fails", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user