mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
325 lines
12 KiB
TypeScript
325 lines
12 KiB
TypeScript
/** @jsxImportSource @opentui/solid */
|
|
import { expect, test } from "bun:test"
|
|
import { SyntaxStyle, TextAttributes, type MarkdownRenderable } from "@opentui/core"
|
|
import { testRender, useRenderer } from "@opentui/solid"
|
|
import { onCleanup, onMount, type JSX } from "solid-js"
|
|
import { ConfigProvider } from "../../src/config"
|
|
import { ClientProvider } from "../../src/context/client"
|
|
import { DataProvider, useData } from "../../src/context/data"
|
|
import { Keymap } from "../../src/context/keymap"
|
|
import { RouteProvider, useRoute } from "../../src/context/route"
|
|
import { TuiAppProvider } from "../../src/context/runtime"
|
|
import { StorageProvider } from "../../src/context/storage"
|
|
import { ThemeProvider } from "../../src/context/theme"
|
|
import { DialogProvider, useDialog } from "../../src/ui/dialog"
|
|
import { useSessionLinks } from "../../src/ui/session-link"
|
|
import { ToastProvider } from "../../src/ui/toast"
|
|
import { emptyThemeSource, tmpdir } from "../fixture/fixture"
|
|
import { createApi, createFetch, directory, json } from "../fixture/tui-client"
|
|
import { waitForHighlightedFrame } from "../fixture/frame"
|
|
import { TestTuiContexts } from "../fixture/tui-environment"
|
|
import { createTuiResolvedConfig } from "../fixture/tui-runtime"
|
|
|
|
const session = (id: string, title: string, archived?: number) => ({
|
|
id,
|
|
title,
|
|
projectID: "project",
|
|
location: { directory },
|
|
cost: 0,
|
|
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
time: { created: 0, updated: 0, ...(archived === undefined ? {} : { archived }) },
|
|
})
|
|
const id = (name: string) => `ses_${name.repeat(26).slice(0, 26)}`
|
|
const current = id("current")
|
|
const known = session(id("known"), "Fix the flaky reconnect test on Windows")
|
|
const archived = session(id("archived"), "Old work", 1)
|
|
const unknown = id("unknown")
|
|
const missing = id("missing")
|
|
|
|
test("session links in markdown", async () => {
|
|
await using state = await tmpdir()
|
|
const lookup = Promise.withResolvers<void>()
|
|
const requested: string[] = []
|
|
const fetched = new Map([missing, unknown].map((sessionID) => [sessionID, Promise.withResolvers<void>()]))
|
|
const calls = createFetch(async (url) => {
|
|
const sessionID = url.pathname.match(/^\/api\/session\/(ses_\w+)$/)?.[1]
|
|
if (!sessionID) return
|
|
requested.push(sessionID)
|
|
fetched.get(sessionID)?.resolve()
|
|
if (sessionID === unknown) return lookup.promise.then(() => json({ data: session(unknown, "Looked up") }))
|
|
return json(
|
|
{ _tag: "SessionNotFoundError", sessionID, message: `Session not found: ${sessionID}` },
|
|
{ status: 404 },
|
|
)
|
|
})
|
|
let data!: ReturnType<typeof useData>
|
|
let route!: ReturnType<typeof useRoute>
|
|
|
|
function Transcript() {
|
|
data = useData()
|
|
route = useRoute()
|
|
data.session.remember({ ...known })
|
|
data.session.remember({ ...archived })
|
|
const renderer = useRenderer()
|
|
const syntaxStyle = SyntaxStyle.fromStyles(
|
|
{ default: { fg: "#ffffff" }, "markup.link.url": { fg: "#00ff00" } },
|
|
renderer.nativeScene,
|
|
)
|
|
onCleanup(() => syntaxStyle.destroy())
|
|
return (
|
|
<markdown
|
|
ref={useSessionLinks()}
|
|
syntaxStyle={syntaxStyle}
|
|
content={[
|
|
`**中文** see ${known.id} now`,
|
|
"",
|
|
`- current ${current}`,
|
|
`- archived ${archived.id}`,
|
|
`- unknown ${unknown}`,
|
|
`- missing ${missing}`,
|
|
"",
|
|
"```",
|
|
known.id,
|
|
"```",
|
|
].join("\n")}
|
|
/>
|
|
)
|
|
}
|
|
|
|
const app = await mount(state.path, calls.fetch, () => <Transcript />, 40, 16)
|
|
// Highlighting runs on the tree-sitter worker and lookups on the fixture server.
|
|
const linked = () =>
|
|
app
|
|
.captureSpans()
|
|
.lines.flatMap((line) => line.spans)
|
|
.filter((span) => span.attributes & TextAttributes.UNDERLINE)
|
|
.map((span) => span.text.trim())
|
|
.join(" ")
|
|
|
|
try {
|
|
await Promise.all([...fetched.values()].map((request) => request.promise))
|
|
// The client marks the session missing once it has read the 404.
|
|
await app.waitFor(() => data.session.missing(missing))
|
|
// In top-level mode every block highlights on its own, so the link can land before the list does.
|
|
await waitForHighlightedFrame(
|
|
app,
|
|
(frame) =>
|
|
linked().length > 0 && [current, archived.id, unknown, missing, known.id].every((id) => frame.includes(id)),
|
|
)
|
|
const frame = app.captureCharFrame()
|
|
// The long title wraps; the current, archived, missing and still-loading sessions stay IDs.
|
|
expect(linked()).toBe("@Fix the flaky reconnect test on Windows")
|
|
for (const plain of [current, archived.id, unknown, missing, known.id]) expect(frame).toContain(plain)
|
|
expect(requested.toSorted()).toEqual([missing, unknown])
|
|
|
|
lookup.resolve()
|
|
// Joins the lookup already in flight rather than fetching again.
|
|
await data.session.sync(unknown)
|
|
await waitForHighlightedFrame(app, () => linked().includes("@Looked up"))
|
|
// The hook runs on the first frame, so a lookup can start before the client connects, which the client redoes.
|
|
const looked = requested.length
|
|
data.session.remember({ ...known, title: "Renamed" })
|
|
await waitForHighlightedFrame(app, () => linked() === "@Renamed @Looked up")
|
|
// Lookups are cached, misses included.
|
|
expect(requested.length).toBe(looked)
|
|
expect([...new Set(requested)].toSorted()).toEqual([missing, unknown])
|
|
|
|
const rows = app.captureCharFrame().split("\n")
|
|
const clicked = rows.findIndex((row) => row.includes("@Looked up"))
|
|
app.renderer.clearSelection()
|
|
await app.mockMouse.click(rows[clicked]!.indexOf("@Looked up") + 4, clicked)
|
|
expect(route.data).toEqual({ type: "session", sessionID: unknown })
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
test("a wrapped title opens its session from either row, and plain text does not", async () => {
|
|
await using state = await tmpdir()
|
|
const calls = createFetch(async () => undefined)
|
|
let route!: ReturnType<typeof useRoute>
|
|
|
|
function Transcript() {
|
|
useData().session.remember({ ...known })
|
|
route = useRoute()
|
|
const renderer = useRenderer()
|
|
const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } }, renderer.nativeScene)
|
|
onCleanup(() => syntaxStyle.destroy())
|
|
return <markdown ref={useSessionLinks()} syntaxStyle={syntaxStyle} content={`**中文** see ${known.id} now`} />
|
|
}
|
|
|
|
const app = await mount(state.path, calls.fetch, () => <Transcript />, 30, 6)
|
|
|
|
try {
|
|
await waitForHighlightedFrame(app, (frame) => frame.includes("@Fix"))
|
|
const rows = app.captureCharFrame().split("\n")
|
|
const head = rows.findIndex((row) => row.includes("@Fix"))
|
|
const tail = rows.findIndex((row) => row.includes("Windows"))
|
|
expect(tail).toBeGreaterThan(head)
|
|
|
|
await app.mockMouse.click(rows[head]!.indexOf("see"), head)
|
|
await app.mockMouse.click(rows[tail]!.indexOf("now"), tail)
|
|
await app.mockMouse.drag(rows[head]!.indexOf("@Fix"), head, rows[head]!.indexOf("@Fix") + 3, head)
|
|
expect(route.data).toEqual({ type: "session", sessionID: current })
|
|
|
|
app.renderer.clearSelection()
|
|
await app.mockMouse.click(rows[tail]!.indexOf("Windows") + 2, tail)
|
|
expect(route.data).toEqual({ type: "session", sessionID: known.id })
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
test("clicking a session link in a dialog closes it and opens the session", async () => {
|
|
await using state = await tmpdir()
|
|
const calls = createFetch(async () => undefined)
|
|
let route!: ReturnType<typeof useRoute>
|
|
let dialog!: ReturnType<typeof useDialog>
|
|
|
|
function Answer() {
|
|
const renderer = useRenderer()
|
|
const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } }, renderer.nativeScene)
|
|
onCleanup(() => syntaxStyle.destroy())
|
|
return <markdown ref={useSessionLinks()} syntaxStyle={syntaxStyle} content={`See ${known.id}`} />
|
|
}
|
|
function Probe() {
|
|
useData().session.remember({ ...known })
|
|
route = useRoute()
|
|
dialog = useDialog()
|
|
onMount(() => dialog.replace(() => <Answer />))
|
|
return null
|
|
}
|
|
|
|
const app = await mount(state.path, calls.fetch, () => <Probe />, 80, 12)
|
|
|
|
try {
|
|
await waitForHighlightedFrame(app, (frame) => frame.includes("@Fix"))
|
|
const rows = app.captureCharFrame().split("\n")
|
|
const y = rows.findIndex((row) => row.includes("@Fix"))
|
|
await app.mockMouse.click(rows[y]!.indexOf("@Fix") + 2, y)
|
|
expect(route.data).toEqual({ type: "session", sessionID: known.id })
|
|
expect(dialog.stack).toHaveLength(0)
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
test.each([true, false])("code spans link only when they are exactly an ID, conceal %p", async (conceal) => {
|
|
await using state = await tmpdir()
|
|
const calls = createFetch(async () => undefined)
|
|
|
|
function Transcript() {
|
|
useData().session.remember({ ...known })
|
|
const renderer = useRenderer()
|
|
const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } }, renderer.nativeScene)
|
|
onCleanup(() => syntaxStyle.destroy())
|
|
return (
|
|
<markdown
|
|
ref={useSessionLinks()}
|
|
conceal={conceal}
|
|
syntaxStyle={syntaxStyle}
|
|
content={`Run \`opencode --session ${known.id}\` or open \`${known.id}\`.`}
|
|
/>
|
|
)
|
|
}
|
|
|
|
const app = await mount(state.path, calls.fetch, () => <Transcript />, 120, 4)
|
|
const linked = () =>
|
|
app
|
|
.captureSpans()
|
|
.lines.flatMap((line) => line.spans)
|
|
.filter((span) => span.attributes & TextAttributes.UNDERLINE)
|
|
.map((span) => span.text.trim())
|
|
|
|
try {
|
|
await waitForHighlightedFrame(app, () => linked().length > 0)
|
|
expect(linked()).toEqual([conceal ? `@${known.title}` : known.id])
|
|
expect(app.captureCharFrame()).toContain(`opencode --session ${known.id}`)
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
test("a label change reruns the hook only on the blocks that mention that session", async () => {
|
|
await using state = await tmpdir()
|
|
const calls = createFetch(async () => undefined)
|
|
const other = session(id("other"), "Other work")
|
|
let data!: ReturnType<typeof useData>
|
|
let markdown!: MarkdownRenderable
|
|
|
|
function Transcript() {
|
|
data = useData()
|
|
data.session.remember({ ...known })
|
|
data.session.remember({ ...other })
|
|
const links = useSessionLinks()
|
|
const renderer = useRenderer()
|
|
const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } }, renderer.nativeScene)
|
|
onCleanup(() => syntaxStyle.destroy())
|
|
return (
|
|
<markdown
|
|
ref={(renderable: MarkdownRenderable) => {
|
|
links(renderable)
|
|
markdown = renderable
|
|
}}
|
|
syntaxStyle={syntaxStyle}
|
|
content={`First ${known.id}\n\nSecond ${other.id}`}
|
|
/>
|
|
)
|
|
}
|
|
|
|
const app = await mount(state.path, calls.fetch, () => <Transcript />, 80, 6)
|
|
|
|
try {
|
|
await waitForHighlightedFrame(app, (frame) => frame.includes("@Fix") && frame.includes("@Other"))
|
|
// Record the blocks the hook runs on again.
|
|
const hook = markdown.inlineHook!
|
|
const reran: string[] = []
|
|
markdown.inlineHook = (spans, context) => (reran.push(context.source), hook(spans, context))
|
|
await app.renderOnce()
|
|
reran.length = 0
|
|
|
|
data.session.remember({ ...known, title: "Renamed" })
|
|
await waitForHighlightedFrame(app, (frame) => frame.includes("@Renamed"))
|
|
expect(reran).toEqual([`First ${known.id}`])
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
async function mount(
|
|
state: string,
|
|
fetch: typeof globalThis.fetch,
|
|
view: () => JSX.Element,
|
|
width: number,
|
|
height: number,
|
|
) {
|
|
const app = await testRender(
|
|
() => (
|
|
<TestTuiContexts paths={{ state }}>
|
|
<TuiAppProvider value={{ name: "test", version: "test", channel: "test" }}>
|
|
<StorageProvider>
|
|
<ConfigProvider config={createTuiResolvedConfig()}>
|
|
<Keymap.Provider>
|
|
<ToastProvider>
|
|
<RouteProvider initialRoute={{ type: "session", sessionID: current }}>
|
|
<ClientProvider api={createApi(fetch)}>
|
|
<DataProvider directory={directory}>
|
|
<ThemeProvider mode="dark" source={emptyThemeSource}>
|
|
<DialogProvider>{view()}</DialogProvider>
|
|
</ThemeProvider>
|
|
</DataProvider>
|
|
</ClientProvider>
|
|
</RouteProvider>
|
|
</ToastProvider>
|
|
</Keymap.Provider>
|
|
</ConfigProvider>
|
|
</StorageProvider>
|
|
</TuiAppProvider>
|
|
</TestTuiContexts>
|
|
),
|
|
{ width, height },
|
|
)
|
|
// Frame waits need the render loop; highlights and lookups land between frames.
|
|
app.renderer.start()
|
|
return app
|
|
}
|