|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { ScrollBoxRenderable, TextareaRenderable, type Renderable } from "@opentui/core" |
| 3 | +import type { SessionInfo } from "@opencode/client" |
| 4 | +import { createAppFixture } from "./fixture/app" |
| 5 | +import { directory, json } from "./fixture/tui-client" |
| 6 | +import { tmpdir } from "./fixture/fixture" |
| 7 | + |
| 8 | +const LAST = 39 |
| 9 | + |
| 10 | +const KP_HOME = "\u001b[57423u" |
| 11 | +const KP_END = "\u001b[57424u" |
| 12 | + |
| 13 | +test("keypad Home and End move the prompt cursor", async () => { |
| 14 | + await using state = await tmpdir() |
| 15 | + await using setup = await fixture(state.path) |
| 16 | + const prompt = await ready(setup) |
| 17 | + const history = transcript(setup) |
| 18 | + |
| 19 | + await setup.mockInput.typeText("aa") |
| 20 | + await setup.mockInput.pressKey("RETURN", { shift: true }) |
| 21 | + await setup.mockInput.typeText("bb") |
| 22 | + await setup.flush() |
| 23 | + expect(prompt.plainText).toBe("aa\nbb") |
| 24 | + // The taller prompt shortens the transcript viewport, so measure the scroll |
| 25 | + // position once the multiline text is in place. |
| 26 | + const top = history.scrollTop |
| 27 | + |
| 28 | + await setup.mockInput.pressKeys([KP_HOME]) |
| 29 | + await setup.mockInput.typeText("x") |
| 30 | + await setup.mockInput.pressKeys([KP_END]) |
| 31 | + await setup.mockInput.typeText("y") |
| 32 | + await setup.flush() |
| 33 | + |
| 34 | + expect(prompt.plainText).toBe("xaa\nbby") |
| 35 | + expect(history.scrollTop).toBe(top) |
| 36 | +}) |
| 37 | + |
| 38 | +async function fixture(state: string) { |
| 39 | + const session = { |
| 40 | + id: `ses_home_end_${crypto.randomUUID()}`, |
| 41 | + title: "Home and End fixture", |
| 42 | + projectID: "proj_home_end", |
| 43 | + location: { directory }, |
| 44 | + agent: "build", |
| 45 | + model: { providerID: "fixture", id: "model" }, |
| 46 | + cost: 0, |
| 47 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 48 | + time: { created: 1, updated: LAST }, |
| 49 | + } satisfies SessionInfo |
| 50 | + const location = { directory, project: { id: session.projectID, directory, canonical: directory } } |
| 51 | + const setup = await createAppFixture({ |
| 52 | + width: 100, |
| 53 | + height: 30, |
| 54 | + state, |
| 55 | + args: { sessionID: session.id }, |
| 56 | + config: { animations: false, tabs: { enabled: false }, session: { sidebar: "hide" } }, |
| 57 | + fetch: async (url) => { |
| 58 | + if (url.pathname === "/api/location") return json(location) |
| 59 | + if (url.pathname === "/api/fs/list") return json({ location, data: [] }) |
| 60 | + if (url.pathname === "/api/agent") |
| 61 | + return json({ location, data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }] }) |
| 62 | + if (url.pathname === "/api/model") |
| 63 | + return json({ location, data: [{ id: "model", providerID: "fixture", name: "Model", variants: [] }] }) |
| 64 | + if (url.pathname === "/api/provider") return json({ location, data: [{ id: "fixture", name: "Fixture" }] }) |
| 65 | + if (url.pathname === "/api/vcs") |
| 66 | + return json({ location, data: { branch: { current: "fixture", default: "dev" } } }) |
| 67 | + if (url.pathname === `/api/session/${session.id}`) return json({ data: session }) |
| 68 | + if (url.pathname === `/api/session/${session.id}/message`) |
| 69 | + return json({ |
| 70 | + data: Array.from({ length: LAST + 1 }, (_, index) => ({ |
| 71 | + id: messageID(index), |
| 72 | + type: "user", |
| 73 | + text: `Transcript item ${String(index).padStart(2, "0")}`, |
| 74 | + time: { created: index + 1 }, |
| 75 | + })).toReversed(), |
| 76 | + cursor: {}, |
| 77 | + }) |
| 78 | + if (url.pathname === `/api/session/${session.id}/inbox`) return json({ data: [] }) |
| 79 | + if (url.pathname === `/api/session/${session.id}/permission`) return json({ data: [] }) |
| 80 | + if (url.pathname === `/api/session/${session.id}/form`) return json({ data: [] }) |
| 81 | + if (url.pathname === `/api/session/${session.id}/prompt`) return json({ data: {} }) |
| 82 | + return undefined |
| 83 | + }, |
| 84 | + }) |
| 85 | + return setup |
| 86 | +} |
| 87 | + |
| 88 | +async function ready(setup: Awaited<ReturnType<typeof fixture>>) { |
| 89 | + await setup.ready |
| 90 | + await setup.waitForFrame((frame) => frame.includes(`Transcript item ${LAST}`) && frame.includes("Build · Model"), { |
| 91 | + maxPasses: 120, |
| 92 | + }) |
| 93 | + await setup.waitFor(() => setup.renderer.currentFocusedEditor instanceof TextareaRenderable) |
| 94 | + return setup.renderer.currentFocusedEditor as TextareaRenderable |
| 95 | +} |
| 96 | + |
| 97 | +function messageID(index: number) { |
| 98 | + return `msg_home_${String(index).padStart(2, "0")}` |
| 99 | +} |
| 100 | + |
| 101 | +function transcript(setup: Awaited<ReturnType<typeof fixture>>) { |
| 102 | + let item: Renderable | null | undefined = setup.renderer.root.findDescendantById(messageID(LAST)) |
| 103 | + while (item && !(item instanceof ScrollBoxRenderable)) item = item.parent |
| 104 | + if (!(item instanceof ScrollBoxRenderable)) |
| 105 | + throw new Error(`Missing transcript scrollbox\n${setup.captureCharFrame()}`) |
| 106 | + return item |
| 107 | +} |
0 commit comments