Files
AFFiNE/scripts/ci/classify-web-host-only.test.mjs
T
DarkSky a28c6e4cbd ci(frontend): skip unrelated mobile checks
Use both paths of copies and renames to classify backend and Web wrapper changes.
Keep Web host and preview checks for Web wrapper changes.
2026-12-03 19:09:43 +09:00

592 lines
18 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 { 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'), '<title>Base</title>\n');
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' +
'backend_only=false\nweb_wrapper_only=true\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
);
const cases = [
{
name: 'backend edits and additions',
files: [
[`${backend}/lib.rs`, 'pub fn second() {}\n'],
[`${backend}/new.rs`, 'pub fn new_file() {}\n'],
],
backendOnly: true,
},
{
name: 'Web wrapper HTML and script edits',
files: [
[`${web}/index.html`, '<title>Updated</title>\n'],
[
`${web}/scripts/verify-idb-real-browser.mjs`,
'export const v = 3;\n',
],
],
webWrapperOnly: true,
},
{
name: 'copy from the common store into an allowlisted Web wrapper path',
files: [[`${web}/desktop/start.js`, sharedContent]],
copyTo: `${web}/desktop/start.js`,
},
{
name: 'copy from the common store into the backend',
files: [[`${backend}/copy.js`, sharedContent]],
copyTo: `${backend}/copy.js`,
},
{
name: 'mixed backend and Web wrapper edits',
files: [
[`${backend}/lib.rs`, 'pub fn second() {}\n'],
[`${web}/index.html`, '<title>Updated</title>\n'],
],
},
];
for (const {
name,
files,
copyTo,
backendOnly = false,
webWrapperOnly = false,
} of cases) {
git('reset', '--hard', baseSha);
for (const [path, content] of files) {
const file = join(tmpRepo, path);
mkdirSync(dirname(file), { recursive: true });
writeFileSync(file, content);
}
git('add', '--', ...files.map(([path]) => path));
git('commit', '-m', name);
if (copyTo) {
assert.equal(
git(
'diff',
'--name-status',
'-C',
'--find-copies-harder',
baseSha,
'HEAD'
),
`C100\t${sharedSource}\t${copyTo}`
);
}
const result = classifyGitDiff({ baseSha, cwd: tmpRepo });
assert.equal(result.backendOnly, backendOnly, name);
assert.equal(result.webWrapperOnly, webWrapperOnly, name);
assert.equal(result.webHostOnly, false, name);
assert.equal(result.desktopLeafOnly, false, name);
assert.equal(result.desktopRsOnly, false, name);
if (backendOnly || webWrapperOnly) {
for (const guard of [
{ forceAll: 'true' },
{ ref: 'refs/heads/darksky/np/mobile' },
{ baseSha: ZERO_SHA },
{ baseSha: 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' },
{ baseSha: git('rev-parse', 'HEAD') },
]) {
const guarded = classifyGitDiff({ baseSha, cwd: tmpRepo, ...guard });
assert.equal(guarded.backendOnly, false, guarded.reason);
assert.equal(guarded.webWrapperOnly, false, guarded.reason);
}
}
}
} finally {
rmSync(tmpRepo, { recursive: true, force: true });
}
});