




















@@ -200,6 +200,31 @@ function makeRemoveAckAfterReplyParams() {
200200};
201201}
202202203+function firstTranscriptionContext(): Record<string, unknown> {
204+const call = transcribeFirstAudioMock.mock.calls[0]?.[0] as
205+| { ctx?: Record<string, unknown> }
206+| undefined;
207+if (!call?.ctx) {
208+throw new Error("expected transcribeFirstAudio ctx");
209+}
210+return call.ctx;
211+}
212+213+function firstDispatchContext(): Record<string, unknown> {
214+const calls = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls as unknown[][];
215+const dispatch = calls[0]?.[0] as { context?: Record<string, unknown> } | undefined;
216+if (!dispatch?.context) {
217+throw new Error("expected WhatsApp dispatch context");
218+}
219+return dispatch.context;
220+}
221+222+function expectContextFields(context: Record<string, unknown>, fields: Record<string, unknown>) {
223+for (const [key, value] of Object.entries(fields)) {
224+expect(context[key]).toEqual(value);
225+}
226+}
227+203228describe("processMessage audio preflight transcription", () => {
204229beforeEach(() => {
205230transcribeFirstAudioMock.mockReset();
@@ -216,24 +241,20 @@ describe("processMessage audio preflight transcription", () => {
216241await processMessage(makeParams());
217242218243expect(transcribeFirstAudioMock).toHaveBeenCalledTimes(1);
219-expect(transcribeFirstAudioMock).toHaveBeenCalledWith(
220-expect.objectContaining({
221-ctx: expect.objectContaining({
222-AccountId: "default",
223-From: "+15550000002",
224-MediaPaths: ["/tmp/voice.ogg"],
225-MediaTypes: ["audio/ogg; codecs=opus"],
226-OriginatingChannel: "whatsapp",
227-OriginatingTo: "+15550000002",
228-Provider: "whatsapp",
229-Surface: "whatsapp",
230-To: "+15550000001",
231-}),
232-}),
233-);
244+expectContextFields(firstTranscriptionContext(), {
245+AccountId: "default",
246+From: "+15550000002",
247+MediaPaths: ["/tmp/voice.ogg"],
248+MediaTypes: ["audio/ogg; codecs=opus"],
249+OriginatingChannel: "whatsapp",
250+OriginatingTo: "+15550000002",
251+Provider: "whatsapp",
252+Surface: "whatsapp",
253+To: "+15550000001",
254+});
234255235-const dispatchCall = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls[0]?.[0];
236-expect(dispatchCall?.context).toMatchObject({
256+const context = firstDispatchContext();
257+expectContextFields(context, {
237258Body: "okay let's test this voice message",
238259BodyForAgent: "okay let's test this voice message",
239260CommandBody: "<media:audio>",
@@ -243,7 +264,7 @@ describe("processMessage audio preflight transcription", () => {
243264});
244265// mediaPath and mediaType must be preserved so inboundAudio detection (used by
245266// features like messages.tts.auto: "inbound") still recognises this as audio.
246-expect(dispatchCall?.context).toMatchObject({
267+expectContextFields(context, {
247268MediaPath: "/tmp/voice.ogg",
248269MediaType: "audio/ogg; codecs=opus",
249270});
@@ -256,8 +277,7 @@ describe("processMessage audio preflight transcription", () => {
256277257278expect(transcribeFirstAudioMock).toHaveBeenCalledTimes(1);
258279259-const dispatchCall = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls[0]?.[0];
260-expect(dispatchCall?.context).toMatchObject({
280+expectContextFields(firstDispatchContext(), {
261281Body: "<media:audio>",
262282BodyForAgent: "<media:audio>",
263283});
@@ -270,8 +290,7 @@ describe("processMessage audio preflight transcription", () => {
270290271291expect(transcribeFirstAudioMock).toHaveBeenCalledTimes(1);
272292273-const dispatchCall = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls[0]?.[0];
274-expect(dispatchCall?.context).toMatchObject({
293+expectContextFields(firstDispatchContext(), {
275294Body: "<media:audio>",
276295BodyForAgent: "<media:audio>",
277296});
@@ -305,8 +324,7 @@ describe("processMessage audio preflight transcription", () => {
305324expect(transcribeFirstAudioMock).not.toHaveBeenCalled();
306325307326// Body passes through as-is without a mediaType to confirm audio
308-const dispatchCall = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls[0]?.[0];
309-expect(dispatchCall?.context).toMatchObject({
327+expectContextFields(firstDispatchContext(), {
310328Body: "<media:audio>",
311329});
312330});
@@ -318,8 +336,7 @@ describe("processMessage audio preflight transcription", () => {
318336319337expect(shouldComputeCommandBodies).toEqual(["<media:audio>"]);
320338321-const dispatchCall = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls[0]?.[0];
322-expect(dispatchCall?.context).toMatchObject({
339+expectContextFields(firstDispatchContext(), {
323340Body: "/new start a new session",
324341BodyForAgent: "/new start a new session",
325342CommandBody: "<media:audio>",
@@ -339,8 +356,7 @@ describe("processMessage audio preflight transcription", () => {
339356340357expect(transcribeFirstAudioMock).not.toHaveBeenCalled();
341358342-const dispatchCall = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls[0]?.[0];
343-expect(dispatchCall?.context).toMatchObject({
359+expectContextFields(firstDispatchContext(), {
344360Body: "pre-computed transcript from fan-out caller",
345361BodyForAgent: "pre-computed transcript from fan-out caller",
346362CommandBody: "<media:audio>",
@@ -419,8 +435,7 @@ describe("processMessage audio preflight transcription", () => {
419435expect(transcribeFirstAudioMock).not.toHaveBeenCalled();
420436421437// Body falls back to the original <media:audio> placeholder, not retried transcript.
422-const dispatchCall = vi.mocked(dispatchWhatsAppBufferedReply).mock.calls[0]?.[0];
423-expect(dispatchCall?.context).toMatchObject({
438+expectContextFields(firstDispatchContext(), {
424439Body: "<media:audio>",
425440});
426441});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。