Files
persona-lenz/apps/api/src/modules/analysis/metric-normalization.service.spec.ts
T

149 lines
4.5 KiB
TypeScript

import { FEATURE_SCHEMA_VERSION, VECTOR_METRICS } from '@persona-lenz/shared';
import type { ConnectionService } from '../global/database/connection.service';
import type { MetricBaseline } from '../global/database/schemas';
import type { ExtractedPlayerMatchFeature } from './analysis.types';
import { MetricNormalizationService } from './metric-normalization.service';
interface ExecQuery<T> {
exec: jest.Mock<Promise<T>, []>;
}
function leanExecResolved<T>(value: T) {
return {
lean: jest.fn((): ExecQuery<T> => ({
exec: jest.fn<Promise<T>, []>().mockResolvedValue(value),
})),
};
}
function createFeature(
overrides: Partial<ExtractedPlayerMatchFeature> = {},
): ExtractedPlayerMatchFeature {
return {
matchId: 'KR_1',
puuid: 'target-puuid',
queueId: 420,
role: 'MIDDLE',
championId: 103,
gameCreation: 1_700_000_000_000,
gameDurationSec: 1_800,
win: true,
patch: '26.13',
features: {
csPerMin: 8,
kda: 30,
},
normalized: {
csPerMin: 0.5,
kda: 0.5,
},
vector: VECTOR_METRICS.map((metric) =>
metric.id === 'kda' || metric.id === 'csPerMin' ? 0.5 : 0,
),
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
normalizationMethod: 'registry-baseline',
confidence: 1,
timelineProcessed: true,
...overrides,
};
}
function createBaseline(
metricId: string,
overrides: Partial<MetricBaseline> = {},
): MetricBaseline {
return {
createdAt: new Date('2026-07-01T00:00:00.000Z'),
updatedAt: new Date('2026-07-01T00:00:00.000Z'),
metricId,
role: 'MIDDLE',
cohort: 'MASTER_PLUS',
patch: 'ALL',
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
sampleCount: 100,
percentileBreakpoints: [
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
170, 180, 190,
],
computedAt: new Date('2026-07-03T12:00:00.000Z'),
...overrides,
};
}
describe('MetricNormalizationService', () => {
it('normalizes observed metrics with population percentile baselines', async () => {
const find = jest.fn().mockReturnValue(
leanExecResolved([
createBaseline('kda'),
createBaseline('csPerMin', {
percentileBreakpoints: [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
],
patch: '26.13',
}),
createBaseline('csPerMin', {
percentileBreakpoints: [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
],
}),
]),
);
const connectionService = {
getModel: jest.fn().mockResolvedValue({ find }),
} as unknown as ConnectionService;
const service = new MetricNormalizationService(connectionService);
const normalizedFeature = await service.normalizeFeature(createFeature());
expect(find).toHaveBeenCalledWith({
cohort: 'MASTER_PLUS',
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
metricId: {
$in: ['csPerMin', 'kda'],
},
patch: {
$in: ['26.13', 'ALL'],
},
role: 'MIDDLE',
});
expect(normalizedFeature.normalizationMethod).toBe('population-percentile');
expect(normalizedFeature.normalized.kda).toBeCloseTo(0.15);
expect(normalizedFeature.normalized.csPerMin).toBeCloseTo(0.4);
expect(normalizedFeature.vector).toEqual(
VECTOR_METRICS.map(
(metric) => normalizedFeature.normalized[metric.id] ?? 0,
),
);
});
it('keeps registry normalization when a baseline is missing', async () => {
const find = jest
.fn()
.mockReturnValue(leanExecResolved([createBaseline('kda')]));
const connectionService = {
getModel: jest.fn().mockResolvedValue({ find }),
} as unknown as ConnectionService;
const service = new MetricNormalizationService(connectionService);
const feature = createFeature();
const normalizedFeature = await service.normalizeFeature(feature);
expect(normalizedFeature).toBe(feature);
expect(normalizedFeature.normalizationMethod).toBe('registry-baseline');
});
it('keeps registry normalization for unknown roles', async () => {
const find = jest.fn();
const connectionService = {
getModel: jest.fn().mockResolvedValue({ find }),
} as unknown as ConnectionService;
const service = new MetricNormalizationService(connectionService);
const feature = createFeature({ role: 'UNKNOWN' });
const normalizedFeature = await service.normalizeFeature(feature);
expect(normalizedFeature).toBe(feature);
expect(find).not.toHaveBeenCalled();
});
});