|
| 1 | +import { SYSTEM_PROMPT } from "../system" |
| 2 | +import { defaultModeSlug, modes } from "../../../shared/modes" |
| 3 | +import * as vscode from "vscode" |
| 4 | +import * as fs from "fs/promises" |
| 5 | + |
| 6 | +// Mock the fs/promises module |
| 7 | +jest.mock("fs/promises", () => ({ |
| 8 | + readFile: jest.fn(), |
| 9 | + mkdir: jest.fn().mockResolvedValue(undefined), |
| 10 | + access: jest.fn().mockResolvedValue(undefined), |
| 11 | +})) |
| 12 | + |
| 13 | +// Get the mocked fs module |
| 14 | +const mockedFs = fs as jest.Mocked<typeof fs> |
| 15 | + |
| 16 | +// Mock the fileExistsAtPath function |
| 17 | +jest.mock("../../../utils/fs", () => ({ |
| 18 | + fileExistsAtPath: jest.fn().mockResolvedValue(true), |
| 19 | + createDirectoriesForFile: jest.fn().mockResolvedValue([]), |
| 20 | +})) |
| 21 | + |
| 22 | +// Create a mock ExtensionContext with relative paths instead of absolute paths |
| 23 | +const mockContext = { |
| 24 | + extensionPath: "mock/extension/path", |
| 25 | + globalStoragePath: "mock/storage/path", |
| 26 | + storagePath: "mock/storage/path", |
| 27 | + logPath: "mock/log/path", |
| 28 | + subscriptions: [], |
| 29 | + workspaceState: { |
| 30 | + get: () => undefined, |
| 31 | + update: () => Promise.resolve(), |
| 32 | + }, |
| 33 | + globalState: { |
| 34 | + get: () => undefined, |
| 35 | + update: () => Promise.resolve(), |
| 36 | + setKeysForSync: () => {}, |
| 37 | + }, |
| 38 | + extensionUri: { fsPath: "mock/extension/path" }, |
| 39 | + globalStorageUri: { fsPath: "mock/settings/path" }, |
| 40 | + asAbsolutePath: (relativePath: string) => `mock/extension/path/${relativePath}`, |
| 41 | + extension: { |
| 42 | + packageJSON: { |
| 43 | + version: "1.0.0", |
| 44 | + }, |
| 45 | + }, |
| 46 | +} as unknown as vscode.ExtensionContext |
| 47 | + |
| 48 | +describe("File-Based Custom System Prompt", () => { |
| 49 | + const experiments = {} |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + // Reset mocks before each test |
| 53 | + jest.clearAllMocks() |
| 54 | + |
| 55 | + // Default behavior: file doesn't exist |
| 56 | + mockedFs.readFile.mockRejectedValue({ code: "ENOENT" }) |
| 57 | + }) |
| 58 | + |
| 59 | + it("should use default generation when no file-based system prompt is found", async () => { |
| 60 | + const customModePrompts = { |
| 61 | + [defaultModeSlug]: { |
| 62 | + roleDefinition: "Test role definition", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + const prompt = await SYSTEM_PROMPT( |
| 67 | + mockContext, |
| 68 | + "test/path", // Using a relative path without leading slash |
| 69 | + false, |
| 70 | + undefined, |
| 71 | + undefined, |
| 72 | + undefined, |
| 73 | + defaultModeSlug, |
| 74 | + customModePrompts, |
| 75 | + undefined, |
| 76 | + undefined, |
| 77 | + undefined, |
| 78 | + undefined, |
| 79 | + experiments, |
| 80 | + true, |
| 81 | + ) |
| 82 | + |
| 83 | + // Should contain default sections |
| 84 | + expect(prompt).toContain("TOOL USE") |
| 85 | + expect(prompt).toContain("CAPABILITIES") |
| 86 | + expect(prompt).toContain("MODES") |
| 87 | + expect(prompt).toContain("Test role definition") |
| 88 | + }) |
| 89 | + |
| 90 | + it("should use file-based custom system prompt when available", async () => { |
| 91 | + // Mock the readFile to return content from a file |
| 92 | + const fileCustomSystemPrompt = "Custom system prompt from file" |
| 93 | + // When called with utf-8 encoding, return a string |
| 94 | + mockedFs.readFile.mockImplementation((filePath, options) => { |
| 95 | + if (filePath.toString().includes(`.roo/system-prompt-${defaultModeSlug}`) && options === "utf-8") { |
| 96 | + return Promise.resolve(fileCustomSystemPrompt) |
| 97 | + } |
| 98 | + return Promise.reject({ code: "ENOENT" }) |
| 99 | + }) |
| 100 | + |
| 101 | + const prompt = await SYSTEM_PROMPT( |
| 102 | + mockContext, |
| 103 | + "test/path", // Using a relative path without leading slash |
| 104 | + false, |
| 105 | + undefined, |
| 106 | + undefined, |
| 107 | + undefined, |
| 108 | + defaultModeSlug, |
| 109 | + undefined, |
| 110 | + undefined, |
| 111 | + undefined, |
| 112 | + undefined, |
| 113 | + undefined, |
| 114 | + experiments, |
| 115 | + true, |
| 116 | + ) |
| 117 | + |
| 118 | + // Should contain role definition and file-based system prompt |
| 119 | + expect(prompt).toContain(modes[0].roleDefinition) |
| 120 | + expect(prompt).toContain(fileCustomSystemPrompt) |
| 121 | + |
| 122 | + // Should not contain any of the default sections |
| 123 | + expect(prompt).not.toContain("TOOL USE") |
| 124 | + expect(prompt).not.toContain("CAPABILITIES") |
| 125 | + expect(prompt).not.toContain("MODES") |
| 126 | + }) |
| 127 | + |
| 128 | + it("should combine file-based system prompt with role definition and custom instructions", async () => { |
| 129 | + // Mock the readFile to return content from a file |
| 130 | + const fileCustomSystemPrompt = "Custom system prompt from file" |
| 131 | + mockedFs.readFile.mockImplementation((filePath, options) => { |
| 132 | + if (filePath.toString().includes(`.roo/system-prompt-${defaultModeSlug}`) && options === "utf-8") { |
| 133 | + return Promise.resolve(fileCustomSystemPrompt) |
| 134 | + } |
| 135 | + return Promise.reject({ code: "ENOENT" }) |
| 136 | + }) |
| 137 | + |
| 138 | + // Define custom role definition |
| 139 | + const customRoleDefinition = "Custom role definition" |
| 140 | + const customModePrompts = { |
| 141 | + [defaultModeSlug]: { |
| 142 | + roleDefinition: customRoleDefinition, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + const prompt = await SYSTEM_PROMPT( |
| 147 | + mockContext, |
| 148 | + "test/path", // Using a relative path without leading slash |
| 149 | + false, |
| 150 | + undefined, |
| 151 | + undefined, |
| 152 | + undefined, |
| 153 | + defaultModeSlug, |
| 154 | + customModePrompts, |
| 155 | + undefined, |
| 156 | + undefined, |
| 157 | + undefined, |
| 158 | + undefined, |
| 159 | + experiments, |
| 160 | + true, |
| 161 | + ) |
| 162 | + |
| 163 | + // Should contain custom role definition and file-based system prompt |
| 164 | + expect(prompt).toContain(customRoleDefinition) |
| 165 | + expect(prompt).toContain(fileCustomSystemPrompt) |
| 166 | + |
| 167 | + // Should not contain any of the default sections |
| 168 | + expect(prompt).not.toContain("TOOL USE") |
| 169 | + expect(prompt).not.toContain("CAPABILITIES") |
| 170 | + expect(prompt).not.toContain("MODES") |
| 171 | + }) |
| 172 | +}) |
0 commit comments