Files
persona-lenz/apps/api/src/modules/analysis/timeline-derived.service.spec.ts
T
2026-07-02 23:50:10 -07:00

677 lines
17 KiB
TypeScript

import { TimelineDerivedService } from './timeline-derived.service';
function createTimelinePayload(
eventsByFrame: Array<Record<string, unknown>[]>,
) {
return {
info: {
frames: eventsByFrame.map((events, index) => ({
timestamp: index * 60_000,
events,
})),
},
};
}
function createTimelinePayloadWithFrames(
frames: Array<{
timestamp?: number;
events?: Record<string, unknown>[];
participantFrames?: Record<string, unknown>;
}>,
) {
return {
info: {
frames: frames.map((frame, index) => ({
timestamp: frame.timestamp ?? index * 60_000,
events: frame.events ?? [],
participantFrames: frame.participantFrames ?? {},
})),
},
};
}
describe('TimelineDerivedService', () => {
const service = new TimelineDerivedService();
it('clusters champion kills transitively by time and distance', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'CHAMPION_KILL',
timestamp: 10_000,
killerId: 1,
victimId: 6,
assistingParticipantIds: [2],
position: { x: 1_000, y: 1_000 },
victimDamageReceived: [
{
participantId: 3,
magicDamageDone: 100,
physicalDamageDone: 50,
trueDamageDone: 0,
},
{
participantId: 1,
magicDamageDone: 20,
physicalDamageDone: 30,
trueDamageDone: 0,
},
],
},
{
type: 'CHAMPION_KILL',
timestamp: 24_000,
killerId: 2,
victimId: 7,
assistingParticipantIds: null,
victimDamageReceived: null,
position: { x: 3_400, y: 1_000 },
},
{
type: 'CHAMPION_KILL',
timestamp: 38_000,
killerId: 4,
victimId: 8,
assistingParticipantIds: [5],
position: { x: 5_800, y: 1_000 },
},
],
]),
);
expect(derived.fightClusters).toEqual([
{
startTimeMs: 10_000,
endTimeMs: 38_000,
killCount: 3,
participantIds: [1, 2, 3, 4, 5, 6, 7, 8],
openerParticipantIds: [1, 2, 3],
cleanupParticipantIds: [4, 5, 8],
isTeamfight: true,
},
]);
});
it('separates champion kills that are not connected by both time and distance', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'CHAMPION_KILL',
timestamp: 10_000,
killerId: 1,
victimId: 6,
position: { x: 1_000, y: 1_000 },
},
{
type: 'CHAMPION_KILL',
timestamp: 20_000,
killerId: 2,
victimId: 7,
position: { x: 7_000, y: 1_000 },
},
{
type: 'CHAMPION_KILL',
timestamp: 40_000,
killerId: 3,
victimId: 8,
position: { x: 7_500, y: 1_000 },
},
],
]),
);
expect(derived.fightClusters).toHaveLength(3);
expect(derived.fightClusters.map((cluster) => cluster.killCount)).toEqual([
1, 1, 1,
]);
expect(
derived.fightClusters.every((cluster) => cluster.isTeamfight === false),
).toBe(true);
});
it('includes every kill within five seconds of the last kill as cleanup', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'CHAMPION_KILL',
timestamp: 10_000,
killerId: 1,
victimId: 6,
position: { x: 1_000, y: 1_000 },
},
{
type: 'CHAMPION_KILL',
timestamp: 20_000,
killerId: 2,
victimId: 7,
position: { x: 2_000, y: 1_000 },
},
{
type: 'CHAMPION_KILL',
timestamp: 24_000,
killerId: 3,
victimId: 8,
position: { x: 2_500, y: 1_000 },
},
],
]),
);
expect(derived.fightClusters[0].cleanupParticipantIds).toEqual([
2, 3, 7, 8,
]);
});
it('uses explicit victim damage totals without adding component damage twice', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'CHAMPION_KILL',
timestamp: 10_000,
killerId: 1,
victimId: 6,
position: { x: 1_000, y: 1_000 },
victimDamageReceived: [
{
participantId: 3,
totalDamageDone: 100,
magicDamageDone: 1_000,
},
{
participantId: 4,
magicDamageDone: 150,
},
],
},
],
]),
);
expect(derived.fightClusters[0].openerParticipantIds).toEqual([1, 4]);
});
it('aggregates duplicate victim damage rows before selecting the opener contributor', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'CHAMPION_KILL',
timestamp: 10_000,
killerId: 1,
victimId: 6,
position: { x: 1_000, y: 1_000 },
victimDamageReceived: [
{
participantId: 3,
magicDamageDone: 80,
},
{
participantId: 3,
physicalDamageDone: 80,
},
{
participantId: 4,
magicDamageDone: 150,
},
],
},
],
]),
);
expect(derived.fightClusters[0].openerParticipantIds).toEqual([1, 3]);
});
it('builds objective windows around elite monster kill timestamps', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'ELITE_MONSTER_KILL',
timestamp: 900_000,
killerId: 4,
killerTeamId: 100,
monsterType: 'DRAGON',
monsterSubType: 'HEXTECH_DRAGON',
position: { x: 9_800, y: 4_400 },
},
{
type: 'WARD_PLACED',
timestamp: 905_000,
},
],
]),
);
expect(derived.objectiveWindows).toEqual([
{
objectiveTimeMs: 900_000,
startTimeMs: 780_000,
endTimeMs: 930_000,
killerParticipantId: 4,
killerTeamId: 100,
monsterType: 'DRAGON',
monsterSubType: 'HEXTECH_DRAGON',
position: { x: 9_800, y: 4_400 },
},
]);
});
it('ignores elite monster kills outside dragon, rift herald, and baron', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'ELITE_MONSTER_KILL',
timestamp: 900_000,
killerId: 4,
killerTeamId: 100,
monsterType: 'HORDE',
},
{
type: 'ELITE_MONSTER_KILL',
timestamp: 1_200_000,
killerId: 5,
killerTeamId: 200,
monsterType: 'BARON_NASHOR',
},
],
]),
);
expect(derived.objectiveWindows).toHaveLength(1);
expect(derived.objectiveWindows[0].monsterType).toBe('BARON_NASHOR');
});
it('builds death events with shutdown bounty, zone, and nearest ally approximation', () => {
const derived = service.buildDerivedData(
createTimelinePayloadWithFrames([
{
timestamp: 0,
participantFrames: {
'1': { position: { x: 9_870, y: 4_414 } },
'7': { position: { x: 9_000, y: 4_000 } },
'8': { position: { x: 12_000, y: 4_000 } },
},
},
{
timestamp: 30_000,
events: [
{
type: 'CHAMPION_KILL',
timestamp: 30_000,
killerId: 1,
victimId: 6,
shutdownBounty: 277,
position: { x: 9_866, y: 4_414 },
},
],
},
{
timestamp: 60_000,
participantFrames: {
'1': { position: { x: 9_870, y: 4_414 } },
'7': { position: { x: 10_000, y: 4_000 } },
'8': { position: { x: 12_000, y: 4_000 } },
},
},
]),
);
expect(derived.deathEvents).toEqual([
{
t: 30_000,
victimParticipantId: 6,
x: 9_866,
y: 4_414,
zone: 'dragonPit',
shutdownBountyGiven: 277,
nearestAllyDistanceApprox: 553,
},
]);
});
it('interpolates missing death positions from victim frames', () => {
const derived = service.buildDerivedData(
createTimelinePayloadWithFrames([
{
timestamp: 0,
participantFrames: {
'1': { position: { x: 4_020, y: 6_000 } },
'6': { position: { x: 3_000, y: 6_000 } },
'7': { position: { x: 8_000, y: 6_000 } },
},
},
{
timestamp: 30_000,
events: [
{
type: 'CHAMPION_KILL',
timestamp: 30_000,
killerId: 1,
victimId: 6,
},
],
},
{
timestamp: 60_000,
participantFrames: {
'1': { position: { x: 4_020, y: 6_000 } },
'6': { position: { x: 5_000, y: 6_000 } },
'7': { position: { x: 10_000, y: 6_000 } },
},
},
]),
);
expect(derived.deathEvents).toEqual([
{
t: 30_000,
victimParticipantId: 6,
x: 4_000,
y: 6_000,
zone: 'southWestJungle',
shutdownBountyGiven: 0,
nearestAllyDistanceApprox: 5_000,
},
]);
});
it('classifies representative Summoner Rift death zones', () => {
const positions = [
{ x: 600, y: 600, zone: 'blueBase' },
{ x: 14_200, y: 14_200, zone: 'redBase' },
{ x: 1_000, y: 9_000, zone: 'topLane' },
{ x: 6_500, y: 6_500, zone: 'middleLane' },
{ x: 9_000, y: 1_000, zone: 'bottomLane' },
{ x: 8_000, y: 7_000, zone: 'river' },
{ x: 2_500, y: 8_500, zone: 'northWestJungle' },
{ x: 12_000, y: 9_000, zone: 'northEastJungle' },
{ x: 4_000, y: 6_000, zone: 'southWestJungle' },
{ x: 13_000, y: 5_000, zone: 'southEastJungle' },
{ x: 9_866, y: 4_414, zone: 'dragonPit' },
{ x: 5_007, y: 10_471, zone: 'baronPit' },
{ x: -1, y: 7_500, zone: 'unknown' },
];
const derived = service.buildDerivedData(
createTimelinePayload([
positions.map((position, index) => ({
type: 'CHAMPION_KILL',
timestamp: (index + 1) * 1_000,
killerId: 1,
victimId: 6,
position: { x: position.x, y: position.y },
})),
]),
);
expect(derived.deathEvents.map((deathEvent) => deathEvent.zone)).toEqual(
positions.map((position) => position.zone),
);
});
it('builds roam frames for non-jungle laners between 8 and 14 minutes', () => {
const rawMatch = {
info: {
participants: [
{ participantId: 1, teamId: 100, teamPosition: 'TOP' },
{ participantId: 2, teamId: 100, teamPosition: 'JUNGLE' },
{ participantId: 6, teamId: 200, teamPosition: 'MIDDLE' },
],
},
};
const derived = service.buildDerivedData(
createTimelinePayloadWithFrames([
{
timestamp: 7 * 60_000,
participantFrames: {
'1': { position: { x: 8_000, y: 7_000 } },
},
},
{
timestamp: 8 * 60_000,
participantFrames: {
'1': { position: { x: 8_000, y: 7_000 } },
'2': { position: { x: 8_000, y: 7_000 } },
'6': { position: { x: 14_200, y: 14_200 } },
},
},
{
timestamp: 9 * 60_000,
participantFrames: {
'1': { position: { x: 1_000, y: 9_000 } },
'6': { position: { x: 8_000, y: 7_000 } },
},
},
{
timestamp: 14 * 60_000,
participantFrames: {
'1': { position: { x: 9_000, y: 1_000 } },
},
},
{
timestamp: 15 * 60_000,
participantFrames: {
'1': { position: { x: 8_000, y: 7_000 } },
},
},
]),
rawMatch,
);
expect(derived.roamFrames).toEqual([
{
t: 8 * 60_000,
participantId: 1,
teamId: 100,
x: 8_000,
y: 7_000,
zone: 'river',
assignedLaneZone: 'topLane',
},
{
t: 9 * 60_000,
participantId: 6,
teamId: 200,
x: 8_000,
y: 7_000,
zone: 'river',
assignedLaneZone: 'middleLane',
},
{
t: 14 * 60_000,
participantId: 1,
teamId: 100,
x: 9_000,
y: 1_000,
zone: 'bottomLane',
assignedLaneZone: 'topLane',
},
]);
});
it('uses the participant own base only when filtering roam frames', () => {
const rawMatch = {
info: {
participants: [{ participantId: 1, teamId: 100, teamPosition: 'TOP' }],
},
};
const derived = service.buildDerivedData(
createTimelinePayloadWithFrames([
{
timestamp: 8 * 60_000,
participantFrames: {
'1': { position: { x: 600, y: 600 } },
},
},
{
timestamp: 9 * 60_000,
participantFrames: {
'1': { position: { x: 14_200, y: 14_200 } },
},
},
]),
rawMatch,
);
expect(derived.roamFrames).toEqual([
{
t: 9 * 60_000,
participantId: 1,
teamId: 100,
x: 14_200,
y: 14_200,
zone: 'redBase',
assignedLaneZone: 'topLane',
},
]);
});
it('omits roam frames when participant id or map zone is unknown', () => {
const rawMatch = {
info: {
participants: [
{ teamId: 100, teamPosition: 'TOP' },
{ participantId: 2, teamId: 100, teamPosition: 'MIDDLE' },
],
},
};
const derived = service.buildDerivedData(
createTimelinePayloadWithFrames([
{
timestamp: 8 * 60_000,
participantFrames: {
'1': { position: { x: 8_000, y: 7_000 } },
'2': { position: { x: -1, y: 7_500 } },
},
},
]),
rawMatch,
);
expect(derived.roamFrames).toEqual([]);
});
it('builds weighted advantage events for kills, plates, towers, and objectives', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'CHAMPION_KILL',
timestamp: 10_000,
killerId: 1,
victimId: 6,
assistingParticipantIds: [2],
victimDamageReceived: [{ participantId: 3, magicDamageDone: 80 }],
},
{
type: 'TURRET_PLATE_DESTROYED',
timestamp: 20_000,
killerId: 0,
teamId: 200,
},
{
type: 'BUILDING_KILL',
timestamp: 30_000,
killerId: 2,
assistingParticipantIds: [3, 4],
buildingType: 'TOWER_BUILDING',
teamId: 200,
towerType: 'OUTER_TURRET',
},
{
type: 'ELITE_MONSTER_KILL',
timestamp: 40_000,
killerId: 3,
killerTeamId: 100,
monsterType: 'RIFTHERALD',
},
{
type: 'ELITE_MONSTER_KILL',
timestamp: 50_000,
killerId: 8,
killerTeamId: 200,
monsterType: 'BARON_NASHOR',
},
],
]),
);
expect(derived.advantageEvents).toEqual([
{
timestampMs: 10_000,
teamId: 100,
type: 'championKill',
score: 1,
participantIds: [1, 2, 3],
},
{
timestampMs: 20_000,
teamId: 100,
type: 'turretPlate',
score: 0.5,
participantIds: [],
},
{
timestampMs: 30_000,
teamId: 100,
type: 'tower',
score: 1.5,
participantIds: [2, 3, 4],
},
{
timestampMs: 40_000,
teamId: 100,
type: 'riftHerald',
score: 1.5,
participantIds: [3],
},
{
timestampMs: 50_000,
teamId: 200,
type: 'baron',
score: 3,
participantIds: [8],
},
]);
});
it('omits advantage events when the scoring team cannot be inferred', () => {
const derived = service.buildDerivedData(
createTimelinePayload([
[
{
type: 'CHAMPION_KILL',
timestamp: 10_000,
killerId: 0,
victimId: 6,
},
{
type: 'TURRET_PLATE_DESTROYED',
timestamp: 20_000,
killerId: 0,
},
{
type: 'BUILDING_KILL',
timestamp: 30_000,
buildingType: 'TOWER_BUILDING',
},
{
type: 'ELITE_MONSTER_KILL',
timestamp: 40_000,
killerId: 0,
monsterType: 'DRAGON',
},
],
]),
);
expect(derived.advantageEvents).toEqual([]);
});
});