import assert from 'node:assert/strict'; import { execFileSync } from 'node:child_process'; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import test from 'node:test'; import { fileURLToPath } from 'node:url'; import { classifyGitDiff, classifyWebHostOnly, WEB_HOST_ONLY_ALLOWLIST, ZERO_SHA, } from './classify-web-host-only.mjs'; const REPO_ROOT = fileURLToPath(new URL('../..', import.meta.url)); const CLI_PATH = join(REPO_ROOT, 'scripts/ci/classify-web-host-only.mjs'); const SAMPLE_BASE = '65460a7f136d32b15d0d4398f0f4976273c51c9d'; await test('allowlist remains frozen to the seven pure web-host paths', () => { assert.deepEqual( [...WEB_HOST_ONLY_ALLOWLIST], [ 'packages/frontend/hosts/web/src/idb-storage.js', 'packages/frontend/hosts/web/desktop/boot.js', 'packages/frontend/hosts/web/desktop/start.js', 'packages/frontend/hosts/web/desktop/index.html', 'packages/frontend/hosts/web/tests/idb-storage.test.mjs', 'packages/frontend/hosts/web/tests/desktop.test.mjs', 'packages/frontend/hosts/web/tests/dual-shell-save.browser.mjs', ] ); }); await test('classifyWebHostOnly handles allowlist, rename, and guards', () => { const cases = [ { name: 'single allowlisted file modification', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-web-fix', nameStatusLines: ['M\tpackages/frontend/hosts/web/src/idb-storage.js'], }, expected: true, }, { name: 'multi-file allowlist modification matching e7236462b016', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-wave-h2-idb-quota', nameStatusLines: [ 'M\tpackages/frontend/hosts/web/desktop/start.js', 'M\tpackages/frontend/hosts/web/src/idb-storage.js', 'M\tpackages/frontend/hosts/web/tests/desktop.test.mjs', 'M\tpackages/frontend/hosts/web/tests/idb-storage.test.mjs', ], }, expected: true, }, { name: 'allowlist files mixed with Rust source or Cargo.toml', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-mixed', nameStatusLines: [ 'M\tpackages/frontend/hosts/web/src/idb-storage.js', 'M\tpackages/frontend/Cargo.toml', ], }, expected: false, }, { name: 'allowlist files mixed with .rs file', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-mixed-rs', nameStatusLines: [ 'M\tpackages/frontend/hosts/web/desktop/start.js', 'M\tpackages/frontend/mobile/src/lib.rs', ], }, expected: false, }, { name: 'preview-workers.js included by Rust tests must be false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-preview-worker', nameStatusLines: [ 'M\tblocksuite/crates/playground/web/preview-workers.js', ], }, expected: false, }, { name: 'R100 rename inside allowlist is true', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-rename-in', nameStatusLines: [ [ 'R100', 'packages/frontend/hosts/web/desktop/start.js', 'packages/frontend/hosts/web/src/idb-storage.js', ].join('\t'), ], }, expected: true, }, { name: 'R100 rename from outside allowlist into allowlist is false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-rename-into', nameStatusLines: [ [ 'R100', 'packages/frontend/hosts/web/src/other.js', 'packages/frontend/hosts/web/src/idb-storage.js', ].join('\t'), ], }, expected: false, }, { name: 'R100 rename from allowlist to outside allowlist is false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-rename-out', nameStatusLines: [ [ 'R100', 'packages/frontend/hosts/web/desktop/start.js', 'packages/frontend/hosts/web/desktop/start-v2.js', ].join('\t'), ], }, expected: false, }, { name: 'C100 copy from outside allowlist into allowlist is false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-copy-into', nameStatusLines: [ [ 'C100', 'packages/frontend/hosts/web/src/other.js', 'packages/frontend/hosts/web/src/idb-storage.js', ].join('\t'), ], }, expected: false, }, { name: 'web host helper script verify-idb-real-browser.mjs must be false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-script', nameStatusLines: [ 'M\tpackages/frontend/hosts/web/scripts/verify-idb-real-browser.mjs', ], }, expected: false, }, { name: 'empty diff must be false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-empty', nameStatusLines: [], }, expected: false, }, { name: 'forceAll=true overrides allowlist diff to false', input: { baseSha: SAMPLE_BASE, forceAll: true, ref: 'refs/heads/darksky/np/mobile-force', nameStatusLines: ['M\tpackages/frontend/hosts/web/src/idb-storage.js'], }, expected: false, }, { name: 'trunk push to refs/heads/darksky/np/mobile must be false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile', nameStatusLines: ['M\tpackages/frontend/hosts/web/src/idb-storage.js'], }, expected: false, }, { name: 'trunk push to refs/heads/canary must be false', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/canary', nameStatusLines: ['M\tpackages/frontend/hosts/web/src/idb-storage.js'], }, expected: false, }, { name: 'zero SHA base must be false', input: { baseSha: ZERO_SHA, ref: 'refs/heads/darksky/np/mobile-zero', nameStatusLines: ['M\tpackages/frontend/hosts/web/src/idb-storage.js'], }, expected: false, }, { name: 'missing base SHA must be false', input: { baseSha: '', ref: 'refs/heads/darksky/np/mobile-missing-base', nameStatusLines: ['M\tpackages/frontend/hosts/web/src/idb-storage.js'], }, expected: false, }, { name: 'NUL-delimited -z output works for allowlist and rejects invalid status', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-z', nameStatus: 'M\0packages/frontend/hosts/web/desktop/index.html\0' + 'M\0packages/frontend/hosts/web/tests/dual-shell-save.browser.mjs\0', }, expected: true, }, { name: 'unknown git status code fails closed', input: { baseSha: SAMPLE_BASE, ref: 'refs/heads/darksky/np/mobile-unknown', nameStatus: 'U\0packages/frontend/hosts/web/src/idb-storage.js\0', }, expected: false, }, ]; for (const tc of cases) { const result = classifyWebHostOnly(tc.input); assert.equal(result.webHostOnly, tc.expected, tc.name); } }); await test('Desktop lanes distinguish Rust changes from mixed web host changes', () => { const desktop = 'packages/frontend/desktop'; const source = `M\t${desktop}/src/window/mod.rs`; const webHost = 'M\tpackages/frontend/hosts/web/src/idb-storage.js'; const cases = [ ['Desktop source', [source], [false, true, true]], ['Desktop test', [`A\t${desktop}/tests/smoke.rs`], [false, true, true]], ['Desktop and web host', [source, webHost], [false, true, false]], ['web host only', [webHost], [true, false, false]], ['empty diff', [], [false, false, false]], [ 'Desktop manifest', [source, `M\t${desktop}/Cargo.toml`], [false, false, false], ], [ 'workspace lockfile', [source, 'M\tpackages/frontend/Cargo.lock'], [false, false, false], ], ['Desktop asset', [`M\t${desktop}/src/icon.png`], [false, false, false]], ['Desktop build script', [`M\t${desktop}/build.rs`], [false, false, false]], [ 'client source', [source, 'M\tpackages/frontend/client/src/lib.rs'], [false, false, false], ], [ 'BlockSuite source', [source, 'M\tblocksuite/crates/editor/src/lib.rs'], [false, false, false], ], [ 'rename from client', [`R100\tpackages/frontend/client/src/lib.rs\t${desktop}/src/moved.rs`], [false, false, false], ], [ 'rename within Desktop', [`R100\t${desktop}/src/old.rs\t${desktop}/src/new.rs`], [false, true, true], ], ['invalid status', [`U\t${desktop}/src/file.rs`], [false, false, false]], ]; for (const [label, nameStatusLines, expected] of cases) { const { webHostOnly, desktopLeafOnly, desktopRsOnly } = classifyWebHostOnly( { nameStatusLines, baseSha: SAMPLE_BASE, } ); assert.deepEqual( [webHostOnly, desktopLeafOnly, desktopRsOnly], expected, label ); } for (const guard of [ { forceAll: true }, { ref: 'refs/heads/darksky/np/mobile' }, { baseSha: ZERO_SHA }, ]) { const result = classifyWebHostOnly({ nameStatusLines: [source], ...guard }); assert.equal(result.webHostOnly, false, result.reason); assert.equal(result.desktopLeafOnly, false, result.reason); assert.equal(result.desktopRsOnly, false, result.reason); } }); await test('classifyGitDiff handles Git commits, lane guards, and CLI outputs', () => { const tmpRepo = mkdtempSync(join(tmpdir(), 'classify-web-host-')); const outputFile = join(tmpRepo, 'github-output.txt'); try { const git = (...args) => execFileSync('git', args, { cwd: tmpRepo, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], }).trim(); git('init', '-b', 'main'); git('config', 'user.name', 'Test User'); git('config', 'user.email', 'test@example.com'); const allowFile = join( tmpRepo, 'packages/frontend/hosts/web/src/idb-storage.js' ); const desktopHtmlFile = join( tmpRepo, 'packages/frontend/hosts/web/desktop/index.html' ); const desktopStartFile = join( tmpRepo, 'packages/frontend/hosts/web/desktop/start.js' ); const otherFile = join( tmpRepo, 'packages/frontend/hosts/web/scripts/verify-idb-real-browser.mjs' ); const otherContent = [ 'export function runVerify(target) {', ' const steps = ["boot", "sync", "persist", "reload"];', ' return steps.map((step, index) => `${index}:${target}:${step}`);', '}', '', ].join('\n'); mkdirSync(join(tmpRepo, 'packages/frontend/hosts/web/src'), { recursive: true, }); mkdirSync(join(tmpRepo, 'packages/frontend/hosts/web/desktop'), { recursive: true, }); mkdirSync(join(tmpRepo, 'packages/frontend/hosts/web/scripts'), { recursive: true, }); const backend = 'packages/backend/server-domain/src'; const web = 'packages/frontend/hosts/web'; const sharedSource = 'packages/common/store/shared.js'; const sharedContent = 'export const shared = "common store";\n'; mkdirSync(join(tmpRepo, backend), { recursive: true }); mkdirSync(join(tmpRepo, 'packages/common/store'), { recursive: true }); writeFileSync(join(tmpRepo, sharedSource), sharedContent); writeFileSync(join(tmpRepo, backend, 'lib.rs'), 'pub fn first() {}\n'); writeFileSync(join(tmpRepo, web, 'index.html'), '