Skip to content

Commit af149e1

Browse files
committed
test(shared): add unit tests for message content block visitor
1 parent 153fed7 commit af149e1

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { visitObjectContentBlocks } from "./message-content-blocks.js";
3+
4+
describe("visitObjectContentBlocks", () => {
5+
it("does not visit when message is null", () => {
6+
const visitor = vi.fn();
7+
visitObjectContentBlocks(null, visitor);
8+
expect(visitor).not.toHaveBeenCalled();
9+
});
10+
11+
it("does not visit when message is undefined", () => {
12+
const visitor = vi.fn();
13+
visitObjectContentBlocks(undefined, visitor);
14+
expect(visitor).not.toHaveBeenCalled();
15+
});
16+
17+
it("does not visit when message is a string", () => {
18+
const visitor = vi.fn();
19+
visitObjectContentBlocks("hello", visitor);
20+
expect(visitor).not.toHaveBeenCalled();
21+
});
22+
23+
it("does not visit when message is a number", () => {
24+
const visitor = vi.fn();
25+
visitObjectContentBlocks(42, visitor);
26+
expect(visitor).not.toHaveBeenCalled();
27+
});
28+
29+
it("does not visit when message has no content property", () => {
30+
const visitor = vi.fn();
31+
visitObjectContentBlocks({ role: "user" }, visitor);
32+
expect(visitor).not.toHaveBeenCalled();
33+
});
34+
35+
it("does not visit when content is not an array", () => {
36+
const visitor = vi.fn();
37+
visitObjectContentBlocks({ content: "plain text" }, visitor);
38+
expect(visitor).not.toHaveBeenCalled();
39+
});
40+
41+
it("visits each object block in the content array", () => {
42+
const visitor = vi.fn();
43+
visitObjectContentBlocks(
44+
{
45+
content: [
46+
{ type: "text", text: "hello" },
47+
{ type: "image", url: "x" },
48+
],
49+
},
50+
visitor,
51+
);
52+
expect(visitor).toHaveBeenCalledTimes(2);
53+
expect(visitor).toHaveBeenNthCalledWith(1, { type: "text", text: "hello" });
54+
expect(visitor).toHaveBeenNthCalledWith(2, { type: "image", url: "x" });
55+
});
56+
57+
it("skips non-object entries in the content array", () => {
58+
const visitor = vi.fn();
59+
visitObjectContentBlocks(
60+
{ content: ["not-an-object", null, { type: "text", text: "ok" }, 123] },
61+
visitor,
62+
);
63+
expect(visitor).toHaveBeenCalledTimes(1);
64+
expect(visitor).toHaveBeenCalledWith({ type: "text", text: "ok" });
65+
});
66+
67+
it("returns undefined (void)", () => {
68+
const visitor = vi.fn();
69+
const result = visitObjectContentBlocks({ content: [{ type: "text", text: "x" }] }, visitor);
70+
expect(result).toBeUndefined();
71+
});
72+
73+
it("handles empty content array", () => {
74+
const visitor = vi.fn();
75+
visitObjectContentBlocks({ content: [] }, visitor);
76+
expect(visitor).not.toHaveBeenCalled();
77+
});
78+
});

0 commit comments

Comments
 (0)