Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalContext.test.ts

211 lines6.8 KB

1import { ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 appendTerminalContextsToPrompt,
6 buildTerminalContextPreviewTitle,
7 buildTerminalContextBlock,
8 countInlineTerminalContextPlaceholders,
9 deriveDisplayedUserMessageState,
10 ensureInlineTerminalContextPlaceholders,
11 extractTrailingTerminalContexts,
12 filterTerminalContextsWithText,
13 formatInlineTerminalContextLabel,
14 formatTerminalContextLabel,
15 hasTerminalContextText,
16 INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
17 insertInlineTerminalContextPlaceholder,
18 isTerminalContextExpired,
19 materializeInlineTerminalContextPrompt,
20 removeInlineTerminalContextPlaceholder,
21 stripInlineTerminalContextPlaceholders,
22 type TerminalContextDraft,
23} from "./terminalContext";
24​
25function makeContext(overrides?: Partial<TerminalContextDraft>): TerminalContextDraft {
26 return {
27 id: "context-1",
28 threadId: ThreadId.make("thread-1"),
29 terminalId: "default",
30 terminalLabel: "Terminal 1",
31 lineStart: 12,
32 lineEnd: 13,
33 text: "git status\nOn branch main",
34 createdAt: "2026-03-13T12:00:00.000Z",
35 ...overrides,
36 };
38​
39describe("terminalContext", () => {
40 it("formats terminal labels with line ranges", () => {
41 expect(formatTerminalContextLabel(makeContext())).toBe("Terminal 1 lines 12-13");
42 expect(
43 formatTerminalContextLabel(
44 makeContext({
45 lineStart: 9,
46 lineEnd: 9,
47 }),
48 ),
49 ).toBe("Terminal 1 line 9");
50 });
51​
52 it("builds a numbered terminal context block", () => {
53 expect(buildTerminalContextBlock([makeContext()])).toBe(
54 [
55 "<terminal_context>",
56 "- Terminal 1 lines 12-13:",
57 " 12 | git status",
58 " 13 | On branch main",
59 "</terminal_context>",
60 ].join("\n"),
61 );
62 });
63​
64 it("appends terminal context blocks after prompt text", () => {
65 expect(appendTerminalContextsToPrompt("Investigate this", [makeContext()])).toBe(
66 [
67 "Investigate this",
68 "",
69 "<terminal_context>",
70 "- Terminal 1 lines 12-13:",
71 " 12 | git status",
72 " 13 | On branch main",
73 "</terminal_context>",
74 ].join("\n"),
75 );
76 });
77​
78 it("replaces inline placeholders with inline terminal labels before appending context blocks", () => {
79 expect(
80 appendTerminalContextsToPrompt(
81 `Investigate ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} carefully`,
82 [makeContext()],
83 ),
84 ).toBe(
85 [
86 "Investigate @terminal-1:12-13 carefully",
87 "",
88 "<terminal_context>",
89 "- Terminal 1 lines 12-13:",
90 " 12 | git status",
91 " 13 | On branch main",
92 "</terminal_context>",
93 ].join("\n"),
94 );
95 });
96​
97 it("extracts terminal context blocks from message text", () => {
98 const prompt = appendTerminalContextsToPrompt("Investigate this", [makeContext()]);
99 expect(extractTrailingTerminalContexts(prompt)).toEqual({
100 promptText: "Investigate this",
101 contextCount: 1,
102 previewTitle: "Terminal 1 lines 12-13\n12 | git status\n13 | On branch main",
103 contexts: [
104 {
105 header: "Terminal 1 lines 12-13",
106 body: "12 | git status\n13 | On branch main",
107 },
108 ],
109 });
110 });
111​
112 it("derives displayed user message state from terminal context prompts", () => {
113 const prompt = appendTerminalContextsToPrompt("Investigate this", [makeContext()]);
114 expect(deriveDisplayedUserMessageState(prompt)).toEqual({
115 visibleText: "Investigate this",
116 copyText: prompt,
117 contextCount: 1,
118 previewTitle: "Terminal 1 lines 12-13\n12 | git status\n13 | On branch main",
119 contexts: [
120 {
121 header: "Terminal 1 lines 12-13",
122 body: "12 | git status\n13 | On branch main",
123 },
124 ],
125 elementContexts: [],
126 });
127 });
128​
129 it("preserves prompt text when no trailing terminal context block exists", () => {
130 expect(extractTrailingTerminalContexts("No attached context")).toEqual({
131 promptText: "No attached context",
132 contextCount: 0,
133 previewTitle: null,
134 contexts: [],
135 });
136 });
137​
138 it("returns null preview title when every context is invalid", () => {
139 expect(
140 buildTerminalContextPreviewTitle([
141 makeContext({
142 terminalId: " ",
143 }),
144 makeContext({
145 id: "context-2",
146 text: "\n\n",
147 }),
148 ]),
149 ).toBeNull();
150 });
151​
152 it("tracks inline terminal context placeholders in prompt text", () => {
153 const placeholder = INLINE_TERMINAL_CONTEXT_PLACEHOLDER;
154 expect(countInlineTerminalContextPlaceholders(`a${placeholder}b${placeholder}`)).toBe(2);
155 expect(ensureInlineTerminalContextPlaceholders("Investigate this", 2)).toBe(
156 `${placeholder}${placeholder}Investigate this`,
157 );
158 expect(insertInlineTerminalContextPlaceholder("abc", 1)).toEqual({
159 prompt: `a ${placeholder} bc`,
160 cursor: 4,
161 contextIndex: 0,
162 });
163 expect(removeInlineTerminalContextPlaceholder(`a${placeholder}b${placeholder}c`, 1)).toEqual({
164 prompt: `a${placeholder}bc`,
165 cursor: 3,
166 });
167 expect(stripInlineTerminalContextPlaceholders(`a${placeholder}b`)).toBe("ab");
168 });
169​
170 it("inserts a placeholder after a file mention when given the expanded prompt cursor", () => {
171 const placeholder = INLINE_TERMINAL_CONTEXT_PLACEHOLDER;
172 expect(insertInlineTerminalContextPlaceholder("Inspect @package.json ", 22)).toEqual({
173 prompt: `Inspect @package.json ${placeholder} `,
174 cursor: 24,
175 contextIndex: 0,
176 });
177 });
178​
179 it("adds a trailing space and consumes an existing trailing space at the insertion point", () => {
180 const placeholder = INLINE_TERMINAL_CONTEXT_PLACEHOLDER;
181 expect(insertInlineTerminalContextPlaceholder("yo whats", 3)).toEqual({
182 prompt: `yo ${placeholder} whats`,
183 cursor: 5,
184 contextIndex: 0,
185 });
186 });
187​
188 it("marks contexts without snapshot text as expired and filters them from sendable contexts", () => {
189 const liveContext = makeContext();
190 const expiredContext = makeContext({
191 id: "context-2",
192 text: "",
193 });
194​
195 expect(hasTerminalContextText(liveContext)).toBe(true);
196 expect(isTerminalContextExpired(liveContext)).toBe(false);
197 expect(hasTerminalContextText(expiredContext)).toBe(false);
198 expect(isTerminalContextExpired(expiredContext)).toBe(true);
199 expect(filterTerminalContextsWithText([expiredContext, liveContext])).toEqual([liveContext]);
200 });
201​
202 it("formats and materializes inline terminal labels from placeholder positions", () => {
203 expect(formatInlineTerminalContextLabel(makeContext())).toBe("@terminal-1:12-13");
204 expect(
205 materializeInlineTerminalContextPrompt(
206 `Investigate ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} carefully`,
207 [makeContext()],
208 ),
209 ).toBe("Investigate @terminal-1:12-13 carefully");
210 });
211});