platform-codebase/features/analytics/backend-api/test/utils/factories.ts

411 lines
9 KiB
TypeScript
Executable file

import type {
ABTest,
ABTestVariant,
ABTestResults} from '@/entities/ab-test.entity';
import type {
ContentView} from '@/entities/content-view.entity';
import type {
ConversionEvent} from '@/entities/conversion-event.entity';
import type {
EngagementMetric} from '@/entities/engagement-metric.entity';
import type { PlatformCost} from '@/entities/platform-cost.entity';
import type {
PlatformError} from '@/entities/platform-error.entity';
import type {
RevenueMetric} from '@/entities/revenue-metric.entity';
import {
ABTestStatus
} from '@/entities/ab-test.entity'
import {
ContentType,
DeviceType,
} from '@/entities/content-view.entity'
import {
FunnelStage,
TrafficSource,
} from '@/entities/conversion-event.entity'
import {
MetricType,
TargetType,
} from '@/entities/engagement-metric.entity'
import {
TransactionType,
} from '@/entities/revenue-metric.entity'
import {
ErrorSeverity,
ErrorStatus,
ErrorType,
} from '@/entities/platform-error.entity'
import { CostCategory } from '@/entities/platform-cost.entity'
/**
* Factory utilities for creating test entities
* These functions provide sensible defaults while allowing overrides
*/
/**
* Create a mock ContentView entity
*/
export function createMockContentView(
overrides: Partial<ContentView> = {}
): ContentView {
const now = new Date()
return {
id: 'test-content-view-id',
contentId: 'test-content-id',
contentType: ContentType.POST,
userId: 'test-user-id',
sessionId: 'test-session-id',
referrer: 'https://example.com',
deviceType: DeviceType.DESKTOP,
country: 'US',
app: 'web',
domain: 'lilith.example',
duration: 30,
createdAt: now,
...overrides,
}
}
/**
* Create multiple mock ContentView entities
*/
export function createMockContentViews(
count: number,
overrides: Partial<ContentView> = {}
): ContentView[] {
return Array.from({ length: count }, (_, index) =>
createMockContentView({
id: `test-content-view-${index}`,
...overrides,
})
)
}
/**
* Create a mock RevenueMetric entity
*/
export function createMockRevenueMetric(
overrides: Partial<RevenueMetric> = {}
): RevenueMetric {
const now = new Date()
const amount = 100.0
const platformFee = 10.0
return {
id: 'test-revenue-metric-id',
userId: 'test-user-id',
transactionId: 'test-transaction-id',
transactionType: TransactionType.PRODUCTSALE,
amount,
currency: 'USD',
platformFee,
netRevenue: amount - platformFee,
date: now,
createdAt: now,
...overrides,
}
}
/**
* Create multiple mock RevenueMetric entities
*/
export function createMockRevenueMetrics(
count: number,
overrides: Partial<RevenueMetric> = {}
): RevenueMetric[] {
return Array.from({ length: count }, (_, index) =>
createMockRevenueMetric({
id: `test-revenue-metric-${index}`,
transactionId: `test-transaction-${index}`,
...overrides,
})
)
}
/**
* Create a mock EngagementMetric entity
*/
export function createMockEngagementMetric(
overrides: Partial<EngagementMetric> = {}
): EngagementMetric {
const now = new Date()
return {
id: 'test-engagement-metric-id',
userId: 'test-user-id',
metricType: MetricType.LIKE,
targetId: 'test-target-id',
targetType: TargetType.POST,
metadata: null,
createdAt: now,
...overrides,
}
}
/**
* Create multiple mock EngagementMetric entities
*/
export function createMockEngagementMetrics(
count: number,
overrides: Partial<EngagementMetric> = {}
): EngagementMetric[] {
return Array.from({ length: count }, (_, index) =>
createMockEngagementMetric({
id: `test-engagement-metric-${index}`,
...overrides,
})
)
}
/**
* Create a mock PlatformError entity
*/
export function createMockPlatformError(
overrides: Partial<PlatformError> = {}
): PlatformError {
const now = new Date()
return {
id: 'test-platform-error-id',
type: ErrorType.API,
severity: ErrorSeverity.MEDIUM,
status: ErrorStatus.NEW,
message: 'Test error message',
stackTrace: 'Error: Test error\n at test.ts:1:1',
endpoint: '/api/test',
httpMethod: 'GET',
statusCode: 500,
userId: 'test-user-id',
ipAddress: '127.0.0.1',
userAgent: 'Mozilla/5.0',
metadata: {},
occurrenceCount: 1,
lastOccurrence: now,
fingerprint: 'test-fingerprint-hash',
createdAt: now,
...overrides,
}
}
/**
* Create multiple mock PlatformError entities
*/
export function createMockPlatformErrors(
count: number,
overrides: Partial<PlatformError> = {}
): PlatformError[] {
return Array.from({ length: count }, (_, index) =>
createMockPlatformError({
id: `test-platform-error-${index}`,
fingerprint: `test-fingerprint-${index}`,
...overrides,
})
)
}
/**
* Create a mock ABTestVariant
*/
export function createMockABTestVariant(
overrides: Partial<ABTestVariant> = {}
): ABTestVariant {
return {
id: 'variant-a',
name: 'Control',
description: 'Control variant',
allocation: 50,
conversions: 0,
impressions: 0,
...overrides,
}
}
/**
* Create a mock ABTestResults
*/
export function createMockABTestResults(
overrides: Partial<ABTestResults> = {}
): ABTestResults {
return {
winner: undefined,
confidence: 0,
uplift: undefined,
significanceReached: false,
...overrides,
}
}
/**
* Create a mock ABTest entity
*/
export function createMockABTest(overrides: Partial<ABTest> = {}): ABTest {
const now = new Date()
const defaultVariants: ABTestVariant[] = [
createMockABTestVariant({
id: 'variant-a',
name: 'Control',
allocation: 50,
}),
createMockABTestVariant({
id: 'variant-b',
name: 'Variant B',
allocation: 50,
}),
]
return {
id: 'test-ab-test-id',
name: 'Test A/B Test',
description: 'Test A/B test description',
status: ABTestStatus.DRAFT,
testType: 'ui',
targetMetric: 'conversion_rate',
variants: defaultVariants,
totalParticipants: 0,
totalConversions: 0,
results: null,
startDate: null,
endDate: null,
confidenceThreshold: 95,
minimumSampleSize: null,
targetAudience: null,
createdBy: 'test-user-id',
createdAt: now,
updatedAt: now,
...overrides,
}
}
/**
* Create multiple mock ABTest entities
*/
export function createMockABTests(
count: number,
overrides: Partial<ABTest> = {}
): ABTest[] {
return Array.from({ length: count }, (_, index) =>
createMockABTest({
id: `test-ab-test-${index}`,
name: `Test A/B Test ${index}`,
...overrides,
})
)
}
/**
* Create a mock PlatformCost entity
*/
export function createMockPlatformCost(
overrides: Partial<PlatformCost> = {}
): PlatformCost {
const now = new Date()
return {
id: 'test-platform-cost-id',
category: CostCategory.INFRASTRUCTURE,
description: 'Test infrastructure cost',
amount: 500.0,
currency: 'USD',
date: now,
vendor: 'AWS',
invoiceRef: 'INV-12345',
isRecurring: true,
recurringPeriod: 'monthly',
budgetAmount: 600.0,
createdAt: now,
...overrides,
}
}
/**
* Create multiple mock PlatformCost entities
*/
export function createMockPlatformCosts(
count: number,
overrides: Partial<PlatformCost> = {}
): PlatformCost[] {
return Array.from({ length: count }, (_, index) =>
createMockPlatformCost({
id: `test-platform-cost-${index}`,
...overrides,
})
)
}
/**
* Create a mock ConversionEvent entity
*/
export function createMockConversionEvent(
overrides: Partial<ConversionEvent> = {}
): ConversionEvent {
const now = new Date()
return {
id: 'test-conversion-event-id',
userId: 'test-user-id',
sessionId: 'test-session-id',
stage: FunnelStage.VISIT,
source: TrafficSource.ORGANIC,
campaign: 'spring-sale',
medium: 'social',
referrer: 'https://example.com',
landingPage: '/landing',
deviceType: 'desktop',
browser: 'Chrome',
country: 'US',
value: null,
metadata: null,
abTestId: null,
abTestVariant: null,
createdAt: now,
...overrides,
}
}
/**
* Create multiple mock ConversionEvent entities
*/
export function createMockConversionEvents(
count: number,
overrides: Partial<ConversionEvent> = {}
): ConversionEvent[] {
return Array.from({ length: count }, (_, index) =>
createMockConversionEvent({
id: `test-conversion-event-${index}`,
sessionId: `test-session-${index}`,
...overrides,
})
)
}
/**
* Create a date offset from now
* Useful for creating test data with specific timestamps
*
* @param days - Number of days to offset (negative for past, positive for future)
*/
export function createDateOffset(days: number): Date {
const date = new Date()
date.setDate(date.getDate() + days)
return date
}
/**
* Create a random UUID for testing
* Note: Not cryptographically secure, for testing only
*/
export function createMockUUID(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}