Files

692 lines
31 KiB
TypeScript

import { expect, test, type Page } from "@playwright/test"
import { SERVER, holdRoute } from "../utils/app"
import { installSseTransport } from "../utils/sse-transport"
import { terminalScreen, terminalText } from "../utils/terminal"
import { openSession } from "../utils/workspace"
test.use({ viewport: { width: 1440, height: 900 } })
test.beforeEach(async ({ context }) => {
await context.grantPermissions(["clipboard-read", "clipboard-write"])
})
const SESSION = "ses_terminals"
test("restores a terminal from the server's snapshot after a reload", async ({ page }) => {
const output = "first-line\r\n"
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_restore", sessionID: SESSION, title: "Build", output }] },
})
const screen = terminalScreen(page, "pty_restore")
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
await expect.poll(() => terminalText(page, screen)).toBe("first-line")
expect(persistentPty.sockets.map((socket) => socket.cursor)).toEqual([Buffer.byteLength(output)])
// Output the page never saw: only the server's snapshot can restore it.
const snapshot = await holdRoute(page, /\/api\/experimental\/persistent-pty\/pty_restore\/snapshot/)
await page.reload()
await snapshot.arrived
persistentPty.output("pty_restore", "while-away\r\n")
snapshot.release()
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toHaveAttribute("aria-selected", "true")
await expect.poll(() => terminalText(page, screen)).toBe("first-line\nwhile-away")
expect(persistentPty.sockets.map((socket) => socket.cursor)).toEqual([
Buffer.byteLength(output),
Buffer.byteLength(output + "while-away\r\n"),
])
})
test("after a reload, the close shortcut closes the restored terminal", async ({ page }) => {
// Elsewhere Ctrl+W reaches the shell, so the terminal hands only macOS's Cmd+W to the app.
await page.addInitScript(() => Object.defineProperty(navigator, "platform", { get: () => "MacIntel" }))
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: {
initial: [
{ id: "pty_build", sessionID: SESSION, title: "Build" },
{ id: "pty_shell", sessionID: SESSION, title: "Shell" },
],
},
})
await openSide(page)
await showTerminal(page, "Shell", "pty_shell")
// Focus comes before the connection; a reload during its connect-token request would leave nothing connected.
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual(["pty_shell"])
// The restored tab is selected before the server lists the session's terminals.
const list = await holdRoute(page, /\/api\/experimental\/session\/ses_terminals\/terminal(\?|$)/, { method: "GET" })
await page.reload()
await list.arrived
list.release()
await expect(side(page).getByRole("tab", { name: "Shell", exact: true })).toHaveAttribute("aria-selected", "true")
// The terminal connects once it has mounted, and only a mounted terminal takes a click.
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual(["pty_shell", "pty_shell"])
await terminalScreen(page, "pty_shell").click()
await expect(terminalScreen(page, "pty_shell").locator("textarea")).toBeFocused()
await page.keyboard.press("Meta+W")
await expect(side(page).getByRole("tab", { name: "Shell", exact: true })).toHaveCount(0)
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toBeVisible()
expect(persistentPty.removed).toEqual(["pty_shell"])
})
test("a terminal another client creates joins the strip without taking it over", async ({ page }) => {
const { persistentPty } = await openSession(page, { name: "Terminals", persistentPty: {} })
await openSide(page)
await expect(side(page).getByRole("tab", { name: "New tab", exact: true })).toHaveAttribute("aria-selected", "true")
persistentPty.add({ id: "pty_remote", sessionID: SESSION, title: "Remote shell", output: "from-elsewhere\r\n" })
const tab = side(page).getByRole("tab", { name: "Remote shell", exact: true })
await expect(tab).toHaveAttribute("aria-selected", "false")
await expect(side(page).getByRole("tab", { name: "New tab", exact: true })).toHaveAttribute("aria-selected", "true")
expect(persistentPty.sockets).toHaveLength(0)
await tab.click()
await expect.poll(() => terminalText(page, terminalScreen(page, "pty_remote"))).toBe("from-elsewhere")
})
test("a new terminal opens focused, takes control, and streams input and output", async ({ page }) => {
const { persistentPty } = await openSession(page, { name: "Terminals", persistentPty: {} })
await openSide(page)
await side(page)
.getByRole("button", { name: /^Terminal/ })
.click()
await expect(side(page).getByRole("tab", { name: "Terminal 1", exact: true })).toHaveAttribute(
"aria-selected",
"true",
)
expect(persistentPty.created.map((info) => ({ title: info.title, cwd: info.cwd }))).toEqual([
{ title: "Terminal 1", cwd: "C:/OpenCode/Terminals" },
])
const id = persistentPty.created[0]!.id
await expect(terminalScreen(page, id).locator("textarea")).toBeFocused()
await expect.poll(() => persistentPty.sockets.map((socket) => socket.takeover)).toEqual([true])
const socket = persistentPty.sockets[0]!
await expect.poll(() => socket.frames.filter((frame) => frame.type === 0).length).toBeGreaterThan(0)
expect(persistentPty.controller(id)).toBe(socket.attachmentID)
await page.keyboard.type("echo hi")
await expect
.poll(() =>
socket.frames
.filter((frame) => frame.type === 1)
.map((frame) => frame.data)
.join(""),
)
.toBe("echo hi")
persistentPty.output(id, "hi\r\n")
await expect.poll(() => terminalText(page, terminalScreen(page, id))).toBe("hi")
})
test("the New terminal shortcut creates the next numbered terminal", async ({ page }) => {
const { persistentPty, editor } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_first", sessionID: SESSION, title: "Terminal 1" }] },
})
await openSide(page)
await expect(side(page).getByRole("tab", { name: "Terminal 1", exact: true })).toBeVisible()
await editor.click()
await page.keyboard.press("Control+Alt+T")
await expect(side(page).getByRole("tab", { name: "Terminal 2", exact: true })).toHaveAttribute(
"aria-selected",
"true",
)
expect(persistentPty.created.map((info) => info.title)).toEqual(["Terminal 2"])
await expect(terminalScreen(page, persistentPty.created[0]!.id).locator("textarea")).toBeFocused()
})
// Closing the palette returns focus to the composer while the terminal is still being created.
test("New terminal from the command palette opens the terminal focused", async ({ page }) => {
const { persistentPty, editor } = await openSession(page, { name: "Terminals", persistentPty: {} })
await editor.click()
await page.keyboard.press("Control+Shift+P")
const palette = page.getByRole("dialog")
const input = palette.getByRole("combobox")
await expect(input).toBeFocused()
await input.fill("New terminal")
await expect(palette.getByRole("option", { name: /^New terminal/ })).toHaveAttribute("aria-selected", "true")
await input.press("Enter")
await expect(side(page).getByRole("tab", { name: "Terminal 1", exact: true })).toHaveAttribute(
"aria-selected",
"true",
)
await expect(terminalScreen(page, persistentPty.created[0]!.id).locator("textarea")).toBeFocused()
})
test("a click into the composer while a new terminal is created keeps the focus there", async ({ page }) => {
const { persistentPty, editor } = await openSession(page, { name: "Terminals", persistentPty: {} })
const create = await holdRoute(page, /\/api\/experimental\/session\/ses_terminals\/terminal(\?|$)/, {
method: "POST",
})
await editor.click()
await page.keyboard.press("Control+Alt+T")
await create.arrived
await editor.click()
create.release()
await expect(side(page).getByRole("tab", { name: "Terminal 1", exact: true })).toHaveAttribute(
"aria-selected",
"true",
)
await expect.poll(() => persistentPty.sockets.length).toBe(1)
await expect(editor).toBeFocused()
})
// Both presses used to send a create, so two tabs both titled "Terminal 1" opened.
test("pressing New terminal again while one is created creates one terminal", async ({ page }) => {
const { persistentPty, editor } = await openSession(page, { name: "Terminals", persistentPty: {} })
const create = await holdRoute(page, /\/api\/experimental\/session\/ses_terminals\/terminal(\?|$)/, {
method: "POST",
})
await editor.click()
await page.keyboard.press("Control+Alt+T")
await create.arrived
await page.keyboard.press("Control+Alt+T")
create.release()
await expect(side(page).getByRole("tab", { name: "Terminal 1", exact: true })).toHaveAttribute(
"aria-selected",
"true",
)
await expect(terminalScreen(page, persistentPty.created[0]!.id).locator("textarea")).toBeFocused()
expect(create.requests).toHaveLength(1)
expect(persistentPty.created.map((info) => info.title)).toEqual(["Terminal 1"])
})
test("another client's control ends at a click, which resizes the terminal to the pane", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_shared", sessionID: SESSION, title: "Shared" }] },
})
await openSide(page)
await side(page).getByRole("tab", { name: "Shared", exact: true }).click()
await expect.poll(() => persistentPty.sockets.length).toBe(1)
const socket = persistentPty.sockets[0]!
await expect.poll(() => persistentPty.controller("pty_shared")).toBe(socket.attachmentID)
// The other client controls the terminal at its own size.
persistentPty.takeControl("pty_shared")
persistentPty.resize("pty_shared", { cols: 40, rows: 10 })
const sent = socket.frames.length
await terminalScreen(page, "pty_shared").click()
await expect.poll(() => persistentPty.controller("pty_shared")).toBe(socket.attachmentID)
await expect.poll(() => socket.frames.length).toBeGreaterThan(sent)
const pane = () => paneSize(socket.frames.at(-1)!)
expect(socket.frames.slice(sent).map(paneSize)).not.toContainEqual({ cols: 40, rows: 10 })
await expect.poll(() => persistentPty.terminals[0]!.size).toEqual(pane())
// While it controls the terminal, the pane's new size follows.
const before = pane()
await page.setViewportSize({ width: 1200, height: 600 })
await expect.poll(() => pane().rows).toBeLessThan(before.rows)
await expect.poll(() => persistentPty.terminals[0]!.size).toEqual(pane())
})
test("a terminal whose shell exits leaves the strip", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: {
initial: [
{ id: "pty_build", sessionID: SESSION, title: "Build" },
{ id: "pty_shell", sessionID: SESSION, title: "Shell" },
],
},
})
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual(["pty_build"])
persistentPty.exit("pty_build", 0)
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toHaveCount(0)
await expect(side(page).getByRole("tab", { name: "Shell", exact: true })).toBeVisible()
await expect(page.getByText("Terminal disconnected", { exact: true })).toHaveCount(0)
})
test("a dropped connection says the terminal disconnected and keeps its tab", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_drop", sessionID: SESSION, title: "Build", output: "still-here\r\n" }] },
})
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
const screen = terminalScreen(page, "pty_drop")
await expect.poll(() => terminalText(page, screen)).toBe("still-here")
await persistentPty.close("pty_drop")
await expect(side(page).getByText("Terminal disconnected", { exact: true })).toBeVisible()
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toHaveAttribute("aria-selected", "true")
expect(persistentPty.sockets).toHaveLength(1)
expect(await terminalText(page, screen)).toBe("still-here")
})
test("a terminal list that fails says why, and Try again lists the terminals", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_build", sessionID: SESSION, title: "Build", output: "still-here\r\n" }] },
})
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
const screen = terminalScreen(page, "pty_build")
await expect.poll(() => terminalText(page, screen)).toBe("still-here")
// The restored session's list fails with an error other than the daemon's 503.
persistentPty.listFails = true
await page.reload()
const failure = side(page).getByRole("alert")
await expect(failure.getByText("Couldn't load terminals", { exact: true })).toBeVisible()
await expect(failure.getByText("UnexpectedStatus: 500", { exact: true })).toBeVisible()
// Without the list the restored tab has no title yet, but it shows and stays selected.
await expect(side(page).getByRole("tab", { name: "Terminal", exact: true })).toHaveAttribute("aria-selected", "true")
persistentPty.listFails = false
await failure.getByRole("button", { name: "Try again", exact: true }).click()
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toHaveAttribute("aria-selected", "true")
await expect.poll(() => terminalText(page, screen)).toBe("still-here")
await expect(side(page).getByText("Couldn't load terminals", { exact: true })).toHaveCount(0)
})
// A list the server accepted and never answered used to leave "Loading terminal." on screen with no way to retry.
test("a terminal list the server never answers says so, and Try again lists the terminals", async ({ page }) => {
await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_build", sessionID: SESSION, title: "Build", output: "still-here\r\n" }] },
})
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
const screen = terminalScreen(page, "pty_build")
await expect.poll(() => terminalText(page, screen)).toBe("still-here")
const list = await holdRoute(page, /\/api\/experimental\/session\/ses_terminals\/terminal(\?|$)/, { method: "GET" })
await page.clock.install()
await page.reload()
await list.arrived
await expect(side(page).getByText("Loading terminal…", { exact: true })).toBeVisible()
// The request budget, `TERMINAL_REQUEST_TIMEOUT_MS`.
await page.clock.fastForward(15_000)
const failure = side(page).getByRole("alert")
await expect(failure.getByText("Couldn't load terminals", { exact: true })).toBeVisible()
await expect(failure.getByText("The server took too long to answer", { exact: true })).toBeVisible()
list.release()
await failure.getByRole("button", { name: "Try again", exact: true }).click()
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toHaveAttribute("aria-selected", "true")
await expect.poll(() => terminalText(page, screen)).toBe("still-here")
})
test("after a server restart, showing the terminal again restores it from a new snapshot", async ({ page }) => {
const output = "before-restart\r\n"
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_handoff", sessionID: SESSION, title: "Build", output }] },
})
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
const screen = terminalScreen(page, "pty_handoff")
await expect.poll(() => terminalText(page, screen)).toBe("before-restart")
await persistentPty.restart()
await expect(side(page).getByText("Terminal disconnected", { exact: true })).toBeVisible()
// The adopted daemon kept the shell running.
persistentPty.output("pty_handoff", "after-restart\r\n")
// Showing the tab again takes a new snapshot and reconnects.
await side(page).getByRole("button", { name: "Add tab", exact: true }).click()
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toHaveAttribute("aria-selected", "false")
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
await expect.poll(() => terminalText(page, screen)).toBe("before-restart\nafter-restart")
await expect(side(page).getByText("Terminal disconnected", { exact: true })).toHaveCount(0)
expect(persistentPty.sockets.map((socket) => socket.cursor)).toEqual([
Buffer.byteLength(output),
Buffer.byteLength(output + "after-restart\r\n"),
])
})
test("once a restarted server reconnects, the shown terminal restores itself from a new snapshot", async ({ page }) => {
const output = "before-restart\r\n"
const transport = await installSseTransport(page, { server: SERVER, retry: 20 })
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_handoff", sessionID: SESSION, title: "Build", output }] },
})
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
const screen = terminalScreen(page, "pty_handoff")
await expect.poll(() => terminalText(page, screen)).toBe("before-restart")
// The terminal socket closes first; the event stream drops and reconnects to the restarted server after it.
await persistentPty.restart()
await expect(side(page).getByText("Terminal disconnected", { exact: true })).toBeVisible()
persistentPty.output("pty_handoff", "after-restart\r\n")
const connection = await transport.waitForConnection()
await transport.disconnect()
await transport.waitForConnection({ after: connection.id })
await expect.poll(() => terminalText(page, screen)).toBe("before-restart\nafter-restart")
await expect(side(page).getByText("Terminal disconnected", { exact: true })).toHaveCount(0)
await expect(side(page).getByRole("tab", { name: "Build", exact: true })).toHaveAttribute("aria-selected", "true")
expect(persistentPty.sockets.map((socket) => socket.cursor)).toEqual([
Buffer.byteLength(output),
Buffer.byteLength(output + "after-restart\r\n"),
])
})
test("a server without persistent terminals offers no terminal commands", async ({ page }) => {
const { editor } = await openSession(page, { name: "Terminals", persistentPty: { capability: false } })
await openSide(page)
const launchers = side(page).getByRole("tabpanel", { name: "New tab", exact: true })
await expect(launchers.getByRole("button", { name: /^Open file/ })).toBeVisible()
await expect(launchers.getByRole("button", { name: /^Terminal/ })).toHaveCount(0)
await editor.click()
await page.keyboard.type("/")
await expect(page.getByRole("button", { name: /^\/open/ })).toBeVisible()
await expect(page.getByRole("button", { name: /^\/terminal/ })).toHaveCount(0)
})
test("selected terminal output quotes into the composer", async ({ page }) => {
await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_quote", sessionID: SESSION, title: "Build", output: "quote-me\r\n" }] },
})
await openSide(page)
await side(page).getByRole("tab", { name: "Build", exact: true }).click()
const screen = terminalScreen(page, "pty_quote")
await expect.poll(() => terminalText(page, screen)).toBe("quote-me")
const toolbar = page.getByRole("toolbar", { name: "Quote selection", exact: true })
await expect(toolbar).toBeVisible()
// A right click opens a context menu without a pointerup; it neither closes the toolbar nor blocks later reads.
await page.locator("body").dispatchEvent("pointerdown", { button: 2 })
await page.locator("body").dispatchEvent("contextmenu")
await expect(toolbar).toBeVisible()
// A cancelled touch ends without a pointerup too; the selection it left in place gets its toolbar back.
await page.locator("body").dispatchEvent("pointerdown", { button: 0 })
await expect(toolbar).toHaveCount(0)
await page.locator("body").dispatchEvent("pointercancel")
await expect(toolbar).toBeVisible()
await toolbar.getByRole("button", { name: "Quote" }).click()
await expect(page.locator('[data-component="composer"]').getByText("quote-me")).toBeVisible()
})
test("renaming a terminal tab renames it on the server", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_named", sessionID: SESSION, title: "Terminal 1" }] },
})
await openSide(page)
const strip = side(page).getByRole("tablist")
await showTerminal(page, "Terminal 1", "pty_named")
await strip.getByText("Terminal 1", { exact: true }).dblclick()
const field = strip.getByRole("textbox", { name: "Rename", exact: true })
await expect(field).toBeFocused()
await field.fill("Dev server")
await field.press("Enter")
await expect(strip.getByRole("tab", { name: "Dev server", exact: true })).toBeVisible()
expect(persistentPty.updates).toEqual([{ id: "pty_named", body: { title: "Dev server" } }])
await expect.poll(() => persistentPty.terminals[0]!.title).toBe("Dev server")
})
test("a rename from another client relabels the tab", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_named", sessionID: SESSION, title: "Terminal 1" }] },
})
await openSide(page)
await expect(side(page).getByRole("tab", { name: "Terminal 1", exact: true })).toBeVisible()
persistentPty.rename("pty_named", "Logs")
await expect(side(page).getByRole("tab", { name: "Logs", exact: true })).toBeVisible()
await expect(side(page).getByRole("tab", { name: "Terminal 1", exact: true })).toHaveCount(0)
})
test("clearing a terminal's name resets a renamed one and leaves a never-renamed one alone", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: {
initial: [
{ id: "pty_plain", sessionID: SESSION, title: "Terminal 1" },
{ id: "pty_renamed", sessionID: SESSION, title: "Terminal 2", userTitle: "Deploy" },
],
},
})
await openSide(page)
const strip = side(page).getByRole("tablist")
const field = strip.getByRole("textbox", { name: "Rename", exact: true })
await showTerminal(page, "Terminal 1", "pty_plain")
await strip.getByText("Terminal 1", { exact: true }).dblclick()
await expect(field).toBeFocused()
await field.fill("")
await field.press("Enter")
await expect(field).toHaveCount(0)
await expect(strip.getByRole("tab", { name: "Terminal 1", exact: true })).toBeVisible()
await showTerminal(page, "Deploy", "pty_renamed")
await strip.getByText("Deploy", { exact: true }).dblclick()
await expect(field).toBeFocused()
await field.fill("")
await field.press("Enter")
await expect(strip.getByRole("tab", { name: "Terminal 2", exact: true })).toBeVisible()
await expect(strip.getByRole("tab", { name: "Deploy", exact: true })).toHaveCount(0)
// The first clear sent nothing: the only update is the second.
expect(persistentPty.updates).toEqual([{ id: "pty_renamed", body: { title: null } }])
})
test("a server whose terminal service cannot rename yet keeps the old name and says when it will", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: {
renameSupported: false,
initial: [{ id: "pty_named", sessionID: SESSION, title: "Terminal 1" }],
},
})
await openSide(page)
const strip = side(page).getByRole("tablist")
await showTerminal(page, "Terminal 1", "pty_named")
await strip.getByText("Terminal 1", { exact: true }).dblclick()
const field = strip.getByRole("textbox", { name: "Rename", exact: true })
await expect(field).toBeFocused()
await field.fill("Dev server")
await field.press("Enter")
await expect(
page.getByText("Terminal renaming will be available after OpenCode's terminal service restarts", { exact: true }),
).toBeVisible()
await expect(strip.getByRole("tab", { name: "Terminal 1", exact: true })).toBeVisible()
await expect(strip.getByRole("tab", { name: "Dev server", exact: true })).toHaveCount(0)
expect(persistentPty.terminals[0]!.title).toBe("Terminal 1")
})
test.describe("narrow screens", () => {
test.use({ viewport: { width: 390, height: 844 } })
test("the Terminal view opens from More... and shows the session's terminal", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_mobile", sessionID: SESSION, title: "Build", output: "on-mobile\r\n" }] },
})
const tabs = page.getByRole("tablist", { name: "Session view", exact: true })
const views = page.getByRole("dialog", { name: "More options", exact: true })
await tabs.getByRole("tab", { name: "More...", exact: true }).click()
await views.getByRole("button", { name: "Terminal", exact: true }).click()
await expect(views).toBeHidden()
const panel = page.locator("#terminal-panel")
await expect(panel.getByRole("tab", { name: "Build", exact: true })).toHaveAttribute("aria-selected", "true")
await expect.poll(() => terminalText(page, terminalScreen(page, "pty_mobile"))).toBe("on-mobile")
expect(persistentPty.created).toEqual([])
})
test("an empty session's Terminal view starts its first terminal", async ({ page }) => {
const { persistentPty } = await openSession(page, { name: "Terminals", persistentPty: {} })
await page.getByRole("tablist", { name: "Session view", exact: true }).getByRole("tab", { name: "More..." }).click()
await page
.getByRole("dialog", { name: "More options", exact: true })
.getByRole("button", { name: "Terminal" })
.click()
const panel = page.locator("#terminal-panel")
await expect(panel.getByRole("tab", { name: "Terminal 1", exact: true })).toHaveAttribute("aria-selected", "true")
expect(persistentPty.created.map((info) => info.title)).toEqual(["Terminal 1"])
await expect(terminalScreen(page, persistentPty.created[0]!.id).locator("textarea")).toBeEditable()
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual([persistentPty.created[0]!.id])
})
test("double-clicking a terminal tab that is not selected yet starts renaming it", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: {
initial: [
{ id: "pty_first", sessionID: SESSION, title: "Build" },
{ id: "pty_second", sessionID: SESSION, title: "Deploy" },
],
},
})
await page.getByRole("tablist", { name: "Session view", exact: true }).getByRole("tab", { name: "More..." }).click()
await page
.getByRole("dialog", { name: "More options", exact: true })
.getByRole("button", { name: "Terminal" })
.click()
const panel = page.locator("#terminal-panel")
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual(["pty_first"])
const field = panel.getByRole("textbox", { name: "Rename", exact: true })
await panel.getByText("Deploy", { exact: true }).dblclick()
await expect(field).toBeFocused()
// The terminal connects after it mounts and asks for focus.
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual(["pty_first", "pty_second"])
await expect(field).toBeFocused()
})
test("Escape cancels a terminal rename and Enter renames once", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_mobile", sessionID: SESSION, title: "Build" }] },
})
await page.getByRole("tablist", { name: "Session view", exact: true }).getByRole("tab", { name: "More..." }).click()
await page
.getByRole("dialog", { name: "More options", exact: true })
.getByRole("button", { name: "Terminal" })
.click()
const panel = page.locator("#terminal-panel")
const field = panel.getByRole("textbox", { name: "Rename", exact: true })
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual(["pty_mobile"])
await panel.getByText("Build", { exact: true }).dblclick()
await expect(field).toBeFocused()
await field.fill("Cancelled")
await field.press("Escape")
await expect(field).toHaveCount(0)
await expect(panel.getByRole("tab", { name: "Build", exact: true })).toBeVisible()
await panel.getByText("Build", { exact: true }).dblclick()
await expect(field).toBeFocused()
await field.fill("Deploy")
await field.press("Enter")
await expect(panel.getByRole("tab", { name: "Deploy", exact: true })).toBeVisible()
expect(persistentPty.updates).toEqual([{ id: "pty_mobile", body: { title: "Deploy" } }])
})
})
// Selecting the tab mounts its terminal, which asks for focus; the rename field keeps it.
test("double-clicking a terminal tab that is not selected yet starts renaming it", async ({ page }) => {
const { persistentPty } = await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_named", sessionID: SESSION, title: "Terminal 1" }] },
})
// The terminal mounts once ghostty-web loads, which here waits until the rename field has focus.
const ghostty = await holdRoute(page, /\/_assets\/ghostty-web-[^/]+\.js$/)
await openSide(page)
const strip = side(page).getByRole("tablist")
const field = strip.getByRole("textbox", { name: "Rename", exact: true })
await strip.getByText("Terminal 1", { exact: true }).dblclick()
await expect(field).toBeFocused()
ghostty.release()
// The terminal connects after it mounts and asks for focus.
await expect.poll(() => persistentPty.sockets.map((socket) => socket.id)).toEqual(["pty_named"])
await expect(field).toBeFocused()
})
// The menu returns focus to the tab as it closes; choosing Rename moves it to the rename field.
test("a terminal tab's context menu starts renaming it", async ({ page }) => {
await openSession(page, {
name: "Terminals",
persistentPty: { initial: [{ id: "pty_named", sessionID: SESSION, title: "Terminal 1" }] },
})
await openSide(page)
const strip = side(page).getByRole("tablist")
await showTerminal(page, "Terminal 1", "pty_named")
await strip.getByText("Terminal 1", { exact: true }).click({ button: "right" })
await page.getByRole("menuitem", { name: "Rename", exact: true }).click()
await expect(strip.getByRole("textbox", { name: "Rename", exact: true })).toBeFocused()
})
function side(page: Page) {
return page.getByRole("complementary", { name: "Review and files", exact: true })
}
async function openSide(page: Page) {
await page.getByRole("button", { name: "Toggle review", exact: true }).click()
await expect(side(page)).toBeVisible()
}
async function showTerminal(page: Page, title: string, ptyID: string) {
await side(page).getByRole("tab", { name: title, exact: true }).click()
await expect(terminalScreen(page, ptyID).locator("textarea")).toBeFocused()
}
function paneSize(frame: { cols: number; rows: number }) {
return { cols: frame.cols, rows: frame.rows }
}