Skip to content

Commit d71db6a

Browse files
test(editor): pin the rollback range, target uri and the touched-document close decision
Two review rows on #1932 read against head 5bc7709: - DiffViewProvider.spec.ts:3165 (Minor, Functional Correctness): makeRollbackDocument answered positionAt(0) and positionAt(len) with the same (0,0), so an empty or reversed range and a wrong target uri both passed while mocked applyEdit reported success. The fixture now answers per offset and the two new-file rollback tests assert the document uri plus both offsets the replacement range is built from. - DiffViewProvider.spec.ts:3378 (Minor, Functional Correctness): the modify rollback only exercised the untouched-document branch, where autoCloseZooOpenedFilesAfterUserEdited and autoCloseZooOpenedNewFiles cannot move the close decision. Added the touched- document case with the three settings set to distinct values: after-user-edited off keeps the tab and re-shows the file rather than closing it. Gates run in this worktree against this commit: - pnpm --dir src exec vitest run integrations/editor/__tests__/DiffViewProvider.spec.ts -> 1 test file passed, 131 tests passed. - pnpm --dir src exec eslint --max-warnings=0 integrations/editor/__tests__/DiffViewProvider.spec.ts -> clean. - pnpm --dir src exec prettier --write/--check on the same file -> clean. - src/eslint-suppressions.json untouched (no suppression entry could grow: eslint reported 0 problems for the file). Not run here: knip, compile, e2e-mock, platform-unit-test matrices, mutation-diff, codecov. No mutant was executed against this suite locally by this lane.
1 parent 5bc7709 commit d71db6a

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,7 +3162,11 @@ describe("DiffViewProvider", () => {
31623162
const makeRollbackDocument = (content: string, isDirty: boolean, saved: boolean) => ({
31633163
uri: makeUri(settleTargetPath),
31643164
getText: vi.fn().mockReturnValue(content),
3165-
positionAt: vi.fn().mockReturnValue({ line: 0, character: 0 }),
3165+
// Distinct answers per offset: a fixture that returned (0,0) for every offset could not tell a
3166+
// full-buffer range from an empty one. The Range double wraps whatever positions it is given.
3167+
positionAt: vi.fn((offset: number) =>
3168+
offset === 0 ? { line: 0, character: 0 } : { line: 1, character: offset },
3169+
),
31663170
isDirty,
31673171
save: vi.fn().mockResolvedValue(saved),
31683172
})
@@ -3220,7 +3224,12 @@ describe("DiffViewProvider", () => {
32203224

32213225
await withTabGroupsAll([makeDirtyFileTab()], () => diffViewProvider.revertChanges())
32223226

3223-
const [, , replacedText] = mockWorkspaceEdit.replace.mock.calls[0]
3227+
// Distinct positions prove the range spans the buffer: an empty or reversed range would hand
3228+
// the Range double the same position twice, and the uri is the document being rolled back.
3229+
const [replacedUri, , replacedText] = mockWorkspaceEdit.replace.mock.calls[0]
3230+
expect(replacedUri).toBe(document.uri)
3231+
expect(document.positionAt).toHaveBeenNthCalledWith(1, 0)
3232+
expect(document.positionAt).toHaveBeenNthCalledWith(2, "partial model output".length)
32243233
expect(replacedText).toBe("the content the file held before this write")
32253234
})
32263235

@@ -3238,7 +3247,12 @@ describe("DiffViewProvider", () => {
32383247

32393248
await withTabGroupsAll([makeDirtyFileTab()], () => diffViewProvider.revertChanges())
32403249

3241-
const [, , replacedText] = mockWorkspaceEdit.replace.mock.calls[0]
3250+
// Distinct positions prove the range spans the buffer: an empty or reversed range would hand
3251+
// the Range double the same position twice, and the uri is the document being rolled back.
3252+
const [replacedUri, , replacedText] = mockWorkspaceEdit.replace.mock.calls[0]
3253+
expect(replacedUri).toBe(document.uri)
3254+
expect(document.positionAt).toHaveBeenNthCalledWith(1, 0)
3255+
expect(document.positionAt).toHaveBeenNthCalledWith(2, "partial model output".length)
32423256
expect(replacedText).toBe("")
32433257
})
32443258

@@ -3396,6 +3410,43 @@ describe("DiffViewProvider", () => {
33963410
expect(showEditedFile).not.toHaveBeenCalled()
33973411
})
33983412

3413+
it("lines 902 and 904: keeps the tab on the rollback path when the user touched the document and the after-user-edited setting is off", async () => {
3414+
// The touched-document branch is the only one where autoCloseZooOpenedFilesAfterUserEdited decides
3415+
// the outcome on a modify rollback, and it is the only branch where autoCloseZooOpenedNewFiles is
3416+
// unreadable. Setting the three flags to distinct values here means a wrong forwarded value moves
3417+
// the outcome: base on + after-user-edited on would close, new-files on alone would close.
3418+
const document = makeRollbackDocument("streamed content", false, true)
3419+
const closeFileTab = vi.fn().mockResolvedValue(undefined)
3420+
const showEditedFile = vi.fn().mockResolvedValue(undefined)
3421+
Object.assign(diffViewProvider, {
3422+
relPath: settleRelPath,
3423+
activeDiffEditor: { document },
3424+
editType: "modify",
3425+
createdDirs: [],
3426+
originalContent: "original",
3427+
documentWasOpen: false,
3428+
userTouchedDocument: true,
3429+
userTouchedDiffEditor: false,
3430+
})
3431+
diffViewProvider["taskRef"] = {
3432+
deref: () =>
3433+
makeTaskDouble({
3434+
autoCloseZooOpenedFiles: true,
3435+
autoCloseZooOpenedFilesAfterUserEdited: false,
3436+
autoCloseZooOpenedNewFiles: false,
3437+
}),
3438+
} as unknown as WeakRef<Task>
3439+
vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true)
3440+
diffViewProvider["closeAllDiffViews"] = vi.fn().mockResolvedValue(undefined)
3441+
diffViewProvider["closeFileTab"] = closeFileTab
3442+
diffViewProvider["showEditedFileWithoutDisruptingFocus"] = showEditedFile
3443+
3444+
await diffViewProvider.revertChanges()
3445+
3446+
expect(closeFileTab).not.toHaveBeenCalled()
3447+
expect(showEditedFile).toHaveBeenCalledWith(settleTargetPath)
3448+
})
3449+
33993450
it("lines 1209 and 1211: stops the rollback and reports the refused restore of the file-scheme buffer by name", async () => {
34003451
// The diff-side restore confirms; the plain file-scheme buffer holding the same unapproved
34013452
// content is the one VS Code refuses to put back. Closing it anyway would hand the user a

0 commit comments

Comments
 (0)