|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + estimateTokens, |
| 4 | + estimateMessagesTokens, |
| 5 | + planCompaction, |
| 6 | + shouldCompact, |
| 7 | + buildSummarizationPrompt, |
| 8 | +} from "./context-compaction"; |
| 9 | +import type { ChatMessage } from "@/types/electron"; |
| 10 | + |
| 11 | +function msg(role: ChatMessage["role"], content: string): ChatMessage { |
| 12 | + return { role, content }; |
| 13 | +} |
| 14 | + |
| 15 | +describe("estimateTokens", () => { |
| 16 | + it("scales roughly with character count", () => { |
| 17 | + expect(estimateTokens("")).toBe(0); |
| 18 | + expect(estimateTokens("a".repeat(400))).toBe(100); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("estimateMessagesTokens", () => { |
| 23 | + it("sums estimated tokens across messages", () => { |
| 24 | + const messages = [msg("user", "a".repeat(40)), msg("assistant", "b".repeat(80))]; |
| 25 | + expect(estimateMessagesTokens(messages)).toBe(10 + 20); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe("planCompaction", () => { |
| 30 | + it("keeps everything when there aren't more messages than keepRecent", () => { |
| 31 | + const messages = Array.from({ length: 5 }, (_, i) => msg("user", `msg ${i}`)); |
| 32 | + const plan = planCompaction(messages, 0, 12); |
| 33 | + expect(plan.toFold).toEqual([]); |
| 34 | + expect(plan.kept).toEqual(messages); |
| 35 | + }); |
| 36 | + |
| 37 | + it("folds everything except the most recent keepRecent messages", () => { |
| 38 | + const messages = Array.from({ length: 20 }, (_, i) => msg("user", `msg ${i}`)); |
| 39 | + const plan = planCompaction(messages, 0, 12); |
| 40 | + expect(plan.toFold).toHaveLength(8); |
| 41 | + expect(plan.kept).toHaveLength(12); |
| 42 | + expect(plan.kept[0].content).toBe("msg 8"); |
| 43 | + expect(plan.foldEndIndex).toBe(8); |
| 44 | + }); |
| 45 | + |
| 46 | + it("never re-folds messages already covered by a previous compaction", () => { |
| 47 | + const messages = Array.from({ length: 20 }, (_, i) => msg("user", `msg ${i}`)); |
| 48 | + const plan = planCompaction(messages, 8, 12); |
| 49 | + expect(plan.toFold).toEqual([]); |
| 50 | + expect(plan.kept).toHaveLength(12); |
| 51 | + }); |
| 52 | + |
| 53 | + it("folds only the newly-eligible slice on a second compaction pass", () => { |
| 54 | + const messages = Array.from({ length: 30 }, (_, i) => msg("user", `msg ${i}`)); |
| 55 | + const plan = planCompaction(messages, 8, 12); |
| 56 | + expect(plan.toFold).toHaveLength(10); |
| 57 | + expect(plan.toFold[0].content).toBe("msg 8"); |
| 58 | + expect(plan.kept).toHaveLength(12); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("shouldCompact", () => { |
| 63 | + const keepRecent = 12; |
| 64 | + const budget = 100; |
| 65 | + |
| 66 | + it("returns false when there's nothing beyond the recent window", () => { |
| 67 | + const messages = Array.from({ length: 12 }, () => msg("user", "x".repeat(1000))); |
| 68 | + expect(shouldCompact(messages, 0, keepRecent, budget)).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it("returns false when the unfolded portion is under budget", () => { |
| 72 | + const messages = Array.from({ length: 20 }, () => msg("user", "hi")); |
| 73 | + expect(shouldCompact(messages, 0, keepRecent, budget)).toBe(false); |
| 74 | + }); |
| 75 | + |
| 76 | + it("returns true when the unfolded portion exceeds the token budget", () => { |
| 77 | + const messages = Array.from({ length: 20 }, () => msg("user", "x".repeat(1000))); |
| 78 | + expect(shouldCompact(messages, 0, keepRecent, budget)).toBe(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it("re-evaluates against only what's beyond the already-folded count", () => { |
| 82 | + const messages = Array.from({ length: 20 }, () => msg("user", "x".repeat(1000))); |
| 83 | + // Everything but the recent window is already folded, so nothing new to weigh. |
| 84 | + expect(shouldCompact(messages, 8, keepRecent, budget)).toBe(false); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("buildSummarizationPrompt", () => { |
| 89 | + it("includes the transcript with speaker labels", () => { |
| 90 | + const prompt = buildSummarizationPrompt(null, [msg("user", "hello"), msg("assistant", "hi there")]); |
| 91 | + expect(prompt).toContain("User: hello"); |
| 92 | + expect(prompt).toContain("Assistant: hi there"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("carries forward an existing summary for incremental compaction", () => { |
| 96 | + const prompt = buildSummarizationPrompt("earlier context here", [msg("user", "more")]); |
| 97 | + expect(prompt).toContain("Existing summary of earlier context:\nearlier context here"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("omits tool-role messages from the excerpt", () => { |
| 101 | + const prompt = buildSummarizationPrompt(null, [ |
| 102 | + msg("user", "run the tests"), |
| 103 | + { role: "tool", content: "42 passed", toolName: "run_command" }, |
| 104 | + msg("assistant", "all green"), |
| 105 | + ]); |
| 106 | + expect(prompt).not.toContain("42 passed"); |
| 107 | + expect(prompt).toContain("run the tests"); |
| 108 | + expect(prompt).toContain("all green"); |
| 109 | + }); |
| 110 | +}); |
0 commit comments