mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
Select mutants from changed lines and ignore logging-only mutants; rank survivors by risk topic from whole-word identifier matches. Publish one quality report comment, edited in place, with the verdict, change risk (coverage gaps and CRAP refactor hints) and mutation survivors grouped by line. Bundle the report data into one quality-report artifact; shard artifacts are internal. Map api/ source aliases into coverage evidence and enforce a per-shard execution budget. Move Redis cluster runtime files out of the checkout.
231 lines
9 KiB
TypeScript
231 lines
9 KiB
TypeScript
import { dirname, resolve } from 'node:path';
|
|
import { appendFileSync, readFileSync, writeFileSync } from 'node:fs';
|
|
import { commonTokens, declareClassPlugin, PluginKind, tokens } from '@stryker-mutator/api/plugin';
|
|
|
|
import type { MutationTestingPlanReadyEvent, Reporter } from '@stryker-mutator/api/report';
|
|
import type { MutantResult, StrykerOptions } from '@stryker-mutator/api/core';
|
|
|
|
import type { mutationSelection } from './risk.mts';
|
|
|
|
type Target = ReturnType<typeof mutationSelection>[number];
|
|
type FunctionProgress = Target['functions'][number] & { file: string; ids: string[] };
|
|
|
|
const duration = (ms: number) => {
|
|
const seconds = Math.floor(ms / 1000);
|
|
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
|
|
};
|
|
const compare = (a: { line: number; column: number }, b: typeof a) =>
|
|
a.line - b.line || a.column - b.column;
|
|
const label = (fn: FunctionProgress) =>
|
|
JSON.stringify(`${fn.file}:${fn.line}:${fn.column} ${fn.function}`);
|
|
|
|
/** Show completed work without guessing which of the pending mutants is running. */
|
|
export class FunctionProgressReporter implements Reporter {
|
|
static inject = tokens(commonTokens.options);
|
|
|
|
private readonly output: string;
|
|
private readonly staticMutants: boolean;
|
|
/** Selected functions only, for owner lookup: a module-scope mutant must fall back to
|
|
* `moduleOwners`, never match a function by an accidentally wide bounding box. */
|
|
private readonly owners: FunctionProgress[];
|
|
/** Every tracked bucket for rendering and counts: functions plus, once a module-scope mutant
|
|
* actually needs one, that file's `<module>` entry (added lazily, so a run with none keeps
|
|
* reporting exactly its selected functions). */
|
|
private readonly functions: FunctionProgress[];
|
|
private readonly moduleOwners: Map<string, FunctionProgress>;
|
|
private readonly addedModuleOwners = new Set<FunctionProgress>();
|
|
private readonly planned = new Map<string, { owner: FunctionProgress; static: boolean }>();
|
|
private readonly results = new Map<string, Readonly<MutantResult>>();
|
|
private readonly started = Date.now();
|
|
private readonly timer: ReturnType<typeof setInterval>;
|
|
private lastResult?: number;
|
|
private phase = 'Preparing mutants and running baseline tests';
|
|
private planReady = false;
|
|
private complete = false;
|
|
|
|
constructor(
|
|
options: Pick<StrykerOptions, 'jsonReporter'> & Partial<Pick<StrykerOptions, 'ignoreStatic'>>,
|
|
) {
|
|
this.staticMutants = !options.ignoreStatic;
|
|
this.output = dirname(options.jsonReporter.fileName);
|
|
const targets: Target[] = JSON.parse(readFileSync(`${this.output}/functions.json`, 'utf8'));
|
|
this.owners = targets.flatMap(({ file, functions }) =>
|
|
functions.map((fn) => ({ ...fn, file, ids: [] })),
|
|
);
|
|
// A module-scope mutant (e.g. a schema or constant) has no entry in `functions`; give every
|
|
// target file a `<module>` catch-all bucket here so it still has somewhere to report.
|
|
this.moduleOwners = new Map(
|
|
targets.map(({ file }) => [
|
|
resolve(file),
|
|
{
|
|
file,
|
|
function: '<module>',
|
|
line: 0,
|
|
column: 0,
|
|
// Looked up by file only (never by containment), so this location is unused; kept to
|
|
// satisfy the shared `FunctionProgress` shape.
|
|
location: { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } },
|
|
ids: [],
|
|
},
|
|
]),
|
|
);
|
|
this.functions = [...this.owners];
|
|
writeFileSync(`${this.output}/progress.jsonl`, '');
|
|
this.render();
|
|
this.timer = setInterval(() => this.render(), 30000);
|
|
this.timer.unref();
|
|
}
|
|
|
|
onDryRunCompleted() {
|
|
this.phase = 'Baseline passed; preparing mutation test plan';
|
|
this.render();
|
|
}
|
|
|
|
onMutationTestingPlanReady({ mutantPlans }: MutationTestingPlanReadyEvent) {
|
|
for (const { mutant } of mutantPlans) {
|
|
// Stryker plans use zero-based positions; completed results use one-based positions.
|
|
const start = {
|
|
line: mutant.location.start.line + 1,
|
|
column: mutant.location.start.column + 1,
|
|
};
|
|
const end = { line: mutant.location.end.line + 1, column: mutant.location.end.column + 1 };
|
|
let owner: FunctionProgress | undefined = this.owners
|
|
.filter(
|
|
(fn) =>
|
|
resolve(fn.file) === resolve(mutant.fileName) &&
|
|
compare(fn.location.start, start) <= 0 &&
|
|
compare(end, fn.location.end) <= 0,
|
|
)
|
|
.sort(
|
|
(a, b) =>
|
|
compare(b.location.start, a.location.start) || compare(a.location.end, b.location.end),
|
|
)[0];
|
|
if (!owner) {
|
|
// A mutant whose file is not one of our targets at all is a real planning divergence,
|
|
// not a module-scope mutant: fail loudly rather than silently dropping it.
|
|
owner = this.moduleOwners.get(resolve(mutant.fileName));
|
|
if (!owner)
|
|
throw new Error(`No selected function or module target owns mutant ${mutant.id}`);
|
|
if (!this.addedModuleOwners.has(owner)) {
|
|
this.addedModuleOwners.add(owner);
|
|
this.functions.push(owner);
|
|
}
|
|
}
|
|
owner.ids.push(mutant.id);
|
|
this.planned.set(mutant.id, { owner, static: mutant.static === true });
|
|
}
|
|
this.planReady = true;
|
|
this.phase = 'Testing mutations';
|
|
for (const fn of this.functions) {
|
|
this.log(
|
|
`PLAN ${label(fn)} | mutants=${fn.ids.length} static=${this.staticIds(fn.ids).length}`,
|
|
);
|
|
}
|
|
this.render();
|
|
}
|
|
|
|
onMutantTested(result: Readonly<MutantResult>) {
|
|
if (this.results.has(result.id)) return;
|
|
const plan = this.planned.get(result.id);
|
|
if (!plan) throw new Error(`No mutation plan for result ${result.id}`);
|
|
const finished = Date.now();
|
|
appendFileSync(
|
|
`${this.output}/progress.jsonl`,
|
|
`${JSON.stringify({ elapsedMs: finished - this.started, ...result })}\n`,
|
|
);
|
|
this.results.set(result.id, result);
|
|
this.lastResult = finished;
|
|
if (plan.owner.ids.every((id) => this.results.has(id))) {
|
|
this.log(`DONE ${label(plan.owner)} | ${this.counts(plan.owner.ids)}`);
|
|
}
|
|
}
|
|
|
|
onMutationTestReportReady() {
|
|
this.complete = this.planReady && this.results.size === this.planned.size;
|
|
this.phase = this.complete ? 'Complete' : 'Incomplete report';
|
|
clearInterval(this.timer);
|
|
this.render();
|
|
}
|
|
|
|
wrapUp() {
|
|
clearInterval(this.timer);
|
|
if (!this.complete) {
|
|
this.phase = 'Stopped before completion';
|
|
this.render();
|
|
}
|
|
}
|
|
|
|
private staticIds(ids: string[]) {
|
|
return ids.filter((id) => this.planned.get(id)?.static);
|
|
}
|
|
|
|
private counts(ids: string[]) {
|
|
const statuses = new Map<string, number>();
|
|
for (const id of ids) {
|
|
const status = this.results.get(id)?.status;
|
|
if (status) statuses.set(status, (statuses.get(status) ?? 0) + 1);
|
|
}
|
|
return [...statuses].map(([status, count]) => `${status}=${count}`).join(' ');
|
|
}
|
|
|
|
private log(message: string) {
|
|
console.log(
|
|
`[mutation ${duration(Date.now() - this.started)} static=${this.staticMutants ? 'on' : 'off'}] ${message}`,
|
|
);
|
|
}
|
|
|
|
private render() {
|
|
const ids = [...this.planned.keys()];
|
|
const staticIds = this.staticIds(ids);
|
|
const completed = (list: string[]) => list.filter((id) => this.results.has(id)).length;
|
|
const functions = this.functions.map((fn) => ({
|
|
file: fn.file,
|
|
function: fn.function,
|
|
line: fn.line,
|
|
column: fn.column,
|
|
completed: completed(fn.ids),
|
|
total: fn.ids.length,
|
|
staticCompleted: completed(this.staticIds(fn.ids)),
|
|
staticTotal: this.staticIds(fn.ids).length,
|
|
}));
|
|
const snapshot = {
|
|
phase: this.phase,
|
|
staticMutants: this.staticMutants,
|
|
complete: this.complete,
|
|
elapsedMs: Date.now() - this.started,
|
|
lastResultAgeMs: this.lastResult === undefined ? null : Date.now() - this.lastResult,
|
|
completed: this.results.size,
|
|
total: ids.length,
|
|
staticCompleted: completed(staticIds),
|
|
staticTotal: staticIds.length,
|
|
functions,
|
|
};
|
|
writeFileSync(`${this.output}/progress.json`, JSON.stringify(snapshot, null, 2));
|
|
if (!this.planReady) {
|
|
this.log(this.phase);
|
|
return;
|
|
}
|
|
const last =
|
|
snapshot.lastResultAgeMs === null ? 'none yet' : `${duration(snapshot.lastResultAgeMs)} ago`;
|
|
this.log(
|
|
`${this.phase} | completed=${snapshot.completed}/${snapshot.total} ` +
|
|
`static=${snapshot.staticCompleted}/${snapshot.staticTotal} ` +
|
|
`other=${snapshot.completed - snapshot.staticCompleted}/${snapshot.total - snapshot.staticTotal} ` +
|
|
`last result=${last}`,
|
|
);
|
|
if (this.phase === 'Testing mutations' || this.phase === 'Stopped before completion') {
|
|
for (const [index, fn] of functions.entries()) {
|
|
if (fn.completed < fn.total) {
|
|
this.log(
|
|
`PENDING ${label(this.functions[index])} | completed=${fn.completed}/${fn.total} ` +
|
|
`static=${fn.staticCompleted}/${fn.staticTotal}`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export const strykerPlugins = [
|
|
declareClassPlugin(PluginKind.Reporter, 'function-progress', FunctionProgressReporter),
|
|
];
|