|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { QwenCodeHandler } from "../qwen-code" |
| 3 | +import { qwenCodeDefaultModelId, qwenCodeModels } from "@roo-code/types" |
| 4 | + |
| 5 | +// Mock fs module |
| 6 | +vi.mock("node:fs", () => ({ |
| 7 | + promises: { |
| 8 | + readFile: vi.fn(), |
| 9 | + writeFile: vi.fn(), |
| 10 | + }, |
| 11 | +})) |
| 12 | + |
| 13 | +// Mock fetch |
| 14 | +global.fetch = vi.fn() |
| 15 | + |
| 16 | +describe("QwenCodeHandler", () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks() |
| 19 | + }) |
| 20 | + |
| 21 | + describe("getModel", () => { |
| 22 | + it("should return default model when no model is specified", () => { |
| 23 | + const handler = new QwenCodeHandler({}) |
| 24 | + const { id, info } = handler.getModel() |
| 25 | + |
| 26 | + expect(id).toBe(qwenCodeDefaultModelId) |
| 27 | + expect(info).toEqual(qwenCodeModels[qwenCodeDefaultModelId]) |
| 28 | + }) |
| 29 | + |
| 30 | + it("should return specified model when valid model is provided", () => { |
| 31 | + const testModelId = "qwen3-coder-flash" |
| 32 | + const handler = new QwenCodeHandler({ |
| 33 | + apiModelId: testModelId, |
| 34 | + }) |
| 35 | + const { id, info } = handler.getModel() |
| 36 | + |
| 37 | + expect(id).toBe(testModelId) |
| 38 | + expect(info).toEqual(qwenCodeModels[testModelId]) |
| 39 | + }) |
| 40 | + |
| 41 | + it("should use default context window when qwenCodeMaxContextWindow is not set", () => { |
| 42 | + const handler = new QwenCodeHandler({}) |
| 43 | + const { info } = handler.getModel() |
| 44 | + |
| 45 | + expect(info.contextWindow).toBe(1_000_000) // Default context window |
| 46 | + }) |
| 47 | + |
| 48 | + it("should limit context window when qwenCodeMaxContextWindow is set", () => { |
| 49 | + const maxContextWindow = 256_000 |
| 50 | + const handler = new QwenCodeHandler({ |
| 51 | + qwenCodeMaxContextWindow: maxContextWindow, |
| 52 | + }) |
| 53 | + const { info } = handler.getModel() |
| 54 | + |
| 55 | + expect(info.contextWindow).toBe(maxContextWindow) |
| 56 | + }) |
| 57 | + |
| 58 | + it("should use original context window when qwenCodeMaxContextWindow is larger", () => { |
| 59 | + const handler = new QwenCodeHandler({ |
| 60 | + qwenCodeMaxContextWindow: 2_000_000, // Larger than default |
| 61 | + }) |
| 62 | + const { info } = handler.getModel() |
| 63 | + |
| 64 | + expect(info.contextWindow).toBe(1_000_000) // Should not exceed original |
| 65 | + }) |
| 66 | + |
| 67 | + it("should ignore qwenCodeMaxContextWindow when it's 0 or negative", () => { |
| 68 | + const handler1 = new QwenCodeHandler({ |
| 69 | + qwenCodeMaxContextWindow: 0, |
| 70 | + }) |
| 71 | + const { info: info1 } = handler1.getModel() |
| 72 | + |
| 73 | + expect(info1.contextWindow).toBe(1_000_000) // Should use default |
| 74 | + |
| 75 | + const handler2 = new QwenCodeHandler({ |
| 76 | + qwenCodeMaxContextWindow: -1, |
| 77 | + }) |
| 78 | + const { info: info2 } = handler2.getModel() |
| 79 | + |
| 80 | + expect(info2.contextWindow).toBe(1_000_000) // Should use default |
| 81 | + }) |
| 82 | + |
| 83 | + it("should apply context window limit to different models", () => { |
| 84 | + const maxContextWindow = 200_000 |
| 85 | + const handler = new QwenCodeHandler({ |
| 86 | + apiModelId: "qwen3-coder-flash", |
| 87 | + qwenCodeMaxContextWindow: maxContextWindow, |
| 88 | + }) |
| 89 | + const { id, info } = handler.getModel() |
| 90 | + |
| 91 | + expect(id).toBe("qwen3-coder-flash") |
| 92 | + expect(info.contextWindow).toBe(maxContextWindow) |
| 93 | + // Other properties should remain unchanged |
| 94 | + expect(info.maxTokens).toBe(qwenCodeModels["qwen3-coder-flash"].maxTokens) |
| 95 | + expect(info.description).toBe(qwenCodeModels["qwen3-coder-flash"].description) |
| 96 | + }) |
| 97 | + }) |
| 98 | +}) |
0 commit comments