|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { createTestRenderer, TestRecorder } from "@opentui/core/testing" |
| 3 | +import { Effect, FileSystem } from "effect" |
| 4 | +import { Global } from "@opencode/util/global" |
| 5 | +import type { SessionMessageInfo } from "@opencode/client" |
| 6 | +import { createEventStream, createFetch, directory, json } from "./fixture/tui-client" |
| 7 | +import { tmpdir } from "./fixture/fixture" |
| 8 | +import { waitForHighlightedFrame } from "./fixture/frame" |
| 9 | + |
| 10 | +// The whole-app fixture needs the package's 30-second budget for its cold app import. |
| 11 | +test.each([100, 48])( |
| 12 | + "scrolling up into older history never flashes the inserted rows at width %s", |
| 13 | + async (width) => { |
| 14 | + await using state = await tmpdir() |
| 15 | + const setup = await createTestRenderer({ width, height: 30, kittyKeyboard: true }) |
| 16 | + await setup.renderer.setupTerminal() |
| 17 | + setup.renderer.start() |
| 18 | + const session = { |
| 19 | + id: "ses_history_reveal", |
| 20 | + title: "History reveal", |
| 21 | + projectID: "proj_test", |
| 22 | + location: { directory }, |
| 23 | + cost: 0, |
| 24 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 25 | + time: { created: 0, updated: 0 }, |
| 26 | + } |
| 27 | + const model = { providerID: "fixture", id: "fixture" } |
| 28 | + const messages: SessionMessageInfo[] = Array.from({ length: 60 }, (_, turn) => [ |
| 29 | + { type: "user" as const, id: `user-${turn}`, text: `Turn ${turn} prompt`, time: { created: turn * 10 } }, |
| 30 | + { |
| 31 | + type: "assistant" as const, |
| 32 | + id: `answer-${turn}`, |
| 33 | + agent: "build", |
| 34 | + model, |
| 35 | + finish: "stop" as const, |
| 36 | + time: { created: turn * 10 + 1, completed: turn * 10 + 2 }, |
| 37 | + content: [{ type: "text" as const, text: `Turn ${turn} answer` }], |
| 38 | + }, |
| 39 | + ]).flat() |
| 40 | + // The first page (turns 40-59) overfills the mounted tail, so scrolling up first mounts its hidden |
| 41 | + // rows, then loads turns 30-39 from the server. Both insert rows above the reader. |
| 42 | + const calls = createFetch((url) => { |
| 43 | + if (url.pathname === "/api/session") return json({ data: [session], cursor: {} }) |
| 44 | + if (url.pathname === `/api/session/${session.id}`) return json({ data: session }) |
| 45 | + if (url.pathname === `/api/session/${session.id}/message`) { |
| 46 | + const cursor = url.searchParams.get("cursor") |
| 47 | + const end = Number(cursor ?? messages.length) |
| 48 | + const start = Math.max(0, end - (cursor ? Number(url.searchParams.get("limit")) : 40)) |
| 49 | + return json({ data: messages.slice(start, end).toReversed(), cursor: start ? { next: String(start) } : {} }) |
| 50 | + } |
| 51 | + if ( |
| 52 | + url.pathname === `/api/session/${session.id}/inbox` || |
| 53 | + url.pathname === `/api/session/${session.id}/permission` |
| 54 | + ) |
| 55 | + return json({ data: [] }) |
| 56 | + }, createEventStream()) |
| 57 | + const server = Bun.serve({ port: 0, idleTimeout: 0, fetch: (request) => calls.fetch(request) }) |
| 58 | + const { run } = await import("../src/app") |
| 59 | + const task = Effect.runPromise( |
| 60 | + run({ |
| 61 | + app: { name: "test", version: "test", channel: "test" }, |
| 62 | + server: { endpoint: { url: server.url.toString() } }, |
| 63 | + config: { get: async () => ({ animations: false, tabs: { enabled: false } }), update: async () => ({}) }, |
| 64 | + packages: { prepare: async () => ({ directory: "" }) }, |
| 65 | + args: { sessionID: session.id }, |
| 66 | + terminalHandoff: async () => ({ renderer: setup.renderer, mode: "dark", complete: () => {} }), |
| 67 | + log: () => {}, |
| 68 | + }).pipe(Effect.provide(Global.layerWith({ state: state.path })), Effect.provide(FileSystem.layerNoop({}))), |
| 69 | + ) |
| 70 | + try { |
| 71 | + await waitForHighlightedFrame(setup, (frame) => frame.includes("Turn 59 answer")) |
| 72 | + await setup.waitForVisualIdle({ quietFrames: 3 }) |
| 73 | + const recorder = new TestRecorder(setup.renderer) |
| 74 | + recorder.rec() |
| 75 | + for (let step = 0; step < 200 && !setup.captureCharFrame().includes("Turn 39 answer"); step++) { |
| 76 | + await setup.mockMouse.scroll(30, 10, "up") |
| 77 | + await setup.renderOnce() |
| 78 | + } |
| 79 | + await setup.waitForVisualIdle({ quietFrames: 3 }) |
| 80 | + recorder.stop() |
| 81 | + |
| 82 | + // A wheel tick moves less than one turn, so the earliest turn on screen steps down by at most one |
| 83 | + // per frame. A frame drawn before the inserted rows are scrolled past shows their first turn, and |
| 84 | + // the next frame jumps back to the reader. |
| 85 | + const turns = recorder.recordedFrames.map((frame) => |
| 86 | + Math.min(...[...frame.frame.matchAll(/Turn (\d+) (?:prompt|answer)/g)].map((match) => Number(match[1]))), |
| 87 | + ) |
| 88 | + expect(turns.at(-1)).toBeLessThanOrEqual(39) |
| 89 | + expect( |
| 90 | + turns.filter((turn, index) => index > 0 && (turn > turns[index - 1] || turn < turns[index - 1] - 1)), |
| 91 | + ).toEqual([]) |
| 92 | + } finally { |
| 93 | + setup.renderer.destroy() |
| 94 | + await task |
| 95 | + await server.stop() |
| 96 | + } |
| 97 | + }, |
| 98 | + 30000, |
| 99 | +) |
0 commit comments