File size: 13,925 Bytes
ecb2a3f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import test from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
import {build} from 'esbuild';
import {createRequire} from 'node:module';
import {fileURLToPath} from 'node:url';
import {caseCanEdit,caseDraftError,caseMetrics,latestDefectReview,nextCaseStates,caseRpc} from '../src/correctiveActions.mjs';
import {reviewBody} from '../src/workflow.mjs';
import {handleCA} from '../../deployment/supabase/functions/corrective-actions/worker.mjs';
const uuid='11111111-1111-4111-8111-111111111111',lease='22222222-2222-4222-8222-222222222222';
const event={event_id:uuid,lease_token:lease,case_id:'33333333-3333-4333-8333-333333333333',kind:'created',version:1,state:'Open',priority:'High',due_at:'2026-10-21T00:00:00Z'};
const env={DEFECTRAG_CA_AUTOMATION_ENABLED:'1',DEFECTRAG_CA_WORKER_SECRET:'w'.repeat(32),DEFECTRAG_CA_CALLBACK_SECRET:'c'.repeat(32),DEFECTRAG_CA_N8N_SECRET:'h'.repeat(32),DEFECTRAG_CA_N8N_URL:'https://n8n.invalid/webhook/ca',SUPABASE_URL:'https://db.invalid',SUPABASE_SERVICE_ROLE_KEY:'service-fixture'};
const request=(callback=false,b={})=>new Request('https://edge.invalid',{method:'POST',headers:{Authorization:'Bearer '+(callback?env.DEFECTRAG_CA_CALLBACK_SECRET:env.DEFECTRAG_CA_WORKER_SECRET),...(callback?{'X-DefectRAG-Operation':'effect'}:{})},body:JSON.stringify(b)});
test('assignment validation checks exact deadline boundary and live active assignments',()=>{
const now=Date.parse('2026-10-26T00:00:00Z'),people=[{user_id:uuid},{user_id:lease}],draft={description:'Confirmed defect needs corrective work',owner:uuid,supervisor:lease,priority:'Normal',due:new Date(now+365*86400000).toISOString()};
assert.equal(caseDraftError(draft,people,now),'');
assert.match(caseDraftError({...draft,due:new Date(now+365*86400000+1).toISOString()},people,now),/next year/);
assert.match(caseDraftError({...draft,due:new Date(now).toISOString()},people,now),/future/);
assert.match(caseDraftError({...draft,due:'not-a-date'},people,now),/date and time/);
assert.match(caseDraftError(draft,[{user_id:uuid,revoked_at:'2026-10-26'},{user_id:lease}],now),/person responsible/);
assert.match(caseDraftError({...draft,supervisor:'unknown'},people,now),/supervisor/);
assert.match(caseDraftError({...draft,description:' '.repeat(20)},people,now),/10 to 2,000/);
});
test('case editor feedback uses current assignments or a verified directory Developer role',()=>{
const row={owner_id:uuid,supervisor_id:lease,confirmed_by:'creator'};
assert.equal(caseCanEdit(row,uuid),true);assert.equal(caseCanEdit(row,lease),true);
assert.equal(caseCanEdit(row,'developer',true),true);
for(const actor of ['unassigned','creator','',null,undefined])assert.equal(caseCanEdit(row,actor),false);
assert.equal(caseCanEdit(row,'unassigned','developer'),false);
assert.equal(caseCanEdit(null,uuid,true),false);
assert.equal(caseCanEdit({...row,owner_id:'replacement'},uuid),false);
});
test('AI anomaly, incomplete review and accepted disposition never qualify as confirmed-defect review',()=>{
assert.equal(latestDefectReview([]),null);
const row={id:uuid,created_at:'2026-10-20T00:00:00Z',body:reviewBody('Reviewed','Reject','Confirmed review')};
assert.equal(latestDefectReview([row]),row);
for(const body of [reviewBody('In review','Pending'),reviewBody('Reviewed','Accept')])assert.equal(latestDefectReview([row,{...row,id:lease,created_at:'2026-10-21T00:00:00Z',body}]),null);
});
test('case states enforce progression, audited progress, reopen and terminal boundaries',()=>{
for(const [a,b] of [['Open','Assigned'],['Assigned','In Progress'],['In Progress','Resolved'],['Resolved','Closed'],['Resolved','In Progress']])assert.ok(nextCaseStates(a).includes(b));
assert.ok(!nextCaseStates('Open').includes('Closed'));assert.deepEqual(nextCaseStates('Closed'),[]);assert.deepEqual(nextCaseStates('Cancelled'),[]);
});
test('metrics use actual resolved timestamps, no fake defaults or reopened resolutions',()=>{
assert.equal(caseMetrics([]).averageResolutionHours,null);
const rows=[{state:'Closed',created_at:'2026-10-20T00:00:00Z',resolved_at:'2026-10-20T04:00:00Z'},
{state:'Resolved',created_at:'2026-10-20T00:00:00Z',resolved_at:'2026-10-20T02:00:00Z'},
{state:'In Progress',created_at:'2026-10-20T00:00:00Z',due_at:'2026-10-20T01:00:00Z',resolved_at:null}];
assert.deepEqual(caseMetrics(rows,Date.parse('2026-10-20T05:00:00Z')),{open:1,resolved:2,overdue:1,averageResolutionHours:3});
});
test('server denial and optimistic-concurrency errors are never converted to successful UI writes',async()=>{
await assert.rejects(()=>caseRpc({rpc:async()=>({error:new Error('AAL2 required')})},'test',{}),/AAL2/);
await assert.rejects(()=>caseRpc({rpc:async()=>({error:new Error('refresh')})},'test',{}),/refresh/);
});
test('disabled/unauthorized bridge never contacts Supabase or n8n',async()=>{
const no=()=>assert.fail('No network');assert.equal((await handleCA(request(),{env:{...env,DEFECTRAG_CA_AUTOMATION_ENABLED:'0'},fetcher:no})).status,503);
assert.equal((await handleCA(request(),{env:{...env,DEFECTRAG_CA_WORKER_SECRET:'different-secret'.repeat(3)},fetcher:no})).status,401);
assert.equal((await handleCA(request(true),{env:{...env,DEFECTRAG_CA_CALLBACK_SECRET:'wrong-secret'.repeat(4)},fetcher:no})).status,401);
});
test('dispatch projects event capability only, retains authoritative completion check',async()=>{
let finished;
const fetcher=async(url,o)=>{
if(url.endsWith('defectrag_ca_schedule'))return Response.json(1);
if(url.endsWith('defectrag_ca_claim'))return Response.json({...event,image:'private',token:'private'});
if(url.endsWith('defectrag_ca_finish')){finished=JSON.parse(o.body);return Response.json(true)}
assert.equal(o.headers['X-DefectRAG-CA-Key'],env.DEFECTRAG_CA_N8N_SECRET);assert.deepEqual(JSON.parse(o.body),event);assert.ok(!JSON.stringify(o).includes(env.SUPABASE_SERVICE_ROLE_KEY));
return Response.json({event_id:uuid,completed:true});
};
const r=await (await handleCA(request(),{env,fetcher})).json();assert.equal(r.workflowCompleted,true);assert.equal(finished.p_success,true);
});
for(const failure of ['offline','rejected','mismatched'])test('workflow '+failure+' leaves SQL to retry/quarantine, no fake completion',async()=>{
let finished;
const fetcher=async(url,o)=>{
if(url.endsWith('defectrag_ca_schedule'))return Response.json(0);
if(url.endsWith('defectrag_ca_claim'))return Response.json(event);
if(url.endsWith('defectrag_ca_finish')){finished=JSON.parse(o.body);return Response.json(true)}
if(failure==='offline')throw Error('private-key');
return failure==='rejected'?new Response('',{status:503}):Response.json({event_id:lease,completed:true});
};
const r=await (await handleCA(request(),{env,fetcher})).json();assert.equal(r.workflowCompleted,false);assert.equal(finished.p_success,false);
});
test('effect callback can only invoke bounded notification RPC with event lease',async()=>{
let called;
const r=await handleCA(request(true,{event_id:uuid,lease_token:lease,operation:'begin',effect:'ticket',arbitrary_table:'staff_members'}),{env,fetcher:async(url,o)=>{called={url,b:JSON.parse(o.body)};return Response.json({execute:true,recipient_email:'fixture@example.invalid'})}});
assert.equal(r.status,200);assert.ok(called.url.endsWith('/defectrag_ca_effect'));assert.equal(called.b.p_effect,'notification');assert.equal(called.b.p_event,uuid);
assert.equal((await handleCA(request(true,{event_id:'wrong',lease_token:lease,operation:'begin'}),{env,fetcher:()=>assert.fail()})).status,400);
});
test('insufficient deadline never schedules, claims or dispatches',async()=>{
assert.deepEqual(await (await handleCA(request(),{env,budgetMs:89999,fetcher:()=>assert.fail()})).json(),{claimed:0,budgetLimited:true});
});
test('n8n template is inactive, header-authenticated, disables execution logs and blocks uncertain effects',async()=>{
const w=JSON.parse(await readFile(new URL('../../deployment/n8n/corrective-actions.json',import.meta.url),'utf8'));
assert.equal(w.active,false);assert.equal(w.settings.saveDataSuccessExecution,'none');assert.equal(w.nodes[0].parameters.authentication,'headerAuth');
const code=name=>new Function('$input','$',w.nodes.find(n=>n.name===name).parameters.jsCode);
assert.equal(code('Validate case event')({first:()=>({json:{body:event}})})[0].json.event_id,uuid);
assert.throws(()=>code('Validate case event')({first:()=>({json:{body:{...event,kind:'AI_prediction'}}})}));
const $=()=>({first:()=>({json:event})});
assert.throws(()=>code('Require durable completion')({first:()=>({json:{done:false,manual_review:true}})},$));
assert.equal(code('Require durable completion')({first:()=>({json:{done:true}})},$)[0].json.completed,true);
assert.ok(!JSON.stringify(w).includes('service_role'));assert.ok(!w.nodes.some(n=>n.type.includes('trello')));
});
test('SQL source contracts require explicit confirmation, live Staff/TOTP, restricted grants and unique effects',async()=>{
const sql=await readFile(new URL('../../deployment/supabase_corrective_actions.sql',import.meta.url),'utf8');
for(const required of ['public.defectrag_is_staff() and public.defectrag_has_aal2()','p_confirmed is distinct from true',"review->>'decision' is distinct from 'Reject'",'p_version is distinct from c.version','unique(case_id,version,kind)','primary key(event_id,effect)',"state='uncertain'",'for update skip locked'])assert.ok(sql.includes(required),required);
assert.ok(!/grant .*insert.* to authenticated/i.test(sql));
assert.ok(sql.includes('auth.uid() is distinct from c.owner_id and auth.uid() is distinct from c.supervisor_id'));
assert.ok(sql.indexOf('auth.uid() is distinct from c.owner_id')<sql.indexOf('if p_version is distinct from c.version'));
// Contract checks are NOT an executed Postgres/RLS integration test.
});
test('new JSX compiles and retains existing-style labels, metrics and explicit confirmation',async()=>{
const r=await build({entryPoints:[new URL('../src/CorrectiveActions.jsx',import.meta.url).pathname.replace(/^\/(.:)/,'$1')],bundle:true,write:false,platform:'browser',logLevel:'silent'});assert.ok(r.outputFiles[0].text.length>0);
const source=await readFile(new URL('../src/CorrectiveActions.jsx',import.meta.url),'utf8');
assert.ok(source.includes('disabled={busy||!!creationBlocked}'));assert.ok(source.includes('defectrag_ca_staff'));assert.ok(source.includes('Open photo and review'));
assert.ok(source.includes('nextCaseStates(current.state).length>0&&canEdit&&<form'));
assert.ok(source.includes('can update this case.'));
});
test('real JSX renders existing card design but waits for native CA assurance before showing private forms',async()=>{
const root=fileURLToPath(new URL('..',import.meta.url));
const r=await build({stdin:{contents:"export {default} from './src/CorrectiveActions.jsx'",resolveDir:root,loader:'jsx'},bundle:true,write:false,platform:'node',format:'cjs',external:['react'],logLevel:'silent',plugins:[{name:'fixture-cloud',setup(b){b.onResolve({filter:/^\.\/cloud\.js$/},()=>({path:'cloud',namespace:'fixture'}));b.onLoad({filter:/.*/,namespace:'fixture'},()=>({contents:'export async function cloudClient(){throw Error("No network during rendering")} export function workspaceChanged(){}'}))}}]});
const require=createRequire(new URL('../package.json',import.meta.url)),module={exports:{}};new Function('require','module','exports',r.outputFiles[0].text)(require,module,module.exports);
const React=require('react'),{renderToStaticMarkup}=require('react-dom/server');
const props={user:{id:uuid},inspection:{id:'a'.repeat(32)},notes:[{id:uuid,created_at:'2026-10-20',body:reviewBody('Reviewed','Reject')}],onEvidence(){}};
const html=renderToStaticMarkup(React.createElement(module.exports.default,props));
for(const text of ['Corrective Actions','class="card case-workspace"','Fix confirmed defects','Checking your access']){
assert.ok(html.includes(text),text);
}
assert.ok(!html.includes('Create corrective action'));assert.ok(!html.includes('Save case update'));
});
test('effect callback forwards the optional person-responsible copy address unchanged',async()=>{
const r=await handleCA(request(true,{event_id:uuid,lease_token:lease,operation:'begin'}),{env,fetcher:async()=>Response.json({execute:true,done:false,recipient_email:'supervisor@example.invalid',cc_email:'owner@example.invalid',case_id:uuid,kind:'created',state:'Open',priority:'High',due_at:'2030-01-01T00:00:00Z'})});
const b=await r.json();assert.equal(b.recipient_email,'supervisor@example.invalid');assert.equal(b.cc_email,'owner@example.invalid');
});
test('n8n workflows copy the person responsible and require every recipient to be accepted',async()=>{
for(const file of ['corrective-actions.json','corrective-actions-live-qa.json']){
const w=JSON.parse(await readFile(new URL('../../deployment/n8n/'+file,import.meta.url),'utf8'));
const send=w.nodes.find(n=>n.type==='n8n-nodes-base.emailSend');
assert.equal(send.parameters.toEmail,'={{ $json.recipient_email }}');assert.equal(send.parameters.options.ccEmail,"={{ $json.cc_email || '' }}");
const verify=new Function('$input','$',w.nodes.find(n=>n.name==='Verify supervisor acceptance').parameters.jsCode);
const reserved={recipient_email:'supervisor@example.invalid',cc_email:'owner@example.invalid'},event={event_id:uuid,lease_token:lease};
const $=reserve=>name=>({first:()=>({json:name==='Reserve notification effect'?reserve:event})});
const input=accepted=>({first:()=>({json:{accepted,rejected:[],messageId:'<id@sink>'}})});
assert.equal(verify(input(['SUPERVISOR@example.invalid','owner@example.invalid']),$(reserved))[0].json.receipt,'<id@sink>');
assert.throws(()=>verify(input(['supervisor@example.invalid']),$(reserved)),/SMTP acceptance not verified/);
assert.equal(verify(input(['supervisor@example.invalid']),$({...reserved,cc_email:null}))[0].json.operation,'finish');
}
});
|