Add analysis pipeline
This commit is contained in:
1 parent
256d776131
commit
770c198ccd
20 files changed
+2572
-1
No files matched your search
@@ -0,0 +1,355 @@
|
||||
import { FEATURE_SCHEMA_VERSION } from '@persona-lenz/shared';
|
||||
import type { ConnectionService } from '../global/database/connection.service';
|
||||
import type { Account } from '../global/database/schemas';
|
||||
import type { RiotApiClient } from '../riot/riot-api.client';
|
||||
import type { UserAnalysisJobData } from './analysis.types';
|
||||
import { FeatureExtractorService } from './feature-extractor.service';
|
||||
import { AnalysisPipelineService } from './analysis-pipeline.service';
|
||||
import { ProfileBuilderService } from './profile-builder.service';
|
||||
|
||||
function createJobData(): UserAnalysisJobData {
|
||||
return {
|
||||
gameName: 'Hide on bush',
|
||||
tagLine: 'KR1',
|
||||
platformRoute: 'kr',
|
||||
regionalRoute: 'asia',
|
||||
queueId: 420,
|
||||
requestedMatchCount: 20,
|
||||
};
|
||||
}
|
||||
|
||||
function createMatchPayload(matchId = 'KR_1') {
|
||||
return {
|
||||
metadata: {
|
||||
matchId,
|
||||
participants: ['target-puuid', 'ally-puuid'],
|
||||
},
|
||||
info: {
|
||||
gameCreation: 1_700_000_000_000,
|
||||
gameDuration: 1800,
|
||||
gameVersion: '26.13.123.456',
|
||||
queueId: 420,
|
||||
participants: [
|
||||
{
|
||||
puuid: 'target-puuid',
|
||||
teamId: 100,
|
||||
teamPosition: 'MIDDLE',
|
||||
championId: 103,
|
||||
kills: 6,
|
||||
deaths: 2,
|
||||
assists: 8,
|
||||
totalDamageDealtToChampions: 24_000,
|
||||
goldEarned: 12_000,
|
||||
totalMinionsKilled: 210,
|
||||
neutralMinionsKilled: 12,
|
||||
visionScore: 24,
|
||||
wardsKilled: 4,
|
||||
detectorWardsPlaced: 3,
|
||||
win: true,
|
||||
challenges: {
|
||||
kda: 7,
|
||||
killParticipation: 0.7,
|
||||
teamDamagePercentage: 0.32,
|
||||
damagePerMinute: 800,
|
||||
goldPerMinute: 400,
|
||||
visionScorePerMinute: 0.8,
|
||||
controlWardsPlaced: 3,
|
||||
soloKills: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
puuid: 'ally-puuid',
|
||||
teamId: 100,
|
||||
teamPosition: 'JUNGLE',
|
||||
championId: 64,
|
||||
kills: 10,
|
||||
totalDamageDealtToChampions: 30_000,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
interface ExecQuery<T> {
|
||||
exec: jest.Mock<Promise<T>, []>;
|
||||
}
|
||||
|
||||
type AcknowledgedUpdate = { acknowledged: boolean };
|
||||
type UpdateOneMock = jest.Mock<
|
||||
ExecQuery<AcknowledgedUpdate>,
|
||||
[Record<string, unknown>, Record<string, unknown>, Record<string, unknown>?]
|
||||
>;
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
function execResolved<T>(value: T): ExecQuery<T> {
|
||||
return { exec: jest.fn<Promise<T>, []>().mockResolvedValue(value) };
|
||||
}
|
||||
|
||||
function leanExecResolved<T>(value: T) {
|
||||
return {
|
||||
lean: jest.fn(() => execResolved(value)),
|
||||
};
|
||||
}
|
||||
|
||||
function createStoredAccount(overrides: Partial<Account> = {}): Account {
|
||||
return {
|
||||
createdAt: new Date('2026-07-01T00:00:00.000Z'),
|
||||
updatedAt: new Date('2026-07-01T00:00:00.000Z'),
|
||||
puuid: 'target-puuid',
|
||||
gameName: 'Hide on bush',
|
||||
tagLine: 'KR1',
|
||||
platformRoute: 'kr',
|
||||
regionalRoute: 'asia',
|
||||
profileIconId: 1,
|
||||
summonerLevel: 500,
|
||||
tiers: [],
|
||||
isSeed: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
interface PipelineHarnessOptions {
|
||||
accountError?: Error;
|
||||
existingMatchRaw?: Record<string, unknown> | null;
|
||||
getMatch?: jest.Mock<Promise<Record<string, unknown>>, [string, string]>;
|
||||
matchIds?: string[];
|
||||
storedAccount?: Account;
|
||||
}
|
||||
|
||||
function createPipelineHarness(options: PipelineHarnessOptions = {}) {
|
||||
const updateAnalysisJob: UpdateOneMock = jest
|
||||
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
|
||||
.mockReturnValue(execResolved({ acknowledged: true }));
|
||||
const findOneAndUpdateAccount = jest
|
||||
.fn()
|
||||
.mockReturnValue(
|
||||
leanExecResolved(options.storedAccount ?? createStoredAccount()),
|
||||
);
|
||||
const updateAccount: UpdateOneMock = jest
|
||||
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
|
||||
.mockReturnValue(execResolved({ acknowledged: true }));
|
||||
const findOneRiotMatch = jest
|
||||
.fn()
|
||||
.mockReturnValue(
|
||||
leanExecResolved(
|
||||
options.existingMatchRaw ? { raw: options.existingMatchRaw } : null,
|
||||
),
|
||||
);
|
||||
const updateRiotMatch: UpdateOneMock = jest
|
||||
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
|
||||
.mockReturnValue(execResolved({ acknowledged: true }));
|
||||
const updatePlayerMatchFeature: UpdateOneMock = jest
|
||||
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
|
||||
.mockReturnValue(execResolved({ acknowledged: true }));
|
||||
const updatePlayerProfile: UpdateOneMock = jest
|
||||
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
|
||||
.mockReturnValue(execResolved({ acknowledged: true }));
|
||||
|
||||
const models = {
|
||||
account: {
|
||||
findOneAndUpdate: findOneAndUpdateAccount,
|
||||
updateOne: updateAccount,
|
||||
},
|
||||
analysisJob: {
|
||||
updateOne: updateAnalysisJob,
|
||||
},
|
||||
playerMatchFeature: {
|
||||
updateOne: updatePlayerMatchFeature,
|
||||
},
|
||||
playerProfile: {
|
||||
updateOne: updatePlayerProfile,
|
||||
},
|
||||
riotMatch: {
|
||||
findOne: findOneRiotMatch,
|
||||
updateOne: updateRiotMatch,
|
||||
},
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn((modelKey: keyof typeof models) =>
|
||||
Promise.resolve(models[modelKey]),
|
||||
),
|
||||
} as unknown as ConnectionService;
|
||||
const getAccountByRiotId = options.accountError
|
||||
? jest.fn().mockRejectedValue(options.accountError)
|
||||
: jest.fn().mockResolvedValue({
|
||||
puuid: 'target-puuid',
|
||||
gameName: 'Hide on bush',
|
||||
tagLine: 'KR1',
|
||||
});
|
||||
const getMatch =
|
||||
options.getMatch ??
|
||||
jest
|
||||
.fn<Promise<Record<string, unknown>>, [string, string]>()
|
||||
.mockResolvedValue(createMatchPayload());
|
||||
const riotApiClient = {
|
||||
getAccountByRiotId,
|
||||
getSummonerByPuuid: jest.fn().mockResolvedValue({
|
||||
id: 'summoner-id',
|
||||
puuid: 'target-puuid',
|
||||
profileIconId: 1,
|
||||
summonerLevel: 500,
|
||||
}),
|
||||
getLeagueEntriesByPuuid: jest.fn().mockResolvedValue([]),
|
||||
getMatchIds: jest.fn().mockResolvedValue(options.matchIds ?? ['KR_1']),
|
||||
getMatch,
|
||||
} as unknown as RiotApiClient;
|
||||
|
||||
return {
|
||||
findOneRiotMatch,
|
||||
getMatch,
|
||||
service: new AnalysisPipelineService(
|
||||
connectionService,
|
||||
riotApiClient,
|
||||
new FeatureExtractorService(),
|
||||
new ProfileBuilderService(),
|
||||
),
|
||||
updateAnalysisJob,
|
||||
updatePlayerMatchFeature,
|
||||
updatePlayerProfile,
|
||||
updateRiotMatch,
|
||||
};
|
||||
}
|
||||
|
||||
describe('AnalysisPipelineService', () => {
|
||||
it('fetches missing match details, stores features, builds a profile, and completes the job', async () => {
|
||||
const {
|
||||
findOneRiotMatch,
|
||||
getMatch,
|
||||
service,
|
||||
updateAnalysisJob,
|
||||
updatePlayerMatchFeature,
|
||||
updatePlayerProfile,
|
||||
updateRiotMatch,
|
||||
} = createPipelineHarness();
|
||||
|
||||
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
|
||||
|
||||
expect(findOneRiotMatch).toHaveBeenCalledWith({ matchId: 'KR_1' });
|
||||
expect(getMatch).toHaveBeenCalledWith('KR_1', 'asia');
|
||||
|
||||
const riotMatchUpdate = updateRiotMatch.mock.calls[0][1];
|
||||
expect(riotMatchUpdate).toMatchObject({
|
||||
$setOnInsert: {
|
||||
matchId: 'KR_1',
|
||||
queueId: 420,
|
||||
},
|
||||
});
|
||||
|
||||
const playerMatchFeatureUpdate = updatePlayerMatchFeature.mock.calls[0][1];
|
||||
expect(playerMatchFeatureUpdate).toMatchObject({
|
||||
$set: {
|
||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||
matchId: 'KR_1',
|
||||
},
|
||||
});
|
||||
|
||||
const playerProfileUpdate = updatePlayerProfile.mock.calls[0][1];
|
||||
expect(playerProfileUpdate).toMatchObject({
|
||||
$set: {
|
||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||
puuid: 'target-puuid',
|
||||
sampleGameCount: 1,
|
||||
},
|
||||
});
|
||||
|
||||
const completedJobUpdate = updateAnalysisJob.mock.calls.find(
|
||||
([, update]) => asRecord(asRecord(update).$set).status === 'completed',
|
||||
);
|
||||
expect(completedJobUpdate).toBeDefined();
|
||||
expect(asRecord(asRecord(completedJobUpdate?.[1]).$set)).toMatchObject({
|
||||
status: 'completed',
|
||||
stage: 'completed',
|
||||
});
|
||||
});
|
||||
|
||||
it('completes without building a profile when no ranked matches are returned', async () => {
|
||||
const { service, updateAnalysisJob, updatePlayerProfile } =
|
||||
createPipelineHarness({
|
||||
matchIds: [],
|
||||
});
|
||||
|
||||
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
|
||||
|
||||
expect(updatePlayerProfile).not.toHaveBeenCalled();
|
||||
const completedJobUpdate = updateAnalysisJob.mock.calls.find(
|
||||
([, update]) => asRecord(asRecord(update).$set).status === 'completed',
|
||||
);
|
||||
expect(completedJobUpdate).toBeDefined();
|
||||
});
|
||||
|
||||
it('uses cached match details without recalling Riot Match-V5', async () => {
|
||||
const { getMatch, service, updatePlayerMatchFeature } =
|
||||
createPipelineHarness({
|
||||
existingMatchRaw: createMatchPayload(),
|
||||
});
|
||||
|
||||
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
|
||||
|
||||
expect(getMatch).not.toHaveBeenCalled();
|
||||
expect(updatePlayerMatchFeature).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('skips individual match fetch failures and still builds from successful matches', async () => {
|
||||
const getMatch = jest
|
||||
.fn<Promise<Record<string, unknown>>, [string, string]>()
|
||||
.mockRejectedValueOnce(new Error('temporary upstream failure'))
|
||||
.mockResolvedValueOnce(createMatchPayload('KR_2'));
|
||||
const { service, updatePlayerProfile } = createPipelineHarness({
|
||||
getMatch,
|
||||
matchIds: ['KR_1', 'KR_2'],
|
||||
});
|
||||
|
||||
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
|
||||
|
||||
expect(getMatch).toHaveBeenCalledTimes(2);
|
||||
const playerProfileUpdate = updatePlayerProfile.mock.calls[0][1];
|
||||
expect(playerProfileUpdate).toMatchObject({
|
||||
$set: {
|
||||
sampleGameCount: 1,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('does not mark the job failed before BullMQ exhausts retries', async () => {
|
||||
const { service, updateAnalysisJob } = createPipelineHarness({
|
||||
accountError: new Error('upstream unavailable'),
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.processUserAnalysisJob(createJobData(), 'analysis-job-1', {
|
||||
isFinalAttempt: false,
|
||||
}),
|
||||
).rejects.toThrow('upstream unavailable');
|
||||
|
||||
const failedJobUpdate = updateAnalysisJob.mock.calls.find(
|
||||
([, update]) => asRecord(asRecord(update).$set).status === 'failed',
|
||||
);
|
||||
expect(failedJobUpdate).toBeUndefined();
|
||||
});
|
||||
|
||||
it('marks the job failed with a public error on the final BullMQ attempt', async () => {
|
||||
const { service, updateAnalysisJob } = createPipelineHarness({
|
||||
accountError: new Error('internal host redis.local refused connection'),
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.processUserAnalysisJob(createJobData(), 'analysis-job-1', {
|
||||
isFinalAttempt: true,
|
||||
}),
|
||||
).rejects.toThrow('internal host redis.local refused connection');
|
||||
|
||||
const failedJobUpdate = updateAnalysisJob.mock.calls.find(
|
||||
([, update]) => asRecord(asRecord(update).$set).status === 'failed',
|
||||
);
|
||||
expect(asRecord(asRecord(failedJobUpdate?.[1]).$set)).toMatchObject({
|
||||
status: 'failed',
|
||||
error: 'Analysis failed during processing.',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user