forked from RooVetGit/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenhance-prompt.test.ts
142 lines (123 loc) · 4.29 KB
/
enhance-prompt.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { singleCompletionHandler } from "../single-completion-handler"
import { ApiConfiguration } from "../../shared/api"
import { buildApiHandler, SingleCompletionHandler } from "../../api"
import { supportPrompt } from "../../shared/support-prompt"
// Mock the API handler
jest.mock("../../api", () => ({
buildApiHandler: jest.fn(),
}))
describe("enhancePrompt", () => {
const mockApiConfig: ApiConfiguration = {
apiProvider: "openai",
openAiApiKey: "test-key",
openAiBaseUrl: "https://api.openai.com/v1",
}
beforeEach(() => {
jest.clearAllMocks()
// Mock the API handler with a completePrompt method
;(buildApiHandler as jest.Mock).mockReturnValue({
completePrompt: jest.fn((prompt, taskId, checkpointNumber) => Promise.resolve("Enhanced prompt")),
createMessage: jest.fn(),
getModel: jest.fn().mockReturnValue({
id: "test-model",
info: {
maxTokens: 4096,
contextWindow: 8192,
supportsPromptCache: false,
},
}),
} as unknown as SingleCompletionHandler)
})
it("enhances prompt using default enhancement prompt when no custom prompt provided", async () => {
const result = await singleCompletionHandler(mockApiConfig, "Test prompt")
expect(result).toBe("Enhanced prompt")
const handler = buildApiHandler(mockApiConfig)
expect((handler as any).completePrompt).toHaveBeenCalledWith(`Test prompt`, undefined, undefined)
})
it("enhances prompt using custom enhancement prompt when provided", async () => {
const customEnhancePrompt = "You are a custom prompt enhancer"
const customEnhancePromptWithTemplate = customEnhancePrompt + "\n\n${userInput}"
const result = await singleCompletionHandler(
mockApiConfig,
supportPrompt.create(
"ENHANCE",
{
userInput: "Test prompt",
},
{
ENHANCE: customEnhancePromptWithTemplate,
},
),
)
expect(result).toBe("Enhanced prompt")
const handler = buildApiHandler(mockApiConfig)
expect((handler as any).completePrompt).toHaveBeenCalledWith(
`${customEnhancePrompt}\n\nTest prompt`,
undefined,
undefined,
)
})
it("throws error for empty prompt input", async () => {
await expect(singleCompletionHandler(mockApiConfig, "")).rejects.toThrow("No prompt text provided")
})
it("throws error for missing API configuration", async () => {
await expect(singleCompletionHandler({} as ApiConfiguration, "Test prompt")).rejects.toThrow(
"No valid API configuration provided",
)
})
it("throws error for API provider that does not support prompt enhancement", async () => {
;(buildApiHandler as jest.Mock).mockReturnValue({
// No completePrompt method
createMessage: jest.fn(),
getModel: jest.fn().mockReturnValue({
id: "test-model",
info: {
maxTokens: 4096,
contextWindow: 8192,
supportsPromptCache: false,
},
}),
})
await expect(singleCompletionHandler(mockApiConfig, "Test prompt")).rejects.toThrow(
"The selected API provider does not support prompt enhancement",
)
})
it("uses appropriate model based on provider", async () => {
const openRouterConfig: ApiConfiguration = {
apiProvider: "openrouter",
openRouterApiKey: "test-key",
openRouterModelId: "test-model",
}
// Mock successful enhancement
;(buildApiHandler as jest.Mock).mockReturnValue({
completePrompt: jest.fn((prompt, taskId, checkpointNumber) => Promise.resolve("Enhanced prompt")),
createMessage: jest.fn(),
getModel: jest.fn().mockReturnValue({
id: "test-model",
info: {
maxTokens: 4096,
contextWindow: 8192,
supportsPromptCache: false,
},
}),
} as unknown as SingleCompletionHandler)
const result = await singleCompletionHandler(openRouterConfig, "Test prompt")
expect(buildApiHandler).toHaveBeenCalledWith(openRouterConfig)
expect(result).toBe("Enhanced prompt")
})
it("propagates API errors", async () => {
;(buildApiHandler as jest.Mock).mockReturnValue({
completePrompt: jest.fn((prompt, taskId, checkpointNumber) => Promise.reject(new Error("API Error"))),
createMessage: jest.fn(),
getModel: jest.fn().mockReturnValue({
id: "test-model",
info: {
maxTokens: 4096,
contextWindow: 8192,
supportsPromptCache: false,
},
}),
} as unknown as SingleCompletionHandler)
await expect(singleCompletionHandler(mockApiConfig, "Test prompt")).rejects.toThrow("API Error")
})
})