Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
1234567891011121314import test from 'node:test';import assert from 'node:assert/strict';import {LiveWatch,validateMetadata,MAX_FRAME_BYTES} from '../../system/public/watch/client.mjs';const id='a'.repeat(32),meta={id,kind:'picture',version:1,mime:'image/png',sequence:1,status:'live'};function fixture(fetch){const timers=new Map(),frames=[],statuses=[],errors=[],ends=[];let timer=0;const watch=new LiveWatch({id,fetch,onFrame:async(metadata,bytes)=>frames.push({metadata,bytes}),onStatus:(...args)=>statuses.push(args),onError:error=>errors.push(error),onEnd:reason=>ends.push(reason),schedule:(fn,delay)=>{timers.set(++timer,{fn,delay});return timer;},cancel:id=>timers.delete(id)});watch.start();return{watch,timers,frames,statuses,errors,ends};}const metadata=(value=meta)=>({ok:true,status:200,json:async()=>value});const frame=()=>({ok:true,status:200,headers:{get:()=>null},arrayBuffer:async()=>new Uint8Array([1,2,3]).buffer});test('one frame request per new sequence, stable nodes on metadata-only polling',async()=>{let count=0;const f=fixture(async(url,options)=>{assert.equal(options.credentials,'omit');count++;return url.includes('&frame=')?frame():metadata();});await f.watch.poll();await f.watch.poll();assert.equal(count,3);assert.equal(f.frames.length,1);assert.equal(f.watch.sequence,1);f.watch.stop();});test('superseded frame keeps prior version and retries metadata',async()=>{const f=fixture(async url=>url.includes('&frame=')?{status:409}:metadata());await f.watch.poll();assert.equal(f.frames.length,0);assert.equal(f.errors.length,0);assert.ok([...f.timers.values()].some(t=>t.delay===250));f.watch.stop();});test('polling is sequential and visibility abort prevents late frame display',async()=>{let resolve,count=0;const f=fixture(async()=>{count++;return new Promise(r=>resolve=r);});const running=f.watch.poll();await f.watch.poll();assert.equal(count,1);f.watch.setVisible(false);resolve(metadata());await running;assert.equal(f.frames.length,0);assert.equal(f.timers.size,0);f.watch.setVisible(true);assert.equal(f.timers.size,1);f.watch.stop();});test('new and expired links wait and recover when the maker connects',async()=>{for(const code of [404,410]){let online=false;const f=fixture(async url=>online?(url.includes('&frame=')?frame():metadata()):{status:code});await f.watch.poll();assert.deepEqual(f.ends,['waiting']);assert.equal(f.watch.running,true);assert.ok([...f.timers.values()].some(t=>t.delay===3000));online=true;await f.watch.poll();assert.equal(f.frames.length,1);f.watch.stop();}});test('connection errors back off without discarding successful frame',async()=>{let online=true;const f=fixture(async url=>{if(!online)throw new Error('offline');return url.includes('&frame=')?frame():metadata();});await f.watch.poll();online=false;await f.watch.poll();await f.watch.poll();assert.equal(f.frames.length,1);assert.equal(f.watch.sequence,1);assert.equal(f.errors.length,2);assert.ok([...f.timers.values()].some(t=>t.delay===1500));f.watch.stop();});test('invalid metadata and oversized frames never reach rendering',async()=>{assert.throws(()=>validateMetadata({...meta,kind:'html'},id));assert.throws(()=>new LiveWatch({id:'../secret'}));const f=fixture(async url=>url.includes('&frame=')?{...frame(),headers:{get:()=>String(MAX_FRAME_BYTES+1)}}:metadata());await f.watch.poll();assert.equal(f.frames.length,0);assert.match(f.errors[0],/too large/);f.watch.stop();});
test('temporary code resolves metadata then fetches the exact artifact ID',async()=>{const requests=[];const f=fixture(async url=>{requests.push(url);return url.includes('&frame=')?frame():metadata();});f.watch.id=id.slice(0,12);await f.watch.poll();assert.equal(requests[0],'/api/easel-live?code='+id.slice(0,12));assert.equal(requests[1],'/api/easel-live?id='+id+'&frame=1');assert.equal(f.frames.length,1);f.watch.stop();});