1
2
3
4
5import { describe, expect, it, vi } from "vitest";
6
7
8
9
10vi.mock("next/link", () => ({ default: (p: object) => p }));
11vi.mock("@tanstack/react-query", () => ({
12 useQuery: () => ({ data: undefined }),
13 useInfiniteQuery: () => ({ data: undefined }),
14 useQueryClient: () => ({ invalidateQueries: vi.fn() }),
15}));
16vi.mock("@/lib/api", () => ({
17 apiFetch: vi.fn(async () => ({ data: [] })),
18 blockApi: { update: vi.fn(async () => ({})) },
19 uploadFile: vi.fn(),
20}));
21
22import { codeBlockText } from "@/components/editor/block-renderer";
23
24describe("codeBlockText (code block content → editable plain text)", () => {
25 it("returns a raw string content verbatim (web edits)", () => {
26 expect(codeBlockText("const x = 1;")).toBe("const x = 1;");
27 expect(codeBlockText("")).toBe("");
28 });
29
30 it("unwraps the API/AI canonical { text } envelope (slash-pick / AI apply)", () => {
31 expect(codeBlockText({ text: "function f() {}\n" })).toBe("function f() {}\n");
32 expect(codeBlockText({ text: "" })).toBe("");
33 });
34
35 it("falls back to pretty JSON for other object content", () => {
36 expect(codeBlockText({ a: 1 })).toBe(JSON.stringify({ a: 1 }, null, 2));
37 });
38
39 it("handles null / undefined content as empty string", () => {
40 expect(codeBlockText(null)).toBe("");
41 expect(codeBlockText(undefined)).toBe("");
42 expect(codeBlockText({})).toBe(JSON.stringify({}, null, 2));
43 });
44
45 it("does not confuse an object with a non-string .text field for empty", () => {
46
47 expect(codeBlockText({ text: 42 })).toBe(JSON.stringify({ text: 42 }, null, 2));
48 });
49});
50