|
| 1 | +import type { Stats } from "fs" |
1 | 2 | import * as fs from "fs/promises" |
2 | 3 | import { DiffViewProvider, DIFF_VIEW_URI_SCHEME, DIFF_VIEW_LABEL_CHANGES } from "../DiffViewProvider" |
3 | 4 | import { DecorationController } from "../DecorationController" |
@@ -1813,6 +1814,69 @@ describe("DiffViewProvider", () => { |
1813 | 1814 | expect(vscode.window.tabGroups.close).not.toHaveBeenCalled() |
1814 | 1815 | }) |
1815 | 1816 |
|
| 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 | + |
1816 | 1880 | it("revertChanges() stops the rollback without closing when the clean save is not confirmed", async () => { |
1817 | 1881 | // document.save() resolving false leaves the buffer's clean state unconfirmed. Closing |
1818 | 1882 | // then would prompt the user over a possibly-dirty tab, and answering yes can write |
@@ -2590,7 +2654,7 @@ describe("DiffViewProvider", () => { |
2590 | 2654 | const document = makeAbandonedDocument(callOrder) |
2591 | 2655 | openAbandonedView(document, [], callOrder) |
2592 | 2656 | // 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) |
2594 | 2658 | vi.mocked(vscode.workspace.applyEdit).mockResolvedValueOnce(true) |
2595 | 2659 |
|
2596 | 2660 | await diffViewProvider.discardUnapprovedStream() |
|
0 commit comments