diff --git a/packages/plugins/plugin-workspace-diff/src/diff-model.ts b/packages/plugins/plugin-workspace-diff/src/diff-model.ts index 4ceeba34272..ca500e6633d 100644 --- a/packages/plugins/plugin-workspace-diff/src/diff-model.ts +++ b/packages/plugins/plugin-workspace-diff/src/diff-model.ts @@ -92,7 +92,6 @@ export function isLongDiffFile(file: Pick) { export function toFileViewModels(diff: WorkspaceDiffResponse | null | undefined): DiffFileViewModel[] { return (diff?.files ?? []).map((file) => { - const patchWarnings = file.patches.flatMap((patch) => patch.warnings); const patches = buildFilePatches(file); const lineCount = patches.reduce((count, patch) => count + patch.lineCount, 0); return { @@ -104,7 +103,7 @@ export function toFileViewModels(diff: WorkspaceDiffResponse | null | undefined) binary: file.binary, oversized: file.oversized, truncated: file.truncated, - warnings: [...file.warnings, ...patchWarnings], + warnings: file.warnings, patchKinds: file.patches.map((patch) => patch.kind), patches, patch: patches.find((patch) => patch.patch)?.patch ?? null, @@ -119,10 +118,7 @@ export function diffSummary(diff: WorkspaceDiffResponse | null | undefined): Dif const fileCount = stats?.fileCount ?? 0; const additions = stats?.additions ?? 0; const deletions = stats?.deletions ?? 0; - const warningCount = (diff?.warnings.length ?? 0) - + (diff?.files ?? []).reduce((count, file) => { - return count + file.warnings.length + file.patches.reduce((patchCount, patch) => patchCount + patch.warnings.length, 0); - }, 0); + const warningCount = diff?.warnings.length ?? 0; return { changedLabel: `${fileCount} ${fileCount === 1 ? "file" : "files"}`, diff --git a/packages/plugins/plugin-workspace-diff/tests/diff-model.spec.ts b/packages/plugins/plugin-workspace-diff/tests/diff-model.spec.ts index dde2656eb86..8ab427c5583 100644 --- a/packages/plugins/plugin-workspace-diff/tests/diff-model.spec.ts +++ b/packages/plugins/plugin-workspace-diff/tests/diff-model.spec.ts @@ -38,20 +38,45 @@ describe("workspace diff UI model", () => { }); it("surfaces truncation and file warnings", () => { + const warning = { code: "patch_truncated" as const, message: "Patch was truncated.", path: "src/app.ts" }; const file = changedFile({ truncated: true, - warnings: [{ code: "patch_truncated", message: "Patch was truncated.", path: "src/app.ts" }], + warnings: [warning], patches: [], }); - const diff = diffResponse({ files: [file], truncated: true }); + const diff = diffResponse({ files: [file], truncated: true, warnings: [warning] }); expect(buildFilePatch(file)).toBeNull(); + expect(toFileViewModels(diff)[0]?.warnings).toEqual([warning]); expect(diffSummary(diff)).toMatchObject({ warningCount: 1, truncated: true, }); }); + it("does not duplicate aggregated patch warnings", () => { + const warning = { code: "patch_truncated" as const, message: "Patch was truncated.", path: "src/app.ts" }; + const file = changedFile({ + warnings: [warning], + patches: [ + { + kind: "unstaged", + patch: null, + additions: 0, + deletions: 0, + binary: false, + oversized: false, + truncated: true, + warnings: [warning], + }, + ], + }); + const diff = diffResponse({ files: [file], warnings: [warning] }); + + expect(toFileViewModels(diff)[0]?.warnings).toEqual([warning]); + expect(diffSummary(diff).warningCount).toBe(1); + }); + it("keeps staged and unstaged patches renderable as separate single-file diffs", () => { const stagedPatch = [ "diff --git a/src/app.ts b/src/app.ts",