| 1 | import { SlashCommandDescWithSource } from "core"; |
| 2 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | import { MockIdeMessenger } from "../../../../context/MockIdeMessenger"; |
| 4 | import { getPromptV2ContextRequests } from "./renderPromptv2"; |
| 5 | |
| 6 | function getTestPromptV2Command(body: string): SlashCommandDescWithSource { |
| 7 | return { |
| 8 | prompt: body, |
| 9 | name: "test-prompt-name", |
| 10 | description: "test-prompt-description", |
| 11 | isLegacy: false, |
| 12 | source: "prompt-file-v2", |
| 13 | }; |
| 14 | } |
| 15 | describe("getPromptV2ContextRequests", () => { |
| 16 | const ideMessenger = new MockIdeMessenger(); |
| 17 | const firstDir = ideMessenger.responses["getWorkspaceDirs"]?.[0] ?? ""; |
| 18 | beforeEach(async () => { |
| 19 | vi.clearAllMocks(); |
| 20 | ideMessenger.resetMocks(); |
| 21 | }); |
| 22 | |
| 23 | it("should render a prompt file with no attachments", async () => { |
| 24 | const rawContent = "Simple prompt with no attachments"; |
| 25 | const command = getTestPromptV2Command(rawContent); |
| 26 | const requests = await getPromptV2ContextRequests(ideMessenger, command); |
| 27 | expect(requests).toHaveLength(0); |
| 28 | }); |
| 29 | |
| 30 | it("should handle URL attachments", async () => { |
| 31 | const rawContent = "Content with URL @https://example.com"; |
| 32 | const command = getTestPromptV2Command(rawContent); |
| 33 | const requests = await getPromptV2ContextRequests(ideMessenger, command); |
| 34 | expect(requests).toHaveLength(1); |
| 35 | expect(requests[0].provider).toBe("url"); |
| 36 | expect(requests[0].query).toBe("https://example.com"); |
| 37 | }); |
| 38 | |
| 39 | it("should resolve file attachments", async () => { |
| 40 | const rawContent = "Content with file attachment @test.txt"; |
| 41 | const command = getTestPromptV2Command(rawContent); |
| 42 | const requests = await getPromptV2ContextRequests(ideMessenger, command); |
| 43 | |
| 44 | expect(requests).toHaveLength(1); |
| 45 | expect(requests[0].provider).toBe("file"); |
| 46 | expect(requests[0].query).toBe(`${firstDir}/test.txt`); |
| 47 | }); |
| 48 | |
| 49 | it("should handle nested prompt files", async () => { |
| 50 | ideMessenger.responses["readFile"] = "Contents @myfile.txt"; |
| 51 | const rawContent = "Main prompt with nested @test.prompt"; |
| 52 | const command = getTestPromptV2Command(rawContent); |
| 53 | const requests = await getPromptV2ContextRequests(ideMessenger, command); |
| 54 | expect(requests).toHaveLength(2); |
| 55 | expect(requests[0].provider).toBe("file"); |
| 56 | expect(requests[0].query).toBe(`${firstDir}/myfile.txt`); |
| 57 | expect(requests[1].provider).toBe("file"); |
| 58 | expect(requests[1].query).toBe(`${firstDir}/test.prompt`); |
| 59 | }); |
| 60 | |
| 61 | it("should ignore unsupported providers", async () => { |
| 62 | ideMessenger.responses["fileExists"] = false; |
| 63 | const rawContent = "Content with custom provider @unsupported"; |
| 64 | const command = getTestPromptV2Command(rawContent); |
| 65 | const requests = await getPromptV2ContextRequests(ideMessenger, command); |
| 66 | expect(requests).toHaveLength(0); |
| 67 | }); |
| 68 | |
| 69 | it("should handle multiple attachments", async () => { |
| 70 | const rawContent = "Content with @file1.txt and @file2.txt"; |
| 71 | const command = getTestPromptV2Command(rawContent); |
| 72 | const requests = await getPromptV2ContextRequests(ideMessenger, command); |
| 73 | |
| 74 | expect(requests).toHaveLength(2); |
| 75 | expect(requests[0].provider).toBe("file"); |
| 76 | expect(requests[0].query).toBe(`${firstDir}/file1.txt`); |
| 77 | expect(requests[1].provider).toBe("file"); |
| 78 | expect(requests[1].query).toBe(`${firstDir}/file2.txt`); |
| 79 | }); |
| 80 | }); |