mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
* 🚜 feat: Support Native PostgreSQL Tool Favorites
* fix: Guard PostgreSQL Reconciliation and Connection Leases
* fix: Scope Favorites Erasure Fences to Deletion Attempts
* fix: Preserve Account Erasure Ownership Until Cancellation Completes
* fix: Reject Unsupported PostgreSQL Command Flags Before Connecting
* fix: Keep Command Validation Compatible With the TypeScript Target
* fix: Keep Migration Previews Read-Only and Preserve Erasure Recovery Identity
* fix: Fence Erasure Recovery Across Tenant and Ownership Changes
* fix: Isolate Migration Dependencies and Keep Mongo Scans Outside Transactions
* fix: Inject PostgreSQL Backend Factories at Composition Roots
* ci: Shard Complete Mutation Work Without Relaxing the Aggregate Gate
---------
Co-authored-by: Lia <lia@librechat.ai>
244 lines
8.9 KiB
TypeScript
244 lines
8.9 KiB
TypeScript
import { parseArgs } from 'node:util';
|
|
import { createRequire } from 'node:module';
|
|
import { resolve, relative } from 'node:path';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { Stryker } from '@stryker-mutator/core';
|
|
import {
|
|
appendFileSync,
|
|
existsSync,
|
|
mkdirSync,
|
|
readFileSync,
|
|
rmSync,
|
|
writeFileSync,
|
|
} from 'node:fs';
|
|
|
|
import { mutationSelection, riskMarkdown, riskReport } from './risk.mts';
|
|
import { changes, git, WORKSPACES, workspaceFor } from './changes.mts';
|
|
import { parseShard, shardTargets } from './shard.mts';
|
|
import { coverageArgs } from './jest.mts';
|
|
|
|
const { values } = parseArgs({
|
|
options: {
|
|
base: { type: 'string', default: 'origin/dev' },
|
|
head: { type: 'string', default: 'HEAD' },
|
|
workspace: { type: 'string' },
|
|
check: { type: 'string' },
|
|
shard: { type: 'string' },
|
|
},
|
|
});
|
|
const workspace = WORKSPACES.find(({ name }) => name === values.workspace);
|
|
if (!workspace || !['risk', 'mutation'].includes(values.check ?? '')) {
|
|
throw new Error(
|
|
'Usage: node scripts/quality/run.mts --base ref --workspace name --check risk|mutation [--shard INDEX/COUNT]',
|
|
);
|
|
}
|
|
const shard = parseShard(values.shard, values.check);
|
|
const shardLabel = shard ? ` (shard ${shard.index}/${shard.total})` : '';
|
|
const selection = changes(values.base, values.head);
|
|
if (selection.head !== git(['rev-parse', 'HEAD']).trim()) {
|
|
throw new Error('Check out the selected head before measuring its source');
|
|
}
|
|
const files = selection.files.filter(({ file }) => workspaceFor(file)?.name === workspace.name);
|
|
const output = resolve('coverage/changes', workspace.name, values.check!);
|
|
rmSync(output, { recursive: true, force: true });
|
|
mkdirSync(output, { recursive: true });
|
|
writeFileSync(
|
|
`${output}/selection.json`,
|
|
JSON.stringify(
|
|
{ ...selection, workspace: workspace.name, files, ...(shard ? { shard } : {}) },
|
|
null,
|
|
2,
|
|
),
|
|
);
|
|
const summary = (markdown: string) => {
|
|
console.log(markdown);
|
|
writeFileSync(`${output}/summary.md`, markdown);
|
|
if (process.env.GITHUB_STEP_SUMMARY) appendFileSync(process.env.GITHUB_STEP_SUMMARY, markdown);
|
|
};
|
|
const emptyMutationReports = () => {
|
|
writeFileSync(
|
|
`${output}/mutation.json`,
|
|
JSON.stringify({ schemaVersion: '1.0', files: {} }, null, 2),
|
|
);
|
|
writeFileSync(
|
|
`${output}/progress.json`,
|
|
JSON.stringify(
|
|
{ complete: true, completed: 0, total: 0, staticCompleted: 0, staticTotal: 0 },
|
|
null,
|
|
2,
|
|
),
|
|
);
|
|
};
|
|
if (!files.length) {
|
|
if (shard) {
|
|
writeFileSync(`${output}/all-functions.json`, '[]');
|
|
writeFileSync(`${output}/functions.json`, '[]');
|
|
emptyMutationReports();
|
|
}
|
|
summary(
|
|
`## ${workspace.name}: ${values.check}${shardLabel}\n\n${shard ? 'No changed functions for shard.' : 'No changed source files.'}\n`,
|
|
);
|
|
process.exit(0);
|
|
}
|
|
// Test binaries and configuration belong to the measured head, not the tooling checkout.
|
|
const require = createRequire(resolve('package.json'));
|
|
const jestBin = require.resolve('jest/bin/jest');
|
|
const localFiles = files.map(({ file }) => relative(workspace.path, file));
|
|
const env = { ...process.env, NODE_ENV: 'test', BABEL_ENV: 'test' };
|
|
const configFile = `${workspace.path}/${workspace.config}`;
|
|
|
|
if (values.check === 'risk') {
|
|
const listed = spawnSync(
|
|
process.execPath,
|
|
[
|
|
jestBin,
|
|
'--config',
|
|
workspace.config,
|
|
'--listTests',
|
|
'--json',
|
|
'--runInBand',
|
|
'--findRelatedTests',
|
|
...localFiles,
|
|
],
|
|
{ cwd: workspace.path, env, encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 },
|
|
);
|
|
if (listed.error || listed.status !== 0) {
|
|
throw listed.error ?? new Error(`Jest test selection failed: ${listed.stderr}`);
|
|
}
|
|
const tests: unknown = JSON.parse(listed.stdout);
|
|
if (!Array.isArray(tests) || tests.some((test) => typeof test !== 'string')) {
|
|
throw new Error('Invalid Jest test selection');
|
|
}
|
|
writeFileSync(`${output}/selected-tests.json`, JSON.stringify(tests, null, 2));
|
|
const result = tests.length
|
|
? spawnSync(
|
|
process.execPath,
|
|
[jestBin, ...coverageArgs(workspace.config, localFiles, output, tests)],
|
|
{ cwd: workspace.path, stdio: 'inherit', env },
|
|
)
|
|
: { error: undefined, status: 0, signal: null };
|
|
if (result.error || result.status !== 0) {
|
|
summary(
|
|
`## ${workspace.name}: change risk\n\nCoverage failed. Scores are unavailable. See the test log.\n`,
|
|
);
|
|
throw result.error ?? new Error(`Jest exited with ${result.status ?? result.signal}`);
|
|
}
|
|
const coverageFile = `${output}/coverage-final.json`;
|
|
if (!existsSync(coverageFile)) {
|
|
// Jest produces no coverage file when no test matches. Keep the lack of evidence explicit.
|
|
if (tests.length !== 0) throw new Error('Jest did not produce coverage');
|
|
writeFileSync(coverageFile, '{}');
|
|
}
|
|
const report = riskReport(coverageFile, files, selection.base);
|
|
writeFileSync(
|
|
`${output}/risk.json`,
|
|
JSON.stringify({ base: selection.base, head: selection.head, ...report }, null, 2),
|
|
);
|
|
summary(riskMarkdown(report));
|
|
} else {
|
|
process.env.NODE_ENV = env.NODE_ENV;
|
|
process.env.BABEL_ENV = env.BABEL_ENV;
|
|
const allTargets = mutationSelection(files, selection.base);
|
|
const targets = shardTargets(allTargets, shard);
|
|
if (shard) {
|
|
writeFileSync(`${output}/all-functions.json`, JSON.stringify(allTargets, null, 2));
|
|
}
|
|
writeFileSync(`${output}/functions.json`, JSON.stringify(targets, null, 2));
|
|
if (!targets.length) {
|
|
if (shard) emptyMutationReports();
|
|
summary(
|
|
`## ${workspace.name}: mutation testing${shardLabel}\n\n${shard ? 'No changed functions for shard.' : 'No added or edited functions.'}\n`,
|
|
);
|
|
process.exit(0);
|
|
}
|
|
const started = Date.now();
|
|
const stryker = new Stryker({
|
|
plugins: [
|
|
import.meta.resolve('@stryker-mutator/jest-runner'),
|
|
new URL('./progress.mts', import.meta.url).href,
|
|
],
|
|
testRunner: 'jest',
|
|
jest: {
|
|
projectType: 'custom',
|
|
configFile,
|
|
enableFindRelatedTests: true,
|
|
config: {
|
|
testResultsProcessor: undefined,
|
|
coverageThreshold: {},
|
|
// Match the existing database mutation lane when npm nests Jest's environment.
|
|
...(workspace.name === 'data-schemas'
|
|
? {
|
|
testEnvironment: require.resolve('jest-environment-node', {
|
|
paths: [require.resolve('jest')],
|
|
}),
|
|
}
|
|
: {}),
|
|
},
|
|
},
|
|
mutate: targets.flatMap(({ mutate }) => mutate),
|
|
ignoreStatic: false,
|
|
coverageAnalysis: 'perTest',
|
|
concurrency: 2,
|
|
incremental: false,
|
|
allowEmpty: true,
|
|
reporters: ['clear-text', 'function-progress', 'json', 'html'],
|
|
jsonReporter: { fileName: `${output}/mutation.json` },
|
|
htmlReporter: { fileName: `${output}/mutation.html` },
|
|
thresholds: { high: 90, low: 70, break: 0 },
|
|
timeoutMS: 10000,
|
|
dryRunTimeoutMinutes: 10,
|
|
ignorePatterns: [
|
|
'/*',
|
|
'!/package.json',
|
|
'!/config/',
|
|
'!/packages/',
|
|
'!/api/',
|
|
'!/client/',
|
|
'**/coverage/**',
|
|
'**/workspace/**',
|
|
'**/.git/**',
|
|
'**/.stryker-tmp/**',
|
|
'**/node_modules/**',
|
|
],
|
|
});
|
|
try {
|
|
const results = await stryker.runMutationTest();
|
|
const progress = JSON.parse(readFileSync(`${output}/progress.json`, 'utf8'));
|
|
if (!progress.complete || progress.completed !== results.length) {
|
|
throw new Error('Mutation progress report is incomplete');
|
|
}
|
|
const counts = new Map<string, number>();
|
|
for (const mutant of results) counts.set(mutant.status, (counts.get(mutant.status) ?? 0) + 1);
|
|
const detected = (counts.get('Killed') ?? 0) + (counts.get('Timeout') ?? 0);
|
|
const tested = detected + (counts.get('Survived') ?? 0) + (counts.get('NoCoverage') ?? 0);
|
|
const runtimeErrors = counts.get('RuntimeError') ?? 0;
|
|
const score = tested ? `${((100 * detected) / tested).toFixed(2)}%` : 'N/A (no valid mutants)';
|
|
summary(
|
|
[
|
|
`## ${workspace.name}: mutation testing (advisory)${shardLabel}`,
|
|
'',
|
|
`Changed functions: ${targets.reduce((total, target) => total + target.functions.length, 0)} in ${targets.length} files. Static mutations are included. Duration: ${((Date.now() - started) / 60000).toFixed(1)} minutes.`,
|
|
'',
|
|
`Mutation score: ${score}. Surviving and uncovered mutants reduce this score. Invalid mutants are excluded.`,
|
|
...(runtimeErrors
|
|
? [
|
|
`${runtimeErrors} mutations had runtime errors. The check fails because their results are incomplete.`,
|
|
]
|
|
: []),
|
|
'',
|
|
'| Result | Mutants |',
|
|
'| --- | ---: |',
|
|
...[...counts].map(([status, count]) => `| ${status} | ${count} |`),
|
|
'',
|
|
'Download the report artifact for the HTML report and each mutation location.',
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
if (runtimeErrors) process.exitCode = 1;
|
|
} catch (error) {
|
|
summary(
|
|
`## ${workspace.name}: mutation testing${shardLabel}\n\nStryker failed. This is not a mutation score. See the test log.\n`,
|
|
);
|
|
throw error;
|
|
}
|
|
}
|