|
| 1 | +// app/store/config.ts participates in an import cycle, so a static import can |
| 2 | +// hit a temporal-dead-zone error. Load it dynamically in beforeAll instead. |
| 3 | +let ModalConfigValidator: { |
| 4 | + model: (x: string) => string; |
| 5 | + max_tokens: (x: number) => number; |
| 6 | + presence_penalty: (x: number) => number; |
| 7 | + frequency_penalty: (x: number) => number; |
| 8 | + temperature: (x: number) => number; |
| 9 | + top_p: (x: number) => number; |
| 10 | +}; |
| 11 | + |
| 12 | +beforeAll(async () => { |
| 13 | + // Warm up the import cycle in a working order before loading the target. |
| 14 | + await import("../app/client/api"); |
| 15 | + ({ ModalConfigValidator } = await import("../app/store/config")); |
| 16 | +}); |
| 17 | + |
| 18 | +describe("ModalConfigValidator", () => { |
| 19 | + test("max_tokens clamps to [0, 512000] and defaults to 1024 on NaN", () => { |
| 20 | + expect(ModalConfigValidator.max_tokens(-5)).toBe(0); |
| 21 | + expect(ModalConfigValidator.max_tokens(600000)).toBe(512000); |
| 22 | + expect(ModalConfigValidator.max_tokens(2048)).toBe(2048); |
| 23 | + expect(ModalConfigValidator.max_tokens(NaN)).toBe(1024); |
| 24 | + }); |
| 25 | + |
| 26 | + test("temperature clamps to [0, 2]", () => { |
| 27 | + expect(ModalConfigValidator.temperature(-1)).toBe(0); |
| 28 | + expect(ModalConfigValidator.temperature(5)).toBe(2); |
| 29 | + expect(ModalConfigValidator.temperature(0.7)).toBe(0.7); |
| 30 | + }); |
| 31 | + |
| 32 | + test("top_p clamps to [0, 1]", () => { |
| 33 | + expect(ModalConfigValidator.top_p(2)).toBe(1); |
| 34 | + expect(ModalConfigValidator.top_p(-1)).toBe(0); |
| 35 | + expect(ModalConfigValidator.top_p(0.9)).toBe(0.9); |
| 36 | + }); |
| 37 | + |
| 38 | + test("presence_penalty and frequency_penalty clamp to [-2, 2]", () => { |
| 39 | + expect(ModalConfigValidator.presence_penalty(-9)).toBe(-2); |
| 40 | + expect(ModalConfigValidator.presence_penalty(9)).toBe(2); |
| 41 | + expect(ModalConfigValidator.frequency_penalty(-9)).toBe(-2); |
| 42 | + expect(ModalConfigValidator.frequency_penalty(9)).toBe(2); |
| 43 | + }); |
| 44 | + |
| 45 | + test("model passes the value through unchanged", () => { |
| 46 | + expect(ModalConfigValidator.model("gpt-4")).toBe("gpt-4"); |
| 47 | + }); |
| 48 | +}); |
0 commit comments