|
1 | | -import type { CodeViewDiffItem, DiffLineAnnotation, SelectedLineRange } from "@pierre/diffs"; |
| 1 | +import type { |
| 2 | + CodeViewDiffItem, |
| 3 | + DiffLineAnnotation, |
| 4 | + FileDiffMetadata, |
| 5 | + SelectedLineRange, |
| 6 | +} from "@pierre/diffs"; |
2 | 7 | import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles"; |
3 | 8 | import { act, StrictMode, type ReactNode } from "react"; |
4 | 9 | import { create, type ReactTestRenderer } from "react-test-renderer"; |
5 | 10 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
6 | 11 |
|
7 | 12 | import { DraftId, useComposerDraftStore } from "~/composerDraftStore"; |
| 13 | +import { buildFileDiffIdentityKey } from "~/lib/diffRendering"; |
8 | 14 |
|
9 | 15 | type Selection = { id: string; range: SelectedLineRange } | null; |
10 | 16 | interface ViewerProps { |
@@ -44,25 +50,38 @@ const target = DraftId.make("diff-draft-test"); |
44 | 50 | const range: SelectedLineRange = { start: 1, end: 1, side: "additions" }; |
45 | 51 | let renderer: ReactTestRenderer; |
46 | 52 |
|
47 | | -function file(name: string, fileVersion = 1) { |
| 53 | +function file( |
| 54 | + name: string, |
| 55 | + fileVersion = 1, |
| 56 | + type: FileDiffMetadata["type"] = "change", |
| 57 | + previousName?: string, |
| 58 | +) { |
48 | 59 | const fileDiff = parsePatchFiles( |
49 | 60 | `diff --git a/${name} b/${name}\n--- a/${name}\n+++ b/${name}\n@@ -1 +1 @@\n-old\n+new\n`, |
50 | 61 | name, |
51 | 62 | )[0]!.files[0]!; |
52 | | - return { fileDiff, filePath: name, fileKey: name, fileVersion, collapsed: false }; |
| 63 | + fileDiff.type = type; |
| 64 | + if (previousName) fileDiff.prevName = previousName; |
| 65 | + return { |
| 66 | + fileDiff, |
| 67 | + filePath: name, |
| 68 | + fileKey: buildFileDiffIdentityKey(fileDiff), |
| 69 | + fileVersion, |
| 70 | + collapsed: false, |
| 71 | + }; |
53 | 72 | } |
54 | 73 |
|
55 | 74 | function view( |
56 | 75 | files: ReturnType<typeof file>[], |
57 | 76 | codeViewKey = "scope:initial", |
58 | | - pendingFilePaths: string[] = [], |
| 77 | + pendingFiles: FileDiffMetadata[] = [], |
59 | 78 | ) { |
60 | 79 | return ( |
61 | 80 | <StrictMode> |
62 | 81 | <AnnotatableCodeView |
63 | 82 | codeViewKey={codeViewKey} |
64 | 83 | files={files} |
65 | | - pendingFilePaths={pendingFilePaths} |
| 84 | + pendingFiles={pendingFiles} |
66 | 85 | sectionId="unstaged" |
67 | 86 | sectionTitle="Uncommitted" |
68 | 87 | composerDraftTarget={target} |
@@ -136,19 +155,51 @@ describe("diff comment drafts", () => { |
136 | 155 | act(() => renderer.update(view([file("a.ts", 2)], "scope:refreshed"))); |
137 | 156 |
|
138 | 157 | expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe("Keep this draft"); |
139 | | - expect(viewer().selectedLines).toEqual({ id: "a.ts", range }); |
| 158 | + expect(viewer().selectedLines).toEqual({ id: file("a.ts").fileKey, range }); |
140 | 159 | expect(viewer().options.enableLineSelection).toBe(false); |
141 | 160 | expect(viewer().options.enableGutterUtility).toBe(false); |
142 | 161 | }); |
143 | 162 |
|
144 | | - it("preserves a draft while its file's patch reloads", () => { |
| 163 | + it.each(["change", "new", "deleted", "rename-pure", "rename-changed"] as const)( |
| 164 | + "preserves a %s draft while its patch reloads", |
| 165 | + (type) => { |
| 166 | + const previousName = type.startsWith("rename") ? "old.ts" : undefined; |
| 167 | + const original = file("a.ts", 1, type, previousName); |
| 168 | + act(() => renderer.update(view([original, file("b.ts")]))); |
| 169 | + beginComment(); |
| 170 | + const pending = { |
| 171 | + ...original.fileDiff, |
| 172 | + type: previousName ? ("rename-changed" as const) : ("change" as const), |
| 173 | + hunks: [], |
| 174 | + cacheKey: "scope:a.ts:pending", |
| 175 | + }; |
| 176 | + |
| 177 | + act(() => renderer.update(view([file("b.ts")], "scope:loading", [pending]))); |
| 178 | + act(() => |
| 179 | + renderer.update(view([file("a.ts", 2, type, previousName), file("b.ts")], "scope:loaded")), |
| 180 | + ); |
| 181 | + |
| 182 | + expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe("Keep this draft"); |
| 183 | + expect(viewer().selectedLines).toEqual({ id: original.fileKey, range }); |
| 184 | + expect(viewer().options.enableGutterUtility).toBe(false); |
| 185 | + }, |
| 186 | + ); |
| 187 | + |
| 188 | + it("releases a replaced rename's draft when the replacement patch is unavailable", () => { |
| 189 | + const original = file("shared.ts", 1, "rename-changed", "old.ts"); |
| 190 | + act(() => renderer.update(view([original, file("b.ts")]))); |
145 | 191 | beginComment(); |
| 192 | + const replacement = file("shared.ts", 2, "rename-changed", "other.ts"); |
| 193 | + const pending = { ...replacement.fileDiff, hunks: [], cacheKey: "scope:shared.ts:pending" }; |
146 | 194 |
|
147 | | - act(() => renderer.update(view([file("b.ts")], "scope:loading", ["a.ts"]))); |
148 | | - act(() => renderer.update(view([file("a.ts", 2), file("b.ts")], "scope:loaded"))); |
| 195 | + // Failed lazy queries retain their placeholder metadata in the pending list. |
| 196 | + act(() => renderer.update(view([file("b.ts")], "scope:failed", [pending]))); |
149 | 197 |
|
150 | | - expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe("Keep this draft"); |
151 | | - expect(viewer().selectedLines).toEqual({ id: "a.ts", range }); |
152 | | - expect(viewer().options.enableGutterUtility).toBe(false); |
| 198 | + expect(renderer.root.findAllByType(DiffCommentAnnotation)).toHaveLength(0); |
| 199 | + expect(viewer().selectedLines).toBeNull(); |
| 200 | + expect(viewer().options.enableLineSelection).toBe(true); |
| 201 | + expect(viewer().options.enableGutterUtility).toBe(true); |
| 202 | + act(() => viewer().options.onGutterUtilityClick(range, { item: viewer().items[0]! })); |
| 203 | + expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe(""); |
153 | 204 | }); |
154 | 205 | }); |
0 commit comments