|
| 1 | +import { base64Encode } from "@opencode-ai/core/util/encode" |
| 2 | +import { expect, test } from "@playwright/test" |
| 3 | +import { mockOpenCodeServer } from "../utils/mock-server" |
| 4 | +import { expectSessionTitle } from "../utils/waits" |
| 5 | + |
| 6 | +const directory = "C:/OpenCode/TerminalComposerFocus" |
| 7 | +const projectID = "proj_terminal_composer_focus" |
| 8 | +const sessionID = "ses_terminal_composer_focus" |
| 9 | +const ptyID = "pty_terminal_composer_focus" |
| 10 | + |
| 11 | +test.use({ viewport: { width: 1440, height: 900 } }) |
| 12 | + |
| 13 | +test("routes typing to the composer unless the open terminal is focused", async ({ page }) => { |
| 14 | + await mockOpenCodeServer(page, { |
| 15 | + directory, |
| 16 | + project: { |
| 17 | + id: projectID, |
| 18 | + worktree: directory, |
| 19 | + vcs: "git", |
| 20 | + name: "terminal-composer-focus", |
| 21 | + time: { created: 1700000000000, updated: 1700000000000 }, |
| 22 | + sandboxes: [], |
| 23 | + }, |
| 24 | + provider: { |
| 25 | + all: [ |
| 26 | + { |
| 27 | + id: "opencode", |
| 28 | + name: "OpenCode", |
| 29 | + models: { test: { id: "test", name: "Test", limit: { context: 200_000 } } }, |
| 30 | + }, |
| 31 | + ], |
| 32 | + connected: ["opencode"], |
| 33 | + default: { providerID: "opencode", modelID: "test" }, |
| 34 | + }, |
| 35 | + sessions: [ |
| 36 | + { |
| 37 | + id: sessionID, |
| 38 | + slug: "terminal-composer-focus", |
| 39 | + projectID, |
| 40 | + directory, |
| 41 | + title: "Terminal composer focus", |
| 42 | + version: "dev", |
| 43 | + time: { created: 1700000000000, updated: 1700000000000 }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + pageMessages: () => ({ items: [] }), |
| 47 | + }) |
| 48 | + await page.route("**/pty", (route) => |
| 49 | + route.fulfill({ |
| 50 | + status: 200, |
| 51 | + contentType: "application/json", |
| 52 | + body: JSON.stringify({ id: ptyID, title: "Terminal 1" }), |
| 53 | + }), |
| 54 | + ) |
| 55 | + await page.route(`**/pty/${ptyID}`, (route) => |
| 56 | + route.fulfill({ status: 200, contentType: "application/json", body: "{}" }), |
| 57 | + ) |
| 58 | + await page.route(`**/pty/${ptyID}/connect-token*`, (route) => |
| 59 | + route.fulfill({ |
| 60 | + status: 200, |
| 61 | + contentType: "application/json", |
| 62 | + headers: { "access-control-allow-origin": "*" }, |
| 63 | + body: JSON.stringify({ ticket: "e2e-ticket" }), |
| 64 | + }), |
| 65 | + ) |
| 66 | + await page.routeWebSocket(new RegExp(`/pty/${ptyID}/connect`), () => undefined) |
| 67 | + await page.addInitScript(() => { |
| 68 | + localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } })) |
| 69 | + }) |
| 70 | + |
| 71 | + await page.goto(`/${base64Encode(directory)}/session/${sessionID}`) |
| 72 | + await expectSessionTitle(page, "Terminal composer focus") |
| 73 | + |
| 74 | + const composer = page.locator('[data-component="prompt-input"]') |
| 75 | + const terminal = page.locator('[data-component="terminal"]') |
| 76 | + await page.keyboard.press("Control+Backquote") |
| 77 | + await expect(terminal).toBeVisible() |
| 78 | + await expect.poll(() => terminal.evaluate((element) => element.contains(document.activeElement))).toBe(true) |
| 79 | + |
| 80 | + await page.keyboard.type("x") |
| 81 | + await expect(composer).toHaveText("") |
| 82 | + |
| 83 | + await page.waitForTimeout(300) |
| 84 | + await page.evaluate(() => (document.activeElement as HTMLElement | null)?.blur()) |
| 85 | + await page.keyboard.type("a") |
| 86 | + |
| 87 | + await expect(composer).toBeFocused() |
| 88 | + await expect(composer).toHaveText("a") |
| 89 | +}) |
0 commit comments