File size: 4,928 Bytes
fc93158 | 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 | import { describe, expect, it, vi } from "vitest";
import { buildTelegramMessageContextForTest } from "./bot-message-context.test-harness.js";
const transcribeFirstAudioMock = vi.fn();
const DEFAULT_MODEL = "anthropic/claude-opus-4-5";
const DEFAULT_WORKSPACE = "/tmp/openclaw";
const DEFAULT_MENTION_PATTERN = "\\bbot\\b";
vi.mock("../media-understanding/audio-preflight.js", () => ({
transcribeFirstAudio: (...args: unknown[]) => transcribeFirstAudioMock(...args),
}));
async function buildGroupVoiceContext(params: {
messageId: number;
chatId: number;
title: string;
date: number;
fromId: number;
firstName: string;
fileId: string;
mediaPath: string;
groupDisableAudioPreflight?: boolean;
topicDisableAudioPreflight?: boolean;
}) {
const groupConfig = {
requireMention: true,
...(params.groupDisableAudioPreflight === undefined
? {}
: { disableAudioPreflight: params.groupDisableAudioPreflight }),
};
const topicConfig =
params.topicDisableAudioPreflight === undefined
? undefined
: { disableAudioPreflight: params.topicDisableAudioPreflight };
return buildTelegramMessageContextForTest({
message: {
message_id: params.messageId,
chat: { id: params.chatId, type: "supergroup", title: params.title },
date: params.date,
text: undefined,
from: { id: params.fromId, first_name: params.firstName },
voice: { file_id: params.fileId },
},
allMedia: [{ path: params.mediaPath, contentType: "audio/ogg" }],
options: { forceWasMentioned: true },
cfg: {
agents: { defaults: { model: DEFAULT_MODEL, workspace: DEFAULT_WORKSPACE } },
channels: { telegram: {} },
messages: { groupChat: { mentionPatterns: [DEFAULT_MENTION_PATTERN] } },
},
resolveGroupActivation: () => true,
resolveGroupRequireMention: () => true,
resolveTelegramGroupConfig: () => ({
groupConfig,
topicConfig,
}),
});
}
function expectTranscriptRendered(
ctx: Awaited<ReturnType<typeof buildGroupVoiceContext>>,
transcript: string,
) {
expect(ctx).not.toBeNull();
expect(ctx?.ctxPayload?.BodyForAgent).toBe(transcript);
expect(ctx?.ctxPayload?.Body).toContain(transcript);
expect(ctx?.ctxPayload?.Body).not.toContain("<media:audio>");
}
function expectAudioPlaceholderRendered(ctx: Awaited<ReturnType<typeof buildGroupVoiceContext>>) {
expect(ctx).not.toBeNull();
expect(ctx?.ctxPayload?.Body).toContain("<media:audio>");
}
describe("buildTelegramMessageContext audio transcript body", () => {
it("uses preflight transcript as BodyForAgent for mention-gated group voice messages", async () => {
transcribeFirstAudioMock.mockResolvedValueOnce("hey bot please help");
const ctx = await buildGroupVoiceContext({
messageId: 1,
chatId: -1001234567890,
title: "Test Group",
date: 1700000000,
fromId: 42,
firstName: "Alice",
fileId: "voice-1",
mediaPath: "/tmp/voice.ogg",
});
expect(transcribeFirstAudioMock).toHaveBeenCalledTimes(1);
expectTranscriptRendered(ctx, "hey bot please help");
});
it("skips preflight transcription when disableAudioPreflight is true", async () => {
transcribeFirstAudioMock.mockClear();
const ctx = await buildGroupVoiceContext({
messageId: 2,
chatId: -1001234567891,
title: "Test Group 2",
date: 1700000100,
fromId: 43,
firstName: "Bob",
fileId: "voice-2",
mediaPath: "/tmp/voice2.ogg",
groupDisableAudioPreflight: true,
});
expect(transcribeFirstAudioMock).not.toHaveBeenCalled();
expectAudioPlaceholderRendered(ctx);
});
it("uses topic disableAudioPreflight=false to override group disableAudioPreflight=true", async () => {
transcribeFirstAudioMock.mockResolvedValueOnce("topic override transcript");
const ctx = await buildGroupVoiceContext({
messageId: 3,
chatId: -1001234567892,
title: "Test Group 3",
date: 1700000200,
fromId: 44,
firstName: "Cara",
fileId: "voice-3",
mediaPath: "/tmp/voice3.ogg",
groupDisableAudioPreflight: true,
topicDisableAudioPreflight: false,
});
expect(transcribeFirstAudioMock).toHaveBeenCalledTimes(1);
expectTranscriptRendered(ctx, "topic override transcript");
});
it("uses topic disableAudioPreflight=true to override group disableAudioPreflight=false", async () => {
transcribeFirstAudioMock.mockClear();
const ctx = await buildGroupVoiceContext({
messageId: 4,
chatId: -1001234567893,
title: "Test Group 4",
date: 1700000300,
fromId: 45,
firstName: "Dan",
fileId: "voice-4",
mediaPath: "/tmp/voice4.ogg",
groupDisableAudioPreflight: false,
topicDisableAudioPreflight: true,
});
expect(transcribeFirstAudioMock).not.toHaveBeenCalled();
expectAudioPlaceholderRendered(ctx);
});
});
|