opencode/packages/app/e2e/regression/terminal-input.spec.ts

207 lines
8.4 KiB
TypeScript

import { base64Encode, checksum } from "@opencode/util/encode"
import { expect, test } from "@playwright/test"
import { sessionHref } from "../utils/app"
import { mockWorkspace, openSession, type WorkspaceInput } from "../utils/workspace"
import { expectSessionTitle } from "../utils/waits"
const workspace = { name: "TerminalInput", pty: {} } satisfies WorkspaceInput
test.use({ viewport: { width: 1440, height: 900 } })
test("clears the terminal line with Command+Delete", async ({ page }) => {
const { pty } = await openSession(page, workspace)
const terminal = page.locator('[data-component="terminal"]')
await page.keyboard.press("Control+Backquote")
await expect(terminal.locator("textarea")).toHaveCount(1)
await expect.poll(() => pty.sockets.length).toBe(1)
await page.keyboard.press("Meta+Backspace")
await expect.poll(() => pty.sockets[0]!.input.join("")).toBe("\x15")
})
test("reveals the terminal after its first output and hides the native caret", async ({ page }) => {
const { pty } = await openSession(page, workspace)
await page.keyboard.press("Control+Backquote")
const terminal = page.locator('[data-component="terminal"]')
await expect(terminal).toHaveAttribute("contenteditable", "true")
await expect(terminal).toHaveCSS("caret-color", "rgba(0, 0, 0, 0)")
await expect(terminal).toHaveCSS("opacity", "0")
await expect.poll(() => pty.sockets.length).toBe(1)
pty.send("\x1b[?25h")
await expect(terminal).toHaveCSS("opacity", "0")
pty.send("ready")
await expect(terminal).toHaveCSS("opacity", "1")
})
test("routes typing to the composer unless the open terminal is focused", async ({ page }) => {
const { pty, editor } = await openSession(page, workspace)
const terminal = page.locator('[data-component="terminal"]')
await editor.click()
await expect(editor).toBeFocused()
await page.keyboard.press("Control+Backquote")
await expect(terminal).toBeVisible()
await expect.poll(() => terminal.evaluate((element) => element.contains(document.activeElement))).toBe(true)
await page.keyboard.type("x")
await expect(editor).toHaveText("")
await expect.poll(() => pty.sockets.length).toBe(1)
pty.send("ready")
await expect(terminal).toHaveCSS("opacity", "1")
await page.evaluate(() => {
if (document.activeElement instanceof HTMLElement) document.activeElement.blur()
})
await page.keyboard.type("a")
await expect(editor).toBeFocused()
await expect(editor).toHaveText("a")
})
for (const mount of ["cached", "explicit"] as const) {
test(`keeps newer composer focus while a ${mount} terminal finishes mounting`, async ({ page }) => {
const ghostty = Promise.withResolvers<void>()
const release = Promise.withResolvers<void>()
await page.route(/ghostty-web/, async (route) => {
ghostty.resolve()
await release.promise
await route.continue()
})
const cached = { id: "pty_cached", title: "Terminal 1" }
const { pty, sessions } = await mockWorkspace(page, {
...workspace,
pty: mount === "cached" ? { initial: [cached] } : {},
seed:
mount === "cached"
? {
panes: { ses_terminalinput: { terminal: true, terminalHeight: 320 } },
// The pre-extension terminal store, which the terminal extension imports.
storage: { [legacyTerminalKey()]: { active: cached.id, all: [{ ...cached, titleNumber: 1 }] } },
}
: undefined,
})
await page.goto(sessionHref(sessions[0]!.id), { waitUntil: "commit" })
await expectSessionTitle(page, "TerminalInput")
const editor = page.locator('[data-component="composer-editor"]')
const terminal = page.locator('[data-component="terminal"]')
if (mount === "explicit") {
await expect(editor).toBeEditable()
await page.keyboard.press("Control+Backquote")
}
await expect(terminal).toBeVisible()
await ghostty.promise
await editor.click()
await expect(editor).toBeFocused()
release.resolve()
await expect(terminal.locator("textarea")).toHaveCount(1)
await expect.poll(() => pty.sockets.length).toBe(1)
pty.send("ready")
await expect(terminal).toHaveCSS("opacity", "1")
await expect(editor).toBeFocused()
if (mount === "cached") expect(pty.created).toEqual([])
})
}
test("reconnects the same terminal after a transient connection failure", async ({ page }) => {
const { pty } = await openSession(page, workspace)
let tickets = 0
await page.route(/\/api\/pty\/[^/]+\/connect-token/, (route) => {
if (route.request().method() === "OPTIONS") return route.fallback()
tickets += 1
if (tickets !== 2) return route.fallback()
return route.fulfill({
status: 503,
headers: { "access-control-allow-origin": "*" },
body: "Service Unavailable",
})
})
await page.keyboard.press("Control+Backquote")
const terminal = page.locator('[data-component="terminal"]')
await expect(terminal.locator("textarea")).toHaveCount(1)
await expect.poll(() => pty.sockets.length).toBe(1)
const id = pty.sockets[0]!.id
await terminal.evaluate((element) => element.setAttribute("data-connection-probe", "original"))
await pty.sockets[0]!.close(1011, "Temporary disconnection")
await expect.poll(() => tickets).toBe(3)
await expect.poll(() => pty.sockets.length).toBe(2)
expect(pty.sockets[1]!.id).toBe(id)
expect(pty.rejected).toEqual([])
await expect(terminal).toHaveAttribute("data-connection-probe", "original")
expect(pty.created.map((item) => item.id)).toEqual([id])
})
test("focuses a terminal created from the new-terminal button", async ({ page }) => {
const { editor } = await openSession(page, workspace)
const terminal = page.locator('[data-component="terminal"]')
await page.keyboard.press("Control+Backquote")
await expect(terminal.locator("textarea")).toHaveCount(1)
await editor.click()
await expect(editor).toBeFocused()
await page.getByRole("button", { name: "New terminal" }).click()
await expect(page.getByRole("tab", { name: "Terminal 2" })).toHaveAttribute("aria-selected", "true")
const active = page.locator('#terminal-wrapper-pty_2 [data-component="terminal"]')
await expect.poll(() => active.evaluate((element) => element.contains(document.activeElement))).toBe(true)
})
test("focuses the terminal opened from the command palette", async ({ page }) => {
const { editor } = await openSession(page, workspace)
const terminal = page.locator('[data-component="terminal"]')
await editor.click()
await page.keyboard.press("ControlOrMeta+Shift+P")
const palette = page.getByRole("dialog")
const search = palette.getByRole("textbox")
await expect(search).toBeFocused()
await search.fill("Toggle terminal")
await expect(palette.getByRole("option", { name: /^Toggle terminal/ })).toHaveAttribute("aria-selected", "true")
await search.press("Enter")
await expect(palette).toHaveCount(0)
await expect(terminal.locator("textarea")).toHaveCount(1)
await expect.poll(() => terminal.evaluate((element) => element.contains(document.activeElement))).toBe(true)
})
test("closing the last restored terminal does not create a hidden replacement", async ({ page }) => {
const cached = { id: "pty_cached", title: "Terminal 1" }
const { pty } = await openSession(page, {
...workspace,
pty: { initial: [cached] },
seed: {
panes: { ses_terminalinput: { terminal: true, terminalHeight: 320 } },
storage: { [legacyTerminalKey()]: { active: cached.id, all: [{ ...cached, titleNumber: 1 }] } },
},
})
const terminal = page.locator('[data-component="terminal"]')
await expect(terminal.locator("textarea")).toHaveCount(1)
await expect.poll(() => pty.sockets.length).toBe(1)
expect(pty.created).toEqual([])
await page.locator("#terminal-panel").getByRole("button", { name: "Close terminal", exact: true }).click()
await expect(terminal).toBeHidden()
await expect.poll(() => pty.removed).toEqual([cached.id])
expect(pty.created).toEqual([])
await page.keyboard.press("Control+Backquote")
await expect(terminal.locator("textarea")).toHaveCount(1)
await expect.poll(() => pty.created.map((item) => item.id)).toEqual(["pty_1"])
await expect(page.locator("#terminal-panel").getByRole("tab", { name: "Terminal 1", exact: true })).toHaveAttribute(
"aria-selected",
"true",
)
})
function legacyTerminalKey() {
const dir = base64Encode("C:/OpenCode/TerminalInput")
const head = dir.slice(0, 12).replace(/[^a-zA-Z0-9._-]/g, "-")
return `opencode.workspace.${head}.${checksum(dir) ?? "0"}.dat:workspace:terminal`
}