Repository navigation
feat(desktop): finish SUPER-1545 — alt-screen exemption, persist guard, hidden-webview LRU #5754
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
180 changes: 180 additions & 0 deletions
180
apps/desktop/src/renderer/lib/terminal/terminal-runtime-registry.test.ts
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,180 @@ | ||
| import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"; | ||
|
|
||
| mock.module("renderer/lib/trpc-client", () => ({ | ||
| electronTrpcClient: { | ||
| keyboardLayout: { | ||
| changes: { subscribe: () => {} }, | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| const { terminalRuntimeRegistry } = await import("./terminal-runtime-registry"); | ||
| const { tryPersistRuntimeState } = await import("./terminal-runtime"); | ||
|
|
||
| interface FakeStorageState { | ||
| values: Map<string, string>; | ||
| storage: Storage; | ||
| } | ||
|
|
||
| function createFakeStorage(): FakeStorageState { | ||
| const values = new Map<string, string>(); | ||
| const storage = { | ||
| get length() { | ||
| return values.size; | ||
| }, | ||
| clear: () => values.clear(), | ||
| getItem: (key: string) => values.get(key) ?? null, | ||
| key: (index: number) => Array.from(values.keys())[index] ?? null, | ||
| removeItem: (key: string) => values.delete(key), | ||
| setItem: (key: string, value: string) => values.set(key, value), | ||
| } as Storage; | ||
| return { values, storage }; | ||
| } | ||
|
|
||
| const originalLocalStorage = globalThis.localStorage; | ||
| let fakeStorage: FakeStorageState; | ||
|
|
||
| beforeEach(() => { | ||
| fakeStorage = createFakeStorage(); | ||
| Object.defineProperty(globalThis, "localStorage", { | ||
| configurable: true, | ||
| value: fakeStorage.storage, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(globalThis, "localStorage", { | ||
| configurable: true, | ||
| value: originalLocalStorage, | ||
| }); | ||
| }); | ||
|
|
||
| describe("terminalRuntimeRegistry eviction cleanup", () => { | ||
| test("keeps a runtime when dimensions fail to persist", () => { | ||
| const terminalId = "dimensions-write-failure"; | ||
| const setItem = fakeStorage.storage.setItem.bind(fakeStorage.storage); | ||
| fakeStorage.storage.setItem = (key: string, value: string) => { | ||
| if (key === `terminal-dims:${terminalId}`) { | ||
| throw new Error("dimensions write failed"); | ||
| } | ||
| setItem(key, value); | ||
| }; | ||
| const runtime = { | ||
| terminalId, | ||
| serializeAddon: { serialize: () => "serialized scrollback" }, | ||
| lastCols: 120, | ||
| lastRows: 32, | ||
| }; | ||
|
|
||
| expect( | ||
| tryPersistRuntimeState( | ||
| runtime as Parameters<typeof tryPersistRuntimeState>[0], | ||
| ), | ||
| ).toBe(false); | ||
| expect(fakeStorage.values.get(`terminal-buffer:${terminalId}`)).toBe( | ||
| "serialized scrollback", | ||
| ); | ||
| expect(fakeStorage.values.has(`terminal-dims:${terminalId}`)).toBe(false); | ||
| }); | ||
|
|
||
| test("dispose clears persisted state even when eviction already removed the entry", () => { | ||
| const terminalId = "evicted-terminal"; | ||
| fakeStorage.values.set(`terminal-buffer:${terminalId}`, "scrollback"); | ||
| fakeStorage.values.set( | ||
| `terminal-dims:${terminalId}`, | ||
| JSON.stringify({ cols: 120, rows: 32 }), | ||
| ); | ||
|
|
||
| expect(terminalRuntimeRegistry.has(terminalId)).toBe(false); | ||
| terminalRuntimeRegistry.dispose(terminalId); | ||
|
|
||
| expect(fakeStorage.values.has(`terminal-buffer:${terminalId}`)).toBe(false); | ||
| expect(fakeStorage.values.has(`terminal-dims:${terminalId}`)).toBe(false); | ||
| }); | ||
|
|
||
| test("reschedules eviction when a parked terminal changes buffers", () => { | ||
| let emitBufferChange: () => void = () => { | ||
| throw new Error("buffer listener was not installed"); | ||
| }; | ||
| let listenerDisposed = false; | ||
| const runtime = { | ||
| container: {} as HTMLDivElement | null, | ||
| terminal: { | ||
| buffer: { | ||
| onBufferChange: (listener: () => void) => { | ||
| emitBufferChange = listener; | ||
| return { dispose: () => (listenerDisposed = true) }; | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| const entry = { | ||
| runtime, | ||
| disposeBufferChangeListener: null as (() => void) | null, | ||
| }; | ||
| const registryInternals = terminalRuntimeRegistry as unknown as { | ||
| observeBufferChanges: (observedEntry: typeof entry) => void; | ||
| pendingEviction: ReturnType<typeof setTimeout> | null; | ||
| }; | ||
|
|
||
| if (registryInternals.pendingEviction !== null) { | ||
| clearTimeout(registryInternals.pendingEviction); | ||
| registryInternals.pendingEviction = null; | ||
| } | ||
| registryInternals.observeBufferChanges(entry); | ||
| emitBufferChange(); | ||
| expect(registryInternals.pendingEviction).toBeNull(); | ||
|
|
||
| runtime.container = null; | ||
| emitBufferChange(); | ||
| expect(registryInternals.pendingEviction).not.toBeNull(); | ||
|
|
||
| if (registryInternals.pendingEviction !== null) { | ||
| clearTimeout(registryInternals.pendingEviction); | ||
| registryInternals.pendingEviction = null; | ||
| } | ||
| entry.disposeBufferChangeListener?.(); | ||
| expect(listenerDisposed).toBe(true); | ||
| }); | ||
|
|
||
| test("defers eviction while flushed output is still being parsed", () => { | ||
| const terminalId = "parser-busy-terminal"; | ||
| let flushed = false; | ||
| const gate = { pending: 1, queued: null as (() => void) | null }; | ||
| const entry = { | ||
| terminalId, | ||
| instanceId: terminalId, | ||
| runtime: { | ||
| container: null, | ||
| gate, | ||
| terminal: { buffer: { active: { type: "normal" } } }, | ||
| }, | ||
| transport: { | ||
| _writeCoalescer: { | ||
| flushSync: () => { | ||
| flushed = true; | ||
| }, | ||
| }, | ||
| }, | ||
| linkManager: null, | ||
| pendingLinkHandlers: null, | ||
| lastUsedAt: 1, | ||
| }; | ||
| const registryInternals = terminalRuntimeRegistry as unknown as { | ||
| entries: Map<string, typeof entry>; | ||
| evictExcessParkedRuntimes: () => void; | ||
| }; | ||
| const entryKey = `${terminalId}\u0000${terminalId}`; | ||
| registryInternals.entries.set(entryKey, entry); | ||
|
|
||
| try { | ||
| registryInternals.evictExcessParkedRuntimes(); | ||
|
|
||
| expect(flushed).toBe(true); | ||
| expect(gate.queued).not.toBeNull(); | ||
| expect(registryInternals.entries.has(entryKey)).toBe(true); | ||
| } finally { | ||
| registryInternals.entries.delete(entryKey); | ||
| } | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.