2026-09-10 18:27:41 -07:00
import test from "node:test" ;
import assert from "node:assert/strict" ;
import fs from "node:fs/promises" ;
import path from "node:path" ;
import os from "node:os" ;
import { execFile as callback } from "node:child_process" ;
import { promisify } from "node:util" ;
import { snapshotRepository , checkWorkspaceCandidate } from "../src/capability-language/candidate-check.js" ;
const execFile = promisify ( callback );
test ( "candidate snapshots include dirty and new files without changing Git history or index" , async ( context ) => {
const directory = await fs . mkdtemp ( path . join ( os . tmpdir (), "qx-candidate-test-" ));
context . after (() => fs . rm ( directory , { recursive : true , force : true }));
const root = path . join ( directory , "source" );
await fs . mkdir ( root );
const git = (... args : string []) => execFile ( "git" , [ "-C" , root , ... args ]);
await git ( "init" );
await fs . writeFile ( path . join ( root , "tracked" ), "original" );
await fs . writeFile ( path . join ( root , ".gitignore" ), "ignored\n" );
await git ( "add" , "." );
const index = await fs . readFile ( path . join ( root , ".git/index" ));
await fs . writeFile ( path . join ( root , "tracked" ), "edited" );
await fs . writeFile ( path . join ( root , "new" ), "new content" );
await fs . writeFile ( path . join ( root , "ignored" ), "not source" );
const snapshot = await snapshotRepository ( root , path . join ( directory , "snapshot" ));
assert . equal ( await fs . readFile ( path . join ( snapshot . directory , "tracked" ), "utf8" ), "edited" );
assert . equal ( await fs . readFile ( path . join ( snapshot . directory , "new" ), "utf8" ), "new content" );
await assert . rejects ( fs . access ( path . join ( snapshot . directory , "ignored" )));
assert . deepEqual ( await fs . readFile ( path . join ( root , ".git/index" )), index );
await assert . rejects ( git ( "rev-parse" , "--verify" , "HEAD" ), "no commit was created" );
await fs . symlink ( "tracked" , path . join ( root , "link" ));
await assert . rejects ( snapshotRepository ( root , path . join ( directory , "rejected" )), /regular file/ );
});
test ( "candidate check produces explicitly non-activation evidence and never overwrites a report" , async ( context ) => {
const directory = await fs . mkdtemp ( path . join ( os . tmpdir (), "qx-check-test-" ));
context . after (() => fs . rm ( directory , { recursive : true , force : true }));
const root = path . join ( directory , "source" ), output = path . join ( directory , "check" );
await fs . mkdir ( root );
2026-09-13 22:07:18 -07:00
await execFile ( "jj" , [ "git" , "init" , "--colocate" , root ]);
2026-09-10 18:27:41 -07:00
await fs . writeFile ( path . join ( root , "quixos.lock" ), `quixos-lock version 1 { quixos source { repository "https://example.test/quixos.git"; commit " ${ "a" . repeat ( 40 ) } "; } }` );
await fs . writeFile ( path . join ( root , "workspace.qx" ), `workspace Test id "workspace:test" revision "workspace:test@1" commit " ${ "b" . repeat ( 40 ) } " { atom Subject id "atom:subject"; }` );
const result = await checkWorkspaceCandidate ({ root , output });
assert . equal ( result . candidateOnly , true );
assert . equal ( result . activationEvidence , false );
assert . deepEqual ( result . blockers , []);
const report = await fs . readFile ( path . join ( output , "report.json" ));
await assert . rejects ( checkWorkspaceCandidate ({ root , output }), /EEXIST/ );
assert . deepEqual ( await fs . readFile ( path . join ( output , "report.json" )), report );
});