From 4c20aa5c8da8fc92dec1e7448ae84d6f8a470b66 Mon Sep 17 00:00:00 2001 From: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com> Date: Fri, 20 Nov 2026 00:00:56 +0200 Subject: [PATCH] test(editor): cover missing-file and malformed session restore (#4615) Adds regression tests for editor session recovery: a deleted selected file is dropped quietly and the remaining tab restores with its caret, a permission-denied tab stays open with its error inline, and truncated session JSON loads as null without writing storage until a valid session replaces it. No production changes. Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com> --- src/editor/core/controller.test.ts | 74 ++++++++++++++++++++++++++- src/editor/core/model/session.test.ts | 50 +++++++++++++++++- 2 files changed, 121 insertions(+), 3 deletions(-) diff --git a/src/editor/core/controller.test.ts b/src/editor/core/controller.test.ts index 943109c39..cb2e72390 100644 --- a/src/editor/core/controller.test.ts +++ b/src/editor/core/controller.test.ts @@ -1,9 +1,11 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { EditorController, openFilePaths, useEditorStatus, useTabs } from "./controller"; +import { EditorController, openFilePaths, useEditorStatus, useTabErrors, useTabs } from "./controller"; import { createMemoryBackend } from "./backend"; import { initialTabsState } from "./model/tabs"; import { useDocMeta } from "./model/document"; +import { storeSession } from "./model/session"; import { resetDistractionFree, setDistractionFree } from "@/workbench/distractionFree"; +import * as contributions from "@/workbench/contributions"; import { registerEditorKind, registerTextEditorWrapper, @@ -11,6 +13,7 @@ import { useProjectViewLead, } from "@/workbench/contributions"; import { registerActions } from "@/platform/actions"; +import { IpcError, type ErrorKind } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { useRemote } from "@/platform/remote"; import { hideContextMenu, resolveMenuItems, useContextMenuStore } from "@/ui"; @@ -29,6 +32,75 @@ afterEach(() => { useEditorStatus.setState({ status: null }); }); +describe("restoring an editor session", () => { + const root = "/session-restore"; + const existing = `${root}/existing.txt`; + const unavailable = `${root}/unavailable.txt`; + const text = "first\nsecond"; + let controller: EditorController | undefined; + + afterEach(() => { + controller?.stop(); + controller = undefined; + storeSession(root, null); + useTabErrors.setState({}, true); + vi.restoreAllMocks(); + }); + + async function restoreWithOpenError(kind: ErrorKind, message: string) { + storeSession(root, { + version: 1, + layout: { kind: "pane", tabs: [{ path: existing }, { path: unavailable }], active: 1 }, + activePane: 0, + files: { [existing]: { anchor: 6, head: 6 } }, + }); + const backend = createMemoryBackend({ [existing]: text }); + // For an absent path the memory backend serves sample text, not a disk error. + const open = backend.open.bind(backend); + vi.spyOn(backend, "open").mockImplementation(async (path) => { + if (path === unavailable) throw new IpcError({ kind, message }); + return open(path); + }); + const editor = (controller = new EditorController(backend)); + editor.start(); + + const restoring = editor.restoreSession(root); + const state = useTabs.getState(); + const selected = state.panes[state.activePane]!.active!; + // Restore opens the selected tab in the background. Join the same slot request. + await Promise.all([restoring, editor.ensureSlot(selected)]); + return { editor, selected }; + } + + it("silently drops a deleted selected file and restores the remaining tab", async () => { + const notify = vi.fn(); + vi.spyOn(contributions, "notifications").mockReturnValue({ notify }); + const { editor, selected } = await restoreWithOpenError("notFound", `No such file: ${unavailable}`); + + expect(notify).not.toHaveBeenCalled(); + expect(useTabErrors.getState()).toEqual({}); + expect(openFilePaths(useTabs.getState())).toEqual([existing]); + expect(editor.slotFor(selected)).toBeNull(); + expect(editor.activeInput()?.path).toBe(existing); + + // In the app the visible tab's editor host requests this slot after selection changes. + const state = useTabs.getState(); + const slot = await editor.ensureSlot(state.panes[state.activePane]!.active!); + expect(slot?.model.path).toBe(existing); + expect(slot?.state.doc.toString()).toBe(text); + expect(slot?.caret()).toMatchObject({ line: 1, col: 0 }); + }); + + it("keeps a tab and reports its open error when access is denied", async () => { + const message = `Permission denied: ${unavailable}`; + const { editor, selected } = await restoreWithOpenError("permissionDenied", message); + + expect(openFilePaths(useTabs.getState())).toEqual([existing, unavailable]); + expect(editor.activeInput()?.path).toBe(unavailable); + expect(useTabErrors.getState()).toEqual({ [selected]: message }); + }); +}); + describe("EditorController", () => { it("returns navigation context to the editor when activating a non-text tab", async () => { ctrl = new EditorController(createMemoryBackend({ "/p/a.ts": "one\n" })); diff --git a/src/editor/core/model/session.test.ts b/src/editor/core/model/session.test.ts index 3b512109f..3e0be06c5 100644 --- a/src/editor/core/model/session.test.ts +++ b/src/editor/core/model/session.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { initialTabsState, moveTabToNewSplit, @@ -9,7 +9,15 @@ import { togglePin, type TabsState, } from "./tabs"; -import { loadSession, parseSession, restoreTabsState, serializeSession, slashPath } from "./session"; +import { + loadSession, + parseSession, + restoreTabsState, + serializeSession, + slashPath, + storeSession, + type EditorSession, +} from "./session"; const file = (path: string) => ({ kind: "file", key: `file:${path}`, path }); const fileKey = (p: string) => `file:${p}`; @@ -103,6 +111,44 @@ describe("editor session", () => { }); }); +describe("loading a session with malformed JSON", () => { + const root = "/malformed-session"; + const key = `openidea.editorSession.v1:${root}`; + const malformed = '{"version":1,"layout":{"kind":"pane","tabs":['; + + beforeEach(() => localStorage.setItem(key, malformed)); + afterEach(() => { + vi.restoreAllMocks(); + localStorage.removeItem(key); + }); + + it("returns null without writing to storage or changing the saved bytes", () => { + const before = localStorage.getItem(key); + expect(before).toBe(malformed); + const setItem = vi.spyOn(Storage.prototype, "setItem"); + + expect(loadSession(root)).toBeNull(); + expect(localStorage.getItem(key)).toBe(before); + expect(setItem).not.toHaveBeenCalled(); + }); + + it("loads a valid session after an explicit save replaces malformed JSON", () => { + expect(loadSession(root)).toBeNull(); + const path = `${root}/recovered.ts`; + const recovered: EditorSession = { + version: 1, + layout: { kind: "pane", tabs: [{ path }], active: 0 }, + activePane: 0, + files: { [path]: { anchor: 1, head: 2 } }, + }; + + storeSession(root, recovered); + + expect(JSON.parse(localStorage.getItem(key)!)).toEqual(recovered); + expect(loadSession(root)).toEqual(recovered); + }); +}); + describe("moveTabToNewSplit", () => { it("moves a tab into a new pane on the given edge", () => { let s = initialTabsState();