Home() useState/useRef 与 JyotishAPIHandler 类方法/__new__ 不得增长。行数改为开工实测基线加余量。产品授权删除参数式 hook 零 React hook 规则。
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import test from "node:test";
|
|
|
|
const pageSource = readFileSync(new URL("../src/app/page.tsx", import.meta.url), "utf8");
|
|
|
|
// Measured 2026-09-16 on origin/staging @ 51a65d92.
|
|
// Line count: (pageSource.match(/\n/g) ?? []).length, same as `wc -l` = 1951.
|
|
// Hook counts use \buseState[<(] / \buseRef[<(] so `useState<Type>(` is not missed.
|
|
// Former line-count assertion lived in tests/chart-view-route.test.ts
|
|
// (`page.tsx does not grow to host the chart page`).
|
|
const PAGE_LINE_COUNT_BASELINE = 1951;
|
|
const PAGE_LINE_COUNT_CAP = PAGE_LINE_COUNT_BASELINE + 150;
|
|
const HOME_USE_STATE_CAP = 66;
|
|
const HOME_USE_REF_CAP = 41;
|
|
|
|
const USE_STATE_RE = /\buseState[<(]/g;
|
|
const USE_REF_RE = /\buseRef[<(]/g;
|
|
|
|
function homeSource(source: string): string {
|
|
const marker = "export default function Home(";
|
|
const start = source.indexOf(marker);
|
|
assert.notEqual(start, -1, "page.tsx must export default function Home(");
|
|
return source.slice(start);
|
|
}
|
|
|
|
function countMatches(source: string, pattern: RegExp): number {
|
|
return source.match(new RegExp(pattern.source, "g"))?.length ?? 0;
|
|
}
|
|
|
|
test("Home() useState count must not grow", () => {
|
|
const n = countMatches(homeSource(pageSource), USE_STATE_RE);
|
|
assert.ok(
|
|
n <= HOME_USE_STATE_CAP,
|
|
`Home() has ${n} useState calls; cap is ${HOME_USE_STATE_CAP}. Extracted hooks and child components should own their own state.`,
|
|
);
|
|
});
|
|
|
|
test("Home() useRef count must not grow", () => {
|
|
const n = countMatches(homeSource(pageSource), USE_REF_RE);
|
|
assert.ok(
|
|
n <= HOME_USE_REF_CAP,
|
|
`Home() has ${n} useRef calls; cap is ${HOME_USE_REF_CAP}. Do not rewrite state as refs to dodge the useState freeze.`,
|
|
);
|
|
});
|
|
|
|
test("page.tsx line count stays within the coarse guardrail", () => {
|
|
const n = (pageSource.match(/\n/g) ?? []).length;
|
|
assert.ok(
|
|
n <= PAGE_LINE_COUNT_CAP,
|
|
`page.tsx has ${n} lines; cap is ${PAGE_LINE_COUNT_CAP} (${PAGE_LINE_COUNT_BASELINE} baseline + 150).`,
|
|
);
|
|
});
|