Skip to content

Commit bafc6c1

Browse files
feat: add tests for addMaxTokensIfNeeded method in MoonshotHandler
1 parent ad4943f commit bafc6c1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/api/providers/__tests__/moonshot.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,66 @@ describe("MoonshotHandler", () => {
294294
expect(result.cacheReadTokens).toBeUndefined()
295295
})
296296
})
297+
298+
describe("addMaxTokensIfNeeded", () => {
299+
it("should always add max_tokens regardless of includeMaxTokens option", () => {
300+
// Create a test subclass to access the protected method
301+
class TestMoonshotHandler extends MoonshotHandler {
302+
public testAddMaxTokensIfNeeded(requestOptions: any, modelInfo: any) {
303+
this.addMaxTokensIfNeeded(requestOptions, modelInfo)
304+
}
305+
}
306+
307+
const testHandler = new TestMoonshotHandler(mockOptions)
308+
const requestOptions: any = {}
309+
const modelInfo = {
310+
maxTokens: 32_000,
311+
}
312+
313+
// Test with includeMaxTokens set to false - should still add max tokens
314+
testHandler.testAddMaxTokensIfNeeded(requestOptions, modelInfo)
315+
316+
expect(requestOptions.max_tokens).toBe(32_000)
317+
})
318+
319+
it("should use modelMaxTokens when provided", () => {
320+
class TestMoonshotHandler extends MoonshotHandler {
321+
public testAddMaxTokensIfNeeded(requestOptions: any, modelInfo: any) {
322+
this.addMaxTokensIfNeeded(requestOptions, modelInfo)
323+
}
324+
}
325+
326+
const customMaxTokens = 5000
327+
const testHandler = new TestMoonshotHandler({
328+
...mockOptions,
329+
modelMaxTokens: customMaxTokens,
330+
})
331+
const requestOptions: any = {}
332+
const modelInfo = {
333+
maxTokens: 32_000,
334+
}
335+
336+
testHandler.testAddMaxTokensIfNeeded(requestOptions, modelInfo)
337+
338+
expect(requestOptions.max_tokens).toBe(customMaxTokens)
339+
})
340+
341+
it("should fall back to modelInfo.maxTokens when modelMaxTokens is not provided", () => {
342+
class TestMoonshotHandler extends MoonshotHandler {
343+
public testAddMaxTokensIfNeeded(requestOptions: any, modelInfo: any) {
344+
this.addMaxTokensIfNeeded(requestOptions, modelInfo)
345+
}
346+
}
347+
348+
const testHandler = new TestMoonshotHandler(mockOptions)
349+
const requestOptions: any = {}
350+
const modelInfo = {
351+
maxTokens: 16_000,
352+
}
353+
354+
testHandler.testAddMaxTokensIfNeeded(requestOptions, modelInfo)
355+
356+
expect(requestOptions.max_tokens).toBe(16_000)
357+
})
358+
})
297359
})

0 commit comments

Comments
 (0)