forked from mtusi2026/mtusi2026lab6
6 lines
1.6 KiB
TypeScript
6 lines
1.6 KiB
TypeScript
import{test,expect}from'bun:test';import{createOwnedFaultRunner,OwnedFaultFailure}from'../scripts/lib/owned-fault';
|
|
test('restores the acquired owned lease after successful exercise',async()=>{const lease={id:'synthetic'},restored:unknown[]=[];const result=await createOwnedFaultRunner({acquire:async()=>lease,exercise:async()=>7,restore:async l=>{restored.push(l)}})();expect(result).toBe(7);expect(restored).toEqual([lease]);});
|
|
test('restores owned lease even when its mutation or traffic fails',async()=>{const lease={id:'synthetic'},failure=new Error('fixture failed'),restored:unknown[]=[];let result;try{await createOwnedFaultRunner({acquire:async()=>lease,exercise:async()=>{throw failure},restore:async l=>{restored.push(l)}})()}catch(e){result=e}expect(restored).toEqual([lease]);expect(result).toBeInstanceOf(OwnedFaultFailure);expect((result as OwnedFaultFailure).primary).toBe(failure);});
|
|
test('preserves primary failure separately from failed restoration',async()=>{const first=new Error('exercise'),second=new Error('cleanup');let result;try{await createOwnedFaultRunner({acquire:async()=>({id:'owned'}),exercise:async()=>{throw first},restore:async()=>{throw second}})()}catch(e){result=e}expect(result).toBeInstanceOf(OwnedFaultFailure);expect((result as OwnedFaultFailure).primary).toBe(first);expect((result as OwnedFaultFailure).cleanup).toBe(second);});
|
|
test('does not restore anything when ownership acquisition fails',async()=>{let restored=0;await expect(createOwnedFaultRunner({acquire:async()=>{throw new Error('ownership')},exercise:async()=>0,restore:async()=>{restored++}})()).rejects.toThrow();expect(restored).toBe(0);});
|