Skip to content

feat(site): add latency to the terminal #7801

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 7 commits into from
Jun 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
BrunoQuaresma committed Jun 5, 2023
commit 06bd79d8027ca1cfcfb2ea026f4fceffd2af8150
119 changes: 48 additions & 71 deletions site/src/pages/TerminalPage/TerminalPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ import "jest-canvas-mock"
import WS from "jest-websocket-mock"
import { rest } from "msw"
import {
MockPrimaryWorkspaceProxy,
MockProxyLatencies,
MockUser,
MockWorkspace,
MockWorkspaceAgent,
MockWorkspaceProxies,
} from "testHelpers/entities"
import { TextDecoder, TextEncoder } from "util"
import { ReconnectingPTYRequest } from "../../api/types"
import { history, render } from "../../testHelpers/renderHelpers"
import {
renderWithAuth,
waitForLoaderToBeRemoved,
} from "../../testHelpers/renderHelpers"
import { server } from "../../testHelpers/server"
import TerminalPage, { Language } from "./TerminalPage"
import { Route, Routes } from "react-router-dom"
import { ProxyContext } from "contexts/ProxyContext"

Object.defineProperty(window, "matchMedia", {
writable: true,
Expand All @@ -35,56 +34,35 @@ Object.defineProperty(window, "TextEncoder", {
value: TextEncoder,
})

const renderTerminal = () => {
// @emyrk using renderWithAuth would be best here, but I was unable to get it to work.
return render(
<Routes>
<Route
path="/:username/:workspace/terminal"
element={
<ProxyContext.Provider
value={{
proxyLatencies: MockProxyLatencies,
proxy: {
proxy: MockPrimaryWorkspaceProxy,
preferredPathAppURL: "",
preferredWildcardHostname: "",
},
proxies: MockWorkspaceProxies,
isFetched: true,
isLoading: false,
setProxy: jest.fn(),
clearProxy: jest.fn(),
refetchProxyLatencies: jest.fn(),
}}
>
<TerminalPage renderer="dom" />
</ProxyContext.Provider>
}
/>
</Routes>,
)
const renderTerminal = async (
route = `/${MockUser.username}/${MockWorkspace.name}/terminal`,
) => {
const utils = renderWithAuth(<TerminalPage renderer="dom" />, {
route,
path: "/:username/:workspace/terminal",
})
await waitForLoaderToBeRemoved()
return utils
}

const expectTerminalText = (container: HTMLElement, text: string) => {
return waitFor(() => {
const elements = container.getElementsByClassName("xterm-rows")
if (elements.length === 0) {
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)
})
return waitFor(
() => {
const elements = container.getElementsByClassName("xterm-rows")
if (elements.length === 0) {
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)
},
{ timeout: 3_000 },
)
}

describe("TerminalPage", () => {
beforeEach(() => {
history.push(`/some-user/${MockWorkspace.name}/terminal`)
})

it("shows an error if fetching workspace fails", async () => {
// Given
server.use(
Expand All @@ -97,7 +75,7 @@ describe("TerminalPage", () => {
)

// When
const { container } = renderTerminal()
const { container } = await renderTerminal()

// Then
await expectTerminalText(container, Language.workspaceErrorMessagePrefix)
Expand All @@ -112,67 +90,66 @@ describe("TerminalPage", () => {
)

// When
const { container } = renderTerminal()
const { container } = await 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 ws = new WS(
`ws://localhost/api/v2/workspaceagents/${MockWorkspaceAgent.id}/pty`,
)
const text = "something to render"

// When
const { container } = renderTerminal()
const { container } = await renderTerminal()

// Then
await server.connected
server.send(text)
await ws.connected
ws.send(text)
await expectTerminalText(container, text)
server.close()
ws.close()
})

it("resizes on connect", async () => {
// Given
const server = new WS(
"ws://localhost/api/v2/workspaceagents/" + MockWorkspaceAgent.id + "/pty",
const ws = new WS(
`ws://localhost/api/v2/workspaceagents/${MockWorkspaceAgent.id}/pty`,
)

// When
renderTerminal()
await renderTerminal()

// Then
await server.connected
const msg = await server.nextMessage
await ws.connected
const msg = await ws.nextMessage
const req: ReconnectingPTYRequest = JSON.parse(
new TextDecoder().decode(msg as Uint8Array),
)

expect(req.height).toBeGreaterThan(0)
expect(req.width).toBeGreaterThan(0)
server.close()
ws.close()
})

it("supports workspace.agent syntax", async () => {
// Given
const server = new WS(
"ws://localhost/api/v2/workspaceagents/" + MockWorkspaceAgent.id + "/pty",
const ws = new WS(
`ws://localhost/api/v2/workspaceagents/${MockWorkspaceAgent.id}/pty`,
)
const text = "something to render"

// When
history.push(
const { container } = await renderTerminal(
`/some-user/${MockWorkspace.name}.${MockWorkspaceAgent.name}/terminal`,
)
const { container } = renderTerminal()

// Then
await server.connected
server.send(text)
await ws.connected
ws.send(text)
await expectTerminalText(container, text)
server.close()
ws.close()
})
})