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

terminalContext.test.ts

210 lines6.7 KB

1import { ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
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.makeUnsafe("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 });
126 });
127​
128 it("preserves prompt text when no trailing terminal context block exists", () => {
129 expect(extractTrailingTerminalContexts("No attached context")).toEqual({
130 promptText: "No attached context",
131 contextCount: 0,
132 previewTitle: null,
133 contexts: [],
134 });
135 });
136​
137 it("returns null preview title when every context is invalid", () => {
138 expect(
139 buildTerminalContextPreviewTitle([
140 makeContext({
141 terminalId: " ",
142 }),
143 makeContext({
144 id: "context-2",
145 text: "\n\n",
146 }),
147 ]),
148 ).toBeNull();
149 });
150​
151 it("tracks inline terminal context placeholders in prompt text", () => {
152 const placeholder = INLINE_TERMINAL_CONTEXT_PLACEHOLDER;
153 expect(countInlineTerminalContextPlaceholders(`a${placeholder}b${placeholder}`)).toBe(2);
154 expect(ensureInlineTerminalContextPlaceholders("Investigate this", 2)).toBe(
155 `${placeholder}${placeholder}Investigate this`,
156 );
157 expect(insertInlineTerminalContextPlaceholder("abc", 1)).toEqual({
158 prompt: `a ${placeholder} bc`,
159 cursor: 4,
160 contextIndex: 0,
161 });
162 expect(removeInlineTerminalContextPlaceholder(`a${placeholder}b${placeholder}c`, 1)).toEqual({
163 prompt: `a${placeholder}bc`,
164 cursor: 3,
165 });
166 expect(stripInlineTerminalContextPlaceholders(`a${placeholder}b`)).toBe("ab");
167 });
168​
169 it("inserts a placeholder after a file mention when given the expanded prompt cursor", () => {
170 const placeholder = INLINE_TERMINAL_CONTEXT_PLACEHOLDER;
171 expect(insertInlineTerminalContextPlaceholder("Inspect @package.json ", 22)).toEqual({
172 prompt: `Inspect @package.json ${placeholder} `,
173 cursor: 24,
174 contextIndex: 0,
175 });
176 });
177​
178 it("adds a trailing space and consumes an existing trailing space at the insertion point", () => {
179 const placeholder = INLINE_TERMINAL_CONTEXT_PLACEHOLDER;
180 expect(insertInlineTerminalContextPlaceholder("yo whats", 3)).toEqual({
181 prompt: `yo ${placeholder} whats`,
182 cursor: 5,
183 contextIndex: 0,
184 });
185 });
186​
187 it("marks contexts without snapshot text as expired and filters them from sendable contexts", () => {
188 const liveContext = makeContext();
189 const expiredContext = makeContext({
190 id: "context-2",
191 text: "",
192 });
193​
194 expect(hasTerminalContextText(liveContext)).toBe(true);
195 expect(isTerminalContextExpired(liveContext)).toBe(false);
196 expect(hasTerminalContextText(expiredContext)).toBe(false);
197 expect(isTerminalContextExpired(expiredContext)).toBe(true);
198 expect(filterTerminalContextsWithText([expiredContext, liveContext])).toEqual([liveContext]);
199 });
200​
201 it("formats and materializes inline terminal labels from placeholder positions", () => {
202 expect(formatInlineTerminalContextLabel(makeContext())).toBe("@terminal-1:12-13");
203 expect(
204 materializeInlineTerminalContextPrompt(
205 `Investigate ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} carefully`,
206 [makeContext()],
207 ),
208 ).toBe("Investigate @terminal-1:12-13 carefully");
209 });
210});