-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-compressor.js
More file actions
283 lines (250 loc) · 13 KB
/
Copy pathtest-compressor.js
File metadata and controls
283 lines (250 loc) · 13 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
'use strict';
/**
* Unit tests for backend/utils/compressor.js
* Run: node backend/tests/test-compressor.js
*/
const { compressString, compressBuffer, RULES } = require('../utils/compressor');
let passed = 0;
let failed = 0;
function assert(label, condition, detail = '') {
if (condition) {
console.log(` ✓ ${label}`);
passed++;
} else {
console.error(` ✗ ${label}${detail ? ` — ${detail}` : ''}`);
failed++;
}
}
function section(name) {
console.log(`\n── ${name} ${'─'.repeat(Math.max(0, 48 - name.length))}`);
}
function jsonBuf(obj) {
return Buffer.from(JSON.stringify(obj), 'utf8');
}
const CT = 'application/json';
// ── Rule inventory ────────────────────────────────────────────────────────────
section('Rule inventory');
const ruleNames = RULES.map(r => r.name);
const expectedRules = [
'trailing-whitespace', 'excess-blank-lines', 'multiple-spaces',
'repeated-exclamation', 'repeated-question', 'repeated-period',
'ai-preamble', 'filler-openers',
'verbose-connectives', 'verbose-instructions', 'redundant-qualifiers',
'duplicate-sentences', 'trim',
];
assert('all expected rules present', expectedRules.every(r => ruleNames.includes(r)));
assert('every rule has name + fn', RULES.every(r => typeof r.name === 'string' && typeof r.fn === 'function'));
assert('every rule has enabled field', RULES.every(r => typeof r.enabled === 'boolean'));
// ── Whitespace rules ──────────────────────────────────────────────────────────
section('Whitespace — trailing spaces');
{
const out = compressString('hello \nworld \t');
assert('trailing spaces stripped from lines', !out.match(/[ \t]+$/m));
}
section('Whitespace — excess blank lines');
{
const out = compressString('line1\n\n\n\n\nline2');
assert('3+ blank lines collapsed to one', out === 'line1\n\nline2');
}
section('Whitespace — multiple spaces');
{
const out = compressString('too many spaces here');
assert('runs of spaces collapsed', out === 'too many spaces here');
}
// ── Punctuation rules ─────────────────────────────────────────────────────────
section('Punctuation — repeated marks');
{
assert('!! → !', compressString('wow!!') === 'wow!');
assert('!!! → !', compressString('wow!!!') === 'wow!');
assert('?? → ?', compressString('really??') === 'really?');
assert('.... → ...',compressString('hmm....') === 'hmm...');
assert('...... → ...',compressString('hmm......') === 'hmm...');
}
// ── AI filler / boilerplate ───────────────────────────────────────────────────
section('AI preamble removal');
{
assert('removes "As an AI language model,"',
!compressString('As an AI language model, I can help.').includes('As an AI'));
assert('removes "As a large language model,"',
!compressString('As a large language model, here is my answer.').includes('large language model'));
}
section('Filler opener removal');
{
assert('"Certainly! " removed',
!compressString('Certainly! Here is the answer.').includes('Certainly'));
assert('"Of course! " removed',
!compressString('Of course! I can do that.').includes('Of course'));
assert('"I\'d be happy to help" removed',
!compressString("I'd be happy to help you with that.").includes("happy to help"));
assert('"I hope this helps." removed',
!compressString('I hope this helps. Let me know if you need more.').includes('I hope this helps'));
assert('"Feel free to ask" sentence removed',
!compressString('Feel free to ask any questions.').includes('Feel free'));
}
// ── Verbose connectives ───────────────────────────────────────────────────────
section('Verbose connectives');
const connectives = [
['In order to run this', 'To run this'],
['Due to the fact that it failed','Because it failed'],
['In the event that it breaks', 'If it breaks'],
['For the purpose of testing', 'To testing'],
['With respect to performance', 'For performance'],
['At this point in time', 'Now'],
['Prior to deployment', 'Before deployment'],
['Subsequent to the merge', 'After the merge'],
['A large number of users', 'Many users'],
['The majority of requests', 'Most requests'],
['Despite the fact that it works','Although it works'],
['It is important to note that', 'Note:'],
['Please note that', 'Note:'],
];
for (const [input, expected] of connectives) {
const out = compressString(input);
assert(`"${input.slice(0, 30)}" → starts with "${expected.slice(0, 20)}"`,
out.startsWith(expected));
}
// ── Verbose instructions ──────────────────────────────────────────────────────
section('Verbose instructions');
{
// verbose-instructions + post-cleanup: "Please make sure that you [verb]" → "[verb]..."
assert('"Please make sure that you restart" reduces tokens',
compressString('Please make sure that you restart the server.').length <
'Please make sure that you restart the server.'.length);
assert('"Make sure to save" reduces tokens',
compressString('Make sure to save your work.').length < 'Make sure to save your work.'.length);
assert('"You must ensure that all tests pass" reduces tokens',
compressString('You must ensure that all tests pass.').length <
'You must ensure that all tests pass.'.length);
}
// ── Redundant qualifiers ──────────────────────────────────────────────────────
section('Redundant qualifiers');
{
assert('"very unique" → "unique"',
compressString('This is very unique.') === 'This is unique.');
assert('"absolutely certain" → "certain"',
compressString('I am absolutely certain.') === 'I am certain.');
assert('"basically" removed',
!compressString('This is basically correct.').includes('basically'));
assert('"literally" removed',
!compressString('It literally works.').includes('literally'));
}
// ── Sentence deduplication ────────────────────────────────────────────────────
section('Sentence deduplication');
{
const dup = 'Always validate input. Always validate input. Check the output.';
const out = compressString(dup);
const count = (out.match(/Always validate input/g) || []).length;
assert('duplicate adjacent sentence removed', count === 1);
assert('non-duplicate sentence kept', out.includes('Check the output'));
}
{
// Different sentences must not be removed
const unique = 'First sentence. Second sentence. Third sentence.';
const out = compressString(unique);
assert('unique sentences all kept', out.includes('First') && out.includes('Second') && out.includes('Third'));
}
// ── Idempotency ───────────────────────────────────────────────────────────────
section('Idempotency');
{
const input = 'In order to test this, please make sure that you run the suite.';
const first = compressString(input);
const second = compressString(first);
assert('double-compress produces same result', first === second);
}
// ── compressBuffer — no-op cases ──────────────────────────────────────────────
section('compressBuffer — no-op cases');
{
const r = compressBuffer(null, CT);
assert('null buffer → report null', r.report === null);
}
{
const r = compressBuffer(Buffer.alloc(0), CT);
assert('empty buffer → report null', r.report === null);
}
{
const buf = jsonBuf({ messages: [{ role: 'user', content: 'hi' }] });
const r = compressBuffer(buf, 'text/plain');
assert('non-JSON content-type → original buffer ref', r.buffer === buf);
}
{
// No compressible content — original buffer reference returned
const buf = jsonBuf({ messages: [{ role: 'user', content: 'hi' }] });
const r = compressBuffer(buf, CT);
assert('nothing to compress → buffer same ref', r.buffer === buf);
assert('nothing to compress → report null', r.report === null);
}
{
const r = compressBuffer(Buffer.from('not-json'), CT);
assert('malformed JSON → report null', r.report === null);
}
// ── compressBuffer — messages array ──────────────────────────────────────────
section('compressBuffer — messages array');
{
const input = jsonBuf({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'As an AI language model, please make sure that you always respond helpfully.' },
{ role: 'user', content: 'In order to understand recursion, please note that it is self-referential.' },
],
});
const { buffer, report } = compressBuffer(input, CT);
const out = JSON.parse(buffer.toString('utf8'));
assert('system message compressed',
!out.messages[0].content.includes('As an AI language model'));
assert('user message compressed',
!out.messages[1].content.includes('In order to'));
assert('report has originalTokens', typeof report.originalTokens === 'number');
assert('report has compressedTokens', typeof report.compressedTokens === 'number');
assert('report savedTokens > 0', report.savedTokens > 0);
assert('report savedPct 1–99', report.savedPct >= 1 && report.savedPct <= 99);
assert('compressedTokens < originalTokens', report.compressedTokens < report.originalTokens);
}
// ── compressBuffer — block-content (OpenAI content array) ────────────────────
section('compressBuffer — block-content');
{
const input = jsonBuf({
messages: [{
role: 'user',
content: [
{ type: 'text', text: 'Certainly! In order to proceed, please make sure that you confirm.' },
{ type: 'image_url', image_url: { url: 'http://img.example.com/img.png' } },
],
}],
});
const { buffer, report } = compressBuffer(input, CT);
const out = JSON.parse(buffer.toString('utf8'));
const textBlock = out.messages[0].content[0];
const imgBlock = out.messages[0].content[1];
assert('text block compressed', !textBlock.text.includes('Certainly'));
assert('image block untouched', imgBlock.image_url.url.includes('http'));
assert('tokens saved > 0', report.savedTokens > 0);
}
// ── compressBuffer — plain prompt field ──────────────────────────────────────
section('compressBuffer — plain prompt field');
{
const input = jsonBuf({ prompt: 'In order to test this, please make sure that you run all the checks carefully.' });
const { buffer, report } = compressBuffer(input, CT);
const out = JSON.parse(buffer.toString('utf8'));
assert('prompt compressed', !out.prompt.includes('In order to'));
assert('tokens saved > 0', report.savedTokens > 0);
}
// ── Token savings sanity check ────────────────────────────────────────────────
section('Token savings — realistic system prompt');
{
const verbose = [
'As an AI language model, you are a helpful assistant.',
'Please make sure that you always respond in a clear and concise manner.',
'In order to assist the user effectively, please note that you should ask clarifying questions.',
'It is important to note that you must ensure that your responses are accurate.',
'Certainly, feel free to ask if you need any clarification.',
].join(' ');
const input = jsonBuf({ model: 'gpt-4o', messages: [{ role: 'system', content: verbose }] });
const { report } = compressBuffer(input, CT);
assert('at least 10 tokens saved on verbose system prompt', report && report.savedTokens >= 10);
assert('at least 10% reduction', report && report.savedPct >= 10);
}
// ── Summary ───────────────────────────────────────────────────────────────────
console.log(`\n${'─'.repeat(52)}`);
console.log(`Total: ${passed + failed} ✓ ${passed} passed ${failed ? `✗ ${failed} failed` : ''}`);
console.log(failed === 0 ? '\n✅ compressor.js — all tests passed.' : '\n❌ Some tests failed.');
process.exit(failed > 0 ? 1 : 0);