peppa/packages/testkit/test/no-prefetch.test.ts
2026-11-26 20:34:02 +00:00

36 lines
1.6 KiB
TypeScript

// PEP-273, spec web-speed «Ссылки не предзагружают страницы»: links of the console go through
// `@/components/Link` (prefetch off by default), never `next/link` directly.
import assert from "node:assert/strict";
import { readdirSync, readFileSync, statSync } from "node:fs";
import { join, relative } from "node:path";
import { describe, test } from "node:test";
const WEB = new URL("../../../apps/web/", import.meta.url).pathname;
const WRAPPER = "components/Link.tsx";
function files(dir: string, out: string[] = []): string[] {
for (const name of readdirSync(dir)) {
if (name === "node_modules" || name.startsWith(".")) continue;
const p = join(dir, name);
if (statSync(p).isDirectory()) files(p, out);
else if (/\.(tsx?|jsx?)$/.test(name)) out.push(p);
}
return out;
}
describe("Ссылки не предзагружают страницы", () => {
test("Прямой импорт next/link", () => {
const bad = ["app", "components", "lib"]
.flatMap((d) => files(join(WEB, d)))
.map((p) => relative(WEB, p))
.filter((p) => p !== WRAPPER && /from\s+["']next\/link["']/.test(readFileSync(join(WEB, p), "utf8")));
assert.deepEqual(bad, [], `import Link from "@/components/Link" instead of "next/link" in: ${bad.join(", ")}`);
});
test("Главная открывается без предзагрузки: обёртка выключает её по умолчанию", () => {
const src = readFileSync(join(WEB, WRAPPER), "utf8");
assert.match(src, /prefetch\s*=\s*false/);
assert.match(src, /prefetch=\{prefetch\}/);
});
});