Files
inkling/tests/unit/telemetryStats.test.ts
altair823 2036c687d2 test(telemetry): add KST regression test for near-midnight UTC bucketing
Original 'counts events per KST day' test used UTC times that bucket
identically under both KST and naive UTC slice — would not catch a regression
where kstDate was replaced with ev.ts.slice(0,10). Add an explicit
near-midnight case (2026-05-01T15:30Z = 2026-05-02 00:30 KST) that fails
under naive UTC and passes under correct KST conversion.

6 tests pass (was 5).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 17:06:29 +09:00

69 lines
3.3 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { aggregateStats } from '@main/services/telemetryStats.js';
import type { TelemetryEvent } from '@main/services/telemetryEvents.js';
const e = (ts: string, kind: TelemetryEvent['kind'], payload: TelemetryEvent['payload']): TelemetryEvent =>
({ ts, kind, payload } as TelemetryEvent);
describe('aggregateStats', () => {
it('produces empty stats for empty input', () => {
const r = aggregateStats([], new Date('2026-05-08T00:00:00Z'));
expect(r.eventCount).toBe(0);
expect(r.md).toContain('총 이벤트: 0');
});
it('counts events per KST day per kind', () => {
const events: TelemetryEvent[] = [
e('2026-05-01T12:00:00Z', 'capture', { noteId: 'n1', rawTextLength: 5, hasMedia: false }),
e('2026-05-01T12:01:00Z', 'capture', { noteId: 'n2', rawTextLength: 3, hasMedia: true }),
e('2026-05-01T12:02:00Z', 'ai_succeeded', { noteId: 'n1', durationMs: 1000, attempts: 0 }),
e('2026-05-02T00:00:00Z', 'ai_failed', { noteId: 'n2', reason: 'unreachable', attempts: 3 })
];
const r = aggregateStats(events, new Date('2026-05-08T00:00:00Z'));
expect(r.eventCount).toBe(4);
expect(r.md).toContain('| 2026-05-01 | 2 | 1 | 0 |');
expect(r.md).toContain('| 2026-05-02 | 0 | 0 | 1 |');
});
it('computes AI 성공률', () => {
const events: TelemetryEvent[] = [
e('2026-05-01T00:00:00Z', 'ai_succeeded', { noteId: 'n1', durationMs: 1, attempts: 0 }),
e('2026-05-01T00:00:01Z', 'ai_succeeded', { noteId: 'n2', durationMs: 1, attempts: 0 }),
e('2026-05-01T00:00:02Z', 'ai_succeeded', { noteId: 'n3', durationMs: 1, attempts: 0 }),
e('2026-05-01T00:00:03Z', 'ai_failed', { noteId: 'n4', reason: 'other', attempts: 1 })
];
const r = aggregateStats(events, new Date('2026-05-02T00:00:00Z'));
expect(r.md).toContain('AI 성공률: 75.0%');
expect(r.md).toContain('3/4');
});
it('AI 성공률 N/A when no AI events', () => {
const events: TelemetryEvent[] = [
e('2026-05-01T00:00:00Z', 'capture', { noteId: 'n1', rawTextLength: 1, hasMedia: false })
];
const r = aggregateStats(events, new Date('2026-05-02T00:00:00Z'));
expect(r.md).toContain('AI 성공률: N/A');
});
it('computes 평균 ai_succeeded durationMs', () => {
const events: TelemetryEvent[] = [
e('2026-05-01T00:00:00Z', 'ai_succeeded', { noteId: 'n1', durationMs: 1000, attempts: 0 }),
e('2026-05-01T00:00:01Z', 'ai_succeeded', { noteId: 'n2', durationMs: 2000, attempts: 0 }),
e('2026-05-01T00:00:02Z', 'ai_succeeded', { noteId: 'n3', durationMs: 3000, attempts: 0 })
];
const r = aggregateStats(events, new Date('2026-05-02T00:00:00Z'));
expect(r.md).toContain('평균 ai_succeeded durationMs: 2000');
});
it('buckets near-midnight UTC events on the correct KST day (regression: not naive UTC)', () => {
// 2026-05-01T15:30:00Z → 2026-05-02 00:30 KST → KST day 2026-05-02
// Naive UTC slice(0,10) would put this on 2026-05-01 — this test catches that regression.
const events: TelemetryEvent[] = [
e('2026-05-01T15:30:00Z', 'capture', { noteId: 'n1', rawTextLength: 1, hasMedia: false })
];
const r = aggregateStats(events, new Date('2026-05-08T00:00:00Z'));
expect(r.md).toContain('| 2026-05-02 | 1 | 0 | 0 |');
expect(r.md).not.toContain('| 2026-05-01 |');
});
});