Skip to content

Commit 837f92a

Browse files
test(diff-view): pin the rollback stop at the file buffer restore refusal
The row that reports a refused restore mocked the first applyEdit to resolve false, which is the diff-editor restore, so the file-scheme buffer's own restore - the gate in front of the tab close in discardFileTab() - stayed unexercised. This row lets the diff-side restore and its save confirm and refuses the second edit, so the refusal the report is about is the one the gate answers for. Control: with that gate disabled the row fails - AssertionError: promise resolved "undefined" instead of rejecting, exit code 1.
1 parent b11c6cc commit 837f92a

1 file changed

Lines changed: 65 additions & 1 deletion

File tree

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Stats } from "fs"
12
import * as fs from "fs/promises"
23
import { DiffViewProvider, DIFF_VIEW_URI_SCHEME, DIFF_VIEW_LABEL_CHANGES } from "../DiffViewProvider"
34
import { DecorationController } from "../DecorationController"
@@ -1813,6 +1814,69 @@ describe("DiffViewProvider", () => {
18131814
expect(vscode.window.tabGroups.close).not.toHaveBeenCalled()
18141815
})
18151816

1817+
it("revertChanges() stops the rollback when the file buffer restore is the refused one", async () => {
1818+
// The row above refuses the first applyEdit, which is the diff-editor restore, so the
1819+
// file-scheme buffer's own restore - the guard in front of the tab close - is never the
1820+
// one under test. Here the diff-side restore and its save both confirm, so the rollback
1821+
// reaches the file-scheme buffer and THAT restore is the refusal: the class has to stop
1822+
// before closing a tab whose buffer still holds the streamed content.
1823+
const editor = buildActiveDiffEditor()
1824+
editor.document.isDirty = true
1825+
editor.document.save = vi.fn().mockResolvedValue(true)
1826+
const streamedBuffer = {
1827+
uri: makeUri(mockTargetPath),
1828+
getText: vi.fn().mockReturnValue("streamed content"),
1829+
isDirty: true,
1830+
positionAt: vi.fn((offset: number) => ({ line: 0, character: offset })),
1831+
save: vi.fn().mockResolvedValue(true),
1832+
}
1833+
const fileTab = {
1834+
input: Object.assign(new vscode.TabInputText(makeUri(mockTargetPath)), {
1835+
uri: makeUri(mockTargetPath),
1836+
}),
1837+
isDirty: true,
1838+
label: "mock-target-file.ts",
1839+
}
1840+
const originalTabs = Object.getOwnPropertyDescriptor(vscode.window.tabGroups, "all")
1841+
const originalDocuments = Object.getOwnPropertyDescriptor(vscode.workspace, "textDocuments")
1842+
Object.defineProperty(vscode.window.tabGroups, "all", {
1843+
get: () => [{ tabs: [fileTab] }],
1844+
configurable: true,
1845+
})
1846+
Object.defineProperty(vscode.workspace, "textDocuments", {
1847+
value: [streamedBuffer],
1848+
configurable: true,
1849+
})
1850+
Object.assign(diffViewProvider, {
1851+
relPath: "mock-target-file.ts",
1852+
activeDiffEditor: editor,
1853+
editType: "create",
1854+
createdDirs: [],
1855+
originalContent: "",
1856+
})
1857+
// The first applyEdit is the diff-editor restore and it lands; the second is the
1858+
// file-scheme buffer's restore, and that is the one VS Code refuses.
1859+
vi.mocked(vscode.workspace.applyEdit).mockResolvedValueOnce(true).mockResolvedValueOnce(false)
1860+
1861+
try {
1862+
await expect(diffViewProvider.revertChanges()).rejects.toThrow(/could not restore the buffer/i)
1863+
} finally {
1864+
if (originalTabs) {
1865+
Object.defineProperty(vscode.window.tabGroups, "all", originalTabs)
1866+
}
1867+
if (originalDocuments) {
1868+
Object.defineProperty(vscode.workspace, "textDocuments", originalDocuments)
1869+
}
1870+
}
1871+
1872+
// Both restores were attempted, so the refusal that stopped the rollback is the second one;
1873+
// the diff-side save confirmed and the file-scheme buffer was neither saved nor closed.
1874+
expect(vscode.workspace.applyEdit).toHaveBeenCalledTimes(2)
1875+
expect(editor.document.save).toHaveBeenCalledTimes(1)
1876+
expect(streamedBuffer.save).not.toHaveBeenCalled()
1877+
expect(vscode.window.tabGroups.close).not.toHaveBeenCalled()
1878+
})
1879+
18161880
it("revertChanges() stops the rollback without closing when the clean save is not confirmed", async () => {
18171881
// document.save() resolving false leaves the buffer's clean state unconfirmed. Closing
18181882
// then would prompt the user over a possibly-dirty tab, and answering yes can write
@@ -2590,7 +2654,7 @@ describe("DiffViewProvider", () => {
25902654
const document = makeAbandonedDocument(callOrder)
25912655
openAbandonedView(document, [], callOrder)
25922656
// 42 bytes on disk: not the empty placeholder this edit created.
2593-
vi.mocked(fs.stat).mockResolvedValueOnce({ size: 42 } as unknown as fs.Stats)
2657+
vi.mocked(fs.stat).mockResolvedValueOnce({ size: 42 } as unknown as Stats)
25942658
vi.mocked(vscode.workspace.applyEdit).mockResolvedValueOnce(true)
25952659

25962660
await diffViewProvider.discardUnapprovedStream()

0 commit comments

Comments
 (0)