|
| 1 | +import type { NodeContext } from "@dafthunk/runtime"; |
| 2 | +import type { Node } from "@dafthunk/types"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { KimiK25Node } from "./kimi-k2-5-node"; |
| 5 | + |
| 6 | +describe("KimiK25Node", () => { |
| 7 | + const createContext = ( |
| 8 | + inputs: Record<string, unknown>, |
| 9 | + aiResponse?: Record<string, unknown> |
| 10 | + ): NodeContext => { |
| 11 | + const mockAI = { |
| 12 | + run: async () => |
| 13 | + aiResponse ?? { |
| 14 | + choices: [ |
| 15 | + { |
| 16 | + message: { |
| 17 | + content: "Hello! How can I help you?", |
| 18 | + reasoning_content: "The user greeted me.", |
| 19 | + }, |
| 20 | + }, |
| 21 | + ], |
| 22 | + usage: { |
| 23 | + prompt_tokens: 10, |
| 24 | + completion_tokens: 20, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }; |
| 28 | + |
| 29 | + return { |
| 30 | + nodeId: "kimi-k2-5", |
| 31 | + inputs, |
| 32 | + getIntegration: async () => { |
| 33 | + throw new Error("No integrations in test"); |
| 34 | + }, |
| 35 | + env: { |
| 36 | + AI: mockAI, |
| 37 | + AI_OPTIONS: {}, |
| 38 | + }, |
| 39 | + } as unknown as NodeContext; |
| 40 | + }; |
| 41 | + |
| 42 | + it("should have correct node type metadata", () => { |
| 43 | + expect(KimiK25Node.nodeType.id).toBe("kimi-k2-5"); |
| 44 | + expect(KimiK25Node.nodeType.name).toBe("Kimi K2.5"); |
| 45 | + expect(KimiK25Node.nodeType.icon).toBe("sparkles"); |
| 46 | + expect(KimiK25Node.nodeType.functionCalling).toBe(true); |
| 47 | + expect(KimiK25Node.nodeType.usage).toBe(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should generate text from a prompt", async () => { |
| 51 | + const node = new KimiK25Node({ |
| 52 | + nodeId: "kimi-k2-5", |
| 53 | + } as unknown as Node); |
| 54 | + const result = await node.execute(createContext({ prompt: "Hello" })); |
| 55 | + |
| 56 | + expect(result.status).toBe("completed"); |
| 57 | + expect(result.outputs?.response).toBe("Hello! How can I help you?"); |
| 58 | + expect(result.outputs?.reasoning).toBe("The user greeted me."); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should generate text from messages JSON", async () => { |
| 62 | + const messages = JSON.stringify([ |
| 63 | + { role: "user", content: "What is 2+2?" }, |
| 64 | + ]); |
| 65 | + const node = new KimiK25Node({ |
| 66 | + nodeId: "kimi-k2-5", |
| 67 | + } as unknown as Node); |
| 68 | + const result = await node.execute( |
| 69 | + createContext({ prompt: "fallback", messages }) |
| 70 | + ); |
| 71 | + |
| 72 | + expect(result.status).toBe("completed"); |
| 73 | + expect(result.outputs?.response).toBe("Hello! How can I help you?"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should fall back to prompt when messages JSON is invalid", async () => { |
| 77 | + const node = new KimiK25Node({ |
| 78 | + nodeId: "kimi-k2-5", |
| 79 | + } as unknown as Node); |
| 80 | + const result = await node.execute( |
| 81 | + createContext({ prompt: "Hello", messages: "not-valid-json" }) |
| 82 | + ); |
| 83 | + |
| 84 | + expect(result.status).toBe("completed"); |
| 85 | + expect(result.outputs?.response).toBe("Hello! How can I help you?"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should return error when AI service is not available", async () => { |
| 89 | + const node = new KimiK25Node({ |
| 90 | + nodeId: "kimi-k2-5", |
| 91 | + } as unknown as Node); |
| 92 | + const context = { |
| 93 | + nodeId: "kimi-k2-5", |
| 94 | + inputs: { prompt: "Hello" }, |
| 95 | + getIntegration: async () => { |
| 96 | + throw new Error("No integrations in test"); |
| 97 | + }, |
| 98 | + env: {}, |
| 99 | + } as unknown as NodeContext; |
| 100 | + |
| 101 | + const result = await node.execute(context); |
| 102 | + expect(result.status).toBe("error"); |
| 103 | + expect(result.error).toContain("AI service is not available"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should return error when neither prompt nor messages provided", async () => { |
| 107 | + const node = new KimiK25Node({ |
| 108 | + nodeId: "kimi-k2-5", |
| 109 | + } as unknown as Node); |
| 110 | + const result = await node.execute(createContext({})); |
| 111 | + |
| 112 | + expect(result.status).toBe("error"); |
| 113 | + expect(result.error).toContain( |
| 114 | + "Either prompt or messages must be provided" |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should handle response without reasoning content", async () => { |
| 119 | + const node = new KimiK25Node({ |
| 120 | + nodeId: "kimi-k2-5", |
| 121 | + } as unknown as Node); |
| 122 | + const result = await node.execute( |
| 123 | + createContext( |
| 124 | + { prompt: "Hello" }, |
| 125 | + { |
| 126 | + choices: [ |
| 127 | + { |
| 128 | + message: { |
| 129 | + content: "Hi there!", |
| 130 | + }, |
| 131 | + }, |
| 132 | + ], |
| 133 | + usage: { |
| 134 | + prompt_tokens: 5, |
| 135 | + completion_tokens: 10, |
| 136 | + }, |
| 137 | + } |
| 138 | + ) |
| 139 | + ); |
| 140 | + |
| 141 | + expect(result.status).toBe("completed"); |
| 142 | + expect(result.outputs?.response).toBe("Hi there!"); |
| 143 | + expect(result.outputs?.reasoning).toBeUndefined(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should handle empty response from model", async () => { |
| 147 | + const node = new KimiK25Node({ |
| 148 | + nodeId: "kimi-k2-5", |
| 149 | + } as unknown as Node); |
| 150 | + const result = await node.execute( |
| 151 | + createContext( |
| 152 | + { prompt: "Hello" }, |
| 153 | + { |
| 154 | + choices: [{ message: {} }], |
| 155 | + usage: { |
| 156 | + prompt_tokens: 5, |
| 157 | + completion_tokens: 0, |
| 158 | + }, |
| 159 | + } |
| 160 | + ) |
| 161 | + ); |
| 162 | + |
| 163 | + expect(result.status).toBe("completed"); |
| 164 | + expect(result.outputs?.response).toBe(""); |
| 165 | + }); |
| 166 | + |
| 167 | + it("should handle AI.run throwing an error", async () => { |
| 168 | + const node = new KimiK25Node({ |
| 169 | + nodeId: "kimi-k2-5", |
| 170 | + } as unknown as Node); |
| 171 | + const context = { |
| 172 | + nodeId: "kimi-k2-5", |
| 173 | + inputs: { prompt: "Hello" }, |
| 174 | + getIntegration: async () => { |
| 175 | + throw new Error("No integrations in test"); |
| 176 | + }, |
| 177 | + env: { |
| 178 | + AI: { |
| 179 | + run: async () => { |
| 180 | + throw new Error("Model inference failed"); |
| 181 | + }, |
| 182 | + }, |
| 183 | + AI_OPTIONS: {}, |
| 184 | + }, |
| 185 | + } as unknown as NodeContext; |
| 186 | + |
| 187 | + const result = await node.execute(context); |
| 188 | + expect(result.status).toBe("error"); |
| 189 | + expect(result.error).toContain("Model inference failed"); |
| 190 | + }); |
| 191 | + |
| 192 | + it("should fall back to text estimation when usage is not available", async () => { |
| 193 | + const node = new KimiK25Node({ |
| 194 | + nodeId: "kimi-k2-5", |
| 195 | + } as unknown as Node); |
| 196 | + const result = await node.execute( |
| 197 | + createContext( |
| 198 | + { prompt: "Hello" }, |
| 199 | + { |
| 200 | + choices: [ |
| 201 | + { |
| 202 | + message: { |
| 203 | + content: "Response text", |
| 204 | + }, |
| 205 | + }, |
| 206 | + ], |
| 207 | + } |
| 208 | + ) |
| 209 | + ); |
| 210 | + |
| 211 | + expect(result.status).toBe("completed"); |
| 212 | + expect(result.outputs?.response).toBe("Response text"); |
| 213 | + expect(result.usage).toBeGreaterThanOrEqual(1); |
| 214 | + }); |
| 215 | +}); |
0 commit comments