test: dedupe outbound envelope coverage

This commit is contained in:
Peter Steinberger 2026-03-13 20:03:09 +00:00
parent ddfa6e66c8
commit 7771444725
4 changed files with 95 additions and 69 deletions

View File

@ -0,0 +1,21 @@
import { describe, expect, it } from "vitest";
import { throwIfAborted } from "./abort.js";
describe("throwIfAborted", () => {
it("does nothing when the signal is missing or not aborted", () => {
expect(() => throwIfAborted()).not.toThrow();
expect(() => throwIfAborted(new AbortController().signal)).not.toThrow();
});
it("throws a standard AbortError when the signal is aborted", () => {
const controller = new AbortController();
controller.abort();
expect(() => throwIfAborted(controller.signal)).toThrowError(
expect.objectContaining({
name: "AbortError",
message: "Operation aborted",
}),
);
});
});

View File

@ -0,0 +1,55 @@
import { describe, expect, it } from "vitest";
import type { ReplyPayload } from "../../auto-reply/types.js";
import { buildOutboundResultEnvelope } from "./envelope.js";
import type { OutboundDeliveryJson } from "./format.js";
describe("buildOutboundResultEnvelope", () => {
const delivery: OutboundDeliveryJson = {
channel: "telegram",
via: "direct",
to: "123",
messageId: "m1",
mediaUrl: null,
chatId: "c1",
};
it("flattens delivery by default when nothing else is present", () => {
expect(buildOutboundResultEnvelope({ delivery })).toEqual(delivery);
});
it("keeps pre-normalized payload JSON entries but clones the array", () => {
const payloads = [{ text: "hi", mediaUrl: null, mediaUrls: undefined }];
const envelope = buildOutboundResultEnvelope({
payloads,
meta: { ok: true },
});
expect(envelope).toEqual({
payloads: [{ text: "hi", mediaUrl: null, mediaUrls: undefined }],
meta: { ok: true },
});
expect((envelope as { payloads: unknown[] }).payloads).not.toBe(payloads);
});
it("normalizes reply payloads and keeps wrapped delivery when flattening is disabled", () => {
const payloads: ReplyPayload[] = [{ text: "hello" }];
expect(
buildOutboundResultEnvelope({
payloads,
delivery,
flattenDelivery: false,
}),
).toEqual({
payloads: [
{
text: "hello",
mediaUrl: null,
channelData: undefined,
},
],
delivery,
});
});
});

View File

@ -19,8 +19,6 @@ import {
recoverPendingDeliveries, recoverPendingDeliveries,
} from "./delivery-queue.js"; } from "./delivery-queue.js";
import { DirectoryCache } from "./directory-cache.js"; import { DirectoryCache } from "./directory-cache.js";
import { buildOutboundResultEnvelope } from "./envelope.js";
import type { OutboundDeliveryJson } from "./format.js";
import { import {
buildOutboundDeliveryJson, buildOutboundDeliveryJson,
formatGatewaySummary, formatGatewaySummary,
@ -670,73 +668,6 @@ describe("DirectoryCache", () => {
}); });
}); });
describe("buildOutboundResultEnvelope", () => {
it("formats envelope variants", () => {
const whatsappDelivery: OutboundDeliveryJson = {
channel: "whatsapp",
via: "gateway",
to: "+1",
messageId: "m1",
mediaUrl: null,
};
const telegramDelivery: OutboundDeliveryJson = {
channel: "telegram",
via: "direct",
to: "123",
messageId: "m2",
mediaUrl: null,
chatId: "c1",
};
const discordDelivery: OutboundDeliveryJson = {
channel: "discord",
via: "gateway",
to: "channel:C1",
messageId: "m3",
mediaUrl: null,
channelId: "C1",
};
const cases = typedCases<{
name: string;
input: Parameters<typeof buildOutboundResultEnvelope>[0];
expected: unknown;
}>([
{
name: "flatten delivery by default",
input: { delivery: whatsappDelivery },
expected: whatsappDelivery,
},
{
name: "keep payloads + meta",
input: {
payloads: [{ text: "hi", mediaUrl: null, mediaUrls: undefined }],
meta: { foo: "bar" },
},
expected: {
payloads: [{ text: "hi", mediaUrl: null, mediaUrls: undefined }],
meta: { foo: "bar" },
},
},
{
name: "include delivery when payloads exist",
input: { payloads: [], delivery: telegramDelivery, meta: { ok: true } },
expected: {
payloads: [],
meta: { ok: true },
delivery: telegramDelivery,
},
},
{
name: "keep wrapped delivery when flatten disabled",
input: { delivery: discordDelivery, flattenDelivery: false },
expected: { delivery: discordDelivery },
},
]);
for (const testCase of cases) {
expect(buildOutboundResultEnvelope(testCase.input), testCase.name).toEqual(testCase.expected);
}
});
});
describe("formatOutboundDeliverySummary", () => { describe("formatOutboundDeliverySummary", () => {
it("formats fallback and channel-specific detail variants", () => { it("formats fallback and channel-specific detail variants", () => {
const cases = [ const cases = [

19
src/infra/ws.test.ts Normal file
View File

@ -0,0 +1,19 @@
import { Buffer } from "node:buffer";
import { describe, expect, it } from "vitest";
import { rawDataToString } from "./ws.js";
describe("rawDataToString", () => {
it("returns string input unchanged", () => {
expect(rawDataToString("hello")).toBe("hello");
});
it("decodes Buffer, Buffer[] and ArrayBuffer inputs", () => {
expect(rawDataToString(Buffer.from("hello"))).toBe("hello");
expect(rawDataToString([Buffer.from("he"), Buffer.from("llo")])).toBe("hello");
expect(rawDataToString(Uint8Array.from([104, 101, 108, 108, 111]).buffer)).toBe("hello");
});
it("falls back to string coercion for other raw data shapes", () => {
expect(rawDataToString(Uint8Array.from([1, 2, 3]) as never)).toBe("1,2,3");
});
});