|
| 1 | +import type { ClineMessage } from "@roo-code/types" |
| 2 | +import { collectContextSummary, formatContextSummaryForParent } from "../collectContextSummary" |
| 3 | + |
| 4 | +describe("collectContextSummary", () => { |
| 5 | + it("extracts files modified from tool messages", () => { |
| 6 | + const messages: ClineMessage[] = [ |
| 7 | + { |
| 8 | + ts: 1, |
| 9 | + type: "ask", |
| 10 | + ask: "tool", |
| 11 | + text: JSON.stringify({ tool: "editedExistingFile", path: "src/app.ts" }), |
| 12 | + }, |
| 13 | + { |
| 14 | + ts: 2, |
| 15 | + type: "ask", |
| 16 | + ask: "tool", |
| 17 | + text: JSON.stringify({ tool: "newFileCreated", path: "src/utils.ts" }), |
| 18 | + }, |
| 19 | + ] |
| 20 | + |
| 21 | + const summary = collectContextSummary(messages, "code", "Done") |
| 22 | + expect(summary.filesModified).toEqual(["src/app.ts", "src/utils.ts"]) |
| 23 | + expect(summary.mode).toBe("code") |
| 24 | + expect(summary.result).toBe("Done") |
| 25 | + }) |
| 26 | + |
| 27 | + it("extracts files read from tool messages", () => { |
| 28 | + const messages: ClineMessage[] = [ |
| 29 | + { ts: 1, type: "ask", ask: "tool", text: JSON.stringify({ tool: "readFile", path: "src/config.ts" }) }, |
| 30 | + ] |
| 31 | + |
| 32 | + const summary = collectContextSummary(messages, "code", "Done") |
| 33 | + expect(summary.filesRead).toEqual(["src/config.ts"]) |
| 34 | + }) |
| 35 | + |
| 36 | + it("removes files from filesRead if they were also modified", () => { |
| 37 | + const messages: ClineMessage[] = [ |
| 38 | + { ts: 1, type: "ask", ask: "tool", text: JSON.stringify({ tool: "readFile", path: "src/app.ts" }) }, |
| 39 | + { |
| 40 | + ts: 2, |
| 41 | + type: "ask", |
| 42 | + ask: "tool", |
| 43 | + text: JSON.stringify({ tool: "editedExistingFile", path: "src/app.ts" }), |
| 44 | + }, |
| 45 | + ] |
| 46 | + |
| 47 | + const summary = collectContextSummary(messages, "code", "Done") |
| 48 | + expect(summary.filesModified).toEqual(["src/app.ts"]) |
| 49 | + expect(summary.filesRead).toEqual([]) |
| 50 | + }) |
| 51 | + |
| 52 | + it("extracts executed commands", () => { |
| 53 | + const messages: ClineMessage[] = [ |
| 54 | + { ts: 1, type: "ask", ask: "command", text: "npm test" }, |
| 55 | + { ts: 2, type: "ask", ask: "command", text: "npm run build" }, |
| 56 | + ] |
| 57 | + |
| 58 | + const summary = collectContextSummary(messages, "code", "Done") |
| 59 | + expect(summary.commandsExecuted).toEqual(["npm test", "npm run build"]) |
| 60 | + expect(summary.toolUsageCounts["execute_command"]).toBe(2) |
| 61 | + }) |
| 62 | + |
| 63 | + it("counts API requests", () => { |
| 64 | + const messages: ClineMessage[] = [ |
| 65 | + { ts: 1, type: "say", say: "api_req_started" }, |
| 66 | + { ts: 2, type: "say", say: "api_req_started" }, |
| 67 | + { ts: 3, type: "say", say: "api_req_started" }, |
| 68 | + ] |
| 69 | + |
| 70 | + const summary = collectContextSummary(messages, "debug", "Fixed it") |
| 71 | + expect(summary.apiRequestCount).toBe(3) |
| 72 | + }) |
| 73 | + |
| 74 | + it("counts tool usage correctly", () => { |
| 75 | + const messages: ClineMessage[] = [ |
| 76 | + { ts: 1, type: "ask", ask: "tool", text: JSON.stringify({ tool: "readFile", path: "a.ts" }) }, |
| 77 | + { ts: 2, type: "ask", ask: "tool", text: JSON.stringify({ tool: "readFile", path: "b.ts" }) }, |
| 78 | + { |
| 79 | + ts: 3, |
| 80 | + type: "ask", |
| 81 | + ask: "tool", |
| 82 | + text: JSON.stringify({ tool: "editedExistingFile", path: "c.ts" }), |
| 83 | + }, |
| 84 | + { ts: 4, type: "ask", ask: "tool", text: JSON.stringify({ tool: "searchFiles" }) }, |
| 85 | + ] |
| 86 | + |
| 87 | + const summary = collectContextSummary(messages, "code", "Done") |
| 88 | + expect(summary.toolUsageCounts["read_file"]).toBe(2) |
| 89 | + expect(summary.toolUsageCounts["write_to_file"]).toBe(1) |
| 90 | + expect(summary.toolUsageCounts["search_files"]).toBe(1) |
| 91 | + }) |
| 92 | + |
| 93 | + it("deduplicates modified files", () => { |
| 94 | + const messages: ClineMessage[] = [ |
| 95 | + { |
| 96 | + ts: 1, |
| 97 | + type: "ask", |
| 98 | + ask: "tool", |
| 99 | + text: JSON.stringify({ tool: "editedExistingFile", path: "src/app.ts" }), |
| 100 | + }, |
| 101 | + { |
| 102 | + ts: 2, |
| 103 | + type: "ask", |
| 104 | + ask: "tool", |
| 105 | + text: JSON.stringify({ tool: "editedExistingFile", path: "src/app.ts" }), |
| 106 | + }, |
| 107 | + ] |
| 108 | + |
| 109 | + const summary = collectContextSummary(messages, "code", "Done") |
| 110 | + expect(summary.filesModified).toEqual(["src/app.ts"]) |
| 111 | + }) |
| 112 | + |
| 113 | + it("handles empty messages array", () => { |
| 114 | + const summary = collectContextSummary([], "code", "Nothing done") |
| 115 | + expect(summary.filesModified).toEqual([]) |
| 116 | + expect(summary.filesRead).toEqual([]) |
| 117 | + expect(summary.commandsExecuted).toEqual([]) |
| 118 | + expect(summary.apiRequestCount).toBe(0) |
| 119 | + expect(summary.result).toBe("Nothing done") |
| 120 | + }) |
| 121 | + |
| 122 | + it("handles malformed tool JSON gracefully", () => { |
| 123 | + const messages: ClineMessage[] = [ |
| 124 | + { ts: 1, type: "ask", ask: "tool", text: "not valid json" }, |
| 125 | + { ts: 2, type: "ask", ask: "tool", text: undefined }, |
| 126 | + ] |
| 127 | + |
| 128 | + // Should not throw |
| 129 | + const summary = collectContextSummary(messages, "code", "Done") |
| 130 | + expect(summary.filesModified).toEqual([]) |
| 131 | + }) |
| 132 | + |
| 133 | + it("sorts files alphabetically", () => { |
| 134 | + const messages: ClineMessage[] = [ |
| 135 | + { |
| 136 | + ts: 1, |
| 137 | + type: "ask", |
| 138 | + ask: "tool", |
| 139 | + text: JSON.stringify({ tool: "editedExistingFile", path: "z-file.ts" }), |
| 140 | + }, |
| 141 | + { |
| 142 | + ts: 2, |
| 143 | + type: "ask", |
| 144 | + ask: "tool", |
| 145 | + text: JSON.stringify({ tool: "editedExistingFile", path: "a-file.ts" }), |
| 146 | + }, |
| 147 | + ] |
| 148 | + |
| 149 | + const summary = collectContextSummary(messages, "code", "Done") |
| 150 | + expect(summary.filesModified).toEqual(["a-file.ts", "z-file.ts"]) |
| 151 | + }) |
| 152 | +}) |
| 153 | + |
| 154 | +describe("formatContextSummaryForParent", () => { |
| 155 | + it("formats a complete summary into readable text", () => { |
| 156 | + const summary = { |
| 157 | + mode: "code", |
| 158 | + filesModified: ["src/app.ts"], |
| 159 | + filesRead: ["src/config.ts"], |
| 160 | + commandsExecuted: ["npm test"], |
| 161 | + toolUsageCounts: { write_to_file: 1, read_file: 1 }, |
| 162 | + apiRequestCount: 3, |
| 163 | + result: "Task completed", |
| 164 | + } |
| 165 | + |
| 166 | + const formatted = formatContextSummaryForParent(summary) |
| 167 | + expect(formatted).toContain("Result:\nTask completed") |
| 168 | + expect(formatted).toContain("Mode: code") |
| 169 | + expect(formatted).toContain("Files Modified:") |
| 170 | + expect(formatted).toContain("src/app.ts") |
| 171 | + expect(formatted).toContain("Files Read:") |
| 172 | + expect(formatted).toContain("src/config.ts") |
| 173 | + expect(formatted).toContain("Commands Executed:") |
| 174 | + expect(formatted).toContain("npm test") |
| 175 | + expect(formatted).toContain("Tool Usage:") |
| 176 | + expect(formatted).toContain("API Requests: 3") |
| 177 | + }) |
| 178 | + |
| 179 | + it("omits empty sections", () => { |
| 180 | + const summary = { |
| 181 | + mode: undefined, |
| 182 | + filesModified: [], |
| 183 | + filesRead: [], |
| 184 | + commandsExecuted: [], |
| 185 | + toolUsageCounts: {}, |
| 186 | + apiRequestCount: 0, |
| 187 | + result: "Done", |
| 188 | + } |
| 189 | + |
| 190 | + const formatted = formatContextSummaryForParent(summary) |
| 191 | + expect(formatted).toContain("Result:\nDone") |
| 192 | + expect(formatted).not.toContain("Files Modified:") |
| 193 | + expect(formatted).not.toContain("Files Read:") |
| 194 | + expect(formatted).not.toContain("Commands Executed:") |
| 195 | + expect(formatted).not.toContain("Tool Usage:") |
| 196 | + expect(formatted).toContain("API Requests: 0") |
| 197 | + }) |
| 198 | +}) |
0 commit comments