dnd-hub/apps/server/test/voiceTranscription.test.ts
2026-03-16 22:15:15 -04:00

97 lines
3.1 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { voiceMonitorService } from "../src/services/voiceMonitorService.js";
import { transcriptionService } from "../src/services/transcriptionService.js";
describe("Voice Transcription", () => {
beforeEach(() => {
// Clean up any existing sessions
});
afterEach(() => {
// Clean up after tests
});
describe("voiceMonitorService", () => {
it("should create and retrieve active voice session", () => {
const guildId = 1;
const voiceChannelId = "voice-channel-123";
const transcriptSessionId = 1;
const campaignId = 1;
const session = voiceMonitorService.startVoiceSession(
guildId,
voiceChannelId,
transcriptSessionId,
campaignId
);
expect(session).toBeDefined();
expect(session.guildId).toBe(guildId);
expect(session.voiceChannelId).toBe(voiceChannelId);
expect(session.transcriptSessionId).toBe(transcriptSessionId);
const retrieved = voiceMonitorService.getActiveVoiceSession(guildId);
expect(retrieved).not.toBeNull();
expect(retrieved?.voiceChannelId).toBe(voiceChannelId);
});
it("should throw error when starting duplicate session", () => {
const guildId = 2;
const voiceChannelId = "voice-channel-456";
const transcriptSessionId = 2;
const campaignId = 1;
voiceMonitorService.startVoiceSession(guildId, voiceChannelId, transcriptSessionId, campaignId);
expect(() => {
voiceMonitorService.startVoiceSession(guildId, voiceChannelId, transcriptSessionId, campaignId);
}).toThrow("Voice session already active");
});
it("should stop voice session", () => {
const guildId = 3;
const voiceChannelId = "voice-channel-789";
const transcriptSessionId = 3;
const campaignId = 1;
voiceMonitorService.startVoiceSession(guildId, voiceChannelId, transcriptSessionId, campaignId);
const stopped = voiceMonitorService.stopVoiceSession(guildId);
expect(stopped).not.toBeNull();
expect(stopped?.voiceChannelId).toBe(voiceChannelId);
const retrieved = voiceMonitorService.getActiveVoiceSession(guildId);
expect(retrieved).toBeNull();
});
it("should return empty participants list when no participants", () => {
const participants = voiceMonitorService.getParticipants(999);
expect(participants).toEqual([]);
});
});
describe("transcriptionService", () => {
it("should clear user buffer without error", () => {
expect(() => {
transcriptionService.clearUserBuffer("test-user-123");
}).not.toThrow();
});
it("should handle empty audio buffer", async () => {
const emptyBuffer = Buffer.alloc(0);
await expect(transcriptionService.transcribeAudioFile(emptyBuffer))
.rejects
.toThrow();
});
it("should fail transcription without API key", async () => {
const audioBuffer = Buffer.from([0x00, 0x01, 0x02]);
await expect(transcriptionService.transcribeAudioFile(audioBuffer))
.rejects
.toThrow(/OpenAI API key not configured/);
});
});
});