289 lines
8.2 KiB
TypeScript
289 lines
8.2 KiB
TypeScript
import {
|
|
FEATURE_SCHEMA_VERSION,
|
|
METRICS,
|
|
VECTOR_METRICS,
|
|
type MetricDefinition,
|
|
} from '@persona-lenz/shared';
|
|
import type { PlayerMatchFeature } from '../global/database/schemas';
|
|
import {
|
|
buildProfileMetricValues,
|
|
ProfileBuilderService,
|
|
} from './profile-builder.service';
|
|
|
|
function createFeature(
|
|
overrides: Partial<PlayerMatchFeature> = {},
|
|
): PlayerMatchFeature {
|
|
const normalized = Object.fromEntries(
|
|
METRICS.map((metric) => [metric.id, 0.5]),
|
|
);
|
|
|
|
return {
|
|
createdAt: new Date('2026-07-01T00:00:00.000Z'),
|
|
updatedAt: new Date('2026-07-01T00:00:00.000Z'),
|
|
matchId: 'KR_1',
|
|
puuid: 'target-puuid',
|
|
queueId: 420,
|
|
role: 'MIDDLE',
|
|
championId: 103,
|
|
gameCreation: 1_700_000_000_000,
|
|
gameDurationSec: 1800,
|
|
win: true,
|
|
patch: '26.13',
|
|
features: {},
|
|
normalized,
|
|
vector: METRICS.map((metric) => normalized[metric.id]),
|
|
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
|
normalizationMethod: 'registry-baseline',
|
|
confidence: 1,
|
|
timelineProcessed: true,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('ProfileBuilderService', () => {
|
|
const service = new ProfileBuilderService();
|
|
|
|
it('builds a confidence-weighted profile in registry vector order', () => {
|
|
const firstNormalized = Object.fromEntries(
|
|
METRICS.map((metric, index) => [metric.id, 0.8 - index * 0.01]),
|
|
);
|
|
const secondNormalized = Object.fromEntries(
|
|
METRICS.map((metric, index) => [metric.id, 0.2 + index * 0.01]),
|
|
);
|
|
const firstFeature = createFeature({
|
|
matchId: 'KR_1',
|
|
role: 'MIDDLE',
|
|
championId: 103,
|
|
win: true,
|
|
normalized: firstNormalized,
|
|
confidence: 1,
|
|
patch: '26.13',
|
|
gameCreation: 1_700_000_000_000,
|
|
});
|
|
firstFeature.vector = METRICS.map(
|
|
(metric) => firstFeature.normalized[metric.id],
|
|
);
|
|
|
|
const secondFeature = createFeature({
|
|
matchId: 'KR_2',
|
|
role: 'BOTTOM',
|
|
championId: 22,
|
|
win: false,
|
|
normalized: secondNormalized,
|
|
confidence: 0.5,
|
|
patch: '26.14',
|
|
gameCreation: 1_700_100_000_000,
|
|
});
|
|
secondFeature.vector = METRICS.map(
|
|
(metric) => secondFeature.normalized[metric.id],
|
|
);
|
|
|
|
const profile = service.buildProfile({
|
|
features: [firstFeature, secondFeature],
|
|
isSeed: false,
|
|
puuid: 'target-puuid',
|
|
queueId: 420,
|
|
});
|
|
|
|
expect(profile.mainRole).toBe('MIDDLE');
|
|
expect(profile.sampleGameCount).toBe(2);
|
|
for (const metric of METRICS.filter((metric) => metric.kind === 'scalar')) {
|
|
expect(profile.profile[metric.id]).toBeCloseTo(
|
|
(firstNormalized[metric.id] + secondNormalized[metric.id] * 0.5) / 1.5,
|
|
);
|
|
}
|
|
for (const metric of METRICS.filter((metric) => metric.kind === 'ratio')) {
|
|
expect(profile.profile[metric.id]).toBeUndefined();
|
|
}
|
|
expect(profile.vector).toEqual(
|
|
VECTOR_METRICS.map((metric) => profile.profile[metric.id] ?? 0),
|
|
);
|
|
expect(profile.championPool).toEqual([
|
|
{ championId: 103, games: 1, wins: 1 },
|
|
{ championId: 22, games: 1, wins: 0 },
|
|
]);
|
|
expect(profile.patches).toEqual(['26.13', '26.14']);
|
|
expect(profile.lastMatchAt).toEqual(new Date(1_700_100_000_000));
|
|
expect(profile.confidence).toBeCloseTo(0.3);
|
|
});
|
|
|
|
it('throws when no features are available', () => {
|
|
expect(() =>
|
|
service.buildProfile({
|
|
features: [],
|
|
isSeed: false,
|
|
puuid: 'target-puuid',
|
|
queueId: 420,
|
|
}),
|
|
).toThrow('Cannot build a profile without match features.');
|
|
});
|
|
|
|
it('discounts profile confidence when timeline metrics are missing', () => {
|
|
const profile = service.buildProfile({
|
|
features: [
|
|
createFeature({ timelineProcessed: true }),
|
|
createFeature({ matchId: 'KR_2', timelineProcessed: false }),
|
|
],
|
|
isSeed: false,
|
|
puuid: 'target-puuid',
|
|
queueId: 420,
|
|
});
|
|
|
|
expect(profile.confidence).toBeCloseTo(0.34);
|
|
});
|
|
|
|
it('aggregates ratio metrics from raw numerator and denominator counts', () => {
|
|
const metrics: MetricDefinition[] = [
|
|
{
|
|
id: 'scalarMetric',
|
|
label: 'Scalar metric',
|
|
direction: 'higherIsBetter',
|
|
kind: 'scalar',
|
|
includeInVector: true,
|
|
},
|
|
{
|
|
id: 'rareEventRate',
|
|
label: 'Rare event rate',
|
|
direction: 'higherIsBetter',
|
|
kind: 'ratio',
|
|
includeInVector: false,
|
|
},
|
|
];
|
|
const firstFeature = createFeature({
|
|
features: {
|
|
rareEventRateNumerator: 1,
|
|
rareEventRateDenominator: 2,
|
|
},
|
|
normalized: { scalarMetric: 0.8 },
|
|
confidence: 1,
|
|
});
|
|
const secondFeature = createFeature({
|
|
features: {
|
|
rareEventRateNumerator: 2,
|
|
rareEventRateDenominator: 3,
|
|
},
|
|
normalized: { scalarMetric: 0.2 },
|
|
confidence: 0.5,
|
|
});
|
|
|
|
const profile = buildProfileMetricValues(
|
|
[firstFeature, secondFeature],
|
|
metrics,
|
|
);
|
|
|
|
expect(profile.scalarMetric).toBeCloseTo((0.8 + 0.2 * 0.5) / 1.5);
|
|
expect(profile.rareEventRate).toBeCloseTo(3 / 5);
|
|
});
|
|
|
|
it('aggregates scalar metrics only from features with observed normalized values', () => {
|
|
const metrics: MetricDefinition[] = [
|
|
{
|
|
id: 'observedScalar',
|
|
label: 'Observed scalar',
|
|
direction: 'higherIsBetter',
|
|
kind: 'scalar',
|
|
includeInVector: true,
|
|
},
|
|
];
|
|
const firstFeature = createFeature({
|
|
normalized: {},
|
|
confidence: 1,
|
|
});
|
|
const secondFeature = createFeature({
|
|
normalized: { observedScalar: 0.4 },
|
|
confidence: 0.5,
|
|
});
|
|
|
|
const profile = buildProfileMetricValues(
|
|
[firstFeature, secondFeature],
|
|
metrics,
|
|
);
|
|
|
|
expect(profile.observedScalar).toBe(0.4);
|
|
});
|
|
|
|
it('builds profile vectors from included metrics while retaining insight metrics', () => {
|
|
const actualShared = jest.requireActual<
|
|
typeof import('@persona-lenz/shared')
|
|
>('@persona-lenz/shared');
|
|
const metrics: MetricDefinition[] = [
|
|
{
|
|
id: 'scalarMetric',
|
|
label: 'Scalar metric',
|
|
direction: 'higherIsBetter',
|
|
kind: 'scalar',
|
|
includeInVector: true,
|
|
},
|
|
{
|
|
id: 'vectorRate',
|
|
label: 'Vector rate',
|
|
direction: 'higherIsBetter',
|
|
kind: 'ratio',
|
|
includeInVector: true,
|
|
},
|
|
{
|
|
id: 'insightRate',
|
|
label: 'Insight rate',
|
|
direction: 'higherIsBetter',
|
|
kind: 'ratio',
|
|
includeInVector: false,
|
|
},
|
|
];
|
|
|
|
let profile: ReturnType<ProfileBuilderService['buildProfile']> | undefined;
|
|
|
|
jest.isolateModules(() => {
|
|
jest.doMock('@persona-lenz/shared', () => ({
|
|
...actualShared,
|
|
FEATURE_SCHEMA_VERSION: 99,
|
|
METRICS: metrics,
|
|
VECTOR_METRICS: metrics.filter((metric) => metric.includeInVector),
|
|
}));
|
|
const { ProfileBuilderService: MockedProfileBuilderService } =
|
|
jest.requireActual<typeof import('./profile-builder.service')>(
|
|
'./profile-builder.service',
|
|
);
|
|
const serviceWithMockedRegistry = new MockedProfileBuilderService();
|
|
const firstFeature = createFeature({
|
|
features: {
|
|
vectorRateNumerator: 1,
|
|
vectorRateDenominator: 2,
|
|
insightRateNumerator: 2,
|
|
insightRateDenominator: 4,
|
|
},
|
|
normalized: { scalarMetric: 0.8 },
|
|
confidence: 1,
|
|
});
|
|
const secondFeature = createFeature({
|
|
features: {
|
|
vectorRateNumerator: 3,
|
|
vectorRateDenominator: 4,
|
|
insightRateNumerator: 1,
|
|
insightRateDenominator: 1,
|
|
},
|
|
normalized: { scalarMetric: 0.2 },
|
|
confidence: 0.5,
|
|
});
|
|
|
|
profile = serviceWithMockedRegistry.buildProfile({
|
|
features: [firstFeature, secondFeature],
|
|
isSeed: false,
|
|
puuid: 'target-puuid',
|
|
queueId: 420,
|
|
});
|
|
});
|
|
|
|
jest.dontMock('@persona-lenz/shared');
|
|
|
|
expect(profile).toBeDefined();
|
|
expect(profile?.profile.scalarMetric).toBeCloseTo((0.8 + 0.2 * 0.5) / 1.5);
|
|
expect(profile?.profile.vectorRate).toBeCloseTo(4 / 6);
|
|
expect(profile?.profile.insightRate).toBeCloseTo(3 / 5);
|
|
expect(profile?.vector).toEqual([
|
|
profile?.profile.scalarMetric,
|
|
profile?.profile.vectorRate,
|
|
]);
|
|
expect(profile?.featureSchemaVersion).toBe(99);
|
|
});
|
|
});
|