-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminifier.test.ts
More file actions
141 lines (124 loc) · 4.89 KB
/
Copy pathminifier.test.ts
File metadata and controls
141 lines (124 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { EventStore } from "../src/history/event-store.js";
import { TokenMinifier } from "../src/refiners/minifier.js";
import { createABEvaluationRecord } from "../src/evaluation/prompt-evaluator.js";
vi.mock("../src/evaluation/prompt-evaluator.js", () => ({
createABEvaluationRecord: vi.fn().mockReturnValue({
heuristicPreference: "A",
variantA: { evaluation: { heuristicScore: 10 } }
})
}));
describe("Token Minifier", () => {
let store: EventStore;
beforeEach(() => {
store = EventStore.getInstance();
const db = (store as any).db;
db.prepare("DELETE FROM events").run();
db.prepare("DELETE FROM prompts").run();
db.prepare("DELETE FROM prompt_templates").run();
db.prepare("DELETE FROM tournaments").run();
});
afterEach(() => {
vi.restoreAllMocks();
const db = (store as any).db;
db.prepare("DELETE FROM events").run();
db.prepare("DELETE FROM prompts").run();
db.prepare("DELETE FROM prompt_templates").run();
db.prepare("DELETE FROM tournaments").run();
});
it("should compress long verbose prompts into templates automatically", async () => {
// Generate a prompt string longer than 300 characters
const longPrompt = "Hello AI, I need you to do something for me. ".repeat(15) + "Please make sure to write a unit test for this.";
// Insert dummy prompt
store.recordPrompt({
id: "prm_1",
client: "API_PROXY",
agent_name: "ProxyClient",
raw_prompt: longPrompt,
repo_id: "test-repo"
});
const mockLocalModel = vi.fn().mockResolvedValue("MUST write a unit test. Format: JSON. Ensure coverage. DO NOT fail.");
const minifier = new TokenMinifier(mockLocalModel);
const count = await minifier.minifyVerbosePrompts();
expect(count).toBe(1);
expect(mockLocalModel).toHaveBeenCalled();
const templates = store.getLearningCandidates("test-repo").templates;
expect(templates).toHaveLength(1);
expect(templates[0].template_text).toBe("MUST write a unit test. Format: JSON. Ensure coverage. DO NOT fail.");
expect(templates[0].approved).toBe(0);
expect(store.getTemplates("test-repo")).toHaveLength(0);
});
it("does not duplicate templates for the same repository and source prompt", async () => {
const longPrompt = "Please preserve every requirement while shortening this prompt. ".repeat(10);
store.recordPrompt({
id: "prm_1",
client: "API_PROXY",
agent_name: "ProxyClient",
raw_prompt: longPrompt,
repo_id: "test-repo"
});
store.recordTemplate({
id: "tpl_existing",
repo_id: "test-repo",
category: "Minified",
title: "Existing",
template_text: "short",
usage_notes: "^" + longPrompt.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + "$",
source_type: "auto",
success_score: 10,
});
const mockLocalModel = vi.fn().mockResolvedValue("shorter");
await expect(new TokenMinifier(mockLocalModel).minifyVerbosePrompts()).resolves.toBe(0);
expect(mockLocalModel).not.toHaveBeenCalled();
});
it("skips short, repository-less, empty, longer, and losing minifications", async () => {
const longPrompt = "Please compress this long prompt while keeping requirements. ".repeat(8);
const longerPrompt = "Please compress this second long prompt while keeping requirements. ".repeat(8);
const losingPrompt = "Please compress this third long prompt while keeping requirements. ".repeat(8);
store.recordPrompt({
id: "short",
client: "API_PROXY",
raw_prompt: "short",
repo_id: "test-repo",
timestamp: "2026-06-24T00:00:05.000Z",
});
store.recordPrompt({
id: "no-repo",
client: "API_PROXY",
raw_prompt: longPrompt,
timestamp: "2026-06-24T00:00:04.000Z",
});
store.recordPrompt({
id: "empty",
client: "API_PROXY",
raw_prompt: longPrompt,
repo_id: "test-repo",
timestamp: "2026-06-24T00:00:03.000Z",
});
store.recordPrompt({
id: "longer",
client: "API_PROXY",
raw_prompt: longerPrompt,
repo_id: "test-repo",
timestamp: "2026-06-24T00:00:02.000Z",
});
store.recordPrompt({
id: "losing",
client: "API_PROXY",
raw_prompt: losingPrompt,
repo_id: "test-repo",
timestamp: "2026-06-24T00:00:01.000Z",
});
vi.mocked(createABEvaluationRecord).mockReturnValueOnce({
heuristicPreference: "B",
variantA: { evaluation: { heuristicScore: 1 } }
} as any);
const mockLocalModel = vi.fn()
.mockResolvedValueOnce("")
.mockResolvedValueOnce(longerPrompt + " not shorter")
.mockResolvedValueOnce("short but loses");
await expect(new TokenMinifier(mockLocalModel).minifyVerbosePrompts()).resolves.toBe(0);
expect(mockLocalModel).toHaveBeenCalledTimes(3);
expect(store.getLearningCandidates("test-repo").templates).toHaveLength(0);
});
});