// Tests for the look capability, with a fake camera and a fake button service. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { Look } from './look.js'; const fakes = ({ signalFails = false, grabFails = false } = {}) => { const log = []; const signal = async () => { if (signalFails) throw new Error('the button service is not listening'); log.push('flash'); return () => log.push('stop'); }; const grab = async () => { log.push('grab'); if (grabFails) throw new Error('camera: 401'); return new Uint8Array([0xff, 0xd8, 0xff]); }; return { log, look: new Look(signal, grab) }; }; test('a look is shown before the frame is taken, and stops after', async () => { const { log, look } = fakes(); assert.equal(await look.look(), '/9j/'); assert.deepEqual(log, ['flash', 'grab', 'stop']); }); test('no indication, no look', async () => { const { log, look } = fakes({ signalFails: true }); await assert.rejects(look.look(), /not listening/); assert.deepEqual(log, []); }); test('the indication stops even when the camera fails', async () => { const { log, look } = fakes({ grabFails: true }); await assert.rejects(look.look(), /401/); assert.deepEqual(log, ['flash', 'grab', 'stop']); }); test('one look at a time', async () => { const { log, look } = fakes(); await Promise.all([look.look(), look.look()]); assert.deepEqual(log, ['flash', 'grab', 'stop', 'flash', 'grab', 'stop']); });