Skip to content

Commit 4345bb1

Browse files
committed
fix(opencode): fix Ink test infrastructure and add MessageList (anomalyco#11768)
- Update preload.ts to skip SolidJS transform for test/cli/ink/ files - Add MessageList component using Ink's Static for message history - All 90 Ink tests now pass Fixes anomalyco#11768
1 parent d6eab3c commit 4345bb1

5 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎packages/opencode/preload.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ const inkExcludedPlugin: BunPlugin = {
3838

3939
const code = await Bun.file(args.path).text()
4040

41-
// Detect if file imports from Ink (React-based)
42-
const isInkFile = /from\s*["']ink["']/.test(code) || /require\(["']ink["']\)/.test(code)
41+
// Detect if file is Ink-related (React-based)
42+
const isInkFile =
43+
/from\s*["']ink["']/.test(code) ||
44+
/from\s*["']ink-/.test(code) ||
45+
/require\(["']ink["']\)/.test(code) ||
46+
/@jsxImportSource\s+react/.test(code) ||
47+
args.path.includes("/cli/ink/") ||
48+
args.path.includes("/test/cli/ink/")
4349

4450
if (isInkFile) {
4551
// Transform with Babel React preset
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** @jsxImportSource react */
2+
import { Static, Box, Text } from "ink"
3+
import type { Message } from "../state/types"
4+
import { theme } from "../theme"
5+
6+
interface MessageListProps {
7+
messages: Message[]
8+
}
9+
10+
export const MessageList = ({ messages }: MessageListProps) => (
11+
<Static items={messages}>
12+
{(message) => (
13+
<Box key={message.id} flexDirection="column" marginBottom={1}>
14+
<Text color={message.role === "user" ? theme.colors.primary : theme.colors.success}>
15+
{message.role === "user" ? "you" : "assistant"}
16+
</Text>
17+
{message.parts.map((part, i) => (
18+
<Text key={i}>{part.content}</Text>
19+
))}
20+
</Box>
21+
)}
22+
</Static>
23+
)

‎packages/opencode/src/cli/ink/components/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export { StreamingProse } from "./StreamingProse"
55
export { InputLine } from "./InputLine"
66
export { SelectMenu } from "./SelectMenu"
77
export { StatusBar } from "./StatusBar"
8+
export { MessageList } from "./MessageList"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/** @jsxImportSource react */
2+
import { describe, it, expect } from "bun:test"
3+
import { render } from "ink-testing-library"
4+
import { MessageList } from "@/cli/ink/components/MessageList"
5+
import type { Message } from "@/cli/ink/state/types"
6+
7+
describe("MessageList", () => {
8+
const messages: Message[] = [
9+
{ id: "msg-1", role: "user", parts: [{ type: "text", content: "Hello" }], complete: true },
10+
{ id: "msg-2", role: "assistant", parts: [{ type: "text", content: "Hi there" }], complete: true },
11+
]
12+
13+
it("renders without errors", () => {
14+
const { lastFrame } = render(<MessageList messages={messages} />)
15+
lastFrame()
16+
})
17+
18+
it("renders empty list without error", () => {
19+
const { lastFrame } = render(<MessageList messages={[]} />)
20+
lastFrame()
21+
})
22+
23+
it("renders with multiple message parts", () => {
24+
const multiPartMessages: Message[] = [
25+
{
26+
id: "msg-1",
27+
role: "user",
28+
parts: [
29+
{ type: "text", content: "First part" },
30+
{ type: "text", content: "Second part" },
31+
],
32+
complete: true,
33+
},
34+
]
35+
const { lastFrame } = render(<MessageList messages={multiPartMessages} />)
36+
lastFrame()
37+
})
38+
})

‎packages/opencode/test/preload.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// IMPORTANT: Load main preload first to register Babel transforms
2+
import "../preload"
3+
14
// IMPORTANT: Set env vars BEFORE any imports from src/ directory
25
// xdg-basedir reads env vars at import time, so we must set these first
36
import os from "os"

0 commit comments

Comments
 (0)