← back to Adsense Fleet Viewer
verification/TK-11352-health/fixture-check.cjs
48 lines
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const crypto = require('node:crypto');
const assert = require('node:assert/strict');
const sourcePath = process.argv[2] || path.join(__dirname,'status-candidate.mjs');
const originalPath = '/Users/macstudio3/.claude/skills/adsense-manager/scripts/status.mjs';
const source = fs.readFileSync(sourcePath, 'utf8');
const capture = JSON.parse(fs.readFileSync(path.join(__dirname,'console-page1.json'), 'utf8'));
const fleetDir = '/Users/macstudio3/Projects/dw-domain-fleet/sites';
const domains = new Set(fs.readdirSync(fleetDir).filter(f=>f.endsWith('.json')).map(f=>{try{return JSON.parse(fs.readFileSync(path.join(fleetDir,f),'utf8'));}catch{return null;}}).filter(s=>s?.monetize===true).map(s=>s.domain));
for(let i=0;domains.size<124;i++) domains.add('fixture-'+i+'.example.com');
const syntheticFullText = [...domains].map(d=>[d,'Ready','—','Authorized'].join('\n')).join('\n')+'\n1 - 124 of 124';
const results = [];
for (const envelope of ['actual-openclaw-envelope', 'unwrapped-control', 'wrong-target', 'wrong-url', 'missing-total', 'missing-target', 'synthetic-complete']) {
const writes = [], calls = [];
let extracted;
const fakeFS = { ...fs, mkdirSync(){}, writeFileSync(p, data){writes.push({path:p, data:JSON.parse(data)});} };
const execSync = cmd => {
calls.push(cmd.split(' --fn ')[0]);
if (cmd.startsWith('openclaw browser status')) return JSON.stringify({running:true});
if (cmd.startsWith('openclaw browser navigate') || cmd === 'sleep 3') return '';
if (cmd.startsWith('openclaw browser evaluate')) {
const fn = JSON.parse(cmd.split(' --fn ')[1].replace(/ 2>\/dev\/null$/, ''));
extracted = vm.runInNewContext('(' + fn + ')()', {document:{body:{innerText:envelope === 'synthetic-complete' ? syntheticFullText : envelope === 'missing-total' ? capture.result.text.replace('1 - 50 of 124','rows unavailable') : capture.result.text}}});
return JSON.stringify(envelope === 'unwrapped-control' ? extracted : {ok:true,targetId:envelope === 'wrong-target' ? 'ANOTHER_TARGET' : capture.targetId,url:envelope === 'wrong-url' ? 'https://example.com/' : capture.url,result:extracted});
}
throw Error('Unexpected subprocess request: '+cmd);
};
const executable = source.replace(/^import .*;\n/gm,'').replaceAll('import.meta.url', JSON.stringify(sourcePath));
vm.runInNewContext(executable, {execSync,fs:fakeFS,path,fileURLToPath:x=>originalPath,process:{env:{ADSENSE_BROWSER_TARGET:envelope === 'missing-target' ? '' : capture.targetId}},console:{log(){}},Date,Set,JSON}, {timeout:5000});
const out = writes.at(-1).data;
results.push({scenario:envelope, extracted_rows:extracted?.rows.length || 0, extracted_total:extracted?.total ?? null, expected_total:124, output_source:out.source, output_complete:out.complete, output_count:out.count, output_expected_count:out.expected_count, output_tally:out.tally, calls, writes_intercepted:writes.map(w=>w.path)});
}
for (const r of results) {
if(r.scenario === 'synthetic-complete') {assert.equal(r.output_complete,true);assert.equal(r.output_count,124);assert.equal(r.output_expected_count,124);assert.equal(r.output_tally.Ready,124);continue;}
assert.equal(r.output_complete,false,r.scenario+' must fail closed');
if (['actual-openclaw-envelope','unwrapped-control'].includes(r.scenario)) {
assert.equal(r.output_source,'openclaw-console'); assert.equal(r.output_expected_count,124);
assert.equal(r.output_tally['Getting ready'],47); assert.equal(r.output_tally['Requires review'],3);
} else if (r.scenario === 'missing-total') { assert.equal(r.output_expected_count,null); }
else { assert.equal(r.output_source,'unavailable'); }
if (r.scenario === 'missing-target') assert.equal(r.calls.length,0);
}
const report={assertions:'PASS: six partial/invalid scenarios plus one synthetic complete inventory',timestamp:new Date().toISOString(),source_path:sourcePath,source_sha256:crypto.createHash('sha256').update(source).digest('hex'),fixture:path.join(__dirname,'console-page1.json'),mode:'in-memory subprocess and write interception; no browser/network/shared-state writes',results};
fs.writeFileSync(process.argv[3] || '/tmp/TK-11352-recheck-results.json', JSON.stringify(report,null,2));
console.log(JSON.stringify(report,null,2));