import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; export const chatSuite = "server/src/__tests__/chat-channels.integration.test.ts"; export const chatManifest = "scripts/chat-shard-durations.json"; const comment = "Generated by node scripts/ci/refresh-chat-shard-durations.mjs from the CI profile test records of the PR chat jobs (profile-ci label). Per collected test entry, keyed by the name that vitest list reports: the summed duration of every case that entry runs, so an it.each line carries all its cases. Median across the given runs. scripts/test-line-shard.mjs uses these weights only to place source lines on chat partitions; they never change which tests run. Missing entries take the median weight."; const placeholder = /%[sdifjo#%]|\$[A-Za-z_][\w.]*|\$\d+/; function literalLength(name) { return name.split(new RegExp(placeholder, "g")).join("").length; } export function entryMatcher(entries) { const patterns = entries.map((name) => ({ name, literal: literalLength(name), regex: new RegExp(`(?:^|> )${name.split(new RegExp(placeholder, "g")) .map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*?")}$`, "s"), })); return (caseName) => { let best = null; for (const pattern of patterns) { if (!pattern.regex.test(caseName)) continue; const exact = caseName === pattern.name || caseName.endsWith(` > ${pattern.name}`); const score = [exact ? 1 : 0, pattern.literal]; if (!best || score[0] > best.score[0] || (score[0] === best.score[0] && score[1] > best.score[1])) { best = { name: pattern.name, score }; } } return best?.name ?? null; }; } export function entryDurations(entries, cases) { const match = entryMatcher(entries); const totals = new Map(); const unmatched = []; for (const { name, duration } of cases) { const entry = match(name); if (entry === null) unmatched.push(name); else totals.set(entry, (totals.get(entry) ?? 0) + duration); } return { totals, unmatched }; } function profileCases(directory) { const cases = []; const walk = (dir) => { for (const entry of readdirSync(dir)) { const file = path.join(dir, entry); if (statSync(file).isDirectory()) walk(file); else if (/^vitest-.+\.ndjson$/.test(entry)) { for (const line of readFileSync(file, "utf8").split("\n")) { if (!line.trim()) continue; const record = JSON.parse(line); if (record.kind !== "test" || record.file !== chatSuite) continue; assert.equal(typeof record.name, "string"); assert.ok(Number.isFinite(record.duration) && record.duration >= 0 && record.duration <= 3_600_000); if (record.status === "passed") cases.push({ name: record.name, duration: record.duration }); } } } }; walk(directory); return cases; } function collectedEntries(repoRoot) { const directory = mkdtempSync(path.join(os.tmpdir(), "chat-durations-")); try { const output = path.join(directory, "list.json"); const result = spawnSync("pnpm", ["exec", "vitest", "list", "--exclude", "**/dist/**", "--project", "@paperclipai/server", chatSuite, "--allowOnly=false", "--includeTaskLocation", `--json=${output}`], { cwd: repoRoot, stdio: "inherit" }); assert.equal(result.status, 0, "vitest list failed"); return [...new Set(JSON.parse(readFileSync(output, "utf8")).map((test) => test.name))]; } finally { rmSync(directory, { recursive: true, force: true }); } } export function chatManifestData(entries, runs, sources) { const samples = new Map(); for (const cases of runs) { const { totals, unmatched } = entryDurations(entries, cases); assert.equal(unmatched.length, 0, `Cases without a collected entry: ${unmatched.slice(0, 5).join(", ")}`); for (const [entry, ms] of totals) samples.set(entry, [...(samples.get(entry) ?? []), ms]); } const durations = {}; for (const [entry, values] of [...samples].sort(([a], [b]) => a.localeCompare(b))) { const sorted = values.sort((a, b) => a - b); const middle = Math.floor(sorted.length / 2); durations[entry] = Math.max(1, Math.round(sorted.length % 2 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2)); } return { $comment: comment, unit: "ms", sources, coverage: { measured: Object.keys(durations).length, entries: entries.length }, durations }; } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const args = process.argv.slice(2); const sources = args.filter((arg) => arg.startsWith("--source=")).map((arg) => arg.slice("--source=".length)); const directories = args.filter((arg) => !arg.startsWith("--")); assert.ok(directories.length > 0, "Usage: refresh-chat-shard-durations.mjs [--source=] ..."); const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", ".."); const data = chatManifestData(collectedEntries(repoRoot), directories.map(profileCases), sources); writeFileSync(path.join(repoRoot, chatManifest), JSON.stringify(data, null, 2) + "\n"); console.log(`${chatManifest}: ${data.coverage.measured} of ${data.coverage.entries} entries measured`); }