feat(sounds): add stream_watch data-channel protocol helpers

This commit is contained in:
Jannis Braun
2026-04-28 14:19:41 +02:00
parent 40dd2ab52f
commit 6a4eac13a3
2 changed files with 71 additions and 0 deletions
@@ -0,0 +1,36 @@
import { describe, it, expect } from 'vitest';
import {
encodeStreamWatch,
parseStreamWatch,
isStreamWatchPayload,
} from './streamWatchProtocol';
describe('streamWatchProtocol', () => {
it('round-trips an encoded payload', () => {
const payload = { type: 'stream_watch' as const, target: 'user-1', watching: true };
const encoded = encodeStreamWatch(payload);
expect(encoded).toBeDefined();
expect(encoded.length).toBeGreaterThan(0);
const parsed = parseStreamWatch(encoded);
expect(parsed).toEqual(payload);
});
it('parseStreamWatch returns null on invalid JSON', () => {
const bad = new TextEncoder().encode('not json');
expect(parseStreamWatch(bad)).toBeNull();
});
it('parseStreamWatch returns null on wrong message type', () => {
const other = new TextEncoder().encode(JSON.stringify({ type: 'deafen', deafened: true }));
expect(parseStreamWatch(other)).toBeNull();
});
it('isStreamWatchPayload validates shape', () => {
expect(isStreamWatchPayload({ type: 'stream_watch', target: 'u', watching: true })).toBe(true);
expect(isStreamWatchPayload({ type: 'stream_watch', target: 'u', watching: 'yes' })).toBe(false);
expect(isStreamWatchPayload({ type: 'stream_watch', watching: true })).toBe(false);
expect(isStreamWatchPayload({ type: 'other', target: 'u', watching: true })).toBe(false);
expect(isStreamWatchPayload(null)).toBe(false);
expect(isStreamWatchPayload('string')).toBe(false);
});
});