oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1// room-mute.test.mjs — room.js's media state (spec §15.19), gated
2// without a browser.
3//
4// node --test test/browser/room-mute.test.mjs
5//
6// Same trick as room-share.test.mjs: the functions are lifted out of the
7// shipped room.js by name, so a rename fails here loudly instead of
8// passing against a copy.
9//
10// mediaFrom what counts as a report: exactly two booleans, from a
11// roster entry or a `media` payload, and nothing looser —
12// an absent or malformed one is unknown, never "on" —
13// plus §15.20's two optional keys, read forgivingly
14// sameMedia whether the hub's word on us already matches ours
15// mediaKey the dedupe key a toggle is compared on
16// mediaPayload the report: two keys, and §15.20's only with bad news
17// mediaChange the sentence the live region says, and when it says
18// nothing at all
19// rosterMedia with reportMedia: when our own report goes — on the
20// roster that admits us, on one that disagrees, on a
21// toggle, never before admission, and again after a 400
22import { test } from 'node:test';
23import assert from 'node:assert/strict';
24import { readFileSync } from 'node:fs';
25import { fileURLToPath } from 'node:url';
26
27const roomJS = fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url));
28const src = readFileSync(roomJS, 'utf8');
29
30function lift(source, name) {
31 const start = source.indexOf('function ' + name + '(');
32 assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing');
33 let depth = 0;
34 for (let i = source.indexOf('{', start); i < source.length; i++) {
35 if (source[i] === '{') depth += 1;
36 else if (source[i] === '}') {
37 depth -= 1;
38 if (depth === 0) return source.slice(start, i + 1);
39 }
40 }
41 assert.fail('unbalanced braces reading ' + name + ' out of room.js');
42}
43
44// build lifts one function, and any it calls that are named after it.
45const build = (name, ...with_) =>
46 // eslint-disable-next-line no-new-func
47 new Function([name, ...with_].map((n) => lift(src, n)).join('\n') + '; return ' + name + ';')();
48
49const mediaFrom = build('mediaFrom');
50const sameMedia = build('sameMedia', 'mediaKey');
51const mediaKey = build('mediaKey');
52const mediaChange = build('mediaChange');
53const mediaPayload = build('mediaPayload');
54
55const P1 = 'a'.repeat(32);
56const P2 = 'b'.repeat(32);
57
58test('mediaFrom takes exactly two booleans and nothing looser', () => {
59 assert.deepEqual(mediaFrom({ muted: true, cameraOff: false }), { muted: true, cameraOff: false });
60 // Extra keys are dropped rather than carried: what is drawn is what
61 // was checked.
62 assert.deepEqual(mediaFrom({ muted: false, cameraOff: true, extra: 1 }), { muted: false, cameraOff: true });
63 for (const bad of [
64 undefined, null, 'muted', 1, [],
65 {}, { muted: true }, { cameraOff: true },
66 { muted: 'true', cameraOff: false }, { muted: 1, cameraOff: 0 }, { muted: null, cameraOff: false },
67 ]) {
68 assert.equal(mediaFrom(bad), null, JSON.stringify(bad) + ' is unknown');
69 }
70});
71
72test('mediaFrom reads §15.20’s optional keys, and drops a bad one on its own', () => {
73 assert.deepEqual(
74 mediaFrom({ muted: false, cameraOff: false, audioBlocked: true, noSoundFrom: [P1, P2] }),
75 { muted: false, cameraOff: false, audioBlocked: true, noSoundFrom: [P1, P2] });
76 // A malformed optional key costs only itself: the booleans still stand.
77 for (const [bad, want] of [
78 [{ audioBlocked: 'yes' }, {}],
79 [{ audioBlocked: false }, {}],
80 [{ noSoundFrom: 'x' }, {}],
81 [{ noSoundFrom: [] }, {}],
82 [{ noSoundFrom: [P1, 'nope', 7, P1.toUpperCase(), P1] }, { noSoundFrom: [P1] }],
83 ]) {
84 assert.deepEqual(mediaFrom({ muted: true, cameraOff: false, ...bad }),
85 { muted: true, cameraOff: false, ...want }, JSON.stringify(bad));
86 }
87 // Never more than the hub's fifteen.
88 const many = Array.from({ length: 20 }, (_, i) => i.toString(16).padStart(32, '0'));
89 assert.equal(mediaFrom({ muted: false, cameraOff: false, noSoundFrom: many }).noSoundFrom.length, 15);
90 // And the required pair is still required.
91 assert.equal(mediaFrom({ muted: 'no', cameraOff: false, audioBlocked: true }), null);
92});
93
94test('sameMedia and mediaKey see a change in hearing as a change', () => {
95 const on = { muted: false, cameraOff: false };
96 assert.equal(sameMedia(on, { ...on, audioBlocked: true }), false);
97 assert.equal(sameMedia(on, { ...on, noSoundFrom: [P1] }), false);
98 assert.equal(sameMedia({ ...on, noSoundFrom: [P1, P2] }, { ...on, noSoundFrom: [P2, P1] }), true,
99 'the same peers in another order are the same report');
100 assert.notEqual(mediaKey({ ...on, noSoundFrom: [P1] }), mediaKey({ ...on, noSoundFrom: [P2] }));
101});
102
103test('mediaPayload is the two-key object unless there is bad news', () => {
104 const mine = { muted: false, cameraOff: true };
105 assert.deepEqual(mediaPayload(mine, false, [], false), { muted: false, cameraOff: true });
106 assert.deepEqual(Object.keys(mediaPayload(mine, false, null, false)), ['muted', 'cameraOff']);
107 assert.deepEqual(mediaPayload(mine, true, [P1], false),
108 { muted: false, cameraOff: true, audioBlocked: true, noSoundFrom: [P1] });
109 // A server that refused the extras once gets the two keys for good.
110 assert.deepEqual(mediaPayload(mine, true, [P1], true), { muted: false, cameraOff: true });
111});
112
113test('sameMedia never matches unknown', () => {
114 const on = { muted: false, cameraOff: false };
115 assert.equal(sameMedia(on, { muted: false, cameraOff: false }), true);
116 assert.equal(sameMedia(on, { muted: true, cameraOff: false }), false);
117 assert.equal(sameMedia(on, { muted: false, cameraOff: true }), false);
118 // The roster's silence about us is not agreement with "on": a fresh
119 // participation must be told, or a peer who joined unmuted stays
120 // unknown to everyone for the whole call.
121 assert.equal(sameMedia(null, on), false);
122 assert.equal(sameMedia(on, null), false);
123});
124
125test('mediaKey tells every state apart', () => {
126 const keys = new Set();
127 for (const muted of [false, true]) {
128 for (const cameraOff of [false, true]) keys.add(mediaKey({ muted, cameraOff }));
129 }
130 assert.equal(keys.size, 4);
131 // And never collides with the empty string mediaSent starts at.
132 assert.equal(keys.has(''), false);
133});
134
135test('mediaChange says what moved, and nothing for a first report', () => {
136 const on = { muted: false, cameraOff: false };
137 assert.equal(mediaChange('Bea', null, { muted: true, cameraOff: false }), '',
138 'a first report is somebody arriving, not news');
139 assert.equal(mediaChange('Bea', on, on), '');
140 assert.equal(mediaChange('Bea', on, { muted: true, cameraOff: false }), 'Bea muted their microphone.');
141 assert.equal(mediaChange('Bea', { muted: true, cameraOff: false }, on), 'Bea unmuted their microphone.');
142 assert.equal(mediaChange('Bea', on, { muted: false, cameraOff: true }), 'Bea turned their camera off.');
143 assert.equal(mediaChange('Bea', { muted: false, cameraOff: true }, on), 'Bea turned their camera on.');
144 assert.equal(mediaChange('Bea', on, { muted: true, cameraOff: true }),
145 'Bea muted their microphone. Bea turned their camera off.');
146});
147
148test('the dispatch routes media, and the report rides the broadcast lane', () => {
149 // Wire-level facts a lift cannot reach: the event name the hub sends,
150 // and that the report is an untargeted broadcast rather than a send()
151 // with a seq and a generation the hub would refuse.
152 assert.match(src, /case 'media': return onMedia\(msg\);/);
153 // An EventSource hears only the names it listens for: the dispatch
154 // above is dead without this, and the roster path hides it (the
155 // browser gate caught exactly that).
156 assert.match(src, /const DOWNSTREAM = \[[^\]]*'media'\]/);
157 assert.match(src, /broadcast\('media', now,/);
158});
159
160// rosterMedia and reportMedia, lifted together over real Maps with only
161// the wire and the local stream stubbed: the decision under test is when
162// a report goes, and that is the pair of them.
163function reporter({ admitted = true, local = { muted: false, cameraOff: false }, answer = true, status = 200 } = {}) {
164 const sent = [];
165 const env = { local, answer, status, blocked: false, noSound: [], heard: null };
166 const made = new Function(
167 'state', 'remoteMedia', 'localMedia', 'broadcast', 'env',
168 ['mediaFrom', 'sameMedia', 'mediaKey', 'mediaPayload', 'learnMedia', 'reportMedia', 'rosterMedia']
169 .map((n) => lift(src, n)).join('\n') +
170 '\nlet mediaSent = "";' +
171 '\nlet mediaExtras = true;' +
172 '\nlet audioBlocked = false;' +
173 '\nlet screenStalled = [];' +
174 // §15.30: the asks this tab makes, and the sender's half, which a
175 // roster also runs; neither is under test here.
176 '\nlet videoAsks = env.asks = {};' +
177 '\nconst pcs = new Map();' +
178 '\nconst answerVideoAsks = () => {};' +
179 '\nconst markAdapt = () => {};' +
180 // §15.28: rosterMedia also records each peer's client.
181 '\nconst clients = env.clients = new Map();' +
182 '\nconst noSoundList = () => env.noSound;' +
183 '\nconst hearingList = () => env.heard;' +
184 '\nreturn { rosterMedia, reportMedia, memo: () => mediaSent, extras: () => mediaExtras,' +
185 '\n block: (on) => { audioBlocked = on; } };',
186 )(
187 { admitted, stopped: false, peer: 'me' },
188 new Map(),
189 () => env.local,
190 (type, payload, onStatus) => {
191 sent.push([type, payload]);
192 // The status of the answer goes to onStatus first, as pumpBroadcast
193 // does it, and then the boolean settles the promise.
194 const extra = Object.keys(payload).length > 2;
195 const status = extra ? env.status : (env.answer ? 200 : 400);
196 if (onStatus) onStatus(status);
197 return Promise.resolve(status === 200);
198 },
199 env,
200 );
201 return { ...made, sent, env };
202}
203
204test('a roster records each other peer\'s client, and a browser as none (§15.28)', () => {
205 const r = reporter();
206 r.rosterMedia({ peers: [{ peer: 'me', client: 'mac 0.4.12' }, { peer: 'bea', client: 'native' }, { peer: 'cal' }] });
207 assert.deepEqual(Array.from(r.env.clients), [['bea', 'native'], ['cal', '']]);
208});
209
210test('a roster that has never heard from us is answered with a report', () => {
211 const r = reporter({ local: { muted: true, cameraOff: false } });
212 r.rosterMedia({ peers: [{ peer: 'me' }, { peer: 'bea' }] });
213 assert.deepEqual(r.sent, [['media', { muted: true, cameraOff: false }]]);
214});
215
216test('a roster that already agrees costs nothing, and a toggle then goes once', () => {
217 const r = reporter();
218 r.rosterMedia({ peers: [{ peer: 'me', media: { muted: false, cameraOff: false } }] });
219 assert.equal(r.sent.length, 0);
220 r.env.local = { muted: true, cameraOff: false };
221 r.reportMedia();
222 r.reportMedia();
223 assert.equal(r.sent.length, 1, 'a second call with nothing changed must not send again');
224});
225
226test('a roster that disagrees — a report lost while the stream was down — is corrected', () => {
227 const r = reporter({ local: { muted: true, cameraOff: true } });
228 r.reportMedia();
229 assert.equal(r.sent.length, 1);
230 r.rosterMedia({ peers: [{ peer: 'me', media: { muted: true, cameraOff: false } }] });
231 assert.equal(r.sent.length, 2);
232 assert.deepEqual(r.sent[1][1], { muted: true, cameraOff: true });
233});
234
235test('nothing is reported before admission', () => {
236 const r = reporter({ admitted: false, local: { muted: true, cameraOff: false } });
237 r.reportMedia();
238 assert.equal(r.sent.length, 0);
239});
240
241test('a refused report is forgotten, so the next chance tries again', async () => {
242 const r = reporter({ answer: false, local: { muted: true, cameraOff: false } });
243 r.reportMedia();
244 await new Promise((resolve) => setTimeout(resolve, 0));
245 assert.equal(r.memo(), '', 'an old server’s 400 must not be remembered as sent');
246 r.reportMedia();
247 assert.equal(r.sent.length, 2);
248});
249
250test('bad news about hearing is reported, and its end too', () => {
251 const r = reporter();
252 r.rosterMedia({ peers: [{ peer: 'me', media: { muted: false, cameraOff: false } }] });
253 assert.equal(r.sent.length, 0);
254 r.env.noSound = [P1];
255 r.reportMedia();
256 assert.deepEqual(r.sent.at(-1)[1], { muted: false, cameraOff: false, noSoundFrom: [P1] });
257 r.block(true);
258 r.reportMedia();
259 assert.deepEqual(r.sent.at(-1)[1], { muted: false, cameraOff: false, audioBlocked: true, noSoundFrom: [P1] });
260 r.reportMedia();
261 assert.equal(r.sent.length, 2, 'nothing changed, nothing sent');
262 r.block(false);
263 r.env.noSound = [];
264 r.reportMedia();
265 assert.deepEqual(r.sent.at(-1)[1], { muted: false, cameraOff: false });
266 assert.deepEqual(Object.keys(r.sent.at(-1)[1]), ['muted', 'cameraOff'],
267 'the ordinary report is exactly the two keys an old server takes');
268});
269
270test('a roster carrying our hearing as we would report it costs nothing', () => {
271 const r = reporter();
272 r.env.noSound = [P1];
273 r.rosterMedia({ peers: [{ peer: 'me', media: { muted: false, cameraOff: false, noSoundFrom: [P1] } }] });
274 assert.equal(r.sent.length, 0);
275 r.rosterMedia({ peers: [{ peer: 'me', media: { muted: false, cameraOff: false } }] });
276 assert.equal(r.sent.length, 1, 'a roster that lost the list is told again');
277});
278
279test('a 400 for the extras falls back to the two keys, at once and for good', async () => {
280 const r = reporter({ status: 400 });
281 r.env.noSound = [P1];
282 r.reportMedia();
283 await new Promise((resolve) => setTimeout(resolve, 0));
284 assert.equal(r.extras(), false);
285 assert.equal(r.sent.length, 2);
286 assert.deepEqual(r.sent[1][1], { muted: false, cameraOff: false },
287 'the fallback goes straight away, without waiting for the next change');
288 r.block(true);
289 r.env.noSound = [P1, P2];
290 r.reportMedia();
291 assert.equal(r.sent.length, 2, 'with the extras off, a change in hearing alone is no change');
292 r.env.local = { muted: true, cameraOff: false };
293 r.reportMedia();
294 assert.deepEqual(r.sent.at(-1)[1], { muted: true, cameraOff: false });
295});
296
297// §15.24: `hearing` is good news and travels whenever it is given, []
298// included — its presence is what tells the others this tab says who it
299// hears — and it is read back the same way: an empty list is kept, and
300// is a different report from one without the key.
301test('hearing is carried even when empty, and read back as given', () => {
302 assert.deepEqual(mediaPayload({ muted: false, cameraOff: false }, false, [], false, [], []),
303 { muted: false, cameraOff: false, hearing: [] });
304 assert.deepEqual(mediaPayload({ muted: false, cameraOff: false }, false, [], false, [], [P1, P2]),
305 { muted: false, cameraOff: false, hearing: [P1, P2] });
306 assert.deepEqual(mediaPayload({ muted: false, cameraOff: false }, false, [], true, [], [P1]),
307 { muted: false, cameraOff: false }, 'a server that refused the extras gets none');
308 assert.deepEqual(mediaFrom({ muted: false, cameraOff: false, hearing: [] }), { muted: false, cameraOff: false, hearing: [] });
309 assert.deepEqual(mediaFrom({ muted: false, cameraOff: false, hearing: [P1, 'nope', P1] }),
310 { muted: false, cameraOff: false, hearing: [P1] });
311 assert.deepEqual(mediaFrom({ muted: false, cameraOff: false, hearing: 'all' }), { muted: false, cameraOff: false });
312 assert.notEqual(mediaKey({ muted: false, cameraOff: false, hearing: [] }), mediaKey({ muted: false, cameraOff: false }));
313 assert.equal(mediaKey({ muted: false, cameraOff: false, hearing: [P2, P1] }), mediaKey({ muted: false, cameraOff: false, hearing: [P1, P2] }));
314});
315
316test('who this tab hears is reported on a change, and a roster that agrees costs nothing', () => {
317 const r = reporter();
318 r.env.heard = [];
319 r.rosterMedia({ peers: [{ peer: 'me' }] });
320 assert.deepEqual(r.sent.at(-1)[1], { muted: false, cameraOff: false, hearing: [] });
321 r.rosterMedia({ peers: [{ peer: 'me', media: { muted: false, cameraOff: false, hearing: [] } }] });
322 assert.equal(r.sent.length, 1);
323 r.env.heard = [P1];
324 r.reportMedia();
325 assert.deepEqual(r.sent.at(-1)[1], { muted: false, cameraOff: false, hearing: [P1] });
326 r.reportMedia();
327 assert.equal(r.sent.length, 2);
328});
329
330test('a 400 for a report carrying hearing falls back to the two keys', async () => {
331 const r = reporter({ status: 400 });
332 r.env.heard = [];
333 r.reportMedia();
334 await new Promise((resolve) => setTimeout(resolve, 0));
335 assert.equal(r.extras(), false);
336 assert.deepEqual(r.sent.at(-1)[1], { muted: false, cameraOff: false });
337});
338