jevstrudel.git / website / src / jev / audioControls.test.mjs
1import { describe, expect, it } from 'vitest';
2import { fromPage } from './sandboxProtocol.mjs';
3
4// gainOf is plain arithmetic, tested here without importing the audio modules
5const gainOf = (percent) => Math.pow(Math.min(100, Math.max(0, percent)) / 100, 2);
6
7describe('the listener\'s volume', () => {
8  it('is full at 100, silent at 0, and a quarter of the gain at the middle', () => {
9    expect(gainOf(100)).toBe(1);
10    expect(gainOf(0)).toBe(0);
11    expect(gainOf(50)).toBeCloseTo(0.25);
12    expect(gainOf(250)).toBe(1);
13  });
14});
15
16describe('the sandbox\'s volume and pause messages', () => {
17  it('passes a gain between 0 and 1, and nothing else', () => {
18    expect(fromPage({ type: 'volume', value: 0.25 })).toEqual({ type: 'volume', value: 0.25 });
19    expect(fromPage({ type: 'volume', value: 2 })).toBeNull();
20    expect(fromPage({ type: 'volume', value: '1' })).toBeNull();
21    expect(fromPage({ type: 'volume' })).toBeNull();
22  });
23  it('passes pause and unpause bare', () => {
24    expect(fromPage({ type: 'pause', extra: 1 })).toEqual({ type: 'pause' });
25    expect(fromPage({ type: 'unpause' })).toEqual({ type: 'unpause' });
26  });
27});