Repository navigation
feat(sessions): restore saved terminal tabs on startup #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { beforeEach, afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const invoke = vi.hoisted(() => vi.fn()); | ||
| vi.mock("@tauri-apps/api/core", () => ({ invoke })); | ||
| const key = "session_restore.desktop"; | ||
| let settings: Map<string, string>; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| settings = new Map(); | ||
| vi.stubGlobal("navigator", { userAgent: "node" }); | ||
| const storage = new Map<string, string>(); | ||
| vi.stubGlobal("localStorage", { | ||
| getItem: (key: string) => storage.get(key) ?? null, | ||
| setItem: (key: string, value: string) => storage.set(key, value), | ||
| removeItem: (key: string) => storage.delete(key), | ||
| }); | ||
| invoke.mockReset(); | ||
| invoke.mockImplementation(async (command: string, args: Record<string, string>) => { | ||
| if (command === "get_setting") return settings.get(args.key) ?? null; | ||
| if (command === "set_setting") { settings.set(args.key, args.value); return; } | ||
| if (command === "list_profiles") return [{ id: "ssh1", name: "Current SSH", host: "new-host", port: 2222 }]; | ||
| if (command === "list_telnet_profiles") return [{ id: "telnet1", name: "Telnet", host: "telnet", port: 23, login_script: "secret" }]; | ||
| if (command === "list_serial_profiles") return []; | ||
| return null; | ||
| }); | ||
| }); | ||
| afterEach(() => vi.unstubAllGlobals()); | ||
|
|
||
| async function modules() { | ||
| return { app: await import("./app.svelte.ts"), restore: await import("./session-restore.svelte.ts") }; | ||
| } | ||
| function saved(terminals: unknown[], active = -1) { | ||
| settings.set(`${key}.enabled`, "true"); | ||
| settings.set(`${key}.snapshot`, JSON.stringify({ version: 1, terminals, active })); | ||
| } | ||
|
|
||
| describe("session restoration", () => { | ||
| it("defaults off without fetching profiles or reopening tabs", async () => { | ||
| const { app, restore } = await modules(); | ||
| expect(await restore.restore()).toBe(false); | ||
| expect(restore.enabled()).toBe(false); | ||
| expect(restore.ready()).toBe(true); | ||
| expect(app.tabs()).toHaveLength(1); | ||
| expect(invoke).not.toHaveBeenCalledWith("list_profiles"); | ||
| }); | ||
|
|
||
| it("reopens duplicates in order with fresh IDs and current profiles, skipping deleted profiles", async () => { | ||
| saved([ | ||
| { type: "ssh", profileId: "ssh1" }, { type: "ssh", profileId: "gone" }, | ||
| { type: "local" }, { type: "ssh", profileId: "ssh1" }, | ||
| { type: "telnet", profileId: "telnet1" }, | ||
| ], 3); | ||
| const { app, restore } = await modules(); | ||
| expect(await restore.restore()).toBe(true); | ||
| expect(app.tabs().map((tab) => tab.type)).toEqual(["home", "ssh", "local", "ssh", "telnet"]); | ||
| expect(new Set(app.tabs().map((tab) => tab.id)).size).toBe(5); | ||
| expect(app.activeTabId()).toBe(app.tabs()[3].id); | ||
| expect(app.tabs()[1].meta).toEqual({ profileId: "ssh1", host: "new-host", port: "2222" }); | ||
| expect(app.tabs()[4].meta).not.toHaveProperty("login_script"); | ||
| }); | ||
|
|
||
| it("persists only profile references, including split panes, without secret metadata or unsupported tabs", async () => { | ||
| const { app, restore } = await modules(); | ||
| app.addTab({ id: "ssh", type: "ssh", label: "Secret label", meta: { profileId: "ssh1", password: "secret", host: "host" } }); | ||
| app.addPane("ssh", "right", { id: "telnet", type: "telnet", label: "Telnet", meta: { profileId: "telnet1", login_script: "secret" } }); | ||
| app.addTab({ id: "serial", type: "serial", label: "Serial", meta: { serialProfileId: "serial1", login_script: "secret" } }); | ||
| app.addTab({ id: "forward", type: "forward", label: "Forward", meta: { forwardId: "f1" } }); | ||
| app.addTab({ id: "container", type: "docker_exec", label: "Container", meta: { connectorSpec: "secret" } }); | ||
| app.addTab({ id: "editor", type: "edit", label: "Editor", meta: { content: "secret" } }); | ||
| app.setActiveWorkspace("ssh"); | ||
| app.setActivePane("telnet"); | ||
| const snapshot = restore.snapshot(); | ||
| expect(JSON.parse(snapshot)).toEqual({ version: 1, terminals: [ | ||
| { type: "ssh", profileId: "ssh1" }, { type: "telnet", profileId: "telnet1" }, | ||
| { type: "serial", profileId: "serial1" }, | ||
| ], active: 1 }); | ||
| expect(snapshot).not.toContain("secret"); | ||
| }); | ||
|
|
||
| it("clears saved sessions when disabled and does not persist while disabled", async () => { | ||
| saved([{ type: "local" }]); | ||
| const { restore } = await modules(); | ||
| await restore.restore(); | ||
| await restore.setEnabled(false); | ||
| restore.persist(restore.snapshot()); | ||
| expect(settings.get(`${key}.enabled`)).toBe("false"); | ||
| expect(settings.get(`${key}.snapshot`)).toBe(""); | ||
| }); | ||
|
|
||
| it.each(["bad JSON", '{"version":2,"terminals":[]}', '{"version":1,"terminals":[{"type":"ssh"}]}']) | ||
| ("handles invalid snapshots: %s", async (snapshot) => { | ||
| settings.set(`${key}.enabled`, "true"); | ||
| settings.set(`${key}.snapshot`, snapshot); | ||
| const { app, restore } = await modules(); | ||
| expect(await restore.restore()).toBe(false); | ||
| expect(app.tabs()).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("does not overwrite the snapshot if profile loading fails", async () => { | ||
| saved([{ type: "ssh", profileId: "ssh1" }]); | ||
| const original = settings.get(`${key}.snapshot`); | ||
| const implementation = invoke.getMockImplementation()!; | ||
| invoke.mockImplementation((command, args) => command === "list_profiles" | ||
| ? Promise.reject(new Error("offline")) : implementation(command, args)); | ||
| const { restore } = await modules(); | ||
| await restore.restore(); | ||
| restore.persist(restore.snapshot()); | ||
| expect(settings.get(`${key}.snapshot`)).toBe(original); | ||
| }); | ||
|
|
||
| it("does not reopen connections after startup cancellation", async () => { | ||
| saved([{ type: "local" }]); | ||
| const controller = new AbortController(); | ||
| controller.abort(); | ||
| const { app, restore } = await modules(); | ||
| expect(await restore.restore(controller.signal)).toBe(false); | ||
| expect(app.tabs()).toHaveLength(1); | ||
| expect(restore.ready()).toBe(false); | ||
| }); | ||
|
|
||
| it("does not take over connections opened manually during startup", async () => { | ||
| saved([{ type: "local" }]); | ||
| const { app, restore } = await modules(); | ||
| app.addTab({ id: "manual", type: "local", label: "Manual" }); | ||
| expect(await restore.restore()).toBe(false); | ||
| expect(app.tabs().map((tab) => tab.id)).toEqual(["home", "manual"]); | ||
| }); | ||
|
|
||
| it("serializes snapshots so the latest write wins", async () => { | ||
| const { restore } = await modules(); | ||
| await restore.restore(); | ||
| await restore.setEnabled(true); | ||
| restore.persist("first"); | ||
| restore.persist("second"); | ||
| await vi.waitFor(() => expect(settings.get(`${key}.snapshot`)).toBe("second")); | ||
| }); | ||
|
|
||
| it("keeps saved ordering even when MRU reordering is enabled", async () => { | ||
| saved([{ type: "ssh", profileId: "ssh1" }, { type: "local" }], 1); | ||
| const { app, restore } = await modules(); | ||
| await app.setTabMru(true); | ||
| await restore.restore(); | ||
| expect(app.tabs().map((tab) => tab.type)).toEqual(["home", "ssh", "local"]); | ||
| expect(app.activeTabId()).toBe(app.tabs()[2].id); | ||
| }); | ||
|
|
||
| it("restores mobile SSH/Telnet while excluding desktop local and serial terminals", async () => { | ||
| vi.stubGlobal("navigator", { userAgent: "Android" }); | ||
| settings.set("session_restore.mobile.enabled", "true"); | ||
| settings.set("session_restore.mobile.snapshot", JSON.stringify({ version: 1, terminals: [ | ||
| { type: "local" }, { type: "serial", profileId: "s1" }, | ||
| { type: "ssh", profileId: "ssh1" }, { type: "telnet", profileId: "telnet1" }, | ||
| ], active: 2 })); | ||
| const { app, restore } = await modules(); | ||
| expect(await restore.restore()).toBe(false); | ||
| expect(app.tabs().map((tab) => tab.type)).toEqual(["home", "ssh", "telnet"]); | ||
| expect(invoke).not.toHaveBeenCalledWith("list_serial_profiles"); | ||
| expect(app.activeTabId()).toBe(app.tabs()[1].id); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the user toggles this setting from a clone or AI-handoff window, that window renders the same Settings UI and writes the shared database key, but this guard prevents it from ever persisting a snapshot while the primary window's per-webview
_enabledvalue remains unchanged. Enabling restore there therefore saves no sessions, while disabling it can be undone when the primary window next changes tabs and writes another snapshot; propagate preference changes to the primary window or make the control unavailable in secondary windows.AGENTS.md reference: AGENTS.md:L211-L216
Useful? React with 👍 / 👎.