add analytics

This commit is contained in:
2026-06-05 00:11:45 -03:00
parent a2947b3bf2
commit fd8e9049bf
35 changed files with 1540 additions and 30 deletions

View File

@@ -0,0 +1,98 @@
import { describe, it, expect } from 'vitest';
import { EventBatcher } from './EventBatcher';
import { ConsentManager } from '../consent/ConsentManager';
import { AnalyticsManager } from './AnalyticsManager';
describe('Analytics System', () => {
describe('EventBatcher', () => {
it('should create batcher with default config', () => {
const batcher = new EventBatcher();
expect(batcher.batchSize).toBe(10);
expect(batcher.batchTimeout).toBe(30000);
});
it('should create batcher with custom config', () => {
const batcher = new EventBatcher({
batchSize: 5,
batchTimeout: 5000,
});
expect(batcher.batchSize).toBe(5);
expect(batcher.batchTimeout).toBe(5000);
});
it('should track pending count', () => {
const batcher = new EventBatcher();
expect(batcher.getPendingCount()).toBe(0);
batcher.add({ eventName: 'test' });
expect(batcher.getPendingCount()).toBe(1);
batcher.destroy();
});
it('should not add null events', () => {
const batcher = new EventBatcher();
batcher.add(null);
batcher.add(undefined);
expect(batcher.getPendingCount()).toBe(0);
});
});
describe('ConsentManager', () => {
beforeEach(() => {
localStorage.clear();
});
it('should manage consent state', () => {
ConsentManager.setConsent(true);
expect(ConsentManager.hasConsent()).toBe(true);
expect(ConsentManager.hasUserDecided()).toBe(true);
});
it('should return false when consent not set', () => {
expect(ConsentManager.hasConsent()).toBe(false);
expect(ConsentManager.hasUserDecided()).toBe(false);
});
it('should reset consent', () => {
ConsentManager.setConsent(true);
ConsentManager.reset();
expect(ConsentManager.hasUserDecided()).toBe(false);
});
it('should store and retrieve consent', () => {
ConsentManager.setConsent(true);
const consent = ConsentManager.getConsent();
expect(consent.accepted).toBe(true);
expect(consent.version).toBe('1');
});
});
describe('AnalyticsManager', () => {
it('should create with noop provider by default', () => {
const manager = new AnalyticsManager();
expect(manager.provider.constructor.name).toBe('NoopProvider');
});
it('should create with GA4Provider', () => {
const manager = new AnalyticsManager({
providerType: 'ga4',
measurementId: 'G-TEST123',
});
expect(manager.provider.constructor.name).toBe('GA4Provider');
});
it('should fallback to noop for unknown provider', () => {
const manager = new AnalyticsManager({
providerType: 'unknown',
});
expect(manager.provider.constructor.name).toBe('NoopProvider');
});
it('should have network detector', () => {
const manager = new AnalyticsManager();
expect(manager.networkDetector).toBeDefined();
expect(typeof manager.networkDetector.getStatus).toBe('function');
});
});
});