|
| 1 | +import MetricsReporter, { HOUR_MS, INTERVAL_MS } from './MetricsReporter.svelte'; |
| 2 | +import { ProjectMetrics } from './projectMetrics'; |
| 3 | +import { PostHogWrapper } from '$lib/analytics/posthog'; |
| 4 | +import { render } from '@testing-library/svelte'; |
| 5 | +import { assert, test, describe, vi, beforeEach, afterEach } from 'vitest'; |
| 6 | + |
| 7 | +const PROJECT_ID = 'test-project'; |
| 8 | +const METRIC_NAME = 'test-metric'; |
| 9 | + |
| 10 | +describe('MetricsReporter', () => { |
| 11 | + let projectMetrics: ProjectMetrics; |
| 12 | + let context: Map<any, any>; |
| 13 | + let posthog: PostHogWrapper; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.useFakeTimers(); |
| 17 | + projectMetrics = new ProjectMetrics(PROJECT_ID); |
| 18 | + posthog = new PostHogWrapper(); |
| 19 | + context = new Map([[PostHogWrapper, posthog]]); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.restoreAllMocks(); |
| 24 | + vi.clearAllTimers(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('should report on interval', async () => { |
| 28 | + const posthogMock = vi.spyOn(posthog, 'capture').mock; |
| 29 | + |
| 30 | + projectMetrics.setMetric(METRIC_NAME, 1); |
| 31 | + render(MetricsReporter, { props: { projectMetrics }, context }); |
| 32 | + |
| 33 | + // Ensure first-run capture works. |
| 34 | + assert.equal(posthogMock.calls.length, 1); |
| 35 | + assert.equal(posthogMock.lastCall?.[0], 'metrics:' + METRIC_NAME); |
| 36 | + assert.deepEqual(posthogMock.lastCall?.[1], { |
| 37 | + project_id: PROJECT_ID, |
| 38 | + value: 1, |
| 39 | + minValue: 1, |
| 40 | + maxValue: 1 |
| 41 | + }); |
| 42 | + |
| 43 | + // Metrics are reset after they have been reported, so we should expect |
| 44 | + // that previous value does not influence next max/min. |
| 45 | + projectMetrics.setMetric(METRIC_NAME, -1); |
| 46 | + projectMetrics.setMetric(METRIC_NAME, 1); |
| 47 | + projectMetrics.setMetric(METRIC_NAME, 0); |
| 48 | + |
| 49 | + // Stop just one millisecond short of the reporting interval, and verify |
| 50 | + // it has not run again. |
| 51 | + await vi.advanceTimersByTimeAsync(INTERVAL_MS - 1); |
| 52 | + assert.equal(posthogMock.calls.length, 1); |
| 53 | + |
| 54 | + // Advance one millisecond and verify newly reported metrics. |
| 55 | + await vi.advanceTimersByTimeAsync(1); |
| 56 | + assert.equal(posthogMock.calls.length, 2); |
| 57 | + assert.deepEqual(posthogMock.lastCall?.[1], { |
| 58 | + project_id: PROJECT_ID, |
| 59 | + value: 0, |
| 60 | + minValue: -1, |
| 61 | + maxValue: 1 |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test('run based on last timestamp', async () => { |
| 66 | + const captureMock = vi.spyOn(posthog, 'capture').mock; |
| 67 | + |
| 68 | + // System time set to 0 plus a full report interval. |
| 69 | + vi.setSystemTime(INTERVAL_MS); |
| 70 | + // Simulate last report to have been sent at hour 1. |
| 71 | + localStorage.setItem('lastMetricsTs-fake-id', HOUR_MS.toString()); |
| 72 | + |
| 73 | + projectMetrics.setMetric(METRIC_NAME, 1); |
| 74 | + render(MetricsReporter, { props: { projectMetrics }, context }); |
| 75 | + |
| 76 | + // Verify it did not fire immediately. |
| 77 | + assert.equal(captureMock.calls.length, 0); |
| 78 | + |
| 79 | + // Advance one hour so that a full interval has elapsed. |
| 80 | + await vi.advanceTimersByTimeAsync(HOUR_MS); |
| 81 | + assert.equal(captureMock.calls.length, 1); |
| 82 | + |
| 83 | + // Set new metric value since last one should have been cleared. |
| 84 | + projectMetrics.setMetric(METRIC_NAME, 1); |
| 85 | + |
| 86 | + // Advance by full interval and ensure it fires again. |
| 87 | + await vi.advanceTimersByTimeAsync(INTERVAL_MS); |
| 88 | + assert.equal(captureMock.calls.length, 2); |
| 89 | + }); |
| 90 | +}); |
0 commit comments