Files
AFFiNE/scripts/ci/classify-web-host-only.test.mjs
T
DarkSky 75483d366d ci(frontend): Skip unrelated checks for Desktop changes
Keep the macOS Desktop tests and check desktop-harness with harness features on macOS.
Skip workspace, iOS, and editor checks for Desktop leaf changes.
Skip the JS room tests when all changes are in Desktop Rust files.
2026-12-03 08:50:50 +09:00

493 lines
15 KiB
JavaScript

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 { 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 six pure web-host paths', () => {
assert.deepEqual(
[...WEB_HOST_ONLY_ALLOWLIST],
[
'packages/frontend/hosts/web/src/idb-storage.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 real repo, missing base, and CLI', () => {
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,
});
writeFileSync(allowFile, 'export const v = 1;\n');
writeFileSync(otherFile, otherContent);
git('add', '.');
git('commit', '-m', 'base');
const baseSha = git('rev-parse', 'HEAD');
// Empty diff against self -> false.
assert.equal(
classifyGitDiff({
baseSha,
ref: 'refs/heads/darksky/np/mobile-test',
cwd: tmpRepo,
}).webHostOnly,
false
);
// Pure allowlist commit -> true.
writeFileSync(allowFile, 'export const v = 2;\n');
git('add', '.');
git('commit', '-m', 'web host only');
const cliOut = execFileSync(
process.execPath,
[
CLI_PATH,
'--base',
baseSha,
'--ref',
'refs/heads/darksky/np/mobile-test',
'--github-output',
outputFile,
],
{ cwd: tmpRepo, encoding: 'utf8' }
);
assert.match(cliOut, /^web_host_only=true /);
assert.equal(
readFileSync(outputFile, 'utf8'),
'web_host_only=true\ndesktop_leaf_only=false\ndesktop_rs_only=false\n'
);
const webHostSha = git('rev-parse', 'HEAD');
const desktopDir = join(tmpRepo, 'packages/frontend/desktop/src');
const desktopFile = join(desktopDir, 'new.rs');
mkdirSync(desktopDir, { recursive: true });
writeFileSync(desktopFile, 'pub fn unique() {}\n');
git('add', desktopFile);
git('commit', '-m', 'add Desktop source');
const desktop = classifyGitDiff({ baseSha: webHostSha, cwd: tmpRepo });
assert.equal(desktop.desktopLeafOnly, true);
assert.equal(desktop.desktopRsOnly, true);
const mixed = classifyGitDiff({ baseSha, cwd: tmpRepo });
assert.equal(mixed.desktopLeafOnly, true);
assert.equal(mixed.desktopRsOnly, false);
assert.equal(mixed.webHostOnly, false);
writeFileSync(desktopFile, otherContent);
git('add', desktopFile);
git('commit', '-m', 'copy into Desktop source');
const desktopCopy = classifyGitDiff({ baseSha: webHostSha, cwd: tmpRepo });
assert.equal(desktopCopy.desktopLeafOnly, false);
assert.equal(desktopCopy.desktopRsOnly, false);
git('reset', '--hard', 'HEAD~2');
// Unreachable / non-existent base -> false.
assert.equal(
classifyGitDiff({
baseSha: 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
ref: 'refs/heads/darksky/np/mobile-test',
cwd: tmpRepo,
}).webHostOnly,
false
);
// Brand-new unique allowlisted file -> still true after copy re-check.
writeFileSync(
desktopHtmlFile,
'<!doctype html><title>AFFiNE Workbench</title>\n'
);
git('add', desktopHtmlFile);
git('commit', '-m', 'add unique allowlisted html');
assert.equal(
classifyGitDiff({
baseSha,
ref: 'refs/heads/darksky/np/mobile-test',
cwd: tmpRepo,
}).webHostOnly,
true
);
// Copying an unmodified non-allowlisted file into a newly added
// allowlisted file -> false (caught by --find-copies-harder).
writeFileSync(desktopStartFile, otherContent);
git('add', desktopStartFile);
git('commit', '-m', 'copy non-allowlisted file into allowlisted path');
const copyResult = classifyGitDiff({
baseSha,
ref: 'refs/heads/darksky/np/mobile-test',
cwd: tmpRepo,
});
assert.equal(copyResult.webHostOnly, false);
assert.equal(copyResult.reason, 'non_allowlist_path');
// Adding a non-allowlisted file -> false.
writeFileSync(otherFile, 'export const s = 2;\n');
git('add', otherFile);
git('commit', '-m', 'touch helper script');
assert.equal(
classifyGitDiff({
baseSha,
ref: 'refs/heads/darksky/np/mobile-test',
cwd: tmpRepo,
}).webHostOnly,
false
);
} finally {
rmSync(tmpRepo, { recursive: true, force: true });
}
});