Files
lislgosms/tools/quality/verify-css-governance.test.mjs
T
hectorzhao 458ddd97df
CSS quality / css-quality (push) Has been cancelled
refactor: 完成全局CSS模块化治理
2026-09-05 00:45:05 +08:00

34 lines
1.5 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { analyzeCss, broadBusinessSelectors, verifyGlobalCssAbsent } from './verify-css-governance.mjs';
test('AST metrics ignore formatting and count selectors and important declarations', () => {
assert.deepEqual(analyzeCss('.page { color: red !important; }\n.page a, .page button { display: block; }'), {
rules: 2,
selectors: 3,
declarations: 2,
important: 1,
selectorList: ['.page', '.page a', '.page button'],
});
});
test('broad business tag selectors are rejected while rooted selectors are allowed', () => {
assert.deepEqual(broadBusinessSelectors('button { color: red; } .report-page button { color: blue; }'), ['button']);
});
test('formatting changes do not affect AST growth metrics', () => {
assert.deepEqual(analyzeCss('.page{color:red}'), analyzeCss('.page {\n color: red;\n}\n'));
});
test('global.css cannot be recreated after migration', () => {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'cmpp-css-gate-'));
fs.mkdirSync(path.join(directory, 'src/styles'), { recursive: true });
assert.doesNotThrow(() => verifyGlobalCssAbsent(directory));
fs.writeFileSync(path.join(directory, 'src/styles/global.css'), '.new-page {}');
assert.throws(() => verifyGlobalCssAbsent(directory), /禁止重新创建/);
fs.rmSync(directory, { recursive: true, force: true });
});