| 1 | import { ContextProviderExtras, ILLM, RangeInFile } from "../.."; |
| 2 | import { testConfigHandler, testIde } from "../../test/fixtures"; |
| 3 | import { |
| 4 | addToTestDir, |
| 5 | setUpTestDir, |
| 6 | tearDownTestDir, |
| 7 | } from "../../test/testDir"; |
| 8 | import { renderPromptFileV2 } from "./renderPromptFile"; |
| 9 | |
| 10 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 11 | |
| 12 | vi.mock( |
| 13 | import("../../context/providers/URLContextProvider"), |
| 14 | async (importOriginal) => { |
| 15 | const actual = await importOriginal(); |
| 16 | return { |
| 17 | ...actual, |
| 18 | getUrlContextItems: vi.fn().mockResolvedValue([ |
| 19 | { |
| 20 | description: "https://example.com", |
| 21 | content: "Example Content", |
| 22 | name: "Example site", |
| 23 | uri: { |
| 24 | type: "url", |
| 25 | value: "https://example.com", |
| 26 | }, |
| 27 | }, |
| 28 | ]), |
| 29 | }; |
| 30 | }, |
| 31 | ); |
| 32 | |
| 33 | describe("renderPromptFileV2", () => { |
| 34 | let extras: ContextProviderExtras; |
| 35 | const mockGetContextItems = vi.fn().mockResolvedValue([ |
| 36 | { |
| 37 | name: "custom", |
| 38 | content: "Custom provider content", |
| 39 | description: "Custom provider", |
| 40 | }, |
| 41 | ]); |
| 42 | beforeEach(async () => { |
| 43 | const { config } = await testConfigHandler.loadConfig(); |
| 44 | |
| 45 | // Setup mock extras |
| 46 | extras = { |
| 47 | config: { |
| 48 | ...config!, |
| 49 | contextProviders: [ |
| 50 | { |
| 51 | description: { |
| 52 | type: "normal", |
| 53 | title: "CustomProvider", |
| 54 | displayTitle: "Custom Provider", |
| 55 | description: "A custom context provider", |
| 56 | }, |
| 57 | loadSubmenuItems: vi.fn().mockResolvedValue([]), |
| 58 | getContextItems: mockGetContextItems, |
| 59 | }, |
| 60 | ], |
| 61 | }, |
| 62 | fullInput: "User input", |
| 63 | embeddingsProvider: null, |
| 64 | reranker: null, |
| 65 | llm: {} as ILLM, |
| 66 | ide: testIde, |
| 67 | selectedCode: [] as RangeInFile[], |
| 68 | fetch: vi.fn(), |
| 69 | }; |
| 70 | |
| 71 | // Clear all mocks |
| 72 | vi.clearAllMocks(); |
| 73 | setUpTestDir(); |
| 74 | }); |
| 75 | |
| 76 | afterEach(() => { |
| 77 | tearDownTestDir(); |
| 78 | }); |
| 79 | |
| 80 | it("should render a prompt file with no attachments", async () => { |
| 81 | const rawContent = "Simple prompt with no attachments"; |
| 82 | const [contextItems, renderedPrompt] = await renderPromptFileV2( |
| 83 | rawContent, |
| 84 | extras, |
| 85 | ); |
| 86 | |
| 87 | expect(contextItems).toHaveLength(0); |
| 88 | expect(renderedPrompt).toBe( |
| 89 | "Simple prompt with no attachments\n\nUser input", |
| 90 | ); |
| 91 | }); |
| 92 | |
| 93 | it("should handle URL attachments", async () => { |
| 94 | const rawContent = "Content with URL @https://example.com"; |
| 95 | |
| 96 | const [contextItems, renderedPrompt] = await renderPromptFileV2( |
| 97 | rawContent, |
| 98 | extras, |
| 99 | ); |
| 100 | |
| 101 | expect(contextItems).toHaveLength(1); |
| 102 | expect(renderedPrompt).toContain("Example Content"); |
| 103 | }); |
| 104 | |
| 105 | it("should resolve file attachments", async () => { |
| 106 | const rawContent = "Content with file attachment @test.txt"; |
| 107 | |
| 108 | addToTestDir([["test.txt", "File content"]]); |
| 109 | |
| 110 | const [contextItems, renderedPrompt] = await renderPromptFileV2( |
| 111 | rawContent, |
| 112 | extras, |
| 113 | ); |
| 114 | |
| 115 | expect(contextItems).toHaveLength(1); |
| 116 | expect(renderedPrompt).toContain("```test.txt\nFile content\n```"); |
| 117 | expect(renderedPrompt).toContain("Content with file attachment @test.txt"); |
| 118 | }); |
| 119 | |
| 120 | it("should handle nested prompt files", async () => { |
| 121 | const rawContent = "Main prompt with nested @nested.prompt"; |
| 122 | addToTestDir([ |
| 123 | ["nested.prompt", "Nested prompt content with @CustomProvider"], |
| 124 | ]); |
| 125 | |
| 126 | const [contextItems, renderedPrompt] = await renderPromptFileV2( |
| 127 | rawContent, |
| 128 | extras, |
| 129 | ); |
| 130 | |
| 131 | expect(renderedPrompt).toContain("Custom provider content"); |
| 132 | }); |
| 133 | |
| 134 | it("should handle custom context providers", async () => { |
| 135 | const rawContent = "Content with custom provider @CustomProvider"; |
| 136 | |
| 137 | const [contextItems, renderedPrompt] = await renderPromptFileV2( |
| 138 | rawContent, |
| 139 | extras, |
| 140 | ); |
| 141 | |
| 142 | expect(contextItems).toHaveLength(1); |
| 143 | expect(renderedPrompt).toContain("Custom provider content"); |
| 144 | expect(mockGetContextItems).toHaveBeenCalled(); |
| 145 | }); |
| 146 | |
| 147 | it("should handle multiple attachments", async () => { |
| 148 | const rawContent = "Content with @file1.txt and @file2.txt"; |
| 149 | addToTestDir([ |
| 150 | ["file1.txt", "Content 1"], |
| 151 | ["file2.txt", "Content 2"], |
| 152 | ]); |
| 153 | |
| 154 | const [contextItems, renderedPrompt] = await renderPromptFileV2( |
| 155 | rawContent, |
| 156 | extras, |
| 157 | ); |
| 158 | |
| 159 | expect(contextItems).toHaveLength(2); |
| 160 | expect(renderedPrompt).toContain("```file1.txt\nContent 1\n```"); |
| 161 | expect(renderedPrompt).toContain("```file2.txt\nContent 2\n```"); |
| 162 | }); |
| 163 | |
| 164 | it("should handle failed attachment resolutions gracefully", async () => { |
| 165 | const rawContent = "Content with @nonexistent"; |
| 166 | |
| 167 | const [contextItems, renderedPrompt] = await renderPromptFileV2( |
| 168 | rawContent, |
| 169 | extras, |
| 170 | ); |
| 171 | |
| 172 | expect(contextItems).toHaveLength(0); |
| 173 | expect(renderedPrompt).toBe("Content with @nonexistent\n\nUser input"); |
| 174 | }); |
| 175 | }); |