mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 19:25:00 +03:00
Use the changed paths to keep native jobs enabled for changes outside the web host allowlist.
395 lines
11 KiB
JavaScript
395 lines
11 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('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\n');
|
|
|
|
// 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 });
|
|
}
|
|
});
|