/** @jsxImportSource @opentui/solid */ import { testRender } from "@opentui/solid" import { MouseButton } from "@opentui/core" import { ManualClock } from "@opentui/core/testing" import { expect, test } from "bun:test" import { batch, createSignal } from "solid-js" import { ConfigProvider, useConfig, type Info } from "../../src/config" import { EMPTY_SESSION_TAB_STATUS, SessionTabs, type SessionTabsController, type SessionTabsStatus, } from "../../src/component/session-tabs" import { SPINNER_FRAMES } from "../../src/component/spinner-frames" import { SESSION_SIDEBAR_WIDTH } from "../../src/ui/layout" import { ClientProvider } from "../../src/context/client" import { DataProvider } from "../../src/context/data" import { LocationProvider } from "../../src/context/location" import { Keymap } from "../../src/context/keymap" import { RouteProvider } from "../../src/context/route" import { TuiAppProvider } from "../../src/context/runtime" import { SessionTabsProvider } from "../../src/context/session-tabs" import { StorageProvider } from "../../src/context/storage" import { ThemeProvider, useTheme } from "../../src/context/theme" import { DialogProvider } from "../../src/ui/dialog" import { ToastProvider } from "../../src/ui/toast" import { emptyThemeSource, tmpdir } from "../fixture/fixture" import { createApi, createEventStream, createFetch } from "../fixture/tui-client" import { TestTuiContexts } from "../fixture/tui-environment" import { createTuiResolvedConfig } from "../fixture/tui-runtime" for (const orientation of ["horizontal", "vertical"] as const) { test(`${orientation} tabs replace ordinals with status without moving titles and keep context menu actions`, async () => { await using temporary = await tmpdir() const [status, setStatus] = createSignal(EMPTY_SESSION_TAB_STATUS) const [active, setActive] = createSignal("second") const [newTab, setNewTab] = createSignal(false) const settings: Info = { tabs: { mode: "on" } } const copied: string[] = [] const reopened: string[] = [] const [closed, setClosed] = createSignal([ { sessionID: "closed-new", title: "Most recently closed" }, { sessionID: "closed-old", title: "Earlier session" }, ]) let config!: ReturnType let theme!: ReturnType function Colors() { config = useConfig() theme = orientation === "vertical" ? useTheme() : useTheme() return null } const controller = { tabs: () => [ { sessionID: "first", title: "First" }, { sessionID: "second", title: "Second" }, ], current: active, newTab, select(sessionID: string) { batch(() => { setActive(sessionID) if (sessionID === "first") setStatus((current) => ({ ...current, unread: undefined })) }) }, close() {}, move() {}, add() {}, recentlyClosed: closed, reopen(sessionID?: string) { if (sessionID) reopened.push(sessionID) setClosed((tabs) => tabs.filter((tab) => tab.sessionID !== sessionID)) }, detail: () => "project", status: (sessionID: string) => (sessionID === "first" ? status() : EMPTY_SESSION_TAB_STATUS), } satisfies SessionTabsController const app = await testRender( () => ( undefined, write: async (text) => void copied.push(text) }} > settings, update: async (update) => { update(settings) return settings }, }} > ), { width: 60, height: 10 }, ) try { app.renderer.start() await app.waitForFrame((frame) => frame.includes(" First") && frame.includes(" Second")) const titleColumn = app .captureCharFrame() .split("\n") .find((line) => line.includes("First"))! .indexOf("First") const states: { status: Partial; label: string }[] = [ { status: { busy: true }, label: SPINNER_FRAMES[0] }, { status: { busy: true, attention: "question" }, label: "?" }, { status: { busy: true, attention: "permission" }, label: "!" }, { status: { unread: "activity" }, label: "\u2022" }, { status: { unread: "error" }, label: "\u2022" }, { status: {}, label: "" }, ] for (const state of states) { setStatus({ ...EMPTY_SESSION_TAB_STATUS, ...state.status }) await app.renderOnce() await app.waitForFrame((frame) => frame.includes(`${state.label.padStart(2)} First`)) const rows = app.captureCharFrame().split("\n") expect(rows.find((line) => line.includes("First"))!.indexOf("First")).toBe(titleColumn) expect(rows[orientation === "vertical" ? 2 : 1]?.trim()).toBe(orientation === "vertical" ? "project" : "") } for (const attention of ["question", "permission"] as const) { setActive("second") setStatus({ ...EMPTY_SESSION_TAB_STATUS, busy: true, attention }) await app.renderOnce() const indicatorColor = () => app .captureSpans() .lines.flatMap((line) => line.spans) .find((span) => span.text.trim() === (attention === "question" ? "?" : "!"))?.fg expect(indicatorColor()?.toInts()).toEqual(theme.hue.accent[200].toInts()) const glow = () => { const colors = app .captureSpans() .lines[ orientation === "vertical" ? 1 : 0 ]!.spans.flatMap((span) => Array.from({ length: span.width }, () => span.bg)) return ( Math.abs(colors[1]!.r - colors[18]!.r) + Math.abs(colors[1]!.g - colors[18]!.g) + Math.abs(colors[1]!.b - colors[18]!.b) ) } const full = glow() expect(full).toBeGreaterThan(0) setActive("first") await app.renderOnce() expect(indicatorColor()?.toInts()).toEqual(theme.hue.accent[200].toInts()) const dim = glow() expect(dim).toBeGreaterThan(0) expect(dim).toBeLessThan(full) } const glyph = "\u2022" for (const unread of ["activity", "error"] as const) { setActive("second") setStatus({ ...EMPTY_SESSION_TAB_STATUS, unread }) await app.renderOnce() const color = app .captureSpans() .lines.flatMap((line) => line.spans) .find((span) => span.text.trim() === glyph)?.fg expect(color?.toInts()).toEqual( (unread === "error" ? theme.text.feedback.error.base : theme.hue.accent[200]).toInts(), ) await app.mockMouse.click(1, orientation === "vertical" ? 1 : 0) await app.renderOnce() expect(active()).toBe("first") expect(status().unread).toBeUndefined() expect(app.captureCharFrame()).toContain(" First") expect(app.captureCharFrame()).not.toContain(`${glyph} First`) } setStatus({ ...EMPTY_SESSION_TAB_STATUS, busy: true }) await config.update((draft) => { draft.tabs.indicators = "numbers" }) await app.waitForFrame((frame) => frame.includes("1 First") && frame.includes("2 Second")) await config.update((draft) => { draft.tabs.indicators = "status" }) await app.waitForFrame((frame) => frame.includes(`${SPINNER_FRAMES[0]} First`)) setStatus(EMPTY_SESSION_TAB_STATUS) setActive("second") await app.renderOnce() const rows = app.captureCharFrame().split("\n") const row = rows.findIndex((line) => line.includes("First")) const column = rows[row]!.indexOf("First") await app.mockMouse.click(column, row, MouseButton.RIGHT) await app.waitForFrame((frame) => frame.includes("Rename")) expect(app.captureCharFrame().split("\n")[row + 2]!.indexOf("Rename")).toBe(column + 1) expect(app.captureCharFrame()).toContain("Copy session ID") expect(app.captureCharFrame()).toContain("Close") expect(app.captureCharFrame()).not.toContain("Keep open") expect(active()).toBe("second") await app.mockMouse.click(column + 1, row + 3) expect(copied).toEqual(["first"]) await app.waitForFrame((frame) => !frame.includes("Rename")) setNewTab(true) await app.waitForFrame((frame) => frame.includes("+ New session")) for (const width of [60, 35]) { app.renderer.resize(width, 10) await app.renderOnce() const rows = app.captureCharFrame().split("\n") const row = rows.findIndex((line) => line.includes("+ New session")) await app.mockMouse.click(rows[row]!.indexOf("+ New session") + 2, row, MouseButton.RIGHT) await app.waitForFrame((frame) => frame.includes("Recently closed tabs")) expect(app.captureCharFrame()).not.toContain("New tab") const menu = app.captureCharFrame().split("\n") expect(menu.some((line) => line.trim() === "New session")).toBe(false) const earlier = menu.findIndex((line) => line.includes("Earlier session")) expect(earlier).toBeGreaterThan(menu.findIndex((line) => line.includes("Most recently closed"))) await app.mockMouse.click(menu[earlier]!.indexOf("Earlier session"), earlier) expect(reopened.at(-1)).toBe("closed-old") await app.waitForFrame((frame) => !frame.includes("Recently closed tabs")) setClosed([ { sessionID: "closed-new", title: "Most recently closed" }, { sessionID: "closed-old", title: "Earlier session" }, ]) } app.renderer.resize(60, 20) setClosed( Array.from({ length: 11 }, (_, index) => ({ sessionID: `closed-${index}`, title: `History ${index + 1}` })), ) await app.renderOnce() const historyRows = app.captureCharFrame().split("\n") const newRow = historyRows.findIndex((line) => line.includes("+ New session")) await app.mockMouse.click(historyRows[newRow]!.indexOf("+ New session") + 2, newRow, MouseButton.RIGHT) await app.waitForFrame((frame) => frame.includes("History 10")) expect(app.captureCharFrame()).not.toContain("History 11") } finally { app.renderer.destroy() } }) } test.each([SESSION_SIDEBAR_WIDTH, 18])( "vertical running tab pulse spans the full %i-column tab width", async (railWidth) => { const clock = new ManualClock() const [busy, setBusy] = createSignal(false) const controller = { tabs: () => [ { sessionID: "first", title: "First session" }, { sessionID: "second", title: "Second session" }, ], current: () => "second", select() {}, close() {}, move() {}, add() {}, detail: () => "opencode", status: (sessionID: string) => sessionID === "second" ? { ...EMPTY_SESSION_TAB_STATUS, busy: busy() } : EMPTY_SESSION_TAB_STATUS, } satisfies SessionTabsController const app = await testRender( () => ( transcript ), { width: 80, height: 10, clock }, ) try { app.renderer.pause() await app.renderOnce() const cellRow = (row: number) => app.captureSpans().lines[row]!.spans.flatMap((span) => Array.from(span.text, (char) => ({ char, fg: span.fg.buffer.join(), bg: span.bg.buffer.join(), })), ) const idleAbove = cellRow(3) const idleTitle = cellRow(4) const idleDetail = cellRow(5) const idleBelow = cellRow(6) setBusy(true) await app.renderOnce() clock.setTime(1850) await app.renderOnce() const runningAbove = cellRow(3) const runningTitle = cellRow(4) const runningDetail = cellRow(5) const runningBelow = cellRow(6) const rightEdge = railWidth - 1 expect(runningAbove.slice(0, railWidth).map((cell) => cell.char)).toEqual(Array(railWidth).fill("▄")) expect(runningBelow.slice(0, railWidth).map((cell) => cell.char)).toEqual(Array(railWidth).fill("▀")) expect(runningAbove[rightEdge]!.fg).not.toBe(idleAbove[rightEdge]!.bg) expect(runningBelow[rightEdge]!.fg).not.toBe(idleBelow[rightEdge]!.bg) expect(runningTitle[rightEdge]!.bg).not.toBe(idleTitle[rightEdge]!.bg) expect(runningDetail[rightEdge]!.bg).not.toBe(idleDetail[rightEdge]!.bg) expect(runningTitle[railWidth]!.bg).toBe(idleTitle[railWidth]!.bg) expect(runningBelow[railWidth]!.char).toBe(" ") } finally { app.renderer.destroy() } }, )