Skip to content
File

Blob: worker/src/shared/contracts/signaling.test.ts

typescript64 lines
1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { channelListSchema, channelsSchema } from './channels.ts';
4import { commandSchema } from './robot.ts';
5import { deviceStartSchema } from './signaling.ts';
6 
7test('Zod rejects hostile control values and unrecognized commands', () => {
8 for (const value of [
9 { led: [-1, 0, 0] },
10 { led: [256, 0, 0] },
11 { led: [1.5, 0, 0] },
12 { action: 'erase' },
13 { action: 'pause', cmd: 'restart_device' },
14 ]) {
15 assert.equal(commandSchema.safeParse(value).success, false);
16 }
17 assert.deepEqual(commandSchema.parse({ led: [0, 10, 32] }), { led: [0, 10, 32] });
18});
19 
20test('device offers retain the bounded startup contract', () => {
21 const input = { bootId: 'a'.repeat(32), sessionDescription: { type: 'offer', sdp: 'v=0\r\n' } };
22 assert.equal(deviceStartSchema.safeParse(input).success, true);
23 assert.equal(deviceStartSchema.safeParse({ ...input, bootId: 'not-a-boot-id' }).success, false);
24 assert.equal(
25 deviceStartSchema.safeParse({
26 ...input,
27 sessionDescription: { type: 'offer', sdp: 'v=0' + 'x'.repeat(16000) },
28 }).success,
29 false,
30 );
31});
32 
33test('viewer channel reliability and IDs must be unambiguous', () => {
34 const valid = [
35 { id: 1, dataChannelName: 'robot' as const, ordered: true },
36 { id: 3, dataChannelName: 'spectrum' as const, ordered: false, maxRetransmits: 0 },
37 ];
38 assert.deepEqual(channelListSchema.parse(valid), valid);
39 assert.throws(() => channelListSchema.parse([valid[0], { ...valid[1], id: 1 }]));
40 assert.throws(() => channelListSchema.parse([valid[0], { ...valid[1], ordered: true }]));
41});
42 
43test('SFU responses need not echo requested channel reliability', () => {
44 const response = channelsSchema.parse({
45 channels: [
46 { id: 1, dataChannelName: 'robot' },
47 { id: 3, dataChannelName: 'spectrum' },
48 ],
49 });
50 assert.deepEqual(response.channels, [
51 { id: 1, dataChannelName: 'robot', ordered: true },
52 { id: 3, dataChannelName: 'spectrum', ordered: false, maxRetransmits: 0 },
53 ]);
54 assert.equal(
55 channelsSchema.safeParse({
56 channels: [
57 { id: 1, dataChannelName: 'robot', ordered: false },
58 { id: 3, dataChannelName: 'spectrum' },
59 ],
60 }).success,
61 false,
62 );
63});