Skip to content

Commit e5ce7f2

Browse files
MarcoDotIOclaude
andcommitted
fix(web): PR diffs list a .logic.test.ts file beside the source it covers
The Code tab orders test files by the source they cover, but it stripped one extension too many from the test's name, so `a.logic.test.ts` looked for a source named `a` and fell to the end of the list. Match the longest dotted prefix that names a source file instead, which still pairs `a.browser.test.tsx` with `a.tsx`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 70e5e38 commit e5ce7f2

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎apps/web/src/components/pullRequest/pullRequestFileOrder.logic.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ describe("orderDiffFiles", () => {
119119
]);
120120
});
121121

122+
it("pairs a test with the source whose name carries its own dotted suffix", () => {
123+
expect(
124+
order([
125+
file("src/c.browser.test.tsx"),
126+
file("src/b.test.ts"),
127+
file("src/a.logic.test.ts"),
128+
file("src/c.tsx"),
129+
file("src/b.ts"),
130+
file("src/a.logic.ts"),
131+
]),
132+
).toEqual([
133+
"src/a.logic.ts",
134+
"src/b.ts",
135+
"src/c.tsx",
136+
"src/a.logic.test.ts",
137+
"src/b.test.ts",
138+
"src/c.browser.test.tsx",
139+
]);
140+
});
141+
122142
it("orders the same diff the same way whatever order it arrives in", () => {
123143
const files = [
124144
file("src/a.ts", ['import { b } from "./b";']),

‎apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,17 @@ function orderByImports(
118118
return ordered;
119119
}
120120

121-
function testedBaseName(path: string): string {
122-
return (path.split("/").at(-1) ?? "").replace(/\.(?:test|spec)\..*$/, "").replace(/\.[^.]+$/, "");
121+
// `a.logic.test.ts` covers `a.logic.ts` while `a.browser.test.ts` covers `a.ts`, so the longest
122+
// dotted prefix that names a source file wins.
123+
function testedSourcePosition(path: string, sourcePositions: ReadonlyMap<string, number>): number {
124+
let name = (path.split("/").at(-1) ?? "").replace(/\.(?:test|spec)\..*$/, "");
125+
while (true) {
126+
const position = sourcePositions.get(name);
127+
if (position !== undefined) return position;
128+
const dot = name.lastIndexOf(".");
129+
if (dot <= 0) return Number.MAX_SAFE_INTEGER;
130+
name = name.slice(0, dot);
131+
}
123132
}
124133

125134
export function orderDiffFiles(
@@ -160,9 +169,10 @@ export function orderDiffFiles(
160169
const orderedTests = [...byPath.keys()]
161170
.filter((path) => tiers.get(path) === "test")
162171
.sort((left, right) => {
163-
const leftPosition = sourcePositions.get(testedBaseName(left)) ?? Number.MAX_SAFE_INTEGER;
164-
const rightPosition = sourcePositions.get(testedBaseName(right)) ?? Number.MAX_SAFE_INTEGER;
165-
return leftPosition - rightPosition || left.localeCompare(right);
172+
return (
173+
testedSourcePosition(left, sourcePositions) -
174+
testedSourcePosition(right, sourcePositions) || left.localeCompare(right)
175+
);
166176
});
167177

168178
const orderedGenerated = [...byPath.keys()]

0 commit comments

Comments
 (0)