peppa/bench/lib/check-kit.mjs
Асель d108197f0f PEP-184: задачи 5–8, тест набора, archive change, память
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 02:09:17 +05:00

70 lines
2.4 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Помощники скрытых проверок эталона (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(/&nbsp;/g, " ")
.replace(/&laquo;/g, "«")
.replace(/&raquo;/g, "»")
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&amp;/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 : [];
}