Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ it.each([
expect(result()).toEqual({ loading: true, files: [] });

await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));
expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] });
expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] });
});
44 changes: 44 additions & 0 deletions apps/web/src/lib/diffRendering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,50 @@ describe("getRenderablePatch", () => {
if (parsed?.kind !== "files") return;
expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
});

it("keeps whitespace at the end of the last hunk", () => {
const patch = (lastLines: string[]) =>
[
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
...lastLines,
"",
].join("\n");

const trailing = getRenderablePatch(patch(["@@ -1 +1 @@", "-a", "+ b \t"]), "review");
expect(trailing?.kind).toBe("files");
if (trailing?.kind !== "files") return;
expect(trailing.files[0]?.additionLines).toEqual([" b \t\n"]);

const blankContext = getRenderablePatch(patch(["@@ -1,2 +1,2 @@", "-a", "+b", " "]), "review");
expect(blankContext?.kind).toBe("files");
if (blankContext?.kind !== "files") return;
expect(blankContext.files[0]?.additionLines).toEqual(["b\n", "\n"]);
expect(blankContext.files[0]?.hunks[0]?.unifiedLineCount).toBe(3);

expect(getRenderablePatch(" \n\t\n", "review")).toBeNull();
});

it("caches patches that differ only in trailing whitespace separately", () => {
const patch = (added: string) =>
[
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
"@@ -1 +1 @@",
"-a",
`+${added}`,
"",
].join("\n");

const plain = getRenderablePatch(patch("b"), "review");
const tabbed = getRenderablePatch(patch("b\t"), "review");
expect(plain?.kind).toBe("files");
expect(tabbed?.kind).toBe("files");
if (plain?.kind !== "files" || tabbed?.kind !== "files") return;
expect(tabbed.files[0]?.cacheKey).not.toBe(plain.files[0]?.cacheKey);
});
});

describe("diff file reconciliation", () => {
Expand Down
24 changes: 7 additions & 17 deletions apps/web/src/lib/diffRendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,9 @@ export function fnv1a32(
}

function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
const normalizedPatch = patch.trim();
const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
const secondary = fnv1a32(
normalizedPatch,
SECONDARY_HASH_SEED,
SECONDARY_HASH_MULTIPLIER,
).toString(36);
return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;
const primary = fnv1a32(patch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
const secondary = fnv1a32(patch, SECONDARY_HASH_SEED, SECONDARY_HASH_MULTIPLIER).toString(36);
return `${scope}:${patch.length}:${primary}:${secondary}`;
}

export type RenderablePatch =
Expand Down Expand Up @@ -164,15 +159,10 @@ export function getRenderablePatch(
cacheScope = "diff-panel",
options: RenderablePatchOptions = {},
): RenderablePatch | null {
if (!patch) return null;
const normalizedPatch = patch.trim();
if (normalizedPatch.length === 0) return null;
if (!patch?.trim()) return null;

try {
const parsedPatches = parsePatchFiles(
normalizedPatch,
buildPatchCacheKey(normalizedPatch, cacheScope),
);
const parsedPatches = parsePatchFiles(patch, buildPatchCacheKey(patch, cacheScope));
const sourceFiles = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
const files = sourceFiles.map((file) => {
const filtered = options.ignoreWhitespace ? hideWhitespaceChanges(file) : file;
Expand All @@ -184,13 +174,13 @@ export function getRenderablePatch(

return {
kind: "raw",
text: normalizedPatch,
text: patch,
reason: "Unsupported diff format. Showing raw patch.",
};
} catch {
return {
kind: "raw",
text: normalizedPatch,
text: patch,
reason: "Failed to parse patch. Showing raw patch.",
};
}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/toolCallDiff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("toolCallDiff", () => {
expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0);
expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0);
expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe(
recordedContent.replace(/\n$/, ""),
recordedContent,
);
}
}
Expand Down
Loading