test(channels): dedupe inbound contract dispatch capture setup

This commit is contained in:
Peter Steinberger
2026-02-18 13:12:22 +00:00
parent 39881a318a
commit e1b491d961
3 changed files with 41 additions and 27 deletions

View File

@@ -1,15 +1,12 @@
import { describe, expect, it, vi } from "vitest";
import { buildDispatchInboundCaptureMock } from "../../../test/helpers/dispatch-inbound-capture.js";
import { buildDispatchInboundContextCapture } from "../../../test/helpers/inbound-contract-capture.js";
import { expectInboundContextContract } from "../../../test/helpers/inbound-contract.js";
import type { MsgContext } from "../../auto-reply/templating.js";
let capturedCtx: MsgContext | undefined;
const capture = vi.hoisted(() => ({ ctx: undefined as MsgContext | undefined }));
vi.mock("../../auto-reply/dispatch.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../auto-reply/dispatch.js")>();
return buildDispatchInboundCaptureMock(actual, (ctx) => {
capturedCtx = ctx as MsgContext;
});
return await buildDispatchInboundContextCapture(importOriginal, capture);
});
import { createSignalEventHandler } from "./event-handler.js";
@@ -20,7 +17,7 @@ import {
describe("signal createSignalEventHandler inbound contract", () => {
it("passes a finalized MsgContext to dispatchInboundMessage", async () => {
capturedCtx = undefined;
capture.ctx = undefined;
const handler = createSignalEventHandler(
createBaseSignalEventHandlerDeps({
@@ -40,9 +37,9 @@ describe("signal createSignalEventHandler inbound contract", () => {
}),
);
expect(capturedCtx).toBeTruthy();
expectInboundContextContract(capturedCtx!);
const contextWithBody = capturedCtx as unknown as { Body?: string };
expect(capture.ctx).toBeTruthy();
expectInboundContextContract(capture.ctx!);
const contextWithBody = capture.ctx as unknown as { Body?: string };
// Sender should appear as prefix in group messages (no redundant [from:] suffix)
expect(String(contextWithBody.Body ?? "")).toContain("Alice");
expect(String(contextWithBody.Body ?? "")).toMatch(/Alice.*:/);
@@ -50,7 +47,7 @@ describe("signal createSignalEventHandler inbound contract", () => {
});
it("normalizes direct chat To/OriginatingTo targets to canonical Signal ids", async () => {
capturedCtx = undefined;
capture.ctx = undefined;
const handler = createSignalEventHandler(
createBaseSignalEventHandlerDeps({
@@ -72,8 +69,8 @@ describe("signal createSignalEventHandler inbound contract", () => {
}),
);
expect(capturedCtx).toBeTruthy();
const context = capturedCtx as unknown as {
expect(capture.ctx).toBeTruthy();
const context = capture.ctx as unknown as {
ChatType?: string;
To?: string;
OriginatingTo?: string;