-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-learner.test.ts
More file actions
369 lines (281 loc) · 11.1 KB
/
pattern-learner.test.ts
File metadata and controls
369 lines (281 loc) · 11.1 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* Tests for PatternLearner and ClickPredictor
*/
import { describe, it, expect } from 'bun:test';
import { PatternLearner } from './pattern-learner';
import { ClickPredictor } from './click-predictor';
describe('PatternLearner', () => {
it('should create a new instance', () => {
const learner = new PatternLearner('./test-data');
expect(learner).toBeDefined();
expect(learner.getStats()).toBeDefined();
});
it('should record click events', () => {
const learner = new PatternLearner('./test-data');
learner.recordClick(100, 200);
const stats = learner.getStats();
expect(stats.totalEvents).toBe(1);
expect(stats.uniqueClicks).toBe(1);
});
it('should record clicks with context', () => {
const learner = new PatternLearner('./test-data');
learner.recordClick(100, 200, {
url: 'https://example.com',
application: 'chrome',
tags: ['navigation'],
action: 'click_button'
});
const patterns = learner.getContextPatterns({ url: 'https://example.com' });
expect(patterns.length).toBe(1);
expect(patterns[0].url).toBe('https://example.com');
});
it('should aggregate clicks at the same grid position', () => {
const learner = new PatternLearner('./test-data');
// Record multiple clicks at similar positions (within grid)
learner.recordClick(100, 200);
learner.recordClick(105, 205);
learner.recordClick(98, 198);
const stats = learner.getStats();
expect(stats.uniqueClicks).toBe(1); // All should map to same grid cell
expect(stats.totalEvents).toBe(3);
});
it('should predict next clicks based on frequency', () => {
const learner = new PatternLearner('./test-data');
// Create a hotspot with multiple clicks
for (let i = 0; i < 10; i++) {
learner.recordClick(300, 400);
}
// Create another hotspot with fewer clicks
for (let i = 0; i < 3; i++) {
learner.recordClick(500, 600);
}
const predictions = learner.predictNextClicks(5);
expect(predictions.length).toBeGreaterThan(0);
expect(predictions[0].x).toBe(300);
expect(predictions[0].y).toBe(400);
expect(predictions[0].count).toBe(10);
});
it('should record and analyze movement patterns', () => {
const learner = new PatternLearner('./test-data');
// Simulate a movement sequence
for (let i = 0; i < 25; i++) {
learner.recordMovement(100 + i * 4, 200 + i * 2);
}
const stats = learner.getStats();
expect(stats.movementPatterns).toBeGreaterThan(0);
});
it('should get hotspots with minimum count filter', () => {
const learner = new PatternLearner('./test-data');
// Create hotspots with different counts
for (let i = 0; i < 5; i++) {
learner.recordClick(100, 200);
}
for (let i = 0; i < 2; i++) {
learner.recordClick(300, 400);
}
const hotspots = learner.getHotspots(3);
expect(hotspots.length).toBe(1);
expect(hotspots[0].count).toBe(5);
});
it('should calculate confidence scores', () => {
const learner = new PatternLearner('./test-data');
learner.recordClick(100, 200);
learner.recordClick(100, 200);
learner.recordClick(100, 200);
const patterns = learner.predictNextClicks(1);
expect(patterns[0].confidence).toBeGreaterThan(0);
expect(patterns[0].confidence).toBeLessThanOrEqual(1);
});
it('should export and import patterns', () => {
const learner = new PatternLearner('./test-data');
learner.recordClick(100, 200, { url: 'https://test.com' });
learner.recordClick(300, 400);
const exported = learner.exportPatterns();
expect(exported).toContain('https://test.com');
const learner2 = new PatternLearner('./test-data');
learner2.importPatterns(exported);
const stats = learner2.getStats();
expect(stats.uniqueClicks).toBe(2);
});
it('should clear patterns', () => {
const learner = new PatternLearner('./test-data');
learner.recordClick(100, 200);
learner.recordClick(300, 400);
learner.clear();
const stats = learner.getStats();
expect(stats.totalEvents).toBe(0);
expect(stats.uniqueClicks).toBe(0);
});
it('should apply session decay', () => {
const learner = new PatternLearner('./test-data');
learner.recordClick(100, 200);
learner.recordClick(100, 200);
const beforeDecay = learner.getStats();
learner.applySessionDecay();
const afterDecay = learner.getStats();
expect(afterDecay.sessionEvents).toBe(0);
});
});
describe('ClickPredictor', () => {
it('should create a new instance', () => {
const predictor = new ClickPredictor();
expect(predictor).toBeDefined();
});
it('should update screen elements', () => {
const predictor = new ClickPredictor();
const elements = [
{ x: 100, y: 200, width: 80, height: 30, type: 'button' as const, importance: 0.8, clickable: true },
{ x: 300, y: 400, width: 100, height: 40, type: 'link' as const, importance: 0.6, clickable: true }
];
predictor.updateScreenElements(elements);
const hotspots = predictor.getHotspots();
expect(hotspots).toBeDefined();
});
it('should record clicks and update pattern learner', () => {
const predictor = new ClickPredictor();
predictor.recordClick(100, 200);
predictor.recordClick(100, 200);
predictor.recordClick(100, 200);
const stats = predictor.getStats();
expect(stats.totalClicks).toBe(3);
});
it('should predict from pattern learning', () => {
const predictor = new ClickPredictor();
// Train with multiple clicks at same location
for (let i = 0; i < 10; i++) {
predictor.recordClick(300, 400);
}
const predictions = predictor.predict(290, 390);
expect(predictions.length).toBeGreaterThan(0);
});
it('should predict from velocity extrapolation', () => {
const predictor = new ClickPredictor();
const predictions = predictor.predict(100, 200, {
mouseVelocity: { x: 500, y: 300 },
acceleration: 100
});
// Should have velocity-based predictions
const velocityPreds = predictions.filter(p => p.source === 'velocity');
expect(velocityPreds.length).toBeGreaterThan(0);
});
it('should predict from screen elements', () => {
const predictor = new ClickPredictor();
predictor.updateScreenElements([
{ x: 200, y: 300, width: 100, height: 40, type: 'button' as const, importance: 0.9, clickable: true, text: 'Submit' }
]);
const predictions = predictor.predict(150, 250);
// Should have screen-based predictions
const screenPreds = predictions.filter(p => p.source === 'screen');
expect(screenPreds.length).toBeGreaterThan(0);
});
it('should combine predictions from multiple sources', () => {
const predictor = new ClickPredictor();
// Train pattern
for (let i = 0; i < 5; i++) {
predictor.recordClick(300, 400);
}
// Add screen elements nearby
predictor.updateScreenElements([
{ x: 280, y: 380, width: 60, height: 40, type: 'button' as const, importance: 0.8, clickable: true }
]);
const predictions = predictor.predict(290, 390, {
mouseVelocity: { x: 100, y: 50 }
});
// Should have combined prediction at the top
if (predictions.length > 0) {
expect(predictions[0].source).toBe('combined');
}
});
it('should filter predictions by minimum confidence', () => {
const predictor = new ClickPredictor({ minConfidence: 0.5 });
predictor.updateScreenElements([
{ x: 100, y: 200, width: 50, height: 20, type: 'text' as const, importance: 0.1, clickable: true }
]);
const predictions = predictor.predict(500, 600);
// All predictions should meet minimum confidence
for (const pred of predictions) {
expect(pred.confidence).toBeGreaterThanOrEqual(0.5);
}
});
it('should record predictions for accuracy tracking', () => {
const predictor = new ClickPredictor();
const prediction = {
x: 300,
y: 400,
confidence: 0.8,
source: 'pattern' as const
};
predictor.recordPrediction(prediction);
const recent = predictor.getRecentPredictions(1);
expect(recent.length).toBe(1);
expect(recent[0].predicted.x).toBe(300);
});
it('should get prediction statistics', () => {
const predictor = new ClickPredictor();
const stats = predictor.getStats();
expect(stats).toBeDefined();
expect(stats.totalPredictions).toBe(0);
expect(stats.accuracyRate).toBe(0);
});
it('should export and import state', () => {
const predictor = new ClickPredictor();
predictor.recordClick(100, 200);
predictor.recordClick(300, 400);
const exported = predictor.exportState();
expect(exported).toContain('weights');
const predictor2 = new ClickPredictor();
predictor2.importState(exported);
const stats = predictor2.getStats();
expect(stats).toBeDefined();
});
it('should provide reasoning for predictions', () => {
const predictor = new ClickPredictor();
// Train pattern
for (let i = 0; i < 5; i++) {
predictor.recordClick(300, 400);
}
const predictions = predictor.predict(290, 390);
for (const pred of predictions) {
expect(pred.reasoning).toBeDefined();
expect(pred.reasoning!.length).toBeGreaterThan(0);
}
});
it('should provide alternative predictions', () => {
const predictor = new ClickPredictor();
// Create multiple hotspots
for (let i = 0; i < 10; i++) {
predictor.recordClick(100 + i * 50, 200);
}
const predictions = predictor.predict(300, 200);
if (predictions.length > 0) {
expect(predictions[0].alternatives).toBeDefined();
}
});
});
describe('Integration Tests', () => {
it('should work together for pattern learning and prediction', () => {
const predictor = new ClickPredictor();
// Simulate user session
predictor.recordClick(100, 200, { url: 'https://app.com' });
predictor.recordClick(150, 250, { url: 'https://app.com' });
predictor.recordClick(100, 200, { url: 'https://app.com' });
// Get predictions for same context
const predictions = predictor.predict(90, 190, { url: 'https://app.com' });
expect(predictions.length).toBeGreaterThan(0);
});
it('should handle context-aware predictions', () => {
const predictor = new ClickPredictor();
// Different contexts
predictor.recordClick(100, 200, { url: 'https://app1.com' });
predictor.recordClick(100, 200, { url: 'https://app1.com' });
predictor.recordClick(500, 600, { url: 'https://app2.com' });
predictor.recordClick(500, 600, { url: 'https://app2.com' });
predictor.recordClick(500, 600, { url: 'https://app2.com' });
// Predictions for app1 should favor first location
const predictions1 = predictor.predict(90, 190, { url: 'https://app1.com' });
expect(predictions1.length).toBeGreaterThan(0);
// Predictions for app2 should favor second location
const predictions2 = predictor.predict(490, 590, { url: 'https://app2.com' });
expect(predictions2.length).toBeGreaterThan(0);
});
});