5875
Os
ollama-space-molt-player / files
ollama-space-molt-player / packages / client / tests / response-summarizer.test.ts
7.5 KB
1import { describe, expect, test } from "bun:test"; 2import { summarizeToolResult } from "../src/response-summarizer.ts"; 3 4describe("Response Summarizer", () => { 5 describe("Error handling", () => { 6 test("should preserve error messages in full", () => { 7 const errorContent = { error: "not_docked", message: "You must be docked to do this" }; 8 const result = summarizeToolResult("dock", errorContent, false); 9 expect(result).toEqual(errorContent); 10 }); 11 }); 12 13 describe("get_status summarization", () => { 14 test("should extract essential player and ship info", () => { 15 const fullStatus = { 16 player: { 17 id: "abc123", 18 username: "TestPlayer", 19 empire: "solarian", 20 credits: 1000, 21 created_at: "2026-01-01T00:00:00Z", 22 last_login_at: "2026-02-03T12:00:00Z", 23 last_active_at: "2026-02-03T12:00:00Z", 24 status_message: "", 25 clan_tag: "", 26 primary_color: "#FFFFFF", 27 secondary_color: "#000000", 28 anonymous: false, 29 is_cloaked: false, 30 current_ship_id: "ship123", 31 current_system: "sol", 32 current_poi: "sol_station", 33 docked_at_base: "sol_base", 34 home_base: "sol_base", 35 skills: {}, 36 skill_xp: {}, 37 experience: 0, 38 stats: { 39 credits_earned: 0, 40 credits_spent: 0, 41 ships_destroyed: 0, 42 }, 43 discovered_systems: { 44 sol: { system_id: "sol", discovered_at: "2026-01-01" }, 45 }, 46 }, 47 ship: { 48 id: "ship123", 49 owner_id: "abc123", 50 class_id: "starter_mining", 51 name: "Prospector", 52 created_at: "2026-01-01T00:00:00Z", 53 hull: 100, 54 max_hull: 100, 55 shield: 50, 56 max_shield: 50, 57 shield_recharge: 1, 58 armor: 5, 59 speed: 2, 60 fuel: 100, 61 max_fuel: 100, 62 cargo_used: 10, 63 cargo_capacity: 50, 64 cpu_used: 2, 65 cpu_capacity: 12, 66 power_used: 5, 67 power_capacity: 25, 68 weapon_slots: 1, 69 defense_slots: 1, 70 utility_slots: 2, 71 modules: ["mod1", "mod2"], 72 cargo: [], 73 }, 74 }; 75 76 const result = summarizeToolResult("get_status", fullStatus, true) as { 77 player: Record<string, unknown>; 78 ship: Record<string, unknown>; 79 }; 80 81 expect(result.player.username).toBe("TestPlayer"); 82 expect(result.player.credits).toBe(1000); 83 expect(result.player.current_system).toBe("sol"); 84 expect(result.player.current_poi).toBe("sol_station"); 85 expect(result.player.docked_at_base).toBe("sol_base"); 86 87 expect(result.player.created_at).toBeUndefined(); 88 expect(result.player.stats).toBeUndefined(); 89 expect(result.player.discovered_systems).toBeUndefined(); 90 expect(result.player.primary_color).toBeUndefined(); 91 92 expect(result.ship.name).toBe("Prospector"); 93 expect(result.ship.hull).toBe(100); 94 expect(result.ship.max_hull).toBe(100); 95 expect(result.ship.fuel).toBe(100); 96 expect(result.ship.max_fuel).toBe(100); 97 expect(result.ship.cargo_used).toBe(10); 98 expect(result.ship.cargo_capacity).toBe(50); 99 expect(result.ship.modules).toEqual(["mod1", "mod2"]); 100 101 expect(result.ship.created_at).toBeUndefined(); 102 }); 103 }); 104 105 describe("get_nearby summarization", () => { 106 test("should limit nearby players and extract key fields", () => { 107 const nearby = Array.from({ length: 20 }, (_, i) => ({ 108 player_id: `player${i}`, 109 username: `User${i}`, 110 anonymous: false, 111 clan_tag: "", 112 in_combat: i % 3 === 0, 113 primary_color: "#FFFFFF", 114 secondary_color: "#000000", 115 ship_class: "starter_mining", 116 status_message: "", 117 })); 118 119 const fullResponse = { count: 20, nearby }; 120 121 const result = summarizeToolResult("get_nearby", fullResponse, true) as { 122 count: number; 123 nearby: Array<Record<string, unknown>>; 124 }; 125 126 expect(result.count).toBe(20); 127 128 expect(result.nearby.length).toBe(10); 129 130 expect(result.nearby[0].username).toBe("User0"); 131 expect(result.nearby[0].player_id).toBe("player0"); 132 expect(result.nearby[0].ship_class).toBe("starter_mining"); 133 expect(result.nearby[0].in_combat).toBe(true); 134 135 expect(result.nearby[0].primary_color).toBeUndefined(); 136 expect(result.nearby[0].secondary_color).toBeUndefined(); 137 expect(result.nearby[0].status_message).toBeUndefined(); 138 }); 139 }); 140 141 describe("travel summarization", () => { 142 test("should extract travel outcome", () => { 143 const fullResponse = { 144 action: "travel", 145 ticks: 3, 146 destination: "sol_belt", 147 message: "Traveling to Asteroid Belt", 148 player: {}, 149 ship: {}, 150 }; 151 152 const result = summarizeToolResult("travel", fullResponse, true) as Record<string, unknown>; 153 154 expect(result.action).toBe("travel"); 155 expect(result.ticks).toBe(3); 156 expect(result.destination).toBe("sol_belt"); 157 expect(result.message).toBe("Traveling to Asteroid Belt"); 158 159 expect(result.player).toBeUndefined(); 160 expect(result.ship).toBeUndefined(); 161 }); 162 }); 163 164 describe("generic summarization", () => { 165 test("should remove skip fields from unknown tools", () => { 166 const fullResponse = { 167 message: "Action completed", 168 created_at: "2026-02-03T12:00:00Z", 169 timestamp: 1234567890, 170 primary_color: "#FFFFFF", 171 important_data: "keep this", 172 }; 173 174 const result = summarizeToolResult("unknown_tool", fullResponse, true) as Record< 175 string, 176 unknown 177 >; 178 179 expect(result.message).toBe("Action completed"); 180 expect(result.important_data).toBe("keep this"); 181 expect(result.created_at).toBeUndefined(); 182 expect(result.timestamp).toBeUndefined(); 183 expect(result.primary_color).toBeUndefined(); 184 }); 185 186 test("should truncate arrays in generic summarization", () => { 187 const fullResponse = { 188 items: Array.from({ length: 20 }, (_, i) => ({ id: i, name: `Item${i}` })), 189 }; 190 191 const result = summarizeToolResult("unknown_tool", fullResponse, true) as { 192 items: Array<{ id: number; name: string }>; 193 }; 194 195 expect(result.items.length).toBe(5); 196 expect(result.items[0].name).toBe("Item0"); 197 }); 198 }); 199 200 describe("register summarization", () => { 201 test("should extract essential registration info", () => { 202 const fullResponse = { 203 message: "Registration successful!", 204 player_id: "abc123", 205 player: { 206 id: "abc123", 207 username: "NewPlayer", 208 empire: "solarian", 209 credits: 100, 210 created_at: "2026-02-03T12:00:00Z", 211 current_system: "sol", 212 current_poi: "sol_station", 213 docked_at_base: "sol_base", 214 }, 215 ship: { 216 id: "ship123", 217 name: "Prospector", 218 class_id: "starter_mining", 219 hull: 100, 220 max_hull: 100, 221 fuel: 100, 222 max_fuel: 100, 223 }, 224 system: { 225 id: "sol", 226 name: "Sol", 227 description: "Long description here...", 228 connections: ["alpha_centauri", "barnard"], 229 pois: ["sol_station", "sol_belt"], 230 }, 231 poi: { 232 id: "sol_station", 233 name: "Sol Central", 234 type: "station", 235 base_id: "sol_base", 236 description: "Long description...", 237 }, 238 token: "secret_token_123", 239 }; 240 241 const result = summarizeToolResult("register", fullResponse, true) as Record<string, unknown>; 242 243 expect(result.message).toBe("Registration successful!"); 244 expect(result.player_id).toBe("abc123"); 245 expect(result.token).toBe("secret_token_123"); 246 247 const player = result.player as Record<string, unknown>; 248 expect(player.username).toBe("NewPlayer"); 249 expect(player.empire).toBe("solarian"); 250 expect(player.current_system).toBe("sol"); 251 252 const ship = result.ship as Record<string, unknown>; 253 expect(ship.name).toBe("Prospector"); 254 expect(ship.hull).toBe(100); 255 256 const system = result.system as Record<string, unknown>; 257 expect(system.id).toBe("sol"); 258 expect(system.name).toBe("Sol"); 259 expect(system.connections).toEqual(["alpha_centauri", "barnard"]); 260 expect(system.description).toBeUndefined(); 261 }); 262 }); 263});