test: audit live best-general rankings

This commit is contained in:
2026-08-02 05:26:16 +00:00
parent 43ec211ff8
commit 28f4778545
6 changed files with 623 additions and 15 deletions
+85 -11
View File
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import type { GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken';
import { RANK_DATA_TYPES } from '@sammo-ts/common';
import type { RedisConnector } from '@sammo-ts/infra';
import { RedisAccessTokenStore } from '../src/auth/accessTokenStore.js';
@@ -32,7 +33,24 @@ const auth: GameSessionTokenPayload = {
sanctions: {},
};
const generalRows = [
interface RankingGeneralRow {
id: number;
name: string;
nationId: number;
userId: string | null;
npcState: number;
picture: string | null;
imageServer: number;
meta: Record<string, string | number>;
experience: number;
dedication: number;
horseCode: string;
weaponCode: string;
bookCode: string;
itemCode: string;
}
const generalRows: RankingGeneralRow[] = [
{
id: 1,
name: '유비',
@@ -81,13 +99,16 @@ const generalRows = [
bookCode: 'None',
itemCode: 'None',
},
] as const;
];
const buildContext = (options?: {
authenticated?: boolean;
isUnited?: boolean;
includeOwnerDisplayName?: boolean;
generals?: RankingGeneralRow[];
rankRows?: Array<{ generalId: number; type: string; value: number }>;
}): GameApiContext => {
const selectedGeneralRows = options?.generals ?? generalRows;
const db = {
worldState: {
findFirst: async () => ({
@@ -112,21 +133,22 @@ const buildContext = (options?: {
},
general: {
findMany: async (args: { where: { npcState: { lt?: number; gte?: number } } }) =>
generalRows.filter((general) =>
selectedGeneralRows.filter((general) =>
args.where.npcState.gte !== undefined
? general.npcState >= args.where.npcState.gte
: general.npcState < (args.where.npcState.lt ?? Number.POSITIVE_INFINITY)
),
},
rankData: {
findMany: async () => [
{ generalId: 1, type: 'firenum', value: 10 },
{ generalId: 2, type: 'firenum', value: 20 },
{ generalId: 3, type: 'firenum', value: 30 },
{ generalId: 1, type: 'dex1', value: 999 },
{ generalId: 2, type: 'dex1', value: 999 },
{ generalId: 3, type: 'dex1', value: 999 },
],
findMany: async () =>
options?.rankRows ?? [
{ generalId: 1, type: 'firenum', value: 10 },
{ generalId: 2, type: 'firenum', value: 20 },
{ generalId: 3, type: 'firenum', value: 30 },
{ generalId: 1, type: 'dex1', value: 999 },
{ generalId: 2, type: 'dex1', value: 999 },
{ generalId: 3, type: 'dex1', value: 999 },
],
},
auction: {
findMany: async () => [{ targetCode: 'che_명마_15_적토마' }],
@@ -237,6 +259,58 @@ describe('ranking.getBestGeneral', () => {
});
});
it('returns positions one through ten for every populated ranking section', async () => {
const generals = Array.from({ length: 12 }, (_, index) => {
const id = index + 1;
const value = id * 1_000;
return {
id,
name: `상위${id}`,
nationId: 1,
userId: `top-${id}`,
npcState: 0,
picture: null,
imageServer: 0,
meta: { dex1: value, dex2: value, dex3: value, dex4: value, dex5: value },
experience: value,
dedication: value,
horseCode: 'None',
weaponCode: 'None',
bookCode: 'None',
itemCode: 'None',
};
});
const rankRows = generals.flatMap((general) =>
RANK_DATA_TYPES.filter(
(type) => type !== 'experience' && type !== 'dedication' && !type.startsWith('dex')
).map((type) => ({
generalId: general.id,
type,
value:
type === 'warnum' || type === 'deathcrew' || type === 'deathcrew_person'
? 1_000
: type === 'ttd' || type === 'ttl' || type === 'tld' || type === 'tll' ||
type === 'tsd' || type === 'tsl' || type === 'tid' || type === 'til' ||
type === 'betgold'
? 1_000
: general.id * 1_000,
}))
);
const result = await appRouter
.createCaller(buildContext({ isUnited: true, generals, rankRows }))
.ranking.getBestGeneral({ view: 'user' });
expect(result.sections).toHaveLength(26);
for (const section of result.sections) {
expect(section.entries, section.title).toHaveLength(10);
expect(new Set(section.entries.map((entry) => entry.id)).size, section.title).toBe(10);
expect(section.entries.every((entry) => entry.value > 0), section.title).toBe(true);
}
expect(result.sections.find((section) => section.title === '계 략 성 공')?.entries.map((entry) => entry.id)).toEqual([
12, 11, 10, 9, 8, 7, 6, 5, 4, 3,
]);
});
it('matches PHP number_format rounding and the legacy fixed color table', () => {
expect(formatLegacyRankingNumber(1.005, 2)).toBe('1.01');
expect(formatLegacyRankingNumber(12345.6, 2)).toBe('12,345.60');
+58 -2
View File
@@ -176,13 +176,14 @@ const runTournamentToCompletion = async (options: {
store: TournamentStore;
prisma: ReturnType<typeof createPrismaMock>;
baseSeed: string;
daemonTransport?: TurnDaemonTransport;
}): Promise<TournamentState> => {
let state = await options.store.getState();
if (!state) {
throw new Error('토너먼트 상태가 없습니다.');
}
const daemonTransport = createNoopDaemonTransport();
const daemonTransport = options.daemonTransport ?? createNoopDaemonTransport();
for (let i = 0; i < 2000; i += 1) {
if (state.stage === 0) {
@@ -198,7 +199,7 @@ const runTournamentToCompletion = async (options: {
continue;
}
if (state.stage >= 7 && state.stage <= 10) {
state = await applyBattle(options.store, state, options.baseSeed);
state = await applyBattle(options.store, state, options.baseSeed, daemonTransport);
continue;
}
break;
@@ -270,6 +271,61 @@ describe('tournament worker (in-memory)', () => {
});
});
it('runs all four tournament types and emits enough rank and NPC-betting commands for a top ten', async () => {
for (const type of [
TournamentType.TOTAL,
TournamentType.LEADERSHIP,
TournamentType.STRENGTH,
TournamentType.INTEL,
]) {
const redis = new MemoryRedis();
const store = new TournamentStore(redis, buildTournamentKeys(`ranking-audit-${type}`));
const participants = createParticipants(16, 16, 32);
await store.setParticipants(participants);
await store.setState(createTournamentState({ stage: 1, type }));
const npcBetting = participants.slice(0, 12).map((entry) => ({
...entry,
meta: {},
npcState: 2,
gold: 10_000,
}));
const commands: TurnDaemonCommand[] = [];
const transport: TurnDaemonTransport = {
sendCommand: async (command) => {
commands.push(command);
return 'ok';
},
requestCommand: async () => null,
requestStatus: async () => null,
};
await runTournamentToCompletion({
store,
prisma: createPrismaMock({ baseSeed: `ranking-audit-${type}`, npcBetting, currentYear: 10 }),
baseSeed: `ranking-audit-${type}`,
daemonTransport: transport,
});
const matchCommands = commands.filter((command) => command.type === 'tournamentMatchResult');
const rankedGeneralIds = new Set(
matchCommands.flatMap((command) =>
command.type === 'tournamentMatchResult' ? [command.attackerId, command.defenderId] : []
)
);
expect(matchCommands.length).toBeGreaterThan(50);
expect(rankedGeneralIds.size).toBeGreaterThanOrEqual(10);
expect(commands).toContainEqual(
expect.objectContaining({
type: 'adjustGeneralMeta',
reason: 'tournamentNpcBet',
adjustments: expect.arrayContaining([
expect.objectContaining({ metaDelta: { betgold: expect.any(Number) } }),
]),
})
);
}
});
it('우승 결과에 따라 베팅 정산 명령이 생성된다', async () => {
const redis = new MemoryRedis();
const store = new TournamentStore(redis, buildTournamentKeys('test-bet'));