Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { $createParagraphNode, $getRoot, $isElementNode, createEditor } from "lexical";
3
4import {
5 $createComposerContextReferenceNode,
6 ComposerContextReferenceNode,
7} from "./ComposerContextReferenceNode";
8import { splitPromptIntoComposerSegments } from "../composer-editor-mentions";
9
10vi.mock("./composerContextPresentation", () => ({
11 ComposerContextReferenceChip: () => null,
12}));
13
14const reference = { kind: "terminal", contextId: "ctx-1", label: "Terminal 1 lines 3-4" };
15const link = "[Terminal 1 lines 3-4](t3-context://v1/terminal/ctx-1)";
16
17function createReferenceEditor() {
18 const editor = createEditor({ nodes: [ComposerContextReferenceNode] });
19 editor.update(
20 () => {
21 $getRoot().append($createParagraphNode());
22 },
23 { discrete: true },
24 );
25 return editor;
26}
27
28function $referenceNodes() {
29 const paragraph = $getRoot().getFirstChildOrThrow();
30 if (!$isElementNode(paragraph)) throw new Error("Expected a paragraph");
31 return paragraph.getChildren().filter((node) => node instanceof ComposerContextReferenceNode);
32}
33
34describe("ComposerContextReferenceNode", () => {
35 it("renders its canonical link as text so the prompt carries identity", () => {
36 const editor = createReferenceEditor();
37 editor.update(
38 () => {
39 const paragraph = $getRoot().getFirstChild();
40 if (!$isElementNode(paragraph)) throw new Error("Expected a paragraph");
41 paragraph.append($createComposerContextReferenceNode(reference, "ref-1"));
42 },
43 { discrete: true },
44 );
45 const text = editor.getEditorState().read(() => $getRoot().getTextContent());
46 expect(text).toBe(link);
47 expect(splitPromptIntoComposerSegments(text)).toEqual([
48 { type: "context-reference", ...reference, source: link },
49 ]);
50 });
51
52 it("round-trips through JSON keeping the occurrence id", () => {
53 const editor = createReferenceEditor();
54 editor.update(
55 () => {
56 const paragraph = $getRoot().getFirstChild();
57 if (!$isElementNode(paragraph)) throw new Error("Expected a paragraph");
58 paragraph.append($createComposerContextReferenceNode(reference, "ref-1"));
59 },
60 { discrete: true },
61 );
62 const serialized = editor.getEditorState().toJSON();
63 const restored = createReferenceEditor();
64 restored.setEditorState(restored.parseEditorState(serialized));
65 const nodes = restored.getEditorState().read(() =>
66 $referenceNodes().map((node) => ({
67 referenceId: node.__referenceId,
68 reference: node.getReference(),
69 })),
70 );
71 expect(nodes).toEqual([{ referenceId: "ref-1", reference }]);
72 });
73
74 it("mints a distinct occurrence id per created node while sharing the payload id", () => {
75 const editor = createReferenceEditor();
76 editor.update(
77 () => {
78 const paragraph = $getRoot().getFirstChild();
79 if (!$isElementNode(paragraph)) throw new Error("Expected a paragraph");
80 paragraph.append(
81 $createComposerContextReferenceNode(reference),
82 $createComposerContextReferenceNode(reference),
83 );
84 },
85 { discrete: true },
86 );
87 const nodes = editor.getEditorState().read(() => $referenceNodes());
88 expect(nodes[0]!.__contextId).toBe(nodes[1]!.__contextId);
89 expect(nodes[0]!.__referenceId).not.toBe(nodes[1]!.__referenceId);
90 expect(nodes[0]!.__referenceId).toMatch(/^[0-9a-f-]{36}$/);
91 });
92});