import { test, expect } from 'bun:test'; import { createAccountBootstrap, accountEmail, type Account, type AccountKey, type Intent, type Capability, type BootstrapPorts } from '../scripts/lib/openobserve-bootstrap'; const account=(key:AccountKey):Account=>({email:accountEmail(key),token:'fixture-'+key+'-token-only'}); function fixture() { const checkpoints:Partial>={},intents:Partial>={},captured:Record={},creates:AccountKey[]=[]; let index:Record|undefined,derived=0,published=0,failCreate:AccountKey|undefined,proof:Partial={},persistedBeforeQuery=false; const ports:BootstrapPorts={readIndex:async()=>index,readAccount:async k=>checkpoints[k],readIntent:async k=>intents[k],makeIntent:k=>({email:accountEmail(k),rawFile:'/fake/'+k,captureId:'create-'+k}),claimIntent:async(k,v)=>{if(intents[k])throw new Error('claim exists');intents[k]=v;},readCaptured:async i=>captured[i.rawFile],create:async(k)=>{creates.push(k);if(k==='query')persistedBeforeQuery=!!checkpoints.ingest;if(k===failCreate)throw new Error('synthetic second create failure');return{status:200,body:{code:200,user:accountEmail(k),token:account(k).token}};},saveAccount:async(k,a)=>{checkpoints[k]=a;},verify:async()=>({role:'service_account',searchHTTP:200,searchHasRows:true,ingestHTTP:200,adminRotateHTTP:403,...proof}),publishIndex:async a=>{published++;index=a;},publishDerived:async()=>{derived++;}}; return{ports,checkpoints,intents,captured,creates,setIndex(v:Record){index=v;},fail(k:AccountKey){failCreate=k;},setProof(v:Partial){proof=v;},get derived(){return derived},get published(){return published},get persistedBeforeQuery(){return persistedBeforeQuery}}; } test('first received credential survives a second account creation failure',async()=>{const f=fixture();f.fail('query');await expect(createAccountBootstrap(f.ports)()).rejects.toThrow();expect(f.checkpoints.ingest).toEqual(account('ingest'));expect(f.derived).toBe(0);}); test('resume reuses one persisted credential instead of repeating its account POST',async()=>{const f=fixture();f.checkpoints.ingest=account('ingest');await createAccountBootstrap(f.ports)();expect(f.creates).toEqual(['query']);}); test('resume recovers a valid captured response without repeating account POST',async()=>{const f=fixture();f.intents.ingest=f.ports.makeIntent('ingest');f.captured['/fake/ingest']={status:200,body:{code:200,user:accountEmail('ingest'),token:account('ingest').token}};await createAccountBootstrap(f.ports)();expect(f.creates).toEqual(['query']);expect(f.checkpoints.ingest).toEqual(account('ingest'));}); test('uncertain intent without recoverable native JSON fails closed without POST retry',async()=>{const f=fixture();f.intents.ingest=f.ports.makeIntent('ingest');await expect(createAccountBootstrap(f.ports)()).rejects.toThrow();expect(f.creates).toHaveLength(0);expect(f.published).toBe(0);expect(f.derived).toBe(0);}); for(const [name,proof]of [['stale search token401',{searchHTTP:401}],['unavailable ingest503',{ingestHTTP:503}],['rotation401 is not the required403 denial',{adminRotateHTTP:401}]] as const){test(name+' prevents derived publication and successful continuation',async()=>{const f=fixture();f.setIndex({ingest:account('ingest'),query:account('query')});f.setProof(proof);let started=false;await expect((async()=>{await createAccountBootstrap(f.ports)();started=true;})()).rejects.toThrow();expect(f.derived).toBe(0);expect(started).toBe(false);});} test('healthy initial bootstrap checkpoints ingest before starting query creation',async()=>{const f=fixture();await createAccountBootstrap(f.ports)();expect(f.persistedBeforeQuery).toBe(true);expect(f.published).toBe(1);expect(f.derived).toBe(1);}); test('healthy saved index revalidates both native identities without new account POST',async()=>{const f=fixture();f.setIndex({ingest:account('ingest'),query:account('query')});await createAccountBootstrap(f.ports)();expect(f.creates).toHaveLength(0);expect(f.derived).toBe(1);});