File size: 9,800 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/** Tests inbound envelope formatting, timestamps, and sender labels. */
import { describe, expect, it, vi } from "vitest";
import {
  formatAgentEnvelope,
  formatAgentEnvelopeTimestamp,
  formatInboundEnvelope,
  resolveEnvelopeFormatOptions,
} from "./envelope.js";

describe("formatAgentEnvelope", () => {
  it("includes channel, from, ip, host, and timestamp", () => {
    const ts = Date.UTC(2025, 0, 2, 3, 4, 5); // 2025-01-02T03:04:05Z
    const body = formatAgentEnvelope({
      channel: "WebChat",
      from: "user1",
      host: "mac-mini",
      ip: "10.0.0.5",
      timestamp: ts,
      envelope: { timezone: "utc" },
      body: "hello",
    });

    expect(body).toBe("[WebChat user1 mac-mini 10.0.0.5 Thu 2025-01-02T03:04:05Z] hello");
  });

  it("formats timestamps in local timezone by default", () => {
    const ts = Date.UTC(2025, 0, 2, 3, 4);
    const expectedTimestamp = formatAgentEnvelopeTimestamp(ts, { timezone: "local" });
    const body = formatAgentEnvelope({
      channel: "WebChat",
      timestamp: ts,
      body: "hello",
    });

    expect(body).toBe(`[WebChat ${expectedTimestamp}] hello`);
  });

  it("formats timestamps in UTC across date boundaries", () => {
    for (const [timestamp, timezone, expected] of [
      ["2025-01-02T03:04:05Z", "utc", "Thu 2025-01-02T03:04:05Z"],
      ["2024-12-31T23:59:59Z", "utc", "Tue 2024-12-31T23:59:59Z"],
      ["2025-01-01T00:00:00Z", "gmt", "Wed 2025-01-01T00:00:00Z"],
      ["2025-01-03T00:00:00Z", "utc", "Fri 2025-01-03T00:00:00Z"],
    ] as const) {
      expect(
        formatAgentEnvelope({
          channel: "WebChat",
          timestamp: new Date(timestamp),
          envelope: { timezone },
          body: "hello",
        }),
      ).toBe(`[WebChat ${expected}] hello`);
    }
  });

  it("preserves UTC fallback and recovery when Intl is replaced or fails", () => {
    const render = (timestamp: string) =>
      formatAgentEnvelope({
        channel: "WebChat",
        timestamp: new Date(timestamp),
        envelope: { timezone: "utc" },
        body: "hello",
      });
    expect(render("2025-01-02T03:04:05Z")).toBe("[WebChat Thu 2025-01-02T03:04:05Z] hello");

    const OriginalDateTimeFormat = Intl.DateTimeFormat;
    let constructorFails = true;
    let newFormatterFails = true;
    let formatFails = false;
    const replacement = vi
      .spyOn(Intl, "DateTimeFormat")
      .mockImplementation(function (locales, options) {
        if (constructorFails) {
          throw new Error("weekday constructor unavailable");
        }
        const formatter = new OriginalDateTimeFormat(locales, options);
        const nativeFormat = formatter.format.bind(formatter);
        const failedAtCreation = newFormatterFails;
        Object.defineProperty(formatter, "format", {
          value: (date?: Date | number) => {
            if (failedAtCreation || formatFails) {
              throw new Error("weekday formatting unavailable");
            }
            return `replacement-${nativeFormat(date)}`;
          },
        });
        return formatter;
      });
    try {
      expect(render("2025-01-02T03:04:05Z")).toBe("[WebChat 2025-01-02T03:04:05Z] hello");
      constructorFails = false;
      expect(render("2025-01-02T03:04:05Z")).toBe("[WebChat 2025-01-02T03:04:05Z] hello");

      newFormatterFails = false;
      expect(render("2025-01-03T00:00:00Z")).toBe(
        "[WebChat replacement-Fri 2025-01-03T00:00:00Z] hello",
      );
      formatFails = true;
      expect(render("2025-01-03T00:00:00Z")).toBe("[WebChat 2025-01-03T00:00:00Z] hello");
      formatFails = false;
      expect(render("2025-01-01T00:00:00Z")).toBe(
        "[WebChat replacement-Wed 2025-01-01T00:00:00Z] hello",
      );
    } finally {
      replacement.mockRestore();
    }
    expect(render("2025-01-01T00:00:00Z")).toBe("[WebChat Wed 2025-01-01T00:00:00Z] hello");
  });

  it("formats timestamps in user timezone when configured", () => {
    const ts = Date.UTC(2025, 0, 2, 3, 4, 5); // 2025-01-02T03:04:05Z (04:04:05 CET)
    const body = formatAgentEnvelope({
      channel: "WebChat",
      timestamp: ts,
      envelope: { timezone: "user", userTimezone: "Europe/Vienna" },
      body: "hello",
    });

    expect(body).toMatch(/\[WebChat Thu 2025-01-02 04:04:05 [^\]]+\] hello/);
  });

  it("falls back to the host timezone for an invalid configured user timezone", () => {
    const ts = Date.UTC(2025, 0, 2, 3, 4, 5);
    const options = resolveEnvelopeFormatOptions({
      agents: { defaults: { userTimezone: "Not/A_Timezone" } },
    });
    expect(options.timezone).toBe("local");
    expect(formatAgentEnvelopeTimestamp(ts, options)).toBe(
      formatAgentEnvelopeTimestamp(ts, { timezone: "local" }),
    );
  });

  it("keeps the UTC fallback for an invalid explicit timezone option", () => {
    const ts = Date.UTC(2025, 0, 2, 3, 4, 5);
    expect(formatAgentEnvelopeTimestamp(ts, { timezone: "Not/A_Timezone" })).toBe(
      formatAgentEnvelopeTimestamp(ts, { timezone: "utc" }),
    );
  });

  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");
  });

  it("formats the Unix epoch timestamp", () => {
    const body = formatAgentEnvelope({
      channel: "WebChat",
      timestamp: 0,
      envelope: { timezone: "utc" },
      body: "hello",
    });
    expect(body).toBe("[WebChat Thu 1970-01-01T00:00:00Z] hello");
  });
});

describe("formatInboundEnvelope", () => {
  it("prefixes sender for non-direct chats", () => {
    const body = formatInboundEnvelope({
      channel: "Discord",
      from: "Guild #general",
      body: "hi",
      chatType: "channel",
      senderLabel: "Alice",
    });
    expect(body).toBe("[Discord Guild #general] Alice: hi");
  });

  it("uses sender fields when senderLabel is missing", () => {
    const body = formatInboundEnvelope({
      channel: "Signal",
      from: "Signal Group id:123",
      body: "ping",
      chatType: "group",
      sender: { name: "Bob", id: "42" },
    });
    expect(body).toBe("[Signal Signal Group id:123] Bob (42): ping");
  });

  it("prefixes direct messages with the header sender", () => {
    const body = formatInboundEnvelope({
      channel: "iMessage",
      from: "+1555",
      body: "hello",
      chatType: "direct",
      senderLabel: "Alice",
    });
    expect(body).toBe("[iMessage +1555] +1555: hello");
  });

  it("uses display text for direct body prefixes when from includes an id", () => {
    const body = formatInboundEnvelope({
      channel: "Telegram",
      from: "Alice id:123",
      body: "hello",
      chatType: "direct",
    });
    expect(body).toBe("[Telegram Alice id:123] Alice: hello");
  });

  it("uses a stable direct body prefix when id display text contains a colon", () => {
    const body = formatInboundEnvelope({
      channel: "Telegram",
      from: "Ops: Alice id:123",
      body: "/status",
      chatType: "direct",
    });
    expect(body).toBe("[Telegram Ops: Alice id:123] (sender): /status");
  });

  it("uses a stable direct body prefix when from is an opaque id label", () => {
    const body = formatInboundEnvelope({
      channel: "LINE",
      from: "user:U123",
      body: "hello",
      chatType: "direct",
    });
    expect(body).toBe("[LINE user:U123] (sender): 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] Alice: follow-up message");
  });

  it("prefixes DM body with (self) when fromMe is true", () => {
    const body = formatInboundEnvelope({
      channel: "WhatsApp",
      from: "+1555",
      body: "outbound msg",
      chatType: "direct",
      fromMe: true,
    });
    expect(body).toBe("[WhatsApp +1555] (self): outbound msg");
  });

  it("does not prefix group messages with (self) when fromMe is true", () => {
    const body = formatInboundEnvelope({
      channel: "WhatsApp",
      from: "Family Chat",
      body: "hello",
      chatType: "group",
      senderLabel: "Alice",
      fromMe: true,
    });
    expect(body).toBe("[WhatsApp Family Chat] Alice: hello");
  });

  it("uses fixed envelope options while preserving the user timezone", () => {
    const options = resolveEnvelopeFormatOptions({
      agents: {
        defaults: {
          envelopeTimezone: "user",
          envelopeTimestamp: "off",
          envelopeElapsed: "off",
          userTimezone: "Europe/Vienna",
        },
      },
    });
    expect(options).toEqual({
      timezone: "Europe/Vienna",
      includeTimestamp: true,
      includeElapsed: true,
      userTimezone: "Europe/Vienna",
    });
  });
});