Files
FitOS/app/test/sound.test.ts
2026-09-04 19:06:19 +02:00

51 lines
1.4 KiB
TypeScript

import { describe, it, expect, vi, afterEach } from 'vitest';
import { playAlarm } from '../src/lib/sound';
function mockAudioContext() {
const start = vi.fn();
const stop = vi.fn();
const connect = vi.fn().mockReturnThis();
const createOscillator = vi.fn(() => ({
type: 'sine',
frequency: { value: 0 },
connect,
start,
stop,
}));
const createGain = vi.fn(() => ({
gain: { setValueAtTime: vi.fn(), exponentialRampToValueAtTime: vi.fn() },
connect,
}));
const AudioContextMock = vi.fn(() => ({
state: 'running',
currentTime: 0,
destination: {},
resume: vi.fn(),
createOscillator,
createGain,
}));
vi.stubGlobal('AudioContext', AudioContextMock);
return { AudioContextMock, createOscillator, start, stop };
}
describe('sound — playAlarm', () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it('odtwarza trzy beepy przez Web Audio', () => {
const { createOscillator, start, stop } = mockAudioContext();
playAlarm();
expect(createOscillator).toHaveBeenCalledTimes(3);
expect(start).toHaveBeenCalledTimes(3);
expect(stop).toHaveBeenCalledTimes(3);
});
it('nie rzuca błędu, gdy AudioContext jest niedostępny', async () => {
vi.resetModules();
vi.stubGlobal('AudioContext', undefined);
const { playAlarm: pa } = await import('../src/lib/sound');
expect(() => pa()).not.toThrow();
});
});