Skip to content

Commit 00aeb1d

Browse files
committed
tools: refine seam test matching
1 parent 05a4e96 commit 00aeb1d

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

scripts/audit-seams.mjs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,21 @@ function buildDuplicatedSeamFamilies(inventory) {
311311
return [
312312
family,
313313
{
314-
count: entries.length,
314+
count: files.length,
315+
importCount: entries.length,
315316
files,
316317
imports: entries,
317318
},
318319
];
319320
})
320321
.filter(([, value]) => value.files.length > 1)
321-
.toSorted((left, right) => right[1].count - left[1].count || left[0].localeCompare(right[0])),
322+
.toSorted((left, right) => {
323+
return (
324+
right[1].count - left[1].count ||
325+
right[1].importCount - left[1].importCount ||
326+
left[0].localeCompare(right[0])
327+
);
328+
}),
322329
);
323330

324331
return duplicated;
@@ -396,6 +403,13 @@ function packageClusterMeta(relativePackagePath) {
396403
}
397404

398405
function classifyMissingPackageCluster(params) {
406+
if (params.hasStaticLeak) {
407+
return {
408+
decision: "required",
409+
reason:
410+
"Cluster already appears in the static graph in this audit run, so treating it as optional would be misleading.",
411+
};
412+
}
399413
if (optionalBundledClusterSet.has(params.cluster)) {
400414
if (params.cluster === "ui") {
401415
return {
@@ -424,7 +438,7 @@ function classifyMissingPackageCluster(params) {
424438
};
425439
}
426440

427-
async function buildMissingPackages() {
441+
async function buildMissingPackages(params = {}) {
428442
const rootPackage = JSON.parse(await fs.readFile(path.join(repoRoot, "package.json"), "utf8"));
429443
const rootDeps = new Set([
430444
...Object.keys(rootPackage.dependencies ?? {}),
@@ -467,6 +481,7 @@ async function buildMissingPackages() {
467481
const classification = classifyMissingPackageCluster({
468482
cluster: meta.cluster,
469483
pluginSdkEntries,
484+
hasStaticLeak: params.staticLeakClusters?.has(meta.cluster) === true,
470485
});
471486
output.push({
472487
cluster: meta.cluster,
@@ -539,13 +554,20 @@ function buildTestIndex(testFiles) {
539554
});
540555
}
541556

557+
function splitNameTokens(name) {
558+
return name
559+
.split(/[^a-zA-Z0-9]+/)
560+
.map((token) => token.trim().toLowerCase())
561+
.filter(Boolean);
562+
}
563+
542564
function matchQualityRank(quality) {
543565
switch (quality) {
544566
case "exact-stem":
545567
return 0;
546568
case "path-nearby":
547569
return 1;
548-
case "dir-basename":
570+
case "dir-token":
549571
return 2;
550572
case "source-echo":
551573
return 3;
@@ -559,20 +581,29 @@ function findRelatedTests(relativePath, testIndex, source) {
559581
const baseName = path.basename(stem);
560582
const dirName = path.dirname(relativePath);
561583
const normalizedDir = dirName.split(path.sep).join("/");
562-
const pathSuffix = `${normalizedDir}/${baseName}`;
584+
const baseTokens = new Set(splitNameTokens(baseName).filter((token) => token.length >= 7));
563585

564586
const matches = testIndex.flatMap((entry) => {
565587
if (entry.stem === stem) {
566588
return [{ file: entry.relativePath, matchQuality: "exact-stem" }];
567589
}
568-
if (entry.relativePath.includes(pathSuffix)) {
590+
if (entry.stem.startsWith(`${stem}.`)) {
569591
return [{ file: entry.relativePath, matchQuality: "path-nearby" }];
570592
}
571593
const entryDir = path.dirname(entry.relativePath).split(path.sep).join("/");
572-
if (entryDir === normalizedDir && entry.baseName.includes(baseName)) {
573-
return [{ file: entry.relativePath, matchQuality: "dir-basename" }];
594+
if (entryDir === normalizedDir && baseTokens.size > 0) {
595+
const entryTokens = splitNameTokens(entry.baseName);
596+
const sharedToken = entryTokens.find((token) => baseTokens.has(token));
597+
if (sharedToken) {
598+
return [{ file: entry.relativePath, matchQuality: "dir-token" }];
599+
}
574600
}
575-
if (baseName.length >= 12 && source.includes(baseName) && entry.relativePath.includes(baseName)) {
601+
if (
602+
baseName.length >= 12 &&
603+
source.includes(baseName) &&
604+
entryDir === normalizedDir &&
605+
(entry.baseName === baseName || entry.baseName.startsWith(`${baseName}.`))
606+
) {
576607
return [{ file: entry.relativePath, matchQuality: "source-echo" }];
577608
}
578609
return [];
@@ -673,11 +704,12 @@ if (args.has("--help") || args.has("-h")) {
673704
await collectWorkspacePackagePaths();
674705
const inventory = await collectCorePluginSdkImports();
675706
const optionalClusterStaticLeaks = await collectOptionalClusterStaticLeaks();
707+
const staticLeakClusters = new Set(optionalClusterStaticLeaks.map((entry) => entry.cluster));
676708
const result = {
677709
duplicatedSeamFamilies: buildDuplicatedSeamFamilies(inventory),
678710
overlapFiles: buildOverlapFiles(inventory),
679711
optionalClusterStaticLeaks: buildOptionalClusterStaticLeaks(optionalClusterStaticLeaks),
680-
missingPackages: await buildMissingPackages(),
712+
missingPackages: await buildMissingPackages({ staticLeakClusters }),
681713
seamTestInventory: await buildSeamTestInventory(),
682714
};
683715

0 commit comments

Comments
 (0)