import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, writeFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; // Exercise the real CLI, including both API orchestration paths. No network. for (const [project, pkg, canary, workflow] of [ [".", "@bitbaum/sitekit", "bitbaum/biaslens", "deploy.yml"], [ "templates/app", "@bitbaum/sitekit", "bitbaum/biaslens", "deploy.yml", ], [".", "@bitbaum/writingkit", "bitbaum/orangecat", "cd.yml"], ]) { test(`${project} ${pkg}: the CLI enforces served receipts as well as green workflows`, () => { const dir = mkdtempSync(join(tmpdir(), "rollout-cli-")); const tip = "c".repeat(40); const lock = (version) => `lockfileVersion: '9.0'\nimporters:\n .:\n dependencies:\n '${pkg}':\n specifier: ^${version}\n version: ${version}\n${pkg === "@bitbaum/writingkit" ? " '@bitbaum/product-local':\n specifier: workspace:*\n version: link:packages/local\n" : ""}`; const receipt = { formatVersion: 1, repo: canary, commit: tip, project: ".", createdAt: "2026-10-20T00:00:00Z", lockSha256: createHash("sha256") .update(lock("0.4.2")) .digest("hex"), artifacts: { [pkg]: "0.4.2" }, versions: { [pkg]: "0.4.2" }, }; if (pkg === "@bitbaum/writingkit") { receipt.artifacts["@bitbaum/product-local"] = `https://codeload.github.com/${canary}/tar.gz/${tip}`; receipt.versions["@bitbaum/product-local"] = "0.2.0"; } try { writeFileSync( join(dir, "gh"), `#!${process.execPath} const [verb, path] = process.argv.slice(2); const project = process.env.FIXTURE_PROJECT; const pkg = ${JSON.stringify(pkg)}, canary = ${JSON.stringify(canary)}, workflow = ${JSON.stringify(workflow)}; const tip = 'c'.repeat(40); const lock = ${lock.toString()}; const manifest = v => JSON.stringify({name:'fixture-app',dependencies:{[pkg]:'^'+v,...(pkg==='@bitbaum/writingkit'?{'@bitbaum/product-local':'workspace:*'}:{})}}); let result; if (verb !== 'api') throw Error('unexpected CLI invocation'); if (path === 'repos/bitbaum/example/pulls/1') result={base:{sha:'a'.repeat(40)},head:{sha:'b'.repeat(40)}}; else if (path.startsWith('repos/bitbaum/example/compare/'+'a'.repeat(40)+'...'+'b'.repeat(40))) result={merge_base_commit:{sha:'a'.repeat(40)}}; else if (path.includes('/pulls/1/files?')) result=[{filename:(project==='.'?'':project+'/')+'package.json'},{filename:(project==='.'?'':project+'/')+'pnpm-lock.yaml'}]; else if (path === 'repos/'+canary) result={default_branch:'main'}; else if (path === 'repos/'+canary+'/commits/main') result={sha:tip}; else if (path.includes('/actions/workflows/'+workflow+'/runs?')) result={workflow_runs:[{conclusion:'success',head_sha:tip,updated_at:'2026-10-20T00:00:00Z'}]}; else if (path.includes('/actions/runs?')) result={workflow_runs:['ci.yml','browser-smoke.yml'].map(p=>({path:'.github/workflows/'+p,event:'push',status:'completed',conclusion:'success',head_sha:new URL('https://api.github.com/'+path).searchParams.get('head_sha')}))}; else if (path.includes('/contents/')) { const [repo,file,ref]=/^repos\\/([^/]+\\/[^/]+)\\/contents\\/(.+)\\?ref=(.+)$/.exec(path).slice(1); const prefix=project==='.'?'':project+'/'; const name=repo===canary?file:file.startsWith(prefix)?file.slice(prefix.length):null; const v=repo===canary||ref==='b'.repeat(40)?'0.4.2':'0.4.0'; const content=name==='package.json'?manifest(v):name==='pnpm-lock.yaml'?lock(v):name==='packages/local/package.json'?JSON.stringify({name:process.env.FIXTURE_LOCAL_BAD?'wrong-name':'@bitbaum/product-local',version:'0.2.0'}):null; if(content!==null) result={content:Buffer.from(content).toString('base64')}; } if(result===undefined){console.error('404');process.exit(1)} console.log(JSON.stringify(result)); `, { mode: 0o755 }, ); writeFileSync( join(dir, "fetch.mjs"), `globalThis.fetch = async (url) => { if(new URL(url).hostname!==${JSON.stringify(canary === "bitbaum/orangecat" ? "orangecat.ch" : "biaslens.orangecat.ch")}) throw Error('unexpected network target'); return new Response(process.env.FIXTURE_RECEIPT,{status:Number(process.env.FIXTURE_STATUS??200),headers:{'content-type':'application/json'}}); };`, ); const run = (value, status = 200, localBad = false) => spawnSync( process.execPath, [ "--import", join(dir, "fetch.mjs"), new URL("./package-rollout-gate.mjs", import.meta.url) .pathname, "bitbaum/example", "1", ], { encoding: "utf8", env: { ...process.env, PATH: dir + ":" + process.env.PATH, FIXTURE_PROJECT: project, FIXTURE_RECEIPT: JSON.stringify(value), FIXTURE_STATUS: String(status), FIXTURE_LOCAL_BAD: localBad ? "1" : "", }, }, ); const good = run(receipt); assert.equal(good.status, 0, good.stdout + good.stderr); for (const bad of [ { ...receipt, commit: "d".repeat(40) }, { ...receipt, lockSha256: "0".repeat(64) }, { ...receipt, artifacts: {} }, ]) { const held = run(bad); assert.equal(held.status, 1, held.stdout + held.stderr); assert.match( held.stdout, /wait for .* to deploy this artifact/, ); } assert.equal(run(receipt, 503).status, 2); if (pkg === "@bitbaum/writingkit") assert.equal(run(receipt, 200, true).status, 2); } finally { rmSync(dir, { recursive: true, force: true }); } }); } test("standalone nested starters use their own lockfile for package rollout evidence", () => { const dir = mkdtempSync(join(tmpdir(), "rollout-nested-cli-")); const base = "a".repeat(40); const head = "b".repeat(40); const tip = "c".repeat(40); const standaloneLock = (version) => `lockfileVersion: '9.0'\nimporters:\n .:\n dependencies:\n '@bitbaum/ai-kit':\n specifier: ^${version}\n version: ${version}\n`; const canaryLock = `lockfileVersion: '9.0'\nimporters:\n .:\n apps/web:\n dependencies:\n '@bitbaum/ai-kit':\n specifier: ^1.21.4\n version: 1.21.4\n '@bitbaum/chatkit':\n specifier: ^0.15.3\n version: 0.15.3\n packages/ai:\n dependencies:\n '@bitbaum/ai-kit':\n specifier: ^1.21.4\n version: 1.21.4\n`; const rootLock = "lockfileVersion: '9.0'\nimporters:\n .:\n"; const receiptArtifacts = { "@bitbaum/ai-kit": "1.21.4", "@bitbaum/chatkit": "0.15.3" }; const receipt = { formatVersion: 1, repo: "bitbaum/kivvi", commit: tip, project: "apps/web", createdAt: new Date().toISOString(), lockSha256: createHash("sha256").update(canaryLock).digest("hex"), artifacts: receiptArtifacts, versions: receiptArtifacts, }; try { writeFileSync( join(dir, "gh"), `#!${process.execPath} const [verb,path] = process.argv.slice(2); const base=${JSON.stringify(base)}, head=${JSON.stringify(head)}, tip=${JSON.stringify(tip)}; const lock=${standaloneLock.toString()}; const canaryLock=${JSON.stringify(canaryLock)}; const rootLock=${JSON.stringify(rootLock)}; const encode = value => ({content:Buffer.from(value).toString('base64')}); let result; if(verb!=='api') throw Error('unexpected CLI invocation'); if(path==='repos/bitbaum/loki/pulls/1') result={base:{sha:base},head:{sha:head}}; else if(path.startsWith('repos/bitbaum/loki/compare/'+base+'...'+head)) result={merge_base_commit:{sha:base}}; else if(path.includes('/pulls/1/files?')) result=[{filename:'scripts/site-template/package.json'},{filename:'scripts/site-template/pnpm-lock.yaml'}]; else if(path==='repos/bitbaum/kivvi') result={default_branch:'main'}; else if(path==='repos/bitbaum/kivvi/commits/main') result={sha:tip}; else if(path.includes('repos/bitbaum/kivvi/actions/workflows/deploy.yml/runs?')) result={workflow_runs:[{conclusion:'success',head_sha:tip,updated_at:new Date().toISOString()}]}; else if(path.includes('repos/bitbaum/kivvi/actions/runs?')) result={workflow_runs:['ci.yml','browser-smoke.yml'].map(p=>({path:'.github/workflows/'+p,event:'push',status:'completed',conclusion:'success',head_sha:new URL('https://api.github.com/'+path).searchParams.get('head_sha')}))}; else if(path.includes('repos/bitbaum/loki/actions/runs?')) result={workflow_runs:[{id:99,path:'.github/workflows/ci.yml',event:'pull_request',status:'completed',conclusion:'success',head_sha:new URL('https://api.github.com/'+path).searchParams.get('head_sha')}]}; else if(path==='repos/bitbaum/loki/actions/runs/99/jobs?filter=latest&per_page=100') result={total_count:1,jobs:[{name:'check',status:'completed',conclusion:'success'}]}; else if(path.includes('/contents/')) { const [repo,file,ref]=/^repos\\/([^/]+\\/[^/]+)\\/contents\\/(.+)\\?ref=(.+)$/.exec(path).slice(1); let value=null; if(repo==='bitbaum/loki' && file==='package.json') value=JSON.stringify({name:'loki',dependencies:{}}); if(repo==='bitbaum/loki' && file==='pnpm-lock.yaml') value=rootLock; if(repo==='bitbaum/loki' && file==='scripts/site-template/package.json') value=JSON.stringify({name:'starter',dependencies:{'@bitbaum/ai-kit':'^'+(ref===base?'1.21.0':'1.21.4')}}); if(repo==='bitbaum/loki' && file==='scripts/site-template/pnpm-lock.yaml') value=lock(ref===base?'1.21.0':'1.21.4'); if(repo==='bitbaum/kivvi' && file==='packages/ai/package.json') value=JSON.stringify({name:'@kivvi/ai',dependencies:{'@bitbaum/ai-kit':'^1.21.4'}}); if(repo==='bitbaum/kivvi' && file==='apps/web/package.json') value=JSON.stringify({name:'@kivvi/web',dependencies:{'@bitbaum/ai-kit':'^1.21.4','@bitbaum/chatkit':'^0.15.3'}}); if(repo==='bitbaum/kivvi' && file==='pnpm-lock.yaml') value=canaryLock; if(value!==null) result=encode(value); } if(result===undefined){console.error('404');process.exit(1)} console.log(JSON.stringify(result)); `, { mode: 0o755 }, ); writeFileSync( join(dir, "fetch.mjs"), `globalThis.fetch = async (url) => { if(new URL(url).hostname!=='kivvi.orangecat.ch') throw Error('unexpected network target'); return new Response(${JSON.stringify(JSON.stringify(receipt))},{status:200,headers:{'content-type':'application/json'}}); };`, ); const run = spawnSync( process.execPath, [ "--import", join(dir, "fetch.mjs"), new URL("./package-rollout-gate.mjs", import.meta.url).pathname, "bitbaum/loki", "1", ], { encoding: "utf8", env: { ...process.env, PATH: `${dir}:${process.env.PATH}` } }, ); assert.equal(run.status, 1, run.stdout + run.stderr); assert.match(run.stdout, /@bitbaum\/ai-kit@1\.21\.4: wait for bitbaum\/kivvi/); assert.doesNotMatch(run.stdout + run.stderr, /no importer-specific lock evidence/); } finally { rmSync(dir, { recursive: true, force: true }); } }); // bitbaum/diplodoctor#59 (2026-10-31): a branch forked before the base moved // @bitbaum/chatkit 0.21.2 -> 0.21.3 never touched a manifest, yet measuring it // from `pr.base.sha` read as a downgrade to 0.21.2, which no canary would ever // run again. The sweep holds before it refreshes a behind branch, so the PR // could never clear. The PR's change is measured from its merge base. test("a stale branch that does not touch the lockfile is not read as a downgrade", () => { const dir = mkdtempSync(join(tmpdir(), "rollout-stale-cli-")); const fork = "e".repeat(40); try { writeFileSync( join(dir, "gh"), `#!${process.execPath} const [verb, path] = process.argv.slice(2); const base='a'.repeat(40), head='b'.repeat(40), fork=${JSON.stringify(fork)}; const lock = v => "lockfileVersion: '9.0'\\nimporters:\\n .:\\n dependencies:\\n '@bitbaum/chatkit':\\n specifier: ^"+v+"\\n version: "+v+"\\n"; const manifest = v => JSON.stringify({name:'fixture-app',dependencies:{'@bitbaum/chatkit':'^'+v}}); let result; if (verb !== 'api') throw Error('unexpected CLI invocation'); if (path === 'repos/bitbaum/example/pulls/1') result={base:{sha:base},head:{sha:head}}; else if (path.startsWith('repos/bitbaum/example/compare/'+base+'...'+head)) result={merge_base_commit:{sha:fork}}; else if (path.includes('/pulls/1/files?')) result=[{filename:'lib/auth/recovery.test.ts'}]; else if (path.includes('/contents/')) { const [file,ref]=/^repos\\/bitbaum\\/example\\/contents\\/(.+)\\?ref=(.+)$/.exec(path).slice(1); const v = ref===base ? '0.21.3' : '0.21.2'; const content = file==='package.json'?manifest(v):file==='pnpm-lock.yaml'?lock(v):null; if (content!==null) result={content:Buffer.from(content).toString('base64')}; } if(result===undefined){console.error('404');process.exit(1)} console.log(JSON.stringify(result)); `, { mode: 0o755 }, ); const run = spawnSync( process.execPath, [new URL("./package-rollout-gate.mjs", import.meta.url).pathname, "bitbaum/example", "1"], { encoding: "utf8", env: { ...process.env, PATH: dir + ":" + process.env.PATH } }, ); assert.equal(run.status, 0, run.stdout + run.stderr); assert.doesNotMatch(run.stdout, /chatkit@0\.21\.2/); } finally { rmSync(dir, { recursive: true, force: true }); } });