Files
Moltbot/src/telegram/voice.test.ts
Azade 🐐 1b95220a99 fix(media): recognize MP3 and M4A as voice-compatible audio (#15438)
* fix(media): recognize MP3 and M4A as voice-compatible audio

Telegram sendVoice supports OGG/Opus, MP3, and M4A, but
isVoiceCompatibleAudio only recognized OGG/Opus formats.

- Add MP3 and M4A extensions and MIME types
- Use explicit MIME set instead of substring matching
- Handle MIME parameters (e.g. 'audio/ogg; codecs=opus')
- Add test coverage for all supported and unsupported formats

* fix: narrow MIME allowlist per review feedback

Remove audio/mp4 and audio/aac from voice MIME types — too broad.
Keep only M4A-specific types (audio/x-m4a, audio/m4a).
Add audio/mp4 and audio/aac as negative test cases.

* fix: align voice compatibility and channel coverage (#15438) (thanks @azade-c)

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-02-14 03:03:02 +01:00

58 lines
1.7 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { resolveTelegramVoiceSend } from "./voice.js";
describe("resolveTelegramVoiceSend", () => {
it("skips voice when wantsVoice is false", () => {
const logFallback = vi.fn();
const result = resolveTelegramVoiceSend({
wantsVoice: false,
contentType: "audio/ogg",
fileName: "voice.ogg",
logFallback,
});
expect(result.useVoice).toBe(false);
expect(logFallback).not.toHaveBeenCalled();
});
it("logs fallback for incompatible media", () => {
const logFallback = vi.fn();
const result = resolveTelegramVoiceSend({
wantsVoice: true,
contentType: "audio/wav",
fileName: "track.wav",
logFallback,
});
expect(result.useVoice).toBe(false);
expect(logFallback).toHaveBeenCalledWith(
"Telegram voice requested but media is audio/wav (track.wav); sending as audio file instead.",
);
});
it("keeps voice when compatible", () => {
const logFallback = vi.fn();
const result = resolveTelegramVoiceSend({
wantsVoice: true,
contentType: "audio/ogg",
fileName: "voice.ogg",
logFallback,
});
expect(result.useVoice).toBe(true);
expect(logFallback).not.toHaveBeenCalled();
});
it.each([
{ contentType: "audio/mpeg", fileName: "track.mp3" },
{ contentType: "audio/mp4", fileName: "track.m4a" },
])("keeps voice for compatible MIME $contentType", ({ contentType, fileName }) => {
const logFallback = vi.fn();
const result = resolveTelegramVoiceSend({
wantsVoice: true,
contentType,
fileName,
logFallback,
});
expect(result.useVoice).toBe(true);
expect(logFallback).not.toHaveBeenCalled();
});
});