Files
order-notification-automation/tests/assistant/repair-validator.test.cjs
T

25 lines
2.5 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),{execFileSync,spawnSync}=require('node:child_process');
test('host repair validator runs fixed and changed tests in a foreign-owned isolated checkout',t=>{
const base=fs.mkdtempSync(path.join(os.tmpdir(),'repair-validator-'));
t.after(()=>fs.rmSync(base,{recursive:true,force:true}));
const host=path.join(base,'host'),repo=path.join(base,'repo'),checkout=path.join(base,'checkout'),validator=path.join(host,'src','modules','assistant','repair-validator.cjs');
fs.mkdirSync(path.dirname(validator),{recursive:true});fs.mkdirSync(path.join(repo,'tests'),{recursive:true});
fs.copyFileSync(path.resolve(__dirname,'../../src/modules/assistant/repair-validator.cjs'),validator);
fs.writeFileSync(path.join(host,'package.json'),JSON.stringify({scripts:{'test:business':'node --test --test-concurrency=1 tests/fixed.test.cjs'}}));
const git=(cwd,...args)=>execFileSync('git',['-c','user.name=Test','-c','user.email=test@example.invalid',...args],{cwd,encoding:'utf8',stdio:['ignore','pipe','pipe']});
const receipt=path.join(base,'ran-tests.txt'),testSource=name=>"require('node:test')("+JSON.stringify(name)+",()=>require('node:fs').appendFileSync("+JSON.stringify(receipt)+","+JSON.stringify(name+'\n')+"));";
fs.writeFileSync(path.join(repo,'tests/fixed.test.cjs'),testSource('fixed manifest test'));
git(repo,'init');git(repo,'add','.');git(repo,'commit','-m','baseline');git(repo,'worktree','add','--detach',checkout);
fs.writeFileSync(path.join(checkout,'tests/changed.test.cjs'),testSource('changed repair test'));
git(checkout,'add','.');git(checkout,'commit','-m','proposal');
const env={...process.env,GIT_CONFIG_NOSYSTEM:'1',GIT_CONFIG_GLOBAL:process.platform==='win32'?'NUL':'/dev/null',GIT_TEST_ASSUME_DIFFERENT_OWNER:'1'};
delete env.NODE_TEST_CONTEXT; // Production runner also excludes test-runner internals.
const result=spawnSync(process.execPath,[validator],{cwd:checkout,env,encoding:'utf8',windowsHide:true});
assert.equal(result.status,0,result.stderr);assert.deepEqual(fs.readFileSync(receipt,'utf8').trim().split('\n').sort(),['changed repair test','fixed manifest test']);
// Trust applies to this one invocation; it must not persist globally or locally.
const untrusted=spawnSync('git',['rev-parse','HEAD'],{cwd:checkout,env,encoding:'utf8',windowsHide:true});
assert.notEqual(untrusted.status,0);assert.match(untrusted.stderr,/dubious ownership/);
});