From 64cfb07c220fc5513dbabb23dc68f0ea5002d4d9 Mon Sep 17 00:00:00 2001 From: Denozordec Date: Sun, 15 Nov 2026 21:59:48 +0700 Subject: [PATCH] =?UTF-8?q?chore(test):=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20test:changed=20=D0=B4=D0=BB=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B3=D1=80=D0=B5=D1=81=D1=81=D0=B0=20=D1=82=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D0=BA=D0=BE=20=D0=B7=D0=B0=D1=82=D1=80=D0=BE=D0=BD=D1=83?= =?UTF-8?q?=D1=82=D1=8B=D1=85=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Скрипт берёт изменённые файлы из Git и запускает только тесты, которые на них ссылаются. Тесты на PostgreSQL он не запускает, а выводит нужный npm-профиль. Co-Authored-By: Claude Sonnet 5.5 --- backend/package.json | 1 + backend/scripts/test-changed.mjs | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 backend/scripts/test-changed.mjs diff --git a/backend/package.json b/backend/package.json index 8fa26ae5..312a2c3b 100644 --- a/backend/package.json +++ b/backend/package.json @@ -16,6 +16,7 @@ "test:traffic-flow:pg": "node scripts/test-mcp-postgres.mjs --flow-writes", "test:traffic-flow:quality:pg": "node scripts/test-mcp-postgres.mjs --flow-quality", "test:traffic-flow:chr-evidence": "tsx --test src/test/traffic-flow-chr-capture-check.test.ts src/test/traffic-flow-chr-wire.test.ts src/test/traffic-flow-chr-target-harness.test.ts src/test/traffic-flow-chr-owned-sequence.test.ts src/test/traffic-flow-chr-reconcile.test.ts", + "test:changed": "node scripts/test-changed.mjs", "test:mcp:pg:all": "node scripts/test-mcp-postgres.mjs --all", "mcp:stdio": "node dist/scripts/mcp-stdio.js", "mcp:stdio:dev": "tsx src/scripts/mcp-stdio.ts", diff --git a/backend/scripts/test-changed.mjs b/backend/scripts/test-changed.mjs new file mode 100644 index 00000000..238123c6 --- /dev/null +++ b/backend/scripts/test-changed.mjs @@ -0,0 +1,63 @@ +// Runs only the backend tests that import a changed file (regression of what was touched). +// npm --prefix backend run test:changed -- [--since=] [--list] +// Default scope: uncommitted and untracked files. --since= uses `git diff ` instead. +// shortcut: one-level import match by file basename, not a real module graph; upgrade if basenames collide. +// PostgreSQL-backed tests are listed with their npm profile, never run here. +import { execFileSync, spawnSync } from "node:child_process" +import { readdirSync, readFileSync } from "node:fs" +import { basename, extname, join, relative, sep } from "node:path" + +const flag = name => process.argv.find(arg => arg.startsWith(`--${name}=`))?.slice(name.length + 3) +const listOnly = process.argv.includes("--list") +const since = flag("since") +const backend = new URL("../", import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1").replace(/\/$/, "") +const git = (...args) => execFileSync("git", args, { encoding: "utf8", maxBuffer: 1 << 28 }).split("\n").filter(Boolean) +const top = git("-C", backend, "rev-parse", "--show-toplevel")[0] +const changed = [...new Set(since ? git("-C", top, "diff", "--name-only", since) + : [...git("-C", top, "diff", "--name-only", "HEAD"), ...git("-C", top, "ls-files", "--others", "--exclude-standard")])] + .filter(file => file.startsWith("backend/") && /\.(ts|mts|mjs|js)$/.test(file)) + +const isTest = file => /\.test\.(ts|mts|mjs)$/.test(file) +const tests = [] +const walk = dir => { + for (const entry of readdirSync(dir, { withFileTypes: true })) { + if (entry.name === "node_modules" || entry.name.startsWith("dist")) continue + const path = join(dir, entry.name) + if (entry.isDirectory()) walk(path) + else if (isTest(entry.name)) tests.push({ path, rel: relative(top, path).split(sep).join("/"), text: readFileSync(path, "utf8") }) + } +} +walk(join(backend, "src")); walk(join(backend, "scripts")) + +const escape = text => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") +const selected = new Map() +for (const file of changed) { + const base = basename(file, extname(file)) + const importer = new RegExp(`[/"'\`]${escape(base)}(\\.(js|ts|mjs))?["'\`]`) + for (const test of tests) if (test.rel === file || basename(test.rel).startsWith(base + ".") || importer.test(test.text)) selected.set(test.rel, test) +} + +const scripts = JSON.parse(readFileSync(join(backend, "package.json"), "utf8")).scripts +const needsPostgres = test => /\.(sql|owned|pg)\.test\./.test(test.rel) || /embedded-postgres|EmbeddedPostgres/.test(test.text) || test.rel.endsWith("modules/mcp/integration.test.ts") +const profileFor = test => Object.entries(scripts).find(([, command]) => command.includes(basename(test.rel)))?.[0] +const runnable = [], database = [] +for (const test of selected.values()) (needsPostgres(test) ? database : runnable).push(test) + +console.log(`changed backend files: ${changed.length}; matching tests: ${selected.size} (run here: ${runnable.length}, PostgreSQL: ${database.length})`) +const profiles = new Set() +for (const test of database) { + const profile = test.rel.endsWith("modules/mcp/integration.test.ts") ? "test:mcp:pg" : profileFor(test) + profiles.add(profile ?? "test:mcp:pg:all") + console.log(` PostgreSQL: ${test.rel} -> ${profile ? `npm --prefix backend run ${profile}` : "no profile found, see test:mcp:pg:all"}`) +} +if (profiles.size) console.log(`PostgreSQL profiles to run separately: ${[...profiles].join(", ")}`) +if (listOnly || !runnable.length) { for (const test of runnable) console.log(` run: ${test.rel}`); process.exit(0) } + +let failed = 0 +for (const [runner, group] of [["tsx", runnable.filter(test => !test.rel.endsWith(".mjs"))], ["node", runnable.filter(test => test.rel.endsWith(".mjs"))]]) { + if (!group.length) continue + const args = runner === "tsx" ? ["tsx", "--test", ...group.map(test => test.path)] : ["--test", ...group.map(test => test.path)] + const result = spawnSync(runner === "tsx" ? "npx" : process.execPath, args, { cwd: backend, stdio: "inherit", shell: runner === "tsx", env: { ...process.env, NODE_ENV: "test" } }) + if (result.status !== 0) failed = 1 +} +process.exit(failed)