Elffuss BraidHub

kikocis/continue public

main · 4.7 KiBRawPermalink
1import { render } from "ink-testing-library";
2import React from "react";
3import { describe, expect, it } from "vitest";
4
5import type {
6 ChatHistoryItem,
7 MessageContent,
8 MessagePart,
9} from "../../../../../core/index.js";
10
11import { MemoizedMessage } from "./MemoizedMessage.js";
12
13describe("MemoizedMessage formatMessageContentForDisplay", () => {
14 const createTestHistoryItem = (content: MessageContent): ChatHistoryItem => ({
15 message: {
16 role: "user",
17 content,
18 },
19 contextItems: [],
20 });
21
22 it("should display string content as-is", () => {
23 const content = "Just a simple text message";
24 const historyItem = createTestHistoryItem(content);
25
26 const { lastFrame } = render(
27 <MemoizedMessage item={historyItem} index={1} />,
28 );
29
30 expect(lastFrame()).toContain("Just a simple text message");
31 });
32
33 it("should display message with images using placeholders", () => {
34 const messageParts: MessagePart[] = [
35 { type: "text", text: "Here is an image: " },
36 { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } },
37 { type: "text", text: " and some more text" },
38 ];
39 const historyItem = createTestHistoryItem(messageParts);
40
41 const { lastFrame } = render(
42 <MemoizedMessage item={historyItem} index={1} />,
43 );
44
45 const output = lastFrame();
46 expect(output).toContain("Here is an image: [Image #1] and some more text");
47 expect(output).not.toContain("data:image/png;base64,abc123");
48 expect(output).not.toContain("imageUrl");
49 });
50
51 it("should display message with multiple images using incrementing placeholders", () => {
52 const messageParts: MessagePart[] = [
53 { type: "text", text: "First " },
54 { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } },
55 { type: "text", text: " then text " },
56 { type: "imageUrl", imageUrl: { url: "data:image/jpeg;base64,def456" } },
57 { type: "text", text: " end" },
58 ];
59 const historyItem = createTestHistoryItem(messageParts);
60
61 const { lastFrame } = render(
62 <MemoizedMessage item={historyItem} index={1} />,
63 );
64
65 const output = lastFrame();
66 expect(output).toContain("First [Image #1] then text [Image #2] end");
67 expect(output).not.toContain("data:image/png;base64");
68 expect(output).not.toContain("data:image/jpeg;base64");
69 });
70
71 it("should display message with only images", () => {
72 const messageParts: MessagePart[] = [
73 { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } },
74 { type: "imageUrl", imageUrl: { url: "data:image/png;base64,def456" } },
75 ];
76 const historyItem = createTestHistoryItem(messageParts);
77
78 const { lastFrame } = render(
79 <MemoizedMessage item={historyItem} index={1} />,
80 );
81
82 const output = lastFrame();
83 expect(output).toContain("[Image #1][Image #2]");
84 });
85
86 it("should handle empty text parts gracefully", () => {
87 const messageParts: MessagePart[] = [
88 { type: "text", text: "" },
89 { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } },
90 { type: "text", text: "" },
91 ];
92 const historyItem = createTestHistoryItem(messageParts);
93
94 const { lastFrame } = render(
95 <MemoizedMessage item={historyItem} index={1} />,
96 );
97
98 const output = lastFrame();
99 expect(output).toContain("[Image #1]");
100 expect(output).not.toContain("data:image/png;base64");
101 });
102
103 it("should display text-only message parts as regular string", () => {
104 const messageParts: MessagePart[] = [
105 { type: "text", text: "Just text, no images here" },
106 ];
107 const historyItem = createTestHistoryItem(messageParts);
108
109 const { lastFrame } = render(
110 <MemoizedMessage item={historyItem} index={1} />,
111 );
112
113 expect(lastFrame()).toContain("Just text, no images here");
114 });
115
116 it("should handle unknown part types by converting to JSON", () => {
117 const messageParts = [
118 { type: "text", text: "Start " },
119 { type: "unknown", data: { some: "data" } },
120 { type: "text", text: " end" },
121 ] as MessagePart[];
122 const historyItem = createTestHistoryItem(messageParts);
123
124 const { lastFrame } = render(
125 <MemoizedMessage item={historyItem} index={1} />,
126 );
127
128 const output = lastFrame();
129 // Verify whitespace preservation - trailing space after "Start" and leading space before "end"
130 expect(output).toContain(
131 'Start {"type":"unknown","data":{"some":"data"}} end',
132 );
133 });
134
135 it("should handle non-array non-string content by converting to JSON", () => {
136 const content = { someObject: "data" } as any;
137 const historyItem = createTestHistoryItem(content);
138
139 const { lastFrame } = render(
140 <MemoizedMessage item={historyItem} index={1} />,
141 );
142
143 expect(lastFrame()).toContain('{"someObject":"data"}');
144 });
145});