Repository navigation
🤖 refactor: the terminal router keeps its sessions across a reconnect #6304
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
6 commits
Select commit
Hold shift + click to select a range
42b5040
refactor: the terminal router keeps its sessions and takes the new cl…
ThomasK33 0865d39
tests: the router's inventory rule checks for setClient()
ThomasK33 3c4fc0a
fix: replace the terminal screen with a VT full reset (RIS), not Term…
ThomasK33 06a7ed5
fix: the terminal router gets only the live client, and attach waits …
ThomasK33 17b9272
fix: an empty first screen after a reconnect keeps the shown screen (…
ThomasK33 0aec776
revert: an empty first screen after a reconnect replaces the screen a…
ThomasK33 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
55 changes: 55 additions & 0 deletions
55
src/browser/components/TerminalView/writeScreenState.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,55 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { dirname, join } from "node:path"; | ||
| import { Ghostty } from "ghostty-web"; | ||
| import { writeScreenState } from "./TerminalView"; | ||
|
|
||
| // From the file: the test DOM's URL rejects the long data: URL that Ghostty.load() uses by default. | ||
| const WASM_PATH = join(dirname(Bun.resolveSync("ghostty-web", import.meta.dir)), "ghostty-vt.wasm"); | ||
|
|
||
| /** Every non-empty line of a real ghostty VT, scrollback first. */ | ||
| function visibleLines(term: ReturnType<Ghostty["createTerminal"]>, rows: number): string[] { | ||
| const text = (cells: Array<{ codepoint: number }> | null) => | ||
| (cells ?? []) | ||
| .map((cell) => String.fromCodePoint(cell.codepoint || 32)) | ||
| .join("") | ||
| .trimEnd(); | ||
| const lines: string[] = []; | ||
| for (let i = 0; i < term.getScrollbackLength(); i++) lines.push(text(term.getScrollbackLine(i))); | ||
| for (let row = 0; row < rows; row++) lines.push(text(term.getLine(row))); | ||
| return lines.filter((line) => line.length > 0); | ||
| } | ||
|
|
||
| // Runs on the real VT (WASM), so a ghostty-web change to how RIS treats the scrollback fails here. | ||
| describe("writeScreenState on the ghostty VT", () => { | ||
| test("a replacing screen leaves no line of the old screen or scrollback behind", async () => { | ||
| const ghostty = await Ghostty.load(WASM_PATH); | ||
| const term = ghostty.createTerminal(80, 24); | ||
| try { | ||
| let before = ""; | ||
| for (let i = 1; i <= 60; i++) before += `line-${i}\r\n`; | ||
| term.write(before); | ||
|
|
||
| // The server's screen state after a reconnect: its own copy of the same scrollback. | ||
| writeScreenState({ write: (data) => term.write(data) }, before + "after-reconnect\r\n", true); | ||
|
|
||
| const lines = visibleLines(term, 24); | ||
| expect(lines.filter((line) => line === "line-1")).toHaveLength(1); | ||
| expect(lines.filter((line) => line === "line-60")).toHaveLength(1); | ||
| expect(lines.at(-1)).toBe("after-reconnect"); | ||
| } finally { | ||
| term.free(); | ||
| } | ||
| }); | ||
|
|
||
| test("a first screen is written as is", async () => { | ||
| const ghostty = await Ghostty.load(WASM_PATH); | ||
| const term = ghostty.createTerminal(80, 24); | ||
| try { | ||
| term.write("kept\r\n"); | ||
| writeScreenState({ write: (data) => term.write(data) }, "first\r\n", false); | ||
| expect(visibleLines(term, 24)).toEqual(["kept", "first"]); | ||
| } finally { | ||
| term.free(); | ||
| } | ||
| }); | ||
| }); |
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,111 @@ | ||
| import "../../../tests/ui/dom"; | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { cleanup, render, waitFor } from "@testing-library/react"; | ||
| import { useEffect } from "react"; | ||
| import { installDom } from "../../../tests/ui/dom"; | ||
| import { createConnectionHarness, GAP_MODES } from "@/browser/contexts/connection.testHarness"; | ||
| import { TerminalRouterProvider, useTerminalRouter } from "./TerminalRouterContext"; | ||
| import type { TerminalSessionRouter } from "./TerminalSessionRouter"; | ||
|
|
||
| /** One entry per connect: the attach calls that connection's server saw. */ | ||
| let attachesPerConnection: number[] = []; | ||
| /** One entry per connect: the sizes that connection's server got. */ | ||
| let resizesPerConnection: Array<Array<{ cols: number; rows: number }>> = []; | ||
|
|
||
| function serverOverrides() { | ||
| const index = attachesPerConnection.length; | ||
| attachesPerConnection.push(0); | ||
| resizesPerConnection.push([]); | ||
| return { | ||
| terminal: { | ||
| attach: () => { | ||
| attachesPerConnection[index] += 1; | ||
| return Promise.resolve( | ||
| (async function* () { | ||
| yield { type: "screenState" as const, data: `screen-${index}` }; | ||
| await new Promise(() => undefined); // stays open until aborted | ||
| })() | ||
| ); | ||
| }, | ||
| // An exit stream that never yields: the process keeps running. | ||
| onExit: () => | ||
| Promise.resolve( | ||
| (async function* () { | ||
| await new Promise(() => undefined); | ||
| yield 0; | ||
| })() | ||
| ), | ||
| resize: (input: { cols: number; rows: number }) => { | ||
| resizesPerConnection[index]?.push({ cols: input.cols, rows: input.rows }); | ||
| return Promise.resolve(); | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| let routers: Array<TerminalSessionRouter | null> = []; | ||
| let subscribes = 0; | ||
| let screens: Array<{ state: string; replace: boolean }> = []; | ||
|
|
||
| /** Subscribes once per router, as TerminalView does. */ | ||
| function TerminalProbe() { | ||
| const router = useTerminalRouter(); | ||
| routers.push(router); | ||
| useEffect(() => { | ||
| if (router == null) return; | ||
| subscribes += 1; | ||
| return router.subscribe("t1", { | ||
| onOutput: () => undefined, | ||
| onScreenState: (state, options) => | ||
| screens.push({ state, replace: options?.replace === true }), | ||
| onExit: () => undefined, | ||
| }); | ||
| }, [router]); | ||
| return null; | ||
| } | ||
|
|
||
| describe.each([...GAP_MODES])("TerminalRouterProvider across a reconnect (%s)", (mode) => { | ||
| let cleanupDom: (() => void) | null = null; | ||
| beforeEach(() => { | ||
| cleanupDom = installDom(); | ||
| attachesPerConnection = []; | ||
| resizesPerConnection = []; | ||
| routers = []; | ||
| subscribes = 0; | ||
| screens = []; | ||
| }); | ||
| afterEach(() => { | ||
| cleanup(); | ||
| cleanupDom?.(); | ||
| cleanupDom = null; | ||
| }); | ||
|
|
||
| test("the terminal keeps its router and subscription, and reattaches on the new client", async () => { | ||
| const harness = createConnectionHarness(mode, serverOverrides); | ||
| render( | ||
| <TerminalRouterProvider> | ||
| <TerminalProbe /> | ||
| </TerminalRouterProvider>, | ||
| { wrapper: harness.Wrapper } | ||
| ); | ||
| await waitFor(() => expect(screens).toEqual([{ state: "screen-0", replace: false }])); | ||
| const router = routers.at(-1) ?? null; | ||
| expect(router).not.toBeNull(); | ||
|
|
||
| await harness.drop(); | ||
| // A resize in the gap goes to no client, and to the PTY once the next client attaches. | ||
| await router?.resize("t1", 100, 30); | ||
| expect(resizesPerConnection[0]).toEqual([]); | ||
| harness.reconnect(); | ||
| await waitFor(() => expect(resizesPerConnection[1]).toEqual([{ cols: 100, rows: 30 }])); | ||
| await waitFor(() => expect(attachesPerConnection).toEqual([1, 1])); | ||
| await waitFor(() => expect(screens.at(-1)).toEqual({ state: "screen-1", replace: true })); | ||
|
|
||
| // A new router (or a null one in the gap) made the view subscribe again and clear itself. | ||
| const distinct: unknown[] = [...new Set(routers.filter((r) => r != null))]; | ||
| expect(distinct).toEqual([router]); | ||
| expect(routers.slice(routers.indexOf(router))).not.toContain(null); | ||
| expect(subscribes).toBe(1); | ||
| }); | ||
| }); |
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
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.