Files
mtusi2026lab6/student-tests/browser-lifecycle.test.ts

18 lines
3.8 KiB
TypeScript

import{test,expect}from'bun:test';import{createBrowserLifecycle}from'../scripts/lib/browser-lifecycle';
function fixture(flags:{setup?:string;primary?:unknown;report?:boolean;close?:boolean;secondaryReport?:boolean;acquire?:boolean}={}){
const primary=Object.hasOwn(flags,'primary')?flags.primary:new Error(flags.setup??'WORK'),reportError=new Error('RESULT_WRITE'),closeError=new Error('CLOSE'),resource={id:'synthetic-browser'},events:string[]=[],secondary:{kind:string;error:unknown}[]=[];let closes=0,acquires=0;
const run=createBrowserLifecycle({acquire:async()=>{acquires++;if(flags.acquire)throw primary;return resource},run:async received=>{expect(received).toBe(resource);events.push('run');if(flags.setup||Object.hasOwn(flags,'primary'))throw primary;return 17},close:async received=>{expect(received).toBe(resource);closes++;events.push('close');if(flags.close)throw closeError},reportResult:async(result,failure)=>{events.push('report');if(flags.report)throw reportError;if(flags.setup||Object.hasOwn(flags,'primary'))expect(failure?.error).toBe(primary);else expect(result).toBe(17)},reportSecondaryFailure:async(kind,error)=>{secondary.push({kind,error});if(flags.secondaryReport)throw Error('SECONDARY_REPORT')}});
return{run,primary,reportError,closeError,events,secondary,closes:()=>closes,acquires:()=>acquires};
}
async function captured(run:()=>Promise<unknown>){try{return{threw:false,value:await run(),error:undefined}}catch(error){return{threw:true,value:undefined,error}}}
test('context setup rejection closes the acquired owned browser',async()=>{const f=fixture({setup:'newContext'}),result=await captured(f.run);expect(f.closes()).toBe(1);expect(result.error).toBe(f.primary)});
test('page setup rejection closes the acquired owned browser',async()=>{const f=fixture({setup:'newPage'}),result=await captured(f.run);expect(f.closes()).toBe(1);expect(result.error).toBe(f.primary)});
test('result-write rejection still closes and remains the primary failure',async()=>{const f=fixture({report:true}),result=await captured(f.run);expect(f.closes()).toBe(1);expect(result.error).toBe(f.reportError)});
test('work failure survives simultaneous result-write failure',async()=>{const primary=new Error('ORIGINAL'),f=fixture({primary,report:true}),result=await captured(f.run);expect(result.error).toBe(primary);expect(f.closes()).toBe(1);expect(f.secondary).toEqual([{kind:'report',error:f.reportError}])});
test('primary failure survives close and secondary-reporter failures',async()=>{const primary=new Error('ORIGINAL'),f=fixture({primary,close:true,secondaryReport:true}),result=await captured(f.run);expect(result.error).toBe(primary);expect(f.closes()).toBe(1);expect(f.secondary).toEqual([{kind:'close',error:f.closeError}])});
test('close failure after success remains a separately reported failure',async()=>{const f=fixture({close:true}),result=await captured(f.run);expect(result.error).toBe(f.closeError);expect(f.closes()).toBe(1);expect(f.secondary).toEqual([{kind:'close',error:f.closeError}])});
test('acquisition failure does not close a nonexistent browser',async()=>{const f=fixture({acquire:true}),result=await captured(f.run);expect(result.error).toBe(f.primary);expect(f.closes()).toBe(0)});
test('missing required ports fail before acquisition',()=>{expect(()=>createBrowserLifecycle({}as any)).toThrow('BROWSER_LIFECYCLE_PORTS_REQUIRED')});
test('thrown undefined is preserved when cleanup also fails',async()=>{const f=fixture({primary:undefined,close:true}),result=await captured(f.run);expect(result.threw).toBe(true);expect(result.error).toBeUndefined();expect(f.closes()).toBe(1);expect(f.secondary).toEqual([{kind:'close',error:f.closeError}])});
test('successful work closes before reporting and returns its value',async()=>{const f=fixture();expect(await f.run()).toBe(17);expect(f.closes()).toBe(1);expect(f.events).toEqual(['run','close','report']);expect(f.secondary).toEqual([])});