test(freeze): 增长冻结改盯耦合,行数降为粗护栏
Home() useState/useRef 与 JyotishAPIHandler 类方法/__new__ 不得增长。行数改为开工实测基线加余量。产品授权删除参数式 hook 零 React hook 规则。
This commit is contained in:
@@ -296,8 +296,9 @@ test("the golden chart-view envelope stays inside the page contract", () => {
|
||||
assert.match(COORDINATE_BOUNDARY.qizheng, /角宿/);
|
||||
});
|
||||
|
||||
test("page.tsx does not grow to host the chart page", () => {
|
||||
assert.ok((pageSource.match(/\n/g) ?? []).length <= 1951);
|
||||
test("page.tsx does not host the chart page", () => {
|
||||
// Line-count freeze moved to tests/home-shell-growth-contract.test.ts
|
||||
// (Home() useState / useRef caps + coarse line-count guardrail).
|
||||
assert.doesNotMatch(pageSource, /chart-page|ChartPageView|\/api\/chart-view/);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
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).`,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user