Elffuss BraidHub

kikocis/continue public

fb08fd5cc0fe · 4.3 KiBRawPermalink
1import { SlashCommandDescWithSource } from "core";
2import { beforeEach, describe, it, vi } from "vitest";
3import {
4 MockIdeMessenger,
5 MockResponseHandler,
6} from "../../../../context/MockIdeMessenger";
7import { getRenderedV1Prompt } from "./renderPromptv1";
8
9function getTestPromptV1Command(body: string): SlashCommandDescWithSource {
10 return {
11 prompt: body,
12 name: "test-prompt-name",
13 description: "test-prompt-description",
14 isLegacy: false,
15 source: "prompt-file-v1",
16 };
17}
18describe("get rendered v1 prompt file", () => {
19 const ideMessenger = new MockIdeMessenger();
20 const firstDir = ideMessenger.responses["getWorkspaceDirs"]?.[0] ?? "";
21 const customGetContextItems: MockResponseHandler<
22 "context/getContextItems"
23 > = async ({ name }) => {
24 if (name === "diff") {
25 return [
26 {
27 id: {
28 itemId: "id",
29 providerTitle: "diff",
30 },
31 name: "name",
32 description: "description",
33 content: "Diff provider content",
34 },
35 ];
36 }
37 return [];
38 };
39 beforeEach(async () => {
40 vi.clearAllMocks();
41 ideMessenger.resetMocks();
42 });
43
44 it("should render a prompt file with no attachments or templated input", async () => {
45 const rawContent = "Simple prompt with no attachments";
46 const command = getTestPromptV1Command(rawContent);
47 const rendered = await getRenderedV1Prompt(
48 ideMessenger,
49 command,
50 "User input",
51 [],
52 );
53 expect(rendered).toBe("Simple prompt with no attachments\n\nUser input");
54 });
55
56 it("should render a prompt file templated input", async () => {
57 const rawContent = "{{{ input }}} Simple prompt with no attachments";
58 const command = getTestPromptV1Command(rawContent);
59 const rendered = await getRenderedV1Prompt(
60 ideMessenger,
61 command,
62 "User input",
63 [],
64 );
65 expect(rendered).toBe("User input Simple prompt with no attachments");
66 });
67
68 it("Should handle file attachments", async () => {
69 ideMessenger.responses["readFile"] = "Test file content";
70 const rawContent = "Content with URL {{{ testFile.txt }}}";
71 const command = getTestPromptV1Command(rawContent);
72 const rendered = await getRenderedV1Prompt(
73 ideMessenger,
74 command,
75 "User input",
76 [],
77 );
78 expect(rendered).toBe("Content with URL Test file content\n\nUser input");
79 });
80
81 it("should handle supported providers", async () => {
82 ideMessenger.responseHandlers["context/getContextItems"] =
83 customGetContextItems;
84 const rawContent = "Tell me about {{{ diff }}} please";
85 const command = getTestPromptV1Command(rawContent);
86 const rendered = await getRenderedV1Prompt(
87 ideMessenger,
88 command,
89 "User input",
90 [],
91 );
92 expect(rendered).toBe(
93 "Tell me about Diff provider content please\n\nUser input",
94 );
95 });
96
97 it("should render file read errors within prompt", async () => {
98 // Mock console.error to prevent error output during test
99 const originalConsoleError = console.error;
100 console.error = vi.fn();
101
102 ideMessenger.responses["fileExists"] = false;
103 // Mock readFile to throw an error for nonexistent files
104 ideMessenger.responseHandlers["readFile"] = async (filepath) => {
105 throw new Error(`File not found: ${filepath}`);
106 };
107 const rawContent = "Content with provider {{{ nonexistent }}}";
108 const command = getTestPromptV1Command(rawContent);
109 const rendered = await getRenderedV1Prompt(
110 ideMessenger,
111 command,
112 "User input",
113 [],
114 );
115 expect(rendered).toBe(
116 `Content with provider [Error reading file "nonexistent"]\n\nUser input`,
117 );
118
119 // Restore console.error
120 console.error = originalConsoleError;
121 });
122
123 it("should handle multiple attachments", async () => {
124 ideMessenger.responseHandlers["context/getContextItems"] =
125 customGetContextItems;
126 ideMessenger.responses["readFile"] = "Test file content";
127 const rawContent = "Content with {{{ diff }}} and {{{ file.txt }}}";
128 const command = getTestPromptV1Command(rawContent);
129 const rendered = await getRenderedV1Prompt(
130 ideMessenger,
131 command,
132 "User input",
133 [],
134 );
135 expect(rendered).toBe(
136 "Content with Diff provider content and Test file content\n\nUser input",
137 );
138 });
139});