feat(sounds): add stream_watch data-channel protocol helpers
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user