Files
persona-lenz/apps/api/src/modules/analysis/timeline-metric.service.spec.ts
T
2026-07-03 00:29:51 -07:00

719 lines
20 KiB
TypeScript

import { VECTOR_METRICS } from '@persona-lenz/shared';
import type { ExtractedPlayerMatchFeature } from './analysis.types';
import { TimelineMetricService } from './timeline-metric.service';
function createBaseFeature(): 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: {
kda: 3,
},
normalized: {
kda: 0.5,
},
vector: [],
featureSchemaVersion: 2,
normalizationMethod: 'registry-baseline',
confidence: 1,
timelineProcessed: false,
laneOpponent: {
puuid: 'enemy-mid-puuid',
championId: 134,
},
};
}
function createRawMatch() {
return {
metadata: {
matchId: 'KR_1',
},
info: {
participants: [
{
participantId: 1,
puuid: 'target-puuid',
teamId: 100,
teamPosition: 'MIDDLE',
},
{
participantId: 2,
puuid: 'ally-jungle-puuid',
teamId: 100,
teamPosition: 'JUNGLE',
},
{
participantId: 3,
puuid: 'ally-top-puuid',
teamId: 100,
teamPosition: 'TOP',
},
{
participantId: 6,
puuid: 'enemy-mid-puuid',
teamId: 200,
teamPosition: 'MIDDLE',
},
{
participantId: 7,
puuid: 'enemy-jungle-puuid',
teamId: 200,
teamPosition: 'JUNGLE',
},
],
},
};
}
function participantFrame(overrides: Record<string, unknown>) {
return {
jungleMinionsKilled: 0,
minionsKilled: 0,
position: { x: 0, y: 0 },
timeEnemySpentControlled: 0,
totalGold: 0,
xp: 0,
...overrides,
};
}
function createTimeline() {
return {
info: {
frames: [
{
timestamp: 8 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({ position: { x: 4_000, y: 4_000 } }),
'2': participantFrame({ position: { x: 5_000, y: 4_000 } }),
'3': participantFrame({ position: { x: 10_000, y: 4_000 } }),
},
},
{
timestamp: 10 * 60_000,
events: [
{
type: 'CHAMPION_KILL',
timestamp: 100_000,
killerId: 1,
victimId: 6,
assistingParticipantIds: [2],
},
{
type: 'CHAMPION_KILL',
timestamp: 200_000,
killerId: 2,
victimId: 7,
assistingParticipantIds: [1],
},
{
type: 'CHAMPION_KILL',
timestamp: 300_000,
killerId: 2,
victimId: 7,
assistingParticipantIds: null,
victimDamageReceived: [{ participantId: 1 }],
},
{
type: 'CHAMPION_KILL',
timestamp: 400_000,
killerId: 6,
victimId: 1,
assistingParticipantIds: [],
},
{
type: 'WARD_PLACED',
timestamp: 500_000,
creatorId: 1,
},
],
participantFrames: {
'1': participantFrame({
jungleMinionsKilled: 5,
minionsKilled: 75,
position: { x: 4_000, y: 4_000 },
timeEnemySpentControlled: 10_000,
totalGold: 4_000,
xp: 5_000,
}),
'2': participantFrame({ position: { x: 4_800, y: 4_000 } }),
'3': participantFrame({ position: { x: 9_000, y: 4_000 } }),
'6': participantFrame({
jungleMinionsKilled: 0,
minionsKilled: 70,
totalGold: 3_500,
xp: 4_500,
}),
},
},
{
timestamp: 14 * 60_000,
events: [
{
type: 'WARD_PLACED',
timestamp: 830_000,
creatorId: 1,
},
],
participantFrames: {
'1': participantFrame({
position: { x: 4_000, y: 4_000 },
timeEnemySpentControlled: 30_000,
totalGold: 6_000,
}),
'2': participantFrame({ position: { x: 5_500, y: 4_000 } }),
'3': participantFrame({ position: { x: 11_000, y: 4_000 } }),
'6': participantFrame({
totalGold: 6_200,
}),
},
},
{
timestamp: 15 * 60_000,
events: [
{
type: 'WARD_PLACED',
timestamp: 850_000,
creatorId: 1,
},
{
type: 'ELITE_MONSTER_KILL',
timestamp: 900_000,
killerId: 2,
killerTeamId: 100,
monsterType: 'DRAGON',
assistingParticipantIds: [1],
},
],
participantFrames: {},
},
{
timestamp: 20 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({
position: { x: 4_000, y: 4_000 },
timeEnemySpentControlled: 60_000,
}),
'2': participantFrame({ position: { x: 7_000, y: 4_000 } }),
'3': participantFrame({ position: { x: 12_000, y: 4_000 } }),
},
},
{
timestamp: 30 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({
timeEnemySpentControlled: 90_000,
}),
},
},
],
},
};
}
function createInsightTimeline() {
return {
info: {
frames: [
{
timestamp: 10 * 60_000,
events: [
{
actualStartTime: 640_000,
type: 'OBJECTIVE_BOUNTY_PRESTART',
timestamp: 590_000,
teamId: 100,
},
],
participantFrames: {
'1': participantFrame({
minionsKilled: 80,
position: { x: 7_500, y: 7_500 },
totalGold: 4_000,
xp: 5_000,
}),
'6': participantFrame({
minionsKilled: 72,
totalGold: 3_400,
xp: 4_700,
}),
},
},
{
timestamp: 11 * 60_000,
events: [
{
type: 'CHAMPION_KILL',
timestamp: 650_000,
killerId: 1,
victimId: 6,
shutdownBounty: 450,
assistingParticipantIds: [],
},
],
participantFrames: {
'1': participantFrame({ position: { x: 7_800, y: 9_000 } }),
},
},
{
timestamp: 12 * 60_000,
events: [
{
type: 'TURRET_PLATE_DESTROYED',
timestamp: 700_000,
teamId: 200,
laneType: 'MID_LANE',
},
{
type: 'OBJECTIVE_BOUNTY_FINISH',
timestamp: 700_000,
teamId: 100,
},
],
participantFrames: {
'1': participantFrame({ position: { x: 7_800, y: 7_800 } }),
},
},
{
timestamp: 800_000,
events: [
{
type: 'CHAMPION_KILL',
timestamp: 800_000,
killerId: 1,
victimId: 7,
assistingParticipantIds: [],
},
],
participantFrames: {
'1': participantFrame({ position: { x: 9_866, y: 4_414 } }),
},
},
{
timestamp: 14 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({
position: { x: 9_866, y: 4_414 },
totalGold: 5_800,
}),
'2': participantFrame({ position: { x: 8_800, y: 4_600 } }),
'3': participantFrame({ position: { x: 1_000, y: 13_000 } }),
'6': participantFrame({ totalGold: 5_500 }),
},
},
{
timestamp: 15 * 60_000,
events: [
{
type: 'ELITE_MONSTER_KILL',
timestamp: 900_000,
killerId: 2,
killerTeamId: 100,
monsterType: 'DRAGON',
assistingParticipantIds: [1],
position: { x: 9_866, y: 4_414 },
},
],
participantFrames: {
'1': participantFrame({ position: { x: 9_866, y: 4_414 } }),
},
},
{
timestamp: 20 * 60_000,
events: [
{
type: 'CHAMPION_KILL',
timestamp: 1_030_000,
killerId: 6,
victimId: 1,
assistingParticipantIds: [],
},
{
type: 'CHAMPION_KILL',
timestamp: 1_215_000,
killerId: 2,
victimId: 7,
assistingParticipantIds: [],
},
],
participantFrames: {
'1': participantFrame({
position: { x: 7_500, y: 7_500 },
totalGold: 8_000,
}),
'6': participantFrame({ totalGold: 7_700 }),
},
},
],
},
};
}
function createDeficitRecoveryTimeline() {
return {
info: {
frames: [
{
timestamp: 10 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({
minionsKilled: 65,
totalGold: 3_200,
xp: 4_200,
}),
'6': participantFrame({
minionsKilled: 75,
totalGold: 3_900,
xp: 4_600,
}),
},
},
{
timestamp: 20 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({ totalGold: 7_500 }),
'6': participantFrame({ totalGold: 7_300 }),
},
},
],
},
};
}
describe('TimelineMetricService', () => {
const service = new TimelineMetricService();
it('extracts timeline vector metrics and ratio counts', () => {
const feature = createBaseFeature();
const result = service.extractTimelineMetrics(
feature,
createRawMatch(),
createTimeline(),
{
advantageEvents: [],
deathEvents: [
{
t: 400_000,
victimParticipantId: 1,
x: 4_000,
y: 4_000,
zone: 'middleLane',
shutdownBountyGiven: 300,
nearestAllyDistanceApprox: 1_500,
},
{
t: 1_000_000,
victimParticipantId: 1,
x: 4_000,
y: 4_000,
zone: 'middleLane',
shutdownBountyGiven: 0,
nearestAllyDistanceApprox: 1_500,
},
],
fightClusters: [
{
startTimeMs: 100_000,
endTimeMs: 130_000,
killCount: 3,
participantIds: [1, 2, 6, 7],
openerParticipantIds: [1],
cleanupParticipantIds: [2],
isTeamfight: true,
},
{
startTimeMs: 600_000,
endTimeMs: 640_000,
killCount: 3,
participantIds: [1, 2, 6, 7],
openerParticipantIds: [2],
cleanupParticipantIds: [1],
isTeamfight: true,
},
],
objectiveWindows: [
{
objectiveTimeMs: 900_000,
startTimeMs: 780_000,
endTimeMs: 930_000,
killerParticipantId: 2,
killerTeamId: 100,
monsterType: 'DRAGON',
},
],
roamFrames: [],
},
);
expect(result.features).toMatchObject({
goldDiffAt10: 500,
goldDiffAt14: -200,
csDiffAt10: 10,
xpDiffAt10: 500,
deathsBefore14: 1,
killParticipationBefore14: 1,
earlyWardsPerMin: 2 / 14,
objectivePreWardRateNumerator: 1,
objectivePreWardRateDenominator: 1,
eliteObjectiveParticipationRateNumerator: 1,
eliteObjectiveParticipationRateDenominator: 1,
teamfightFirstEngagementParticipationRateNumerator: 1,
teamfightFirstEngagementParticipationRateDenominator: 2,
teamfightCleanupKillParticipationRateNumerator: 1,
teamfightCleanupKillParticipationRateDenominator: 2,
deathWithShutdownBountyGivenRateNumerator: 1,
deathWithShutdownBountyGivenRateDenominator: 2,
ccTimePerMinute: 3,
});
expect(result.features.isolationTendency).toBeCloseTo(
(1_000 + 800 + 1_500 + 3_000) / 4,
);
expect(result.normalized.objectivePreWardRate).toBe(1);
expect(result.normalized.eliteObjectiveParticipationRate).toBe(1);
expect(result.normalized.teamfightFirstEngagementParticipationRate).toBe(
0.5,
);
expect(result.vector).toHaveLength(VECTOR_METRICS.length);
expect(
result.vector[
VECTOR_METRICS.findIndex((metric) => metric.id === 'goldDiffAt10')
],
).toBeCloseTo(result.normalized.goldDiffAt10);
expect(
result.vector[
VECTOR_METRICS.findIndex(
(metric) => metric.id === 'objectivePreWardRate',
)
],
).toBe(1);
expect(
result.vector[
VECTOR_METRICS.findIndex(
(metric) => metric.id === 'deathWithShutdownBountyGivenRate',
)
],
).toBe(0.5);
expect(result.vector[VECTOR_METRICS.length - 1]).toBe(
result.normalized.ccTimePerMinute,
);
});
it('extracts timeline insight metrics outside the vector', () => {
const feature = createBaseFeature();
const result = service.extractTimelineMetrics(
feature,
createRawMatch(),
createInsightTimeline(),
{
advantageEvents: [
{
timestampMs: 600_000,
teamId: 100,
type: 'championKill',
score: 1,
participantIds: [1],
},
{
timestampMs: 650_000,
teamId: 100,
type: 'championKill',
score: 1,
participantIds: [1],
},
{
timestampMs: 1_030_000,
teamId: 200,
type: 'championKill',
score: 1,
participantIds: [6],
},
{
timestampMs: 1_215_000,
teamId: 100,
type: 'championKill',
score: 1,
participantIds: [2],
},
],
deathEvents: [
{
t: 1_000_000,
victimParticipantId: 1,
x: 7_500,
y: 7_500,
zone: 'middleLane',
shutdownBountyGiven: 0,
nearestAllyDistanceApprox: 5_000,
},
{
t: 1_200_000,
victimParticipantId: 1,
x: 7_500,
y: 7_500,
zone: 'middleLane',
shutdownBountyGiven: 0,
nearestAllyDistanceApprox: 1_000,
},
],
fightClusters: [
{
startTimeMs: 850_000,
endTimeMs: 870_000,
killCount: 2,
participantIds: [1, 2, 6, 7],
openerParticipantIds: [1],
cleanupParticipantIds: [2],
isTeamfight: false,
},
],
objectiveWindows: [
{
objectiveTimeMs: 900_000,
startTimeMs: 780_000,
endTimeMs: 930_000,
killerParticipantId: 2,
killerTeamId: 100,
monsterType: 'DRAGON',
position: { x: 9_866, y: 4_414 },
},
],
roamFrames: [
{
t: 600_000,
participantId: 1,
teamId: 100,
x: 7_500,
y: 7_500,
zone: 'river',
assignedLaneZone: 'middleLane',
},
{
t: 660_000,
participantId: 1,
teamId: 100,
x: 8_000,
y: 7_000,
zone: 'river',
assignedLaneZone: 'middleLane',
},
],
},
);
expect(result.features).toMatchObject({
objectiveFightOpeningParticipationRateNumerator: 1,
objectiveFightOpeningParticipationRateDenominator: 1,
objectiveFightOpeningConvertedToObjectiveRateNumerator: 1,
objectiveFightOpeningConvertedToObjectiveRateDenominator: 1,
preObjectivePitPresenceRateNumerator: 1,
preObjectivePitPresenceRateDenominator: 1,
preObjectivePickParticipationRateNumerator: 1,
preObjectivePickParticipationRateDenominator: 1,
deathFollowedByTeamLossRateNumerator: 1,
deathFollowedByTeamLossRateDenominator: 2,
deathTradedForTeamAdvantageRateNumerator: 1,
deathTradedForTeamAdvantageRateDenominator: 2,
deathWhileIsolatedFromNearestAllyRateNumerator: 1,
deathWhileIsolatedFromNearestAllyRateDenominator: 2,
laneLeadConversionRateNumerator: 1,
laneLeadConversionRateDenominator: 1,
roamConvertedToTeamAdvantageRateNumerator: 1,
roamConvertedToTeamAdvantageRateDenominator: 1,
shutdownGoldCollectedPerGame: 450,
platePresenceParticipationRateNumerator: 1,
platePresenceParticipationRateDenominator: 1,
objectiveBountyComebackParticipationRateNumerator: 1,
objectiveBountyComebackParticipationRateDenominator: 1,
});
expect(result.normalized.objectiveFightOpeningParticipationRate).toBe(1);
expect(result.normalized.deathFollowedByTeamLossRate).toBe(0.5);
expect(result.normalized.deathTradedForTeamAdvantageRate).toBe(0.5);
expect(result.normalized.deathWhileIsolatedFromNearestAllyRate).toBe(0.5);
expect(result.vector).toHaveLength(VECTOR_METRICS.length);
expect(
VECTOR_METRICS.some(
(metric) => metric.id === 'shutdownGoldCollectedPerGame',
),
).toBe(false);
});
it('extracts early deficit recovery as a sparse ratio metric', () => {
const feature = createBaseFeature();
const result = service.extractTimelineMetrics(
feature,
createRawMatch(),
createDeficitRecoveryTimeline(),
{
advantageEvents: [],
deathEvents: [],
fightClusters: [],
objectiveWindows: [],
roamFrames: [],
},
);
expect(result.features).toMatchObject({
goldDiffAt10: -700,
earlyDeficitRecoveryRateNumerator: 1,
earlyDeficitRecoveryRateDenominator: 1,
laneLeadConversionRateNumerator: 0,
laneLeadConversionRateDenominator: 0,
});
expect(result.normalized.earlyDeficitRecoveryRate).toBe(1);
});
it('does not count the death kill event as a follow-up team loss', () => {
const feature = createBaseFeature();
const result = service.extractTimelineMetrics(
feature,
createRawMatch(),
createTimeline(),
{
advantageEvents: [
{
timestampMs: 400_000,
teamId: 200,
type: 'championKill',
score: 1,
participantIds: [6],
},
],
deathEvents: [
{
t: 400_000,
victimParticipantId: 1,
x: 4_000,
y: 4_000,
zone: 'middleLane',
shutdownBountyGiven: 0,
nearestAllyDistanceApprox: 1_500,
},
],
fightClusters: [],
objectiveWindows: [],
roamFrames: [],
},
);
expect(result.features).toMatchObject({
deathFollowedByTeamLossRateNumerator: 0,
deathFollowedByTeamLossRateDenominator: 1,
});
expect(result.normalized.deathFollowedByTeamLossRate).toBe(0);
});
});