Files
persona-lenz/apps/api/src/modules/analysis/profile-builder.service.spec.ts
T
2026-07-02 20:40:49 -07:00

110 lines
3.2 KiB
TypeScript

import { FEATURE_SCHEMA_VERSION, METRICS } from '@persona-lenz/shared';
import type { PlayerMatchFeature } from '../global/database/schemas';
import { 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: false,
...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) {
expect(profile.profile[metric.id]).toBeCloseTo(
(firstNormalized[metric.id] + secondNormalized[metric.id] * 0.5) / 1.5,
);
}
expect(profile.vector).toEqual(
METRICS.map((metric) => profile.profile[metric.id]),
);
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.');
});
});