-
Notifications
You must be signed in to change notification settings - Fork 953
feat: Add web terminal with reconnecting TTYs #1186
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
31f27bc
feat: Add web terminal with reconnecting TTYs
kylecarbs 15d843e
Add xstate service
kylecarbs cb5ae98
Add the webpage for accessing a web terminal
kylecarbs 229c7e4
Add terminal page tests
kylecarbs 621aeb1
Merge branch 'main' into webterm
kylecarbs 3e1a0a4
Use Ticker instead of Timer
kylecarbs 4ef7106
Active Windows mode on Windows
kylecarbs 19c7b54
Merge branch 'main' into webterm
kylecarbs 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
Add terminal page tests
- Loading branch information
commit 229c7e480d12bc80c971b055bf6daef7390a9959
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { waitFor } from "@testing-library/react" | ||
import crypto from "crypto" | ||
import "jest-canvas-mock" | ||
import WS from "jest-websocket-mock" | ||
import { rest } from "msw" | ||
import React from "react" | ||
import { Route, Routes } from "react-router-dom" | ||
import { TextDecoder, TextEncoder } from "util" | ||
import { ReconnectingPTYRequest } from "../../api/types" | ||
import { history, MockWorkspaceAgent, render } from "../../testHelpers" | ||
import { server } from "../../testHelpers/server" | ||
import { Language, TerminalPage } from "./TerminalPage" | ||
|
||
Object.defineProperty(window, "matchMedia", { | ||
writable: true, | ||
value: jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // deprecated | ||
removeListener: jest.fn(), // deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}) | ||
|
||
Object.defineProperty(window, "crypto", { | ||
value: { | ||
randomUUID: () => crypto.randomUUID(), | ||
}, | ||
}) | ||
|
||
Object.defineProperty(window, "TextEncoder", { | ||
value: TextEncoder, | ||
}) | ||
|
||
const renderTerminal = () => { | ||
return render( | ||
<Routes> | ||
<Route path="/:username/:workspace/terminal" element={<TerminalPage renderer="dom" />} /> | ||
</Routes>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooh, curious why you put Routes in here, it might help me with some pain points I've had There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It said it was required for me to use |
||
) | ||
} | ||
|
||
const expectTerminalText = (container: HTMLElement, text: string) => { | ||
return waitFor(() => { | ||
const elements = container.getElementsByClassName("xterm-rows") | ||
if (elements.length < 1) { | ||
throw new Error("no xterm-rows") | ||
} | ||
const row = elements[0] as HTMLDivElement | ||
if (!row.textContent) { | ||
throw new Error("no text content") | ||
} | ||
expect(row.textContent).toContain(text) | ||
}) | ||
} | ||
|
||
describe("TerminalPage", () => { | ||
beforeEach(() => { | ||
history.push("/some-user/my-workspace/terminal") | ||
}) | ||
|
||
it("shows an error if fetching organizations fails", async () => { | ||
// Given | ||
server.use( | ||
rest.get("/api/v2/users/me/organizations", async (req, res, ctx) => { | ||
return res(ctx.status(500), ctx.json({ message: "nope" })) | ||
}), | ||
) | ||
|
||
// When | ||
const { container } = renderTerminal() | ||
|
||
// Then | ||
await expectTerminalText(container, Language.organizationsErrorMessagePrefix) | ||
}) | ||
|
||
it("shows an error if fetching workspace fails", async () => { | ||
// Given | ||
server.use( | ||
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => { | ||
return res(ctx.status(500), ctx.json({ id: "workspace-id" })) | ||
}), | ||
) | ||
|
||
// When | ||
const { container } = renderTerminal() | ||
|
||
// Then | ||
await expectTerminalText(container, Language.workspaceErrorMessagePrefix) | ||
}) | ||
|
||
it("shows an error if fetching workspace agent fails", async () => { | ||
// Given | ||
server.use( | ||
rest.get("/api/v2/workspacebuilds/:workspaceId/resources", (req, res, ctx) => { | ||
return res(ctx.status(500), ctx.json({ message: "nope" })) | ||
}), | ||
) | ||
|
||
// When | ||
const { container } = renderTerminal() | ||
|
||
// Then | ||
await expectTerminalText(container, Language.workspaceAgentErrorMessagePrefix) | ||
}) | ||
|
||
it("shows an error if the websocket fails", async () => { | ||
// Given | ||
server.use( | ||
rest.get("/api/v2/workspaceagents/:agentId/pty", (req, res, ctx) => { | ||
return res(ctx.status(500), ctx.json({})) | ||
}), | ||
) | ||
|
||
// When | ||
const { container } = renderTerminal() | ||
|
||
// Then | ||
await expectTerminalText(container, Language.websocketErrorMessagePrefix) | ||
}) | ||
|
||
it("renders data from the backend", async () => { | ||
// Given | ||
const server = new WS("ws://localhost/api/v2/workspaceagents/" + MockWorkspaceAgent.id + "/pty") | ||
const text = "something to render" | ||
|
||
// When | ||
const { container } = renderTerminal() | ||
|
||
// Then | ||
await server.connected | ||
server.send(text) | ||
await expectTerminalText(container, text) | ||
server.close() | ||
}) | ||
|
||
it("resizes on connect", async () => { | ||
// Given | ||
const server = new WS("ws://localhost/api/v2/workspaceagents/" + MockWorkspaceAgent.id + "/pty") | ||
|
||
// When | ||
renderTerminal() | ||
|
||
// Then | ||
await server.connected | ||
const msg = await server.nextMessage | ||
const req: ReconnectingPTYRequest = JSON.parse(new TextDecoder().decode(msg as Uint8Array)) | ||
|
||
expect(req.height).toBeGreaterThan(0) | ||
expect(req.width).toBeGreaterThan(0) | ||
server.close() | ||
}) | ||
}) |
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.