@cachly-dev/mcp-server 0.9.22 → 0.9.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,285 @@
1
+ /**
2
+ * Tests for confidence.ts
3
+ *
4
+ * Covers:
5
+ * calculateConfidence – decay model (fresh / aging / warn / stale)
6
+ * confidenceBadge – emoji + human label
7
+ * simpleHash – determinism, uniqueness, fixed length
8
+ * STRUCTURED_TEMPLATES – required fields, hints
9
+ * Constants – sensible defaults and relationships
10
+ *
11
+ * Run: npx vitest run src/__tests__/confidence.test.ts
12
+ */
13
+
14
+ import { describe, it, expect } from 'vitest';
15
+ import {
16
+ calculateConfidence,
17
+ confidenceBadge,
18
+ STRUCTURED_TEMPLATES,
19
+ CONFIDENCE_WARN_DAYS,
20
+ CONFIDENCE_STALE_DAYS,
21
+ CONFIDENCE_WARN_VALUE,
22
+ CONFIDENCE_STALE_VALUE,
23
+ simpleHash,
24
+ } from '../confidence.js';
25
+
26
+ // ── Constant sanity ───────────────────────────────────────────────────────────
27
+
28
+ describe('Confidence constants', () => {
29
+ it('CONFIDENCE_WARN_DAYS < CONFIDENCE_STALE_DAYS', () => {
30
+ expect(CONFIDENCE_WARN_DAYS).toBeLessThan(CONFIDENCE_STALE_DAYS);
31
+ });
32
+
33
+ it('CONFIDENCE_WARN_VALUE > CONFIDENCE_STALE_VALUE', () => {
34
+ expect(CONFIDENCE_WARN_VALUE).toBeGreaterThan(CONFIDENCE_STALE_VALUE);
35
+ });
36
+
37
+ it('CONFIDENCE_WARN_VALUE < 1.0 and > 0', () => {
38
+ expect(CONFIDENCE_WARN_VALUE).toBeLessThan(1.0);
39
+ expect(CONFIDENCE_WARN_VALUE).toBeGreaterThan(0);
40
+ });
41
+
42
+ it('CONFIDENCE_STALE_VALUE < 1.0 and > 0', () => {
43
+ expect(CONFIDENCE_STALE_VALUE).toBeLessThan(1.0);
44
+ expect(CONFIDENCE_STALE_VALUE).toBeGreaterThan(0);
45
+ });
46
+
47
+ it('defaults to 5 warn days and 10 stale days (when env vars absent)', () => {
48
+ // These may be overridden via env, but assert sensible defaults
49
+ expect(CONFIDENCE_WARN_DAYS).toBeGreaterThanOrEqual(1);
50
+ expect(CONFIDENCE_STALE_DAYS).toBeGreaterThanOrEqual(CONFIDENCE_WARN_DAYS + 1);
51
+ });
52
+ });
53
+
54
+ // ── calculateConfidence ───────────────────────────────────────────────────────
55
+
56
+ describe('calculateConfidence', () => {
57
+ /** Returns an ISO timestamp that is `days` old from now. */
58
+ function tsAgo(days: number): string {
59
+ return new Date(Date.now() - days * 86400000).toISOString();
60
+ }
61
+
62
+ it('returns 1.0 for brand-new lesson (age ≈ 0)', () => {
63
+ const c = calculateConfidence({ ts: new Date().toISOString() });
64
+ expect(c).toBeCloseTo(1.0, 2);
65
+ });
66
+
67
+ it('prefers verified_at over ts', () => {
68
+ // ts is very old, but verified_at is recent → should NOT be stale
69
+ const c = calculateConfidence({
70
+ ts: tsAgo(365),
71
+ verified_at: tsAgo(0),
72
+ });
73
+ expect(c).toBeCloseTo(1.0, 2);
74
+ });
75
+
76
+ it('returns exactly CONFIDENCE_WARN_VALUE at exactly CONFIDENCE_WARN_DAYS', () => {
77
+ const c = calculateConfidence({ ts: tsAgo(CONFIDENCE_WARN_DAYS) });
78
+ // At exactly the warn threshold the function returns CONFIDENCE_WARN_VALUE
79
+ expect(c).toBeCloseTo(CONFIDENCE_WARN_VALUE, 3);
80
+ });
81
+
82
+ it('returns exactly CONFIDENCE_STALE_VALUE at exactly CONFIDENCE_STALE_DAYS', () => {
83
+ const c = calculateConfidence({ ts: tsAgo(CONFIDENCE_STALE_DAYS) });
84
+ expect(c).toBe(CONFIDENCE_STALE_VALUE);
85
+ });
86
+
87
+ it('caps at CONFIDENCE_STALE_VALUE for very old lessons', () => {
88
+ const c = calculateConfidence({ ts: tsAgo(365) });
89
+ expect(c).toBe(CONFIDENCE_STALE_VALUE);
90
+ });
91
+
92
+ it('linearly decreases between 0 and CONFIDENCE_WARN_DAYS', () => {
93
+ const midDays = CONFIDENCE_WARN_DAYS / 2;
94
+ const c = calculateConfidence({ ts: tsAgo(midDays) });
95
+ // at half-way to warn: 1.0 - (0.5 * (1.0 - CONFIDENCE_WARN_VALUE))
96
+ const expected = 1.0 - 0.5 * (1.0 - CONFIDENCE_WARN_VALUE);
97
+ expect(c).toBeCloseTo(expected, 3);
98
+ });
99
+
100
+ it('is monotonically decreasing with age', () => {
101
+ const c0 = calculateConfidence({ ts: tsAgo(0) });
102
+ const c1 = calculateConfidence({ ts: tsAgo(CONFIDENCE_WARN_DAYS * 0.3) });
103
+ const c2 = calculateConfidence({ ts: tsAgo(CONFIDENCE_WARN_DAYS * 0.7) });
104
+ const c3 = calculateConfidence({ ts: tsAgo(CONFIDENCE_WARN_DAYS) });
105
+ const c4 = calculateConfidence({ ts: tsAgo(CONFIDENCE_STALE_DAYS) });
106
+ expect(c0).toBeGreaterThanOrEqual(c1);
107
+ expect(c1).toBeGreaterThan(c2);
108
+ expect(c2).toBeGreaterThan(c3);
109
+ expect(c3).toBeGreaterThan(c4);
110
+ });
111
+
112
+ it('value between WARN and STALE ages equals CONFIDENCE_WARN_VALUE', () => {
113
+ const betweenDays = (CONFIDENCE_WARN_DAYS + CONFIDENCE_STALE_DAYS) / 2;
114
+ const c = calculateConfidence({ ts: tsAgo(betweenDays) });
115
+ expect(c).toBe(CONFIDENCE_WARN_VALUE);
116
+ });
117
+
118
+ it('ignores recall_count (not used in calculation)', () => {
119
+ const ts = new Date().toISOString();
120
+ const c1 = calculateConfidence({ ts, recall_count: 0 });
121
+ const c2 = calculateConfidence({ ts, recall_count: 100 });
122
+ expect(c1).toBeCloseTo(c2, 5);
123
+ });
124
+ });
125
+
126
+ // ── confidenceBadge ───────────────────────────────────────────────────────────
127
+
128
+ describe('confidenceBadge', () => {
129
+ it('returns ✅ for confidence >= 0.9 (fresh)', () => {
130
+ expect(confidenceBadge(1.0, 0)).toBe('✅');
131
+ expect(confidenceBadge(0.9, 1)).toBe('✅');
132
+ expect(confidenceBadge(0.95, 2)).toBe('✅');
133
+ });
134
+
135
+ it('returns ⚠️ prefix for warn range [0.7, 0.9)', () => {
136
+ const badge = confidenceBadge(0.7, 6);
137
+ expect(badge).toMatch(/^⚠️/);
138
+ expect(badge).toContain('6d old');
139
+ expect(badge).toContain('70%');
140
+ expect(badge).toContain('verify before applying');
141
+ });
142
+
143
+ it('⚠️ badge rounds age to nearest day', () => {
144
+ const badge = confidenceBadge(0.75, 4.6);
145
+ expect(badge).toContain('5d old'); // Math.round(4.6) = 5
146
+ });
147
+
148
+ it('returns 🔴 STALE prefix for confidence < 0.7', () => {
149
+ const badge = confidenceBadge(0.5, 12);
150
+ expect(badge).toMatch(/^🔴 STALE/);
151
+ expect(badge).toContain('12d old');
152
+ expect(badge).toContain('50%');
153
+ expect(badge).toContain('likely outdated');
154
+ });
155
+
156
+ it('🔴 badge for exactly 0.0 confidence', () => {
157
+ const badge = confidenceBadge(0.0, 99);
158
+ expect(badge).toMatch(/^🔴 STALE/);
159
+ expect(badge).toContain('0%');
160
+ });
161
+
162
+ it('edge: confidence exactly 0.7 returns ⚠️ (not 🔴)', () => {
163
+ const badge = confidenceBadge(0.7, 5);
164
+ expect(badge).toMatch(/^⚠️/);
165
+ });
166
+
167
+ it('edge: confidence exactly 0.9 returns ✅ (not ⚠️)', () => {
168
+ expect(confidenceBadge(0.9, 1)).toBe('✅');
169
+ });
170
+ });
171
+
172
+ // ── simpleHash ────────────────────────────────────────────────────────────────
173
+
174
+ describe('simpleHash', () => {
175
+ it('returns exactly 12 characters', () => {
176
+ expect(simpleHash('hello world')).toHaveLength(12);
177
+ expect(simpleHash('')).toHaveLength(12);
178
+ expect(simpleHash('x'.repeat(10000))).toHaveLength(12);
179
+ });
180
+
181
+ it('returns only hex characters [0-9a-f]', () => {
182
+ const hash = simpleHash('test input 123');
183
+ expect(hash).toMatch(/^[0-9a-f]{12}$/);
184
+ });
185
+
186
+ it('is deterministic — same input always yields same hash', () => {
187
+ const input = 'docker compose up -d --build api';
188
+ const h1 = simpleHash(input);
189
+ const h2 = simpleHash(input);
190
+ const h3 = simpleHash(input);
191
+ expect(h1).toBe(h2);
192
+ expect(h2).toBe(h3);
193
+ });
194
+
195
+ it('different inputs yield different hashes (collision resistance)', () => {
196
+ const inputs = [
197
+ 'deploy:api',
198
+ 'deploy:web',
199
+ 'fix:redis-timeout',
200
+ 'infra:clickhouse',
201
+ '',
202
+ 'a',
203
+ ' ',
204
+ '0',
205
+ ];
206
+ const hashes = inputs.map(simpleHash);
207
+ const unique = new Set(hashes);
208
+ expect(unique.size).toBe(inputs.length);
209
+ });
210
+
211
+ it('is sensitive to character order', () => {
212
+ expect(simpleHash('abc')).not.toBe(simpleHash('cba'));
213
+ expect(simpleHash('ab')).not.toBe(simpleHash('ba'));
214
+ });
215
+
216
+ it('is sensitive to whitespace differences', () => {
217
+ expect(simpleHash('hello world')).not.toBe(simpleHash('helloworld'));
218
+ expect(simpleHash('hello world')).not.toBe(simpleHash('hello world'));
219
+ });
220
+
221
+ it('works with unicode input', () => {
222
+ const h1 = simpleHash('日本語テスト');
223
+ const h2 = simpleHash('中文测试');
224
+ expect(h1).toHaveLength(12);
225
+ expect(h2).toHaveLength(12);
226
+ expect(h1).not.toBe(h2);
227
+ });
228
+
229
+ it('works with newlines and special chars', () => {
230
+ const h = simpleHash('line1\nline2\ttab\r\n');
231
+ expect(h).toHaveLength(12);
232
+ expect(h).toMatch(/^[0-9a-f]{12}$/);
233
+ });
234
+ });
235
+
236
+ // ── STRUCTURED_TEMPLATES ──────────────────────────────────────────────────────
237
+
238
+ describe('STRUCTURED_TEMPLATES', () => {
239
+ it('contains all expected category keys', () => {
240
+ const expected = ['deploy', 'bash', 'infra', 'pricing', 'stripe'];
241
+ for (const key of expected) {
242
+ expect(STRUCTURED_TEMPLATES, `Missing key: "${key}"`).toHaveProperty(key);
243
+ }
244
+ });
245
+
246
+ it('every entry has a non-empty hint string', () => {
247
+ for (const [key, tmpl] of Object.entries(STRUCTURED_TEMPLATES)) {
248
+ expect(typeof tmpl.hint, `hint for "${key}" is not a string`).toBe('string');
249
+ expect(tmpl.hint.length, `hint for "${key}" is empty`).toBeGreaterThan(0);
250
+ }
251
+ });
252
+
253
+ it('every entry has a required array', () => {
254
+ for (const [key, tmpl] of Object.entries(STRUCTURED_TEMPLATES)) {
255
+ expect(Array.isArray(tmpl.required), `required for "${key}" is not an array`).toBe(true);
256
+ }
257
+ });
258
+
259
+ it('deploy / bash / infra require commands[]', () => {
260
+ for (const key of ['deploy', 'bash', 'infra']) {
261
+ expect(STRUCTURED_TEMPLATES[key].required, `${key} should require commands`).toContain('commands');
262
+ }
263
+ });
264
+
265
+ it('pricing / stripe have empty required list (free-form)', () => {
266
+ expect(STRUCTURED_TEMPLATES['pricing'].required).toHaveLength(0);
267
+ expect(STRUCTURED_TEMPLATES['stripe'].required).toHaveLength(0);
268
+ });
269
+
270
+ it('hints mention the category key', () => {
271
+ for (const [key, tmpl] of Object.entries(STRUCTURED_TEMPLATES)) {
272
+ expect(tmpl.hint.toLowerCase(), `hint for "${key}" should mention the key`).toContain(key);
273
+ }
274
+ });
275
+
276
+ it('is a plain object (not prototype-polluted)', () => {
277
+ expect(Object.getPrototypeOf(STRUCTURED_TEMPLATES)).toBe(Object.prototype);
278
+ });
279
+
280
+ it('all category keys are valid topic prefixes (lowercase, no special chars)', () => {
281
+ for (const key of Object.keys(STRUCTURED_TEMPLATES)) {
282
+ expect(key).toMatch(/^[a-z][a-z0-9_-]*$/);
283
+ }
284
+ });
285
+ });