mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-23 18:55:37 -04:00
9e0d3976e1
Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: James Long <longster@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com> Co-authored-by: Adam <2363879+adamdotdevin@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: Simon Klee <hello@simonklee.dk> Co-authored-by: Jay <air@live.ca> Co-authored-by: David Hill <1879069+iamdavidhill@users.noreply.github.com>
146 lines
4.7 KiB
TypeScript
146 lines
4.7 KiB
TypeScript
import { base64Encode } from "@opencode-ai/core/util/encode"
|
|
import { expect, test, type Page } from "@playwright/test"
|
|
import { mockOpenCodeServer } from "../utils/mock-server"
|
|
import { expectSessionTitle } from "../utils/waits"
|
|
|
|
const directory = "C:/OpenCode/TerminalTabSwitch"
|
|
const projectID = "proj_terminal_tab_switch"
|
|
const sessionA = "ses_terminal_tab_a"
|
|
const sessionB = "ses_terminal_tab_b"
|
|
const titleA = "Alpha session"
|
|
const titleB = "Beta session"
|
|
const ptyID = "pty_tab_switch"
|
|
const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
|
|
// Marks the terminal DOM node so a remount (fresh node) is detectable.
|
|
const PROBE = "original"
|
|
|
|
test.use({ viewport: { width: 1440, height: 900 } })
|
|
|
|
// Terminals are workspace-scoped: switching between session tabs in the same
|
|
// workspace must keep the terminal mounted and its PTY connection open instead
|
|
// of tearing it down and reconnecting.
|
|
test("keeps the terminal session alive when switching session tabs in a workspace", async ({ page }) => {
|
|
const connections = await setup(page)
|
|
|
|
await page.goto(sessionHref(sessionA))
|
|
await expectSessionTitle(page, titleA)
|
|
|
|
await page.keyboard.press("Control+Backquote")
|
|
const terminal = page.locator('[data-component="terminal"]')
|
|
await expect(terminal).toBeVisible()
|
|
await expect.poll(() => connections.length).toBe(1)
|
|
await writeProbe(page)
|
|
|
|
await switchTab(page, titleB)
|
|
await expectSessionTitle(page, titleB)
|
|
await expect(terminal).toBeVisible()
|
|
expect(await readProbe(page)).toBe(PROBE)
|
|
expect(connections.length).toBe(1)
|
|
|
|
await switchTab(page, titleA)
|
|
await expectSessionTitle(page, titleA)
|
|
await expect(terminal).toBeVisible()
|
|
expect(await readProbe(page)).toBe(PROBE)
|
|
expect(connections.length).toBe(1)
|
|
})
|
|
|
|
type Probed = HTMLElement & { __e2eProbe?: string }
|
|
|
|
async function switchTab(page: Page, title: string) {
|
|
await page.locator("[data-titlebar-tab-slot]", { hasText: title }).click()
|
|
}
|
|
|
|
async function writeProbe(page: Page) {
|
|
await page.locator('[data-component="terminal"]').evaluate((el, probe) => {
|
|
;(el as Probed).__e2eProbe = probe
|
|
}, PROBE)
|
|
}
|
|
|
|
async function readProbe(page: Page) {
|
|
return page.locator('[data-component="terminal"]').evaluate((el) => (el as Probed).__e2eProbe)
|
|
}
|
|
|
|
async function setup(page: Page) {
|
|
await mockOpenCodeServer(page, {
|
|
directory,
|
|
project: {
|
|
id: projectID,
|
|
worktree: directory,
|
|
vcs: "git",
|
|
name: "terminal-tab-switch",
|
|
time: { created: 1700000000000, updated: 1700000000000 },
|
|
sandboxes: [],
|
|
},
|
|
provider: {
|
|
all: [
|
|
{
|
|
id: "opencode",
|
|
name: "OpenCode",
|
|
models: { test: { id: "test", name: "Test", limit: { context: 200_000 } } },
|
|
},
|
|
],
|
|
connected: ["opencode"],
|
|
default: { providerID: "opencode", modelID: "test" },
|
|
},
|
|
sessions: [session(sessionA, titleA, 1700000000000), session(sessionB, titleB, 1700000001000)],
|
|
pageMessages: () => ({ items: [] }),
|
|
})
|
|
await page.route("**/pty", (route) =>
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: JSON.stringify({ id: ptyID, title: "Terminal 1" }),
|
|
}),
|
|
)
|
|
await page.route(`**/pty/${ptyID}`, (route) =>
|
|
route.fulfill({ status: 200, contentType: "application/json", body: "{}" }),
|
|
)
|
|
await page.route(`**/pty/${ptyID}/connect-token*`, (route) =>
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
headers: { "access-control-allow-origin": "*" },
|
|
body: JSON.stringify({ ticket: "e2e-ticket" }),
|
|
}),
|
|
)
|
|
const connections: string[] = []
|
|
await page.routeWebSocket(new RegExp(`/pty/${ptyID}/connect`), (ws) => {
|
|
connections.push(ws.url())
|
|
})
|
|
|
|
await page.addInitScript(
|
|
({ directory, server, sessions }) => {
|
|
localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } }))
|
|
localStorage.setItem(
|
|
"opencode.global.dat:server",
|
|
JSON.stringify({
|
|
projects: { local: [{ worktree: directory, expanded: true }] },
|
|
lastProject: { local: directory },
|
|
}),
|
|
)
|
|
localStorage.setItem(
|
|
"opencode.window.browser.dat:tabs",
|
|
JSON.stringify(sessions.map((sessionId: string) => ({ type: "session", server, sessionId }))),
|
|
)
|
|
},
|
|
{ directory, server, sessions: [sessionA, sessionB] },
|
|
)
|
|
return connections
|
|
}
|
|
|
|
function session(id: string, title: string, created: number) {
|
|
return {
|
|
id,
|
|
slug: id,
|
|
projectID,
|
|
directory,
|
|
title,
|
|
version: "dev",
|
|
time: { created, updated: created },
|
|
}
|
|
}
|
|
|
|
function sessionHref(sessionID: string) {
|
|
return `/server/${base64Encode(server)}/session/${sessionID}`
|
|
}
|