import './setup_env'; import { test, expect, mock } from 'bun:test'; // No scanner or satellite is available. Distribution must still succeed. mock.module('../src/cluster', () => ({allEnrichedNodes: async () => [], callNodeAbsolute: async () => {throw new Error('must not call a satellite')}})); mock.module('../src/sso',()=>({resolveSsoEmail:async(h:Headers)=>h.get('x-test-admin') === 'yes' ? 'test-admin@bytedance.com' : null})); mock.module('../src/admin_db',()=>({isAdminPrefix:async(p:string)=>p === 'test-admin',normPrefix:(s:string)=>s.split('@')[0]})); const run = process.env.TEST_DATABASE_URL ? test : test.skip; run('catalog distribution is independent of model review; admin can classify and distribute legacy material', async () => { const {createCandidate,getFix,compatIndex,setFixState,recordVerification,listProblems,listAdvisories,packFor,searchKb}=await import('../src/eddy_db'); const {validateCandidate,payloadDigest}=await import('../src/eddy_rules'); const {eddyApi}=await import('../src/routes_eddy'); const {createOpenPlatformKey,revokeOpenPlatformKey}=await import('../src/openplatform_keys'); const {db,closePool}=await import('../src/db_client'); const schema=await import('../src/schema'); const {eq,and}=await import('drizzle-orm'); const baseline=`test-eddy-${crypto.randomUUID()}`; const knowledge={category:'app-compat',confidence:'suspected',summary:'layout recursion',cause:'Catalyst recursion',solution:'scoped UIKit hook',validation:'120 seconds bounded fixture',rollback:'dispose and restart'}; const b64=Buffer.from('return {install(ctx){ctx.status("installed")},dispose(){}}').toString('base64'); const body={problem:{signature:'hang:layout'},knowledge,tier:'frida-guard',target:{baseline,bundle:'com.test',version:'1',build:'1',architecture:'arm64',platform:6,executableUUID:'11111111-2222-3333-4444-555555555555',binarySha256:'a'.repeat(64)},payload:{path:'guard.js',sha256:payloadDigest(b64)},payload_b64:b64}; const post = (path:string,body:unknown,admin=true)=>eddyApi.request(path,{method:'POST',headers:{'content-type':'application/json',...(admin?{'x-test-admin':'yes'}:{})},body:JSON.stringify(body)}); const create = async (b:unknown)=>{const v=validateCandidate(b);if(!v.ok)throw new Error(v.error);return createCandidate(v.value,'test')}; let keyId: string | undefined; try { expect((await post('/compat/candidates',body,false)).status).toBe(401); const {secret,key}=await createOpenPlatformKey({label:'eddy integration',source:'test',createdBy:'test'});keyId=key.id; const response=await eddyApi.request('/compat/candidates',{method:'POST',headers:{'content-type':'application/json',authorization:`Bearer ${secret}`,'x-eddy-user':'spoofed'},body:JSON.stringify(body)}); expect(response.status).toBe(200); const candidate=await response.json() as {id:string;rev:number;state:string}; expect(candidate.state).toBe('published'); const fix=(await getFix(candidate.id))!; expect(fix.author).toBe(`api-key:${key.id}`); expect(fix.safety).toBeNull(); expect(fix.distribution?.mode).toBe('auto');expect(fix.distribution?.payloadSha256).toBe(body.payload.sha256); expect((await compatIndex(baseline,1)).fixes[0].state).toBe('candidate'); expect((await compatIndex(baseline,2)).fixes[0].state).toBe('candidate'); expect((await compatIndex(baseline,3)).fixes[0].state).toBe('published'); expect((await post(`/admin/fixes/${fix.id}/1/disabled`,{},false)).status).toBe(401); expect((await post(`/admin/fixes/${fix.id}/1/disabled`,{})).status).toBe(200); await recordVerification(fix.id,1,{passed:true,runner:'satellite'}); expect((await getFix(fix.id))?.state).toBe('disabled'); expect((await create(body)).state).toBe('disabled'); // identical re-submit cannot undo a stop expect((await post(`/admin/fixes/${fix.id}/1/published`,{})).status).toBe(200); expect((await getFix(fix.id))?.distribution?.actor).toBe('test-admin'); // Legacy record has neither ownership classification nor SHA-256 binary identity. const legacy=await create({...body,problem:{signature:'hang:legacy'},knowledge:undefined,target:{baseline}}); const original=await packFor(legacy.id,1); expect(legacy.state).toBe('candidate'); await db.update(schema.eddy_fixes).set({safety:JSON.stringify({verdict:'block',reason:'旧规则缺少分类'})}).where(eq(schema.eddy_fixes.id,legacy.id)); expect((await post(`/admin/fixes/${legacy.id}/1/published`,{})).status).toBe(409); const classification={baseline,signature:'hang:legacy',category:'app-compat',confidence:'suspected'}; expect((await post('/admin/problems/classification',classification,false)).status).toBe(401); expect((await post('/admin/problems/classification',{...classification,category:'invalid'})).status).toBe(400); expect((await post('/admin/problems/classification',classification)).status).toBe(200); expect((await getFix(legacy.id))?.state).toBe('candidate'); // classification alone does not publish old code expect((await post(`/admin/fixes/${legacy.id}/1/published`,{})).status).toBe(200); expect((await getFix(legacy.id))?.distribution?.mode).toBe('admin'); expect((await getFix(legacy.id))?.safety?.verdict).toBe('block'); // never forge a safety verdict expect(await packFor(legacy.id,1)).toEqual(original); // immutable code and manifest // A late runtime failure may reject a fix, but an admin can make an explicit catalog decision. await recordVerification(legacy.id,1,{passed:false}); expect((await getFix(legacy.id))?.state).toBe('rejected'); expect((await post(`/admin/fixes/${legacy.id}/1/published`,{})).status).toBe(200); // Classification away from app-compat withdraws material and is authoritative over future submissions. expect((await post('/admin/problems/classification',{...classification,category:'devicesim'})).status).toBe(200); expect((await getFix(legacy.id))?.state).toBe('disabled'); expect((await post(`/admin/fixes/${legacy.id}/1/published`,{})).status).toBe(409); const changed=await create({...body,problem:{signature:'hang:legacy'}}); expect(changed.state).toBe('candidate'); expect((await listProblems(baseline)).find(p=>p.signature==='hang:legacy')?.knowledge?.category).toBe('devicesim'); // Both record-only categories stay private to management. for(const category of ['source-bug','devicesim']) await create({...body,problem:{signature:`hang:${category}`},tier:'advisory',advisory:'record only',knowledge:{...knowledge,category}}); expect((await compatIndex(baseline)).advisories).toHaveLength(0); const advice=await create({...body,problem:{signature:'layout:advice'},tier:'advisory',advisory:'A scoped Catalyst workaround'}); expect(advice.state).toBe('verified'); expect((await compatIndex(baseline)).advisories).toHaveLength(1); expect(await searchKb('Catalyst',['compat'],baseline,10)).toHaveLength(1); expect((await post(`/admin/advisories/${advice.id}/disabled`,{})).status).toBe(200); expect((await compatIndex(baseline)).advisories).toHaveLength(0); expect((await post(`/admin/advisories/${advice.id}/verified`,{})).status).toBe(200); expect((await listAdvisories()).find(a=>a.id===advice.id)?.distribution?.actor).toBe('test-admin'); const legacyAdvice=await create({...body,problem:{signature:'hang:old-advice'},tier:'advisory',advisory:'Legacy text',knowledge:undefined}); expect(legacyAdvice.state).toBe('unverified'); await post('/admin/problems/classification',{...classification,signature:'hang:old-advice'}); expect((await post(`/admin/advisories/${legacyAdvice.id}/verified`,{})).status).toBe(200); expect((await compatIndex(baseline)).advisories).toHaveLength(2); // An administrator can still address an older record beyond the overview's 500-row window. const [oldAdvice] = await db.select().from(schema.eddy_advisories).where(eq(schema.eddy_advisories.id,legacyAdvice.id)); await db.update(schema.eddy_advisories).set({created:1}).where(eq(schema.eddy_advisories.id,legacyAdvice.id)); await db.insert(schema.eddy_advisories).values(Array.from({length:501},(_,n)=>({...oldAdvice,id:`${baseline}-list-padding-${n}`,created:Date.now()+n,state:'unverified'}))); expect((await post(`/admin/advisories/${legacyAdvice.id}/disabled`,{})).status).toBe(200); expect((await post(`/admin/advisories/${legacyAdvice.id}/verified`,{})).status).toBe(200); // Corrupt bytes never become published via the administrator path. await setFixState(fix.id,1,'disabled'); await db.update(schema.eddy_fixes).set({payload:Buffer.from('tampered').toString('base64')}).where(and(eq(schema.eddy_fixes.id,fix.id),eq(schema.eddy_fixes.rev,1))); expect((await post(`/admin/fixes/${fix.id}/1/published`,{})).status).toBe(409); expect((await eddyApi.request('/compat/candidates/x/1/verification',{method:'POST',headers:{'content-type':'application/json'},body:'{"passed":true}'})).status).toBe(401); } finally { await db.delete(schema.eddy_fixes).where(eq(schema.eddy_fixes.baseline,baseline)); await db.delete(schema.eddy_advisories).where(eq(schema.eddy_advisories.baseline,baseline)); await db.delete(schema.eddy_problems).where(eq(schema.eddy_problems.baseline,baseline)); if(keyId) await revokeOpenPlatformKey(keyId); await closePool(); } },15000);