forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse-reliability.test.ts
More file actions
517 lines (493 loc) · 13 KB
/
Copy pathresponse-reliability.test.ts
File metadata and controls
517 lines (493 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import { describe, expect, it } from "vitest";
import { numberValue } from "../response-reliability";
import {
buildChannelReliabilityPayloads,
buildResponseReliabilityPayload,
RELIABILITY_ROW_LIMIT,
responseReliabilityQueries,
trimDailyToCoverage,
} from "../response-reliability";
describe("response reliability", () => {
it("aggregates delivery rate, failures, exclusions, gaps, and full-answer speed", () => {
const payload = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
chatRows: [
[
"2026-07-20",
"chat_agent_query_started",
"main_chat",
"unknown",
12,
0,
],
[
"2026-07-20",
"chat_agent_query_completed",
"main_chat",
"unknown",
8,
32_000,
],
["2026-07-20", "chat_agent_error", "main_chat", "timeout", 2, 0],
[
"2026-07-20",
"chat_agent_query_cancelled",
"main_chat",
"unknown",
1,
0,
],
],
voiceRows: [
[
"2026-07-20",
"voice_turn_started",
"unknown",
"unknown",
"unknown",
"hold",
6,
0,
],
[
"2026-07-20",
"voice_turn_terminal",
"success",
"success",
"hub",
"hold",
4,
20_000,
],
[
"2026-07-20",
"voice_turn_terminal",
"failure",
"playback_failed",
"hub",
"hold",
1,
0,
],
[
"2026-07-20",
"voice_turn_terminal",
"excluded",
"cancelled",
"hub",
"hold",
1,
0,
],
],
});
expect(payload.summary.chat).toMatchObject({
attempts: 12,
success: 8,
failure: 2,
excluded: 1,
unresolved: 1,
successRate: 80,
averageFullAnswerSeconds: 4,
});
expect(payload.summary.voice).toMatchObject({
attempts: 6,
success: 4,
failure: 1,
excluded: 1,
unresolved: 0,
successRate: 80,
averageFullAnswerSeconds: 5,
});
expect(payload.summary.overall).toMatchObject({
success: 12,
failure: 3,
successRate: 80,
});
expect(payload.failureReasons).toEqual([
{ source: "chat", reason: "timeout", count: 2 },
{ source: "voice", reason: "playback_failed", count: 1 },
]);
expect(payload.availability.voiceCoverageStart).toBe("2026-07-20");
expect(payload.daily.at(-1)).toMatchObject({
chatSuccessRate: 80,
voiceSuccessRate: 80,
chatAverageSeconds: 4,
voiceAverageSeconds: 5,
});
});
it("marks an unavailable source partial instead of fabricating zero reliability", () => {
const payload = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: false,
chatRows: [
[
"2026-07-20",
"chat_agent_query_completed",
"main_chat",
"unknown",
2,
4_000,
],
],
voiceRows: [],
});
expect(payload.partial).toBe(true);
expect(payload.summary.chat?.successRate).toBe(100);
expect(payload.summary.voice).toBeNull();
expect(payload.daily.at(-1)?.voiceSuccessRate).toBeNull();
});
it("shows historical floating voice reliability until physical shortcut telemetry exists", () => {
const legacy = [
[
"2026-07-20",
"voice_turn_started",
"unknown",
"unknown",
"floating_voice",
"legacy",
5,
0,
"floating_voice",
],
[
"2026-07-20",
"voice_turn_terminal",
"success",
"unknown",
"floating_voice",
"legacy",
4,
8_000,
"floating_voice",
],
[
"2026-07-20",
"voice_turn_terminal",
"failure",
"timeout",
"floating_voice",
"legacy",
1,
0,
"floating_voice",
],
];
const legacyPayload = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
chatRows: [],
voiceRows: legacy,
});
expect(legacyPayload.summary.voice).toMatchObject({
attempts: 5,
success: 4,
failure: 1,
successRate: 80,
averageFullAnswerSeconds: 2,
});
const physicalPayload = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
chatRows: [],
voiceRows: [
...legacy,
[
"2026-07-20",
"voice_turn_started",
"unknown",
"unknown",
"hub",
"hold",
2,
0,
"physical_shortcut",
],
[
"2026-07-20",
"voice_turn_terminal",
"success",
"success",
"hub",
"hold",
2,
6_000,
"physical_shortcut",
],
],
});
expect(physicalPayload.summary.voice).toMatchObject({
attempts: 2,
success: 2,
failure: 0,
successRate: 100,
averageFullAnswerSeconds: 3,
});
});
it("splits response reliability by the user's release channel", () => {
const chatRows = [
[
"2026-07-20",
"chat_agent_query_started",
"main_chat",
"unknown",
2,
0,
"prod-user",
],
[
"2026-07-20",
"chat_agent_query_completed",
"main_chat",
"unknown",
2,
4_000,
"prod-user",
],
[
"2026-07-20",
"chat_agent_query_started",
"main_chat",
"unknown",
1,
0,
"beta-user",
],
[
"2026-07-20",
"chat_agent_error",
"main_chat",
"timeout",
1,
0,
"beta-user",
],
];
const channels = buildChannelReliabilityPayloads({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
chatRows,
voiceRows: [],
channelByActor: new Map([
["prod-user", "production"],
["beta-user", "beta"],
]),
});
expect(channels.production.summary.chat).toMatchObject({
attempts: 2,
success: 2,
failure: 0,
successRate: 100,
});
expect(channels.beta.summary.chat).toMatchObject({
attempts: 1,
success: 0,
failure: 1,
successRate: 0,
});
});
it("clamps the query window before interpolating HogQL", () => {
const queries = responseReliabilityQueries(500);
expect(queries.chat).toContain("toStartOfDay(now()) - INTERVAL 89 DAY");
expect(queries.voice).toContain("toStartOfDay(now()) - INTERVAL 89 DAY");
expect(queries.voice).toContain(
"toString(properties.surface) = 'floating_voice'",
);
expect(queries.chat).toContain("distinct_id AS actor_id");
expect(queries.voice).toContain("distinct_id AS actor_id");
});
// Static query-contract check: PostHog's query API silently fills LIMIT 100
// into any HogQL (sub)query without one. These queries are ordered day ASC,
// so that default cut exactly the newest days off the dashboard (lines
// stopped days before today). A trailing LIMIT after UNION ALL binds to the
// last arm only, so the voice union must be wrapped in a subquery with one
// outer LIMIT.
it("carries an explicit LIMIT so PostHog's default 100-row cap cannot drop the newest days", () => {
const queries = responseReliabilityQueries(30);
expect(queries.chat).toContain(`LIMIT ${RELIABILITY_ROW_LIMIT}`);
expect(queries.voice).toContain("SELECT * FROM (");
expect(queries.voice.replace(/\s+/g, " ")).toContain(
`) ORDER BY day ASC LIMIT ${RELIABILITY_ROW_LIMIT}`,
);
// The route detects truncation via rows.length >= RELIABILITY_ROW_LIMIT,
// so the constant must equal PostHog's served maximum — verified live:
// LIMIT 100000 returns exactly 50000 rows. A larger constant would make
// the overflow check unreachable and truncation undetectable.
expect(RELIABILITY_ROW_LIMIT).toBe(50_000);
});
it("marks the payload partial when rows hit the query row limit", () => {
const truncatedPayload = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
truncated: true,
chatRows: [
[
"2026-07-20",
"chat_agent_query_completed",
"main_chat",
"unknown",
2,
4_000,
],
],
voiceRows: [],
});
expect(truncatedPayload.partial).toBe(true);
const channels = buildChannelReliabilityPayloads({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
truncated: true,
chatRows: [],
voiceRows: [],
channelByActor: new Map(),
});
expect(channels.production.partial).toBe(true);
expect(channels.beta.partial).toBe(true);
});
it("trims leading empty days to telemetry coverage but keeps interior gaps", () => {
const payload = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
chatRows: [
[
"2026-07-17",
"chat_agent_query_started",
"main_chat",
"unknown",
2,
0,
],
[
"2026-07-17",
"chat_agent_query_completed",
"main_chat",
"unknown",
2,
9000,
],
[
"2026-07-19",
"chat_agent_query_started",
"main_chat",
"unknown",
1,
0,
],
["2026-07-19", "chat_agent_error", "main_chat", "agent_error", 1, 0],
],
voiceRows: [],
});
const trimmed = trimDailyToCoverage(payload.daily);
expect(payload.daily).toHaveLength(7);
expect(trimmed.map((point) => point.date)).toEqual([
"2026-07-17",
"2026-07-18",
"2026-07-19",
"2026-07-20",
]);
expect(trimmed[0].chatSuccessRate).toBe(100);
expect(trimmed[1].chatSuccessRate).toBeNull();
expect(trimmed[2].chatSuccessRate).toBe(0);
expect(trimDailyToCoverage([])).toEqual([]);
const empty = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
chatRows: [],
voiceRows: [],
});
expect(trimDailyToCoverage(empty.daily)).toEqual([]);
// A first day with only cancellations is coverage (an all-cancelled
// rollout day is a signal), not a pre-coverage gap to trim away.
const excludedOnly = buildResponseReliabilityPayload({
days: 7,
now: new Date("2026-07-20T12:00:00Z"),
chatAvailable: true,
voiceAvailable: true,
chatRows: [
[
"2026-07-18",
"chat_agent_query_started",
"main_chat",
"unknown",
3,
0,
],
[
"2026-07-18",
"chat_agent_query_cancelled",
"main_chat",
"unknown",
3,
0,
],
[
"2026-07-19",
"chat_agent_query_started",
"main_chat",
"unknown",
1,
0,
],
[
"2026-07-19",
"chat_agent_query_completed",
"main_chat",
"unknown",
1,
5_000,
],
],
voiceRows: [],
});
expect(trimDailyToCoverage(excludedOnly.daily)[0]?.date).toBe("2026-07-18");
});
});
describe("numberValue", () => {
it("returns the number for a valid number", () => {
expect(numberValue(42)).toBe(42);
expect(numberValue(-42)).toBe(-42);
expect(numberValue(0)).toBe(0);
expect(numberValue(1.5)).toBe(1.5);
});
it("parses valid number strings", () => {
expect(numberValue("42")).toBe(42);
expect(numberValue("-42")).toBe(-42);
expect(numberValue("1.5")).toBe(1.5);
expect(numberValue("0")).toBe(0);
});
it("returns 0 for undefined and null", () => {
expect(numberValue(undefined)).toBe(0);
expect(numberValue(null)).toBe(0);
});
it("returns 0 for non-finite values and unparseable strings", () => {
expect(numberValue(NaN)).toBe(0);
expect(numberValue(Infinity)).toBe(0);
expect(numberValue(-Infinity)).toBe(0);
expect(numberValue("not a number")).toBe(0);
expect(numberValue({})).toBe(0);
});
it("returns 0 for boolean values (unless we expect otherwise, Number(true) is 1)", () => {
// Number(true) is 1, Number(false) is 0
expect(numberValue(true)).toBe(1);
expect(numberValue(false)).toBe(0);
});
});