feat: make inbound envelopes configurable
Co-authored-by: Shiva Prasad <shiv19@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { formatAgentEnvelope, formatInboundEnvelope } from "./envelope.js";
|
||||
import {
|
||||
formatAgentEnvelope,
|
||||
formatInboundEnvelope,
|
||||
resolveEnvelopeFormatOptions,
|
||||
} from "./envelope.js";
|
||||
|
||||
describe("formatAgentEnvelope", () => {
|
||||
it("includes channel, from, ip, host, and timestamp", () => {
|
||||
@@ -38,6 +42,46 @@ describe("formatAgentEnvelope", () => {
|
||||
expect(body).toBe("[WebChat 2025-01-02T03:04Z] hello");
|
||||
});
|
||||
|
||||
it("formats timestamps in local timezone when configured", () => {
|
||||
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 (19:04 PST)
|
||||
const body = formatAgentEnvelope({
|
||||
channel: "WebChat",
|
||||
timestamp: ts,
|
||||
envelope: { timezone: "local" },
|
||||
body: "hello",
|
||||
});
|
||||
|
||||
process.env.TZ = originalTz;
|
||||
|
||||
expect(body).toMatch(/\[WebChat 2025-01-01 19:04 [^\]]+\] hello/);
|
||||
});
|
||||
|
||||
it("formats timestamps in user timezone when configured", () => {
|
||||
const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z (04:04 CET)
|
||||
const body = formatAgentEnvelope({
|
||||
channel: "WebChat",
|
||||
timestamp: ts,
|
||||
envelope: { timezone: "user", userTimezone: "Europe/Vienna" },
|
||||
body: "hello",
|
||||
});
|
||||
|
||||
expect(body).toMatch(/\[WebChat 2025-01-02 04:04 [^\]]+\] hello/);
|
||||
});
|
||||
|
||||
it("omits timestamps when configured", () => {
|
||||
const ts = Date.UTC(2025, 0, 2, 3, 4);
|
||||
const body = formatAgentEnvelope({
|
||||
channel: "WebChat",
|
||||
timestamp: ts,
|
||||
envelope: { includeTimestamp: false },
|
||||
body: "hello",
|
||||
});
|
||||
expect(body).toBe("[WebChat] hello");
|
||||
});
|
||||
|
||||
it("handles missing optional fields", () => {
|
||||
const body = formatAgentEnvelope({ channel: "Telegram", body: "hi" });
|
||||
expect(body).toBe("[Telegram] hi");
|
||||
@@ -77,4 +121,53 @@ describe("formatInboundEnvelope", () => {
|
||||
});
|
||||
expect(body).toBe("[iMessage +1555] hello");
|
||||
});
|
||||
|
||||
it("includes elapsed time when previousTimestamp is provided", () => {
|
||||
const now = Date.now();
|
||||
const twoMinutesAgo = now - 2 * 60 * 1000;
|
||||
const body = formatInboundEnvelope({
|
||||
channel: "Telegram",
|
||||
from: "Alice",
|
||||
body: "follow-up message",
|
||||
timestamp: now,
|
||||
previousTimestamp: twoMinutesAgo,
|
||||
chatType: "direct",
|
||||
envelope: { includeTimestamp: false },
|
||||
});
|
||||
expect(body).toContain("Alice +2m");
|
||||
expect(body).toContain("follow-up message");
|
||||
});
|
||||
|
||||
it("omits elapsed time when disabled", () => {
|
||||
const now = Date.now();
|
||||
const body = formatInboundEnvelope({
|
||||
channel: "Telegram",
|
||||
from: "Alice",
|
||||
body: "follow-up message",
|
||||
timestamp: now,
|
||||
previousTimestamp: now - 2 * 60 * 1000,
|
||||
chatType: "direct",
|
||||
envelope: { includeElapsed: false, includeTimestamp: false },
|
||||
});
|
||||
expect(body).toBe("[Telegram Alice] follow-up message");
|
||||
});
|
||||
|
||||
it("resolves envelope options from config", () => {
|
||||
const options = resolveEnvelopeFormatOptions({
|
||||
agents: {
|
||||
defaults: {
|
||||
envelopeTimezone: "user",
|
||||
envelopeTimestamp: "off",
|
||||
envelopeElapsed: "off",
|
||||
userTimezone: "Europe/Vienna",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(options).toEqual({
|
||||
timezone: "user",
|
||||
includeTimestamp: false,
|
||||
includeElapsed: false,
|
||||
userTimezone: "Europe/Vienna",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user