// Помощники скрытых проверок эталона (PEP-184). import assert from "node:assert/strict"; import { readFileSync, readdirSync, statSync } from "node:fs"; import { join, relative } from "node:path"; import { createServer } from "node:net"; /** Свободный порт на 127.0.0.1. */ export async function freePort() { return new Promise((ok, fail) => { const s = createServer().once("error", fail).listen(0, "127.0.0.1", () => { const { port } = s.address(); s.close(() => ok(port)); }); }); } /** Видимый текст страницы: без тегов, скриптов, стилей; пробелы схлопнуты. */ export function visibleText(html) { return String(html) .replace(/<(script|style)[\s\S]*?<\/\1>/gi, " ") .replace(/<[^>]+>/g, " ") .replace(/ /g, " ") .replace(/«/g, "«") .replace(/»/g, "»") .replace(/"/g, '"') .replace(/'/g, "'") .replace(/</g, "<") .replace(/>/g, ">") .replace(/&/g, "&") .replace(/[\s  ]+/g, " ") .trim(); } /** Цифры без пробелов-разделителей: «1 500 ₸» → «1500 ₸». */ export function digits(s) { return String(s).replace(/(\d)[\s  ](?=\d{3}\b)/g, "$1"); } export function assertText(html, needle, what = needle) { const text = digits(visibleText(html)).toLowerCase(); assert.ok(text.includes(digits(needle).toLowerCase()), `на странице нет «${what}»`); } export async function page(t, path) { const r = await t.app.get(path); assert.equal(r.status, 200, `${path} ответил ${r.status}`); return r.text; } /** Все файлы рабочей копии (без .git и данных), пути относительно корня. */ export function files(root, dir = root) { const out = []; for (const name of readdirSync(dir)) { if (name === ".git" || name === "node_modules" || name === ".bench-data") continue; const p = join(dir, name); if (statSync(p).isDirectory()) out.push(...files(root, p)); else out.push(relative(root, p)); } return out; } export function read(root, path) { return readFileSync(join(root, path), "utf8"); } /** Вопросы продакту за задачу (контекст фабрики). */ export function questions(t) { return Array.isArray(t.ctx.questions) ? t.ctx.questions : []; }