gongxiangp2/scripts/check-dark-variants.test.mjs

26 lines
1.2 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
import { checkDarkVariants } from './check-dark-variants.mjs';
test('UI sources stay within the dark variant baseline', () => {
checkDarkVariants(fileURLToPath(new URL('../', import.meta.url)));
});
test('counts every occurrence in TS, TSX and CSS and rejects an increase', t => {
const root = mkdtempSync(join(tmpdir(), 'dark-variants-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
for (const [dir, file] of [['app', 'page.tsx'], ['components', 'theme.css'], ['hooks', 'use-theme.ts']]) {
mkdirSync(join(root, dir));
writeFileSync(join(root, dir, file), 'dark:bg-black dark:text-white\n');
}
writeFileSync(join(root, 'hooks', 'notes.md'), 'dark:example');
writeFileSync(join(root, 'theme.css'), 'dark:outside');
assert.equal(checkDarkVariants(root, 6), 6);
assert.equal(checkDarkVariants(root, 7), 6);
assert.throws(() => checkDarkVariants(root, 5), /超过基线 5/);
});