diff --git a/apps/api/src/modules/auction/auction-durability.scheduler.spec.ts b/apps/api/src/modules/auction/auction-durability.scheduler.spec.ts index 1cd482f4..01e8300d 100644 --- a/apps/api/src/modules/auction/auction-durability.scheduler.spec.ts +++ b/apps/api/src/modules/auction/auction-durability.scheduler.spec.ts @@ -233,6 +233,56 @@ describe('auction durability projection helpers', () => { expect(session.endSession).toHaveBeenCalledTimes(1); }); + it('notifies history only after commit, including when state rebroadcast is disabled', async () => { + let finishCommit!: () => void; + const session = { + startTransaction: jest.fn(), + commitTransaction: jest.fn().mockImplementation( + () => + new Promise((resolve) => { + finishCommit = resolve; + }), + ), + abortTransaction: jest.fn().mockResolvedValue(undefined), + endSession: jest.fn().mockResolvedValue(undefined), + }; + const updateOne = jest.fn().mockReturnValue({ + exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }), + }); + const broadcastHistoryUpdated = jest.fn(); + const scheduler = new AuctionDurabilityScheduler( + { + createSession: async () => session, + getModel: async () => ({ updateOne }), + } as never, + {} as never, + undefined, + undefined, + undefined, + { broadcastHistoryUpdated } as never, + ); + const pending = scheduler.applyDurabilityPayload({ + ...payload, + shouldBroadcastState: false, + }); + await new Promise((resolve) => setImmediate(resolve)); + expect(session.commitTransaction).toHaveBeenCalledTimes(1); + expect(broadcastHistoryUpdated).not.toHaveBeenCalled(); + finishCommit(); + await pending; + expect(broadcastHistoryUpdated).toHaveBeenCalledWith({ + matchId: 'match-1', + auctionLifecycleId: 'life-1', + auctionStateTransitionId: 'transition-1', + }); + session.commitTransaction.mockRejectedValueOnce(new Error('commit failed')); + await expect(scheduler.applyDurabilityPayload(payload)).rejects.toThrow( + 'commit failed', + ); + expect(broadcastHistoryUpdated).toHaveBeenCalledTimes(1); + expect(session.abortTransaction).toHaveBeenCalledTimes(1); + }); + it('broadcasts latest Redis state when durability payload requests it', async () => { const session = { abortTransaction: jest.fn().mockResolvedValue(undefined), @@ -265,7 +315,7 @@ describe('auction durability projection helpers', () => { undefined, undefined, undefined, - { broadcastState } as never, + { broadcastState, broadcastHistoryUpdated: jest.fn() } as never, { readAuctionStateView } as never, ); @@ -315,7 +365,7 @@ describe('auction durability projection helpers', () => { undefined, undefined, undefined, - { broadcastState } as never, + { broadcastState, broadcastHistoryUpdated: jest.fn() } as never, { readAuctionStateView } as never, ); @@ -407,60 +457,71 @@ describe('auction durability projection helpers', () => { ); }); - it('skips destructive history mutation for stale payloads', async () => { - const session = { - abortTransaction: jest.fn().mockResolvedValue(undefined), - commitTransaction: jest.fn().mockResolvedValue(undefined), - endSession: jest.fn().mockResolvedValue(undefined), - startTransaction: jest.fn(), - }; - const historyDeleteMany = jest.fn(); - const historyUpdateOne = jest.fn(); - const auctionUpdateOne = jest.fn().mockReturnValue({ - exec: jest.fn().mockResolvedValue({ matchedCount: 0, modifiedCount: 0 }), - }); - const projectionExec = jest.fn().mockResolvedValue({ - auctionLifecycleId: 'life-1', - revision: 10, - }); - const auctionFindById = jest.fn().mockReturnValue({ - select: jest.fn().mockReturnValue({ - session: jest.fn().mockReturnValue({ - lean: jest.fn().mockReturnValue({ exec: projectionExec }), + it.each(['life-1', 'new-life'])( + 'skips stale history changes and notifications for current lifecycle %s', + async (auctionLifecycleId) => { + const broadcastHistoryUpdated = jest.fn(); + const session = { + abortTransaction: jest.fn().mockResolvedValue(undefined), + commitTransaction: jest.fn().mockResolvedValue(undefined), + endSession: jest.fn().mockResolvedValue(undefined), + startTransaction: jest.fn(), + }; + const historyDeleteMany = jest.fn(); + const historyUpdateOne = jest.fn(); + const auctionUpdateOne = jest.fn().mockReturnValue({ + exec: jest + .fn() + .mockResolvedValue({ matchedCount: 0, modifiedCount: 0 }), + }); + const projectionExec = jest.fn().mockResolvedValue({ + auctionLifecycleId, + revision: 10, + }); + const auctionFindById = jest.fn().mockReturnValue({ + select: jest.fn().mockReturnValue({ + session: jest.fn().mockReturnValue({ + lean: jest.fn().mockReturnValue({ exec: projectionExec }), + }), }), - }), - }); - const scheduler = new AuctionDurabilityScheduler( - { - createSession: jest.fn().mockResolvedValue(session), - getModel: jest.fn((name: string) => { - if (name === 'auctionHistory') { - return { - deleteMany: historyDeleteMany, - updateOne: historyUpdateOne, - }; - } - if (name === 'auction') { - return { findById: auctionFindById, updateOne: auctionUpdateOne }; - } - return { updateOne: jest.fn() }; - }), - } as never, - { raw: jest.fn() } as never, - ); + }); + const scheduler = new AuctionDurabilityScheduler( + { + createSession: jest.fn().mockResolvedValue(session), + getModel: jest.fn((name: string) => { + if (name === 'auctionHistory') { + return { + deleteMany: historyDeleteMany, + updateOne: historyUpdateOne, + }; + } + if (name === 'auction') { + return { findById: auctionFindById, updateOne: auctionUpdateOne }; + } + return { updateOne: jest.fn() }; + }), + } as never, + { raw: jest.fn() } as never, + undefined, + undefined, + undefined, + { broadcastHistoryUpdated } as never, + ); - await scheduler.applyDurabilityPayload({ - ...payload, - historyMutation: { - targetHistorySequence: 2, - type: 'deleteAfterSequenceAndAppendUndo', - }, - }); + await scheduler.applyDurabilityPayload({ + ...payload, + historyMutation: { + targetHistorySequence: 2, + type: 'deleteAfterSequenceAndAppendUndo', + }, + }); - expect(historyDeleteMany).not.toHaveBeenCalled(); - expect(historyUpdateOne).not.toHaveBeenCalled(); - expect(auctionUpdateOne).not.toHaveBeenCalled(); - }); + expect(historyDeleteMany).not.toHaveBeenCalled(); + expect(historyUpdateOne).not.toHaveBeenCalled(); + expect(auctionUpdateOne).not.toHaveBeenCalled(); + expect(broadcastHistoryUpdated).not.toHaveBeenCalled(); + }, + ); it('recovers stale append payloads as history-only records', async () => { const session = { @@ -597,6 +658,7 @@ describe('auction durability projection helpers', () => { }); it('clears stale append payloads after history-only recovery', async () => { + const broadcastHistoryUpdated = jest.fn(); const sendCommand = jest .fn() .mockResolvedValueOnce(['payload-1']) @@ -653,6 +715,10 @@ describe('auction durability projection helpers', () => { getCurrentRedisTimestampMS: jest.fn().mockResolvedValue(1000), raw: jest.fn().mockReturnValue({ sendCommand }), } as never, + undefined, + undefined, + undefined, + { broadcastHistoryUpdated } as never, ); await expect(scheduler.processDueDurabilityPayloads(10)).resolves.toEqual({ @@ -662,6 +728,11 @@ describe('auction durability projection helpers', () => { }); expect(historyUpdateOne).toHaveBeenCalledTimes(1); + expect(broadcastHistoryUpdated).toHaveBeenCalledWith({ + matchId: 'match-1', + auctionLifecycleId: 'life-1', + auctionStateTransitionId: 'transition-1', + }); expect(auctionUpdateOne).not.toHaveBeenCalled(); expect(sideEffectUpdateOne).not.toHaveBeenCalled(); expect(sendCommand).toHaveBeenNthCalledWith(3, [ @@ -1006,7 +1077,7 @@ describe('auction durability projection helpers', () => { { reserveForMatch } as never, { scheduleInitialPoll } as never, { mirrorMatchSideEffectStatus } as never, - { broadcastMatchSideEffect } as never, + { broadcastMatchSideEffect, broadcastHistoryUpdated: jest.fn() } as never, ); await scheduler.applyDurabilityPayload(completePayload); @@ -1203,7 +1274,7 @@ describe('auction durability projection helpers', () => { { reserveForMatch } as never, { scheduleInitialPoll } as never, { mirrorMatchSideEffectStatus } as never, - { broadcastMatchSideEffect } as never, + { broadcastMatchSideEffect, broadcastHistoryUpdated: jest.fn() } as never, ); await expect( @@ -1332,7 +1403,7 @@ describe('auction durability projection helpers', () => { { releaseFromMatch } as never, { cancelPolling } as never, { mirrorMatchSideEffectStatus } as never, - { broadcastMatchSideEffect } as never, + { broadcastMatchSideEffect, broadcastHistoryUpdated: jest.fn() } as never, ); await scheduler.applyDurabilityPayload(resetPayload); diff --git a/apps/api/src/modules/auction/auction-durability.scheduler.ts b/apps/api/src/modules/auction/auction-durability.scheduler.ts index ecae2efe..f57ac424 100644 --- a/apps/api/src/modules/auction/auction-durability.scheduler.ts +++ b/apps/api/src/modules/auction/auction-durability.scheduler.ts @@ -356,6 +356,15 @@ export class AuctionDurabilityScheduler if (session) await session.endSession(); } + if (shouldApplyHistoryMutation) { + // This is independent of the primary state broadcast. HTTP reads on every + // node can see the committed history before clients are notified. + this.auctionEventBusService?.broadcastHistoryUpdated({ + matchId: String(payload.matchId), + auctionLifecycleId: payload.auctionLifecycleId, + auctionStateTransitionId: payload.auctionStateTransitionId, + }); + } await this.broadcastLatestRedisAuctionStateIfRequested(payload); if (sideEffectMutation && shouldApplySideEffects) { diff --git a/apps/api/src/modules/auction/auction-event-bus.service.ts b/apps/api/src/modules/auction/auction-event-bus.service.ts index 93b70908..01cc32eb 100644 --- a/apps/api/src/modules/auction/auction-event-bus.service.ts +++ b/apps/api/src/modules/auction/auction-event-bus.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@nestjs/common'; import type { Namespace } from 'socket.io'; +import type { AuctionHistoryUpdatedEvent } from '@lolympus/contracts'; import type { AuctionStateResponse } from './auction.service'; import type { MatchSideEffectDelta } from './auction-side-effect.service'; import { serializeRealtimeAuction } from './auction-realtime-serializer'; @@ -25,6 +26,10 @@ export class AuctionEventBusService { .emit('state', serializeRealtimeAuction(state)); } + public broadcastHistoryUpdated(event: AuctionHistoryUpdatedEvent): void { + this.auctionNamespace?.to(event.matchId).emit('history-updated', event); + } + public broadcastReset(matchId: string, auctionLifecycleId: string): void { this.auctionNamespace ?.to(matchId) diff --git a/apps/api/src/modules/auction/auction.gateway.spec.ts b/apps/api/src/modules/auction/auction.gateway.spec.ts index d3aa0f67..816fcf42 100644 --- a/apps/api/src/modules/auction/auction.gateway.spec.ts +++ b/apps/api/src/modules/auction/auction.gateway.spec.ts @@ -37,7 +37,11 @@ describe('AuctionGateway', () => { {} as never, ); const socket = { data: { matchId }, join: jest.fn(), emit: jest.fn() }; - await gateway.handleJoin(socket as never, { matchId }); + const ack = jest.fn(); + await gateway.handleJoin(socket as never, { matchId }, ack); + expect(ack).toHaveBeenCalledWith( + expect.objectContaining({ ok: false, code: 'AUCTION_STATE_RECOVERING' }), + ); expect(socket.emit).toHaveBeenCalledTimes(1); expect(socket.emit).toHaveBeenCalledWith('state-pending', { retryAfterMS: 500, @@ -92,9 +96,12 @@ describe('AuctionGateway', () => { function createGateway(params?: { getCurrentRedisTimestampMS?: jest.Mock; placeBidDelta?: jest.Mock; + getRealtimeIdentity?: jest.Mock; }) { const auctionService = { placeBidDelta: params?.placeBidDelta ?? jest.fn(), + getRealtimeIdentity: + params?.getRealtimeIdentity ?? jest.fn().mockResolvedValue(null), }; const auctionRedisService = { getCurrentRedisTimestampMS: @@ -387,6 +394,46 @@ describe('AuctionGateway', () => { ); }); + it('adds lightweight auction identity to heartbeats for the joined match', async () => { + const matchId = new mongoose.Types.ObjectId().toString(); + const identity = { + auctionLifecycleId: 'life-1', + revision: 11, + matchSideEffectRevision: 3, + }; + const getRealtimeIdentity = jest.fn().mockResolvedValue(identity); + const { gateway } = createGateway({ getRealtimeIdentity }); + const ack = jest.fn(); + await gateway.handleTimeSync({ sequence: 1 }, ack, { + data: { matchId }, + } as never); + expect(getRealtimeIdentity).toHaveBeenCalledWith(matchId); + expect(ack).toHaveBeenCalledWith( + expect.objectContaining({ ok: true, auctionState: identity }), + ); + }); + + it('continues clock sync when the optional auction identity read fails', async () => { + const matchId = new mongoose.Types.ObjectId().toString(); + const { gateway } = createGateway({ + getRealtimeIdentity: jest + .fn() + .mockRejectedValue(new Error('identity unavailable')), + }); + const ack = jest.fn(); + await gateway.handleTimeSync({ sequence: 2 }, ack, { + data: { matchId }, + } as never); + expect(ack).toHaveBeenCalledWith( + expect.objectContaining({ + ok: true, + sequence: 2, + currentServerTimestampMS: 1780000000000, + auctionStateUnavailable: true, + }), + ); + }); + it('rate limits excessive bid requests from the same socket', async () => { jest.spyOn(Date, 'now').mockReturnValue(1780000000000); const matchId = new mongoose.Types.ObjectId().toString(); diff --git a/apps/api/src/modules/auction/auction.gateway.ts b/apps/api/src/modules/auction/auction.gateway.ts index ad2c6c22..93b8da6a 100644 --- a/apps/api/src/modules/auction/auction.gateway.ts +++ b/apps/api/src/modules/auction/auction.gateway.ts @@ -180,9 +180,11 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit { public async handleJoin( @ConnectedSocket() socket: AuctionSocket, @MessageBody() payload: JoinAuctionPayload, + @Ack() ack?: (response: Record) => void, ) { if (!payload?.matchId || !mongoose.isValidObjectId(payload.matchId)) { this.emitError(socket, 'Invalid matchId.'); + ack?.({ ok: false, code: 'INVALID_MATCH_ID' }); return; } @@ -201,8 +203,10 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit { ); this.emitChatHistory(socket, auction); socket.emit('state', serializeRealtimeAuction(auctionState)); + ack?.({ ok: true }); } catch (error) { const rejection = this.resolveErrorPayload(error); + ack?.({ ...rejection, ok: false }); if (rejection.code === 'AUCTION_STATE_RECOVERING') { socket.emit('state-pending', { retryAfterMS: 500 }); } else { @@ -347,14 +351,27 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit { public async handleTimeSync( @MessageBody() payload: TimeSyncPayload, @Ack() ack?: (response: Record) => void, + @ConnectedSocket() socket?: AuctionSocket, ) { const receivedAtMonotonicMS = performance.now(); try { - const currentServerTimestampMS = - await this.auctionRedisService.getCurrentRedisTimestampMS(); + const [currentServerTimestampMS, auctionState] = await Promise.all([ + this.auctionRedisService.getCurrentRedisTimestampMS(), + socket?.data.matchId + ? this.auctionService + .getRealtimeIdentity(socket.data.matchId) + .catch(() => undefined) + : undefined, + ]); ack?.({ clientSentAtTimestampMS: payload?.clientSentAtTimestampMS, currentServerTimestampMS, + ...(socket?.data.matchId + ? { + auctionState, + auctionStateUnavailable: auctionState === undefined, + } + : {}), ok: true, sequence: payload?.sequence, serverProcessingDurationMS: diff --git a/apps/api/src/modules/auction/auction.service.spec.ts b/apps/api/src/modules/auction/auction.service.spec.ts index 72fa3f78..c7e00720 100644 --- a/apps/api/src/modules/auction/auction.service.spec.ts +++ b/apps/api/src/modules/auction/auction.service.spec.ts @@ -215,8 +215,7 @@ describe('AuctionService', () => { expect(auctionRedisCommandService.placeBid).not.toHaveBeenCalled(); }); - it('lists auction history with a summary projection and short local cache', async () => { - const nowSpy = jest.spyOn(Date, 'now').mockReturnValue(1000); + it('reads committed history on every node without retaining a local cache', async () => { const matchId = new mongoose.Types.ObjectId(); const communityId = new mongoose.Types.ObjectId(); const match = { @@ -242,7 +241,7 @@ describe('AuctionService', () => { const historySort = jest.fn().mockReturnValue({ limit: historyLimit }); const historySelect = jest.fn().mockReturnValue({ sort: historySort }); const historyFind = jest.fn().mockReturnValue({ select: historySelect }); - const { service } = createService({ + const models = { auctionHistory: { find: historyFind, }, @@ -251,7 +250,9 @@ describe('AuctionService', () => { select: jest.fn().mockResolvedValue(match), }), }, - }); + }; + const { service } = createService(models); + const otherNode = createService(models).service; await expect( service.listHistory({ matchId, communityId }), @@ -271,7 +272,7 @@ describe('AuctionService', () => { ]); await service.listHistory({ matchId, communityId }); - expect(historyFind).toHaveBeenCalledTimes(1); + expect(historyFind).toHaveBeenCalledTimes(2); expect(historySelect).toHaveBeenCalledWith({ _id: 0, action: 1, @@ -281,9 +282,17 @@ describe('AuctionService', () => { expect(historySort).toHaveBeenCalledWith({ sequence: -1 }); expect(historyLimit).toHaveBeenCalledWith(100); - nowSpy.mockReturnValue(3101); - await service.listHistory({ matchId, communityId }); - expect(historyFind).toHaveBeenCalledTimes(2); + historyExec.mockResolvedValue([ + { action: 'undo', sequence: 5, takenAt: new Date() }, + historyRows[1], + ]); + const [firstNodeHistory, secondNodeHistory] = await Promise.all([ + service.listHistory({ matchId, communityId }), + otherNode.listHistory({ matchId, communityId }), + ]); + expect(firstNodeHistory.map(({ sequence }) => sequence)).toEqual([5, 3]); + expect(secondNodeHistory).toEqual(firstNodeHistory); + expect(historyFind).toHaveBeenCalledTimes(4); }); it('starts an auction without reserving tournament codes before teams are complete', async () => { diff --git a/apps/api/src/modules/auction/auction.service.ts b/apps/api/src/modules/auction/auction.service.ts index e47d6d10..e4a3d1f6 100644 --- a/apps/api/src/modules/auction/auction.service.ts +++ b/apps/api/src/modules/auction/auction.service.ts @@ -10,6 +10,7 @@ import { Optional, } from '@nestjs/common'; import { randomUUID } from 'crypto'; +import type { AuctionStateIdentity } from '@lolympus/contracts'; import { AuctionLifecycleLock, AuctionLifecycleBusyError, @@ -69,8 +70,6 @@ const AUCTION_MAX_STORED_ACTION_REQUEST_RESULT_COUNT = 500; const AUCTION_ACTION_REQUEST_REPLAY_WINDOW_MS = 5 * 60 * 1000; const AUCTION_ACTION_REQUEST_LEASE_DURATION_MS = 15 * 1000; const AUCTION_HISTORY_LIST_LIMIT = 100; -const AUCTION_HISTORY_LIST_CACHE_TTL_MS = 2 * 1000; -const AUCTION_HISTORY_LIST_CACHE_MAX_ENTRIES = 256; interface StartAuctionParams { matchId: mongoose.Types.ObjectId; @@ -163,11 +162,6 @@ interface AuctionHistorySummaryRecord { takenAt: Date; } -interface AuctionHistoryListCacheEntry { - entries: AuctionHistoryView[]; - expiresAtTimestampMS: number; -} - export type AuctionStateResponse = AuctionDocument | Record; export interface AuctionBidDeltaResponse { @@ -211,10 +205,6 @@ function isMongoDuplicateKeyError(error: unknown): boolean { @Injectable() export class AuctionService { private readonly logger = new Logger(AuctionService.name); - private readonly auctionHistoryListCache = new Map< - string, - AuctionHistoryListCacheEntry - >(); constructor( private readonly connectionService: ConnectionService, @@ -579,6 +569,12 @@ export class AuctionService { } } + public getRealtimeIdentity( + matchId: string, + ): Promise { + return this.auctionRedisStateReaderService.readLiveStateIdentity(matchId); + } + private async getStateByMatchIdOnce( matchId: mongoose.Types.ObjectId, ): Promise { @@ -737,10 +733,6 @@ export class AuctionService { throw new BadRequestException('Match does not belong to this community.'); } - const cacheKey = this.buildAuctionHistoryListCacheKey(params); - const cachedHistory = this.getCachedAuctionHistoryList(cacheKey); - if (cachedHistory) return cachedHistory; - const entries = (await AuctionHistoryModel.find({ matchId: params.matchId }) .select({ _id: 0, action: 1, sequence: 1, takenAt: 1 }) .sort({ sequence: -1 }) @@ -754,49 +746,9 @@ export class AuctionService { actionLabel: this.formatHistoryActionLabel(entry.action), takenAt: entry.takenAt, })); - this.setCachedAuctionHistoryList(cacheKey, history); return history; } - private buildAuctionHistoryListCacheKey(params: { - matchId: mongoose.Types.ObjectId; - communityId: mongoose.Types.ObjectId; - }): string { - return `${params.communityId.toString()}:${params.matchId.toString()}`; - } - - private getCachedAuctionHistoryList( - cacheKey: string, - ): AuctionHistoryView[] | null { - const cached = this.auctionHistoryListCache.get(cacheKey); - if (!cached) return null; - if (cached.expiresAtTimestampMS <= Date.now()) { - this.auctionHistoryListCache.delete(cacheKey); - return null; - } - return cached.entries; - } - - private setCachedAuctionHistoryList( - cacheKey: string, - entries: AuctionHistoryView[], - ): void { - if ( - !this.auctionHistoryListCache.has(cacheKey) && - this.auctionHistoryListCache.size >= - AUCTION_HISTORY_LIST_CACHE_MAX_ENTRIES - ) { - const oldestKey = this.auctionHistoryListCache.keys().next().value as - | string - | undefined; - if (oldestKey) this.auctionHistoryListCache.delete(oldestKey); - } - this.auctionHistoryListCache.set(cacheKey, { - entries, - expiresAtTimestampMS: Date.now() + AUCTION_HISTORY_LIST_CACHE_TTL_MS, - }); - } - public async openBidding( matchId: mongoose.Types.ObjectId, actionRequest: AuctionStructureActionRequestParams = {}, diff --git a/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.spec.ts b/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.spec.ts index 3a9eeea7..4950fcca 100644 --- a/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.spec.ts +++ b/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.spec.ts @@ -30,17 +30,23 @@ function makeService( describe('AuctionRedisStateReaderService', () => { it('reads live state identity', async () => { - const { service, sendCommand } = makeService([['life-1', '7']]); + const { service, sendCommand } = makeService([ + ['life-1', '7', '3', 'transition-1'], + ]); await expect(service.readLiveStateIdentity('match-1')).resolves.toEqual({ auctionLifecycleId: 'life-1', revision: 7, + matchSideEffectRevision: 3, + matchSideEffectTransitionId: 'transition-1', }); expect(sendCommand).toHaveBeenCalledWith([ 'HMGET', 'auction:match-1:liveAuctionState', 'auctionLifecycleId', 'revision', + 'matchSideEffectRevision', + 'matchSideEffectTransitionId', ]); }); @@ -118,7 +124,9 @@ describe('AuctionRedisStateReaderService', () => { expect(view).toMatchObject({ auctionBidHistory: [{ bidPointAmount: 100, bidderTeamIndex: 2 }], + auctionEngineVersion: 'redis-v1', auctionLifecycleId: 'life-1', + matchId: 'match-1', auctionTeams: [{ teamIndex: 1 }], canPlaceBidInCurrentBidRound: false, currentHighestBidderTeamIndex: 2, diff --git a/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.ts b/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.ts index 09acdbcc..2443ab65 100644 --- a/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.ts +++ b/apps/api/src/modules/auction/redis/auction-redis-state-reader.service.ts @@ -1,4 +1,5 @@ import { Injectable } from '@nestjs/common'; +import type { AuctionStateIdentity } from '@lolympus/contracts'; import { buildAuctionRedisKeys } from './auction-redis.keys'; import { AuctionRedisService } from './auction-redis.service'; import { assembleAuctionViewFromRedisState } from './auction-redis.state'; @@ -17,10 +18,7 @@ const NUMERIC_LIVE_STATE_FIELDS = new Set([ 'revision', ]); -export interface AuctionRedisLiveStateIdentity { - auctionLifecycleId?: string; - revision?: number; -} +export type AuctionRedisLiveStateIdentity = AuctionStateIdentity; export interface AuctionRedisBidContext { auctionLifecycleId: string; @@ -49,6 +47,8 @@ export class AuctionRedisStateReaderService { keys.liveStateKey, 'auctionLifecycleId', 'revision', + 'matchSideEffectRevision', + 'matchSideEffectTransitionId', ]); if (!Array.isArray(reply)) return null; const auctionLifecycleId = @@ -61,6 +61,12 @@ export class AuctionRedisStateReaderService { return { auctionLifecycleId, revision: Number.isFinite(revision) ? revision : undefined, + matchSideEffectRevision: + reply[2] !== null && Number.isFinite(Number(reply[2])) + ? Number(reply[2]) + : undefined, + matchSideEffectTransitionId: + typeof reply[3] === 'string' && reply[3] ? reply[3] : undefined, }; } @@ -147,6 +153,7 @@ export class AuctionRedisStateReaderService { structureState: parseJsonRecord(structureStateJson), }); view.matchId = matchId; + view.auctionEngineVersion = 'redis-v1'; if (options.includeInternalLiveStateFields) { view.redisInternalPhase = liveState.phase; } diff --git a/apps/web/src/api/auction-polling.ts b/apps/web/src/api/auction-polling.ts new file mode 100644 index 00000000..8153137b --- /dev/null +++ b/apps/web/src/api/auction-polling.ts @@ -0,0 +1,12 @@ +export const AUCTION_SAFETY_POLL_INTERVAL_MS = 30_000; + +/** Supplements the fixed safety poll without restarting its timer. */ +export function startAuctionRecoveryPolling( + refetch: () => void, + isVisible = () => document.visibilityState === 'visible', +): () => void { + const timer = setInterval(() => { + if (isVisible()) refetch(); + }, 5000); + return () => clearInterval(timer); +} diff --git a/apps/web/src/api/auction-resync.ts b/apps/web/src/api/auction-resync.ts index 6c4f4926..faebd8fb 100644 --- a/apps/web/src/api/auction-resync.ts +++ b/apps/web/src/api/auction-resync.ts @@ -27,6 +27,7 @@ export function createAuctionResyncRequester( fetchState: () => Promise, applyState: (state: ApiAuction) => void, wait = waitForRetry, + onFailure?: () => void, ) { let pending: Promise | undefined; let controller = new AbortController(); @@ -67,6 +68,7 @@ export function createAuctionResyncRequester( } if (attempt < 2 && !signal.aborted) await wait(retryAfterMS, signal); } + if (!signal.aborted) onFailure?.(); })().finally(() => { if (!signal.aborted) pending = undefined; }); diff --git a/apps/web/src/api/auction-state.ts b/apps/web/src/api/auction-state.ts index fbdd4ba4..aa97fba9 100644 --- a/apps/web/src/api/auction-state.ts +++ b/apps/web/src/api/auction-state.ts @@ -1,4 +1,69 @@ import type { ApiAuction } from "./types"; +import type { AuctionStateIdentity } from "@lolympus/contracts"; + +export function resolveCurrentBidPointAmount(auction: ApiAuction): number { + const amount = Number( + auction.currentHighestBidPointAmount ?? auction.currentBid ?? 0, + ); + return Number.isFinite(amount) ? Math.max(0, amount) : 0; +} + +export function shouldResyncForAuctionIdentity( + current: ApiAuction | null, + identity: AuctionStateIdentity | null, +): boolean { + if (!current) return true; + // Completed runtimes expire while their archived Mongo state remains valid. + if (!identity) { + return ( + current.auctionEngineVersion === "redis-v1" && + current.phase !== "complete" + ); + } + if (identity.auctionLifecycleId !== current.auctionLifecycleId) return true; + if ((identity.revision ?? -1) > (current.revision ?? -1)) return true; + // A delayed heartbeat must not undo a newer state already received by socket. + if ((identity.revision ?? -1) < (current.revision ?? -1)) return false; + return ( + (identity.matchSideEffectRevision ?? -1) > + (current.matchSideEffectRevision ?? -1) + ); +} + +export function createAuctionHeartbeatMonitor() { + let pendingIdentity: AuctionStateIdentity | null | undefined; + let consecutiveFailures = 0; + return { + reset() { + pendingIdentity = undefined; + consecutiveFailures = 0; + }, + recordFailure(): boolean { + pendingIdentity = undefined; + consecutiveFailures += 1; + return consecutiveFailures >= 2; + }, + inspect( + current: ApiAuction | null, + identity: AuctionStateIdentity | null | undefined, + ) { + consecutiveFailures = 0; + const previousIdentity = pendingIdentity; + const isCurrent = + identity === undefined || + !shouldResyncForAuctionIdentity(current, identity); + pendingIdentity = undefined; + if (isCurrent) return { isCurrent: true, needsResync: false }; + // Bids received since the first heartbeat may have closed that gap, even + // when this heartbeat observes another newer bid still in transit. + const needsResync = + previousIdentity !== undefined && + shouldResyncForAuctionIdentity(current, previousIdentity); + if (!needsResync) pendingIdentity = identity; + return { isCurrent: false, needsResync }; + }, + }; +} export interface BidDeltaPayload { currentBidRoundId?: string; diff --git a/apps/web/src/api/hooks.ts b/apps/web/src/api/hooks.ts index 53f5e8f3..f303cc4c 100644 --- a/apps/web/src/api/hooks.ts +++ b/apps/web/src/api/hooks.ts @@ -423,10 +423,10 @@ export function useAuction( ) { return useQuery({ queryKey: ['auction', communityId, matchId ?? null], - queryFn: () => + queryFn: ({ signal }) => apiFetch( `/communities/${communityId}/matches/${matchId}/auction`, - { auth: 'none' }, + { auth: 'none', signal }, ), enabled: Boolean(communityId && matchId), retry: false, @@ -441,9 +441,10 @@ export function useAuctionHistory( ) { return useQuery({ queryKey: ['auction-history', communityId, matchId ?? null], - queryFn: () => + queryFn: ({ signal }) => apiFetch( `/communities/${communityId}/matches/${matchId}/auction/history`, + { signal }, ), enabled: Boolean(communityId && matchId && (options.enabled ?? true)), retry: false, diff --git a/apps/web/src/api/types.ts b/apps/web/src/api/types.ts index 71f469a2..db3a9d58 100644 --- a/apps/web/src/api/types.ts +++ b/apps/web/src/api/types.ts @@ -196,6 +196,7 @@ export interface ApiAuctionChatMessage { export interface ApiAuction { _id: string; matchId: string; + auctionEngineVersion?: 'legacy-renamed' | 'redis-v1'; phase: AuctionPhase; teams?: ApiAuctionTeam[]; auctionTeams: ApiAuctionTeam[]; diff --git a/apps/web/src/api/useAuctionSocket.ts b/apps/web/src/api/useAuctionSocket.ts index 12173b15..7a185e74 100644 --- a/apps/web/src/api/useAuctionSocket.ts +++ b/apps/web/src/api/useAuctionSocket.ts @@ -2,7 +2,12 @@ import { InteractionStatus } from '@azure/msal-browser'; import { useMsal } from '@azure/msal-react'; import { useCallback, useEffect, useRef, useState } from 'react'; import { io, type Socket } from 'socket.io-client'; -import type { AuctionReactionKind, AuctionReactionReply } from '@lolympus/contracts'; +import type { + AuctionHistoryUpdatedEvent, + AuctionReactionKind, + AuctionReactionReply, + AuctionStateIdentity, +} from '@lolympus/contracts'; import { appendAuctionReaction, isAuctionReaction, REACTION_LIFETIME_MS, sendAuctionReaction, type ActiveAuctionReaction } from './auction-reactions'; import { API_BASE_URL, useAuthUserId } from '../config'; import { acquireAccessToken } from '../lib/auth/token-provider'; @@ -19,6 +24,8 @@ import { shouldResyncForBidDelta, shouldApplyPolledAuctionState, mergeAuctionState, + resolveCurrentBidPointAmount, + createAuctionHeartbeatMonitor, type BidDeltaPayload, } from './auction-state'; import { resolveAuctionActionRejection } from './auction-action-retry'; @@ -105,6 +112,8 @@ interface BidAckPayload { } interface TimeSyncAckPayload { + auctionState?: AuctionStateIdentity | null; + auctionStateUnavailable?: boolean; clientSentAtTimestampMS?: number; currentServerTimestampMS?: number; ok?: boolean; @@ -169,6 +178,8 @@ export function useAuctionSocket(matchId: string | undefined) { const timeSyncSequenceRef = useRef(0); const hasSyncedServerTimeRef = useRef(false); const latestAuctionRef = useRef(null); + const synchronizedSocketRef = useRef(null); + const heartbeatMonitorRef = useRef(createAuctionHeartbeatMonitor()); const cancelledLifecycleIdsRef = useRef(new Set()); const latestConfirmedBidPointAmountRef = useRef(0); const [auction, setAuction] = useState(null); @@ -207,6 +218,11 @@ export function useAuctionSocket(matchId: string | undefined) { key: string; status: ConnectionStatus; }>({ key: '', status: 'idle' }); + const [synchronizationState, setSynchronizationState] = useState({ + key: '', + ready: false, + }); + const [historyUpdateState, setHistoryUpdateState] = useState({ key: '', count: 0 }); const [errorState, setErrorState] = useState<{ key: string; message: string | null; @@ -334,15 +350,32 @@ export function useAuctionSocket(matchId: string | undefined) { timeSyncTimeoutsRef.current = {}; } - function requestServerTimeSync(socket: Socket) { + const markSynchronized = useCallback( + (socket: Socket, key: string, ready: boolean) => { + if (socketRef.current !== socket) return; + setSynchronizationState((current) => + current.key === key && current.ready === ready ? current : { key, ready }, + ); + }, + [], + ); + + const requestServerTimeSync = useCallback((socket: Socket, key: string) => { if (!socket.connected) return; + const recordFailure = () => { + if (socketRef.current === socket && heartbeatMonitorRef.current.recordFailure()) { + markSynchronized(socket, key, false); + } + }; + const sequence = timeSyncSequenceRef.current + 1; timeSyncSequenceRef.current = sequence; const clientSentAtTimestampMS = Date.now(); timeSyncTimeoutsRef.current[sequence] = setTimeout(() => { delete timeSyncTimeoutsRef.current[sequence]; + recordFailure(); }, TIME_SYNC_TIMEOUT_MS); socket.emit( @@ -354,15 +387,35 @@ export function useAuctionSocket(matchId: string | undefined) { if (!timeoutId) return; clearTimeout(timeoutId); delete timeSyncTimeoutsRef.current[sequence]; + if (socketRef.current !== socket) return; if ( !ack?.ok || ack.sequence !== sequence || typeof ack.currentServerTimestampMS !== 'number' ) { + recordFailure(); return; } + if (ack.auctionStateUnavailable) { + recordFailure(); + } else { + const inspection = heartbeatMonitorRef.current.inspect( + latestAuctionRef.current, + ack.auctionState, + ); + if (inspection.needsResync) { + markSynchronized(socket, key, false); + void resyncRequesterRef.current?.request( + ack.auctionState?.revision, + ack.auctionState?.auctionLifecycleId, + ); + } else if (inspection.isCurrent) { + markSynchronized(socket, key, synchronizedSocketRef.current === socket); + } + } + const totalRoundTripDurationMS = Math.max( 0, clientReceivedAtTimestampMS - clientSentAtTimestampMS, @@ -391,7 +444,7 @@ export function useAuctionSocket(matchId: string | undefined) { ); }, ); - } + }, [markSynchronized]); function captureBidLatencyDiagnostics(ack?: BidAckPayload) { if (!ack?.bidDiagnostics && !ack?.bidServerTiming) return; @@ -472,6 +525,8 @@ export function useAuctionSocket(matchId: string | undefined) { const activeConnectionKey = connectionKey; const cancelledLifecycleIds = cancelledLifecycleIdsRef.current; let disposed = false; + let removeResumeListeners = () => {}; + let hasReceivedSnapshot = false; void (async () => { const accessToken = await acquireAccessToken().catch((error) => { @@ -490,7 +545,16 @@ export function useAuctionSocket(matchId: string | undefined) { }); socketRef.current = socket; + const notifyHistoryUpdated = () => { + setHistoryUpdateState((current) => ({ + key: activeConnectionKey, + count: current.key === activeConnectionKey ? current.count + 1 : 1, + })); + }; const receiveAuctionState = (nextAuction: ApiAuction, source: string) => { + if ( + disposed || socketRef.current !== socket || nextAuction.matchId !== matchId + ) return; const previousRevision = latestAuctionRef.current?.revision ?? -1; const didApplyState = commitAuctionState(nextAuction, source); if (didApplyState && (nextAuction.revision ?? 0) > previousRevision) { @@ -501,6 +565,13 @@ export function useAuctionSocket(matchId: string | undefined) { setChatMessages(mergeChatMessages([], nextAuction.chatMessages)); } if (didApplyState) { + if (hasReceivedSnapshot && synchronizedSocketRef.current !== socket) { + notifyHistoryUpdated(); + } + hasReceivedSnapshot = true; + synchronizedSocketRef.current = socket; + heartbeatMonitorRef.current.reset(); + markSynchronized(socket, activeConnectionKey, true); setError(null); } }; @@ -515,27 +586,44 @@ export function useAuctionSocket(matchId: string | undefined) { (state) => { receiveAuctionState(state, 'socket-resync'); }, + undefined, + () => markSynchronized(socket, activeConnectionKey, false), ); resyncRequesterRef.current = resyncRequester; socket.on('connect', () => { + synchronizedSocketRef.current = null; + heartbeatMonitorRef.current.reset(); + markSynchronized(socket, activeConnectionKey, false); setConnectionState({ key: activeConnectionKey, status: 'connected', }); setError(null); - socket.emit('join', { matchId }); - requestServerTimeSync(socket); + void socket.timeout(3000).emitWithAck('join', { matchId }) + .then((reply: { ok?: boolean }) => { + if (!reply?.ok && synchronizedSocketRef.current !== socket) { + markSynchronized(socket, activeConnectionKey, false); + } + }).catch(() => { + if (synchronizedSocketRef.current !== socket) { + markSynchronized(socket, activeConnectionKey, false); + } + }); + requestServerTimeSync(socket, activeConnectionKey); if (timeSyncIntervalRef.current) { clearInterval(timeSyncIntervalRef.current); } timeSyncIntervalRef.current = setInterval( - () => requestServerTimeSync(socket), + () => requestServerTimeSync(socket, activeConnectionKey), TIME_SYNC_INTERVAL_MS, ); }); socket.on('disconnect', () => { + synchronizedSocketRef.current = null; + heartbeatMonitorRef.current.reset(); + markSynchronized(socket, activeConnectionKey, false); setReactionState({ key: activeConnectionKey, items: [] }); resyncRequester.cancel(); clearTimeSyncTimers(); @@ -545,6 +633,34 @@ export function useAuctionSocket(matchId: string | undefined) { }); }); + socket.on('connect_error', (error: Error) => { + markSynchronized(socket, activeConnectionKey, false); + setConnectionState({ key: activeConnectionKey, status: 'error' }); + if (!socket.active) setError(resolveAuctionErrorMessage(error.message)); + }); + + socket.on('history-updated', (event: AuctionHistoryUpdatedEvent) => { + if (event.matchId !== matchId) return; + const lifecycle = latestAuctionRef.current?.auctionLifecycleId; + if (lifecycle && lifecycle !== event.auctionLifecycleId) return; + // Older append retries may fill a gap without increasing the maximum + // sequence. Every committed-history notification refreshes the list. + notifyHistoryUpdated(); + }); + + const resyncAfterResume = () => { + if (document.visibilityState === 'visible' && socket.connected) { + notifyHistoryUpdated(); + void resyncRequester.request(); + } + }; + document.addEventListener('visibilitychange', resyncAfterResume); + window.addEventListener('online', resyncAfterResume); + removeResumeListeners = () => { + document.removeEventListener('visibilitychange', resyncAfterResume); + window.removeEventListener('online', resyncAfterResume); + }; + socket.on('state-pending', () => { void resyncRequester.request(); }); @@ -673,6 +789,7 @@ export function useAuctionSocket(matchId: string | undefined) { return () => { disposed = true; + removeResumeListeners(); resyncRequesterRef.current?.cancel(); resyncRequesterRef.current = null; const socket = socketRef.current; @@ -689,6 +806,7 @@ export function useAuctionSocket(matchId: string | undefined) { pendingBidTimeoutsRef.current = {}; pendingActionTimeoutsRef.current = {}; latestAuctionRef.current = null; + synchronizedSocketRef.current = null; cancelledLifecycleIds.clear(); latestConfirmedBidPointAmountRef.current = 0; hasSyncedServerTimeRef.current = false; @@ -707,6 +825,8 @@ export function useAuctionSocket(matchId: string | undefined) { connectionKey, matchId, receiveReaction, + markSynchronized, + requestServerTimeSync, setError, ]); @@ -906,6 +1026,11 @@ export function useAuctionSocket(matchId: string | undefined) { pendingBidRequests, resetAt, connectionStatus: visibleConnectionStatus, + isSynchronized: + synchronizationState.key === connectionKey && + synchronizationState.ready && visibleConnectionStatus === 'connected', + historyUpdateCount: + historyUpdateState.key === connectionKey ? historyUpdateState.count : 0, error: visibleError, bidLatencyDiagnostics, serverRoundTripMs: matchId ? serverRoundTripMs : null, @@ -1027,15 +1152,6 @@ function createClientRequestId(): string { return `${Date.now()}-${Math.random().toString(16).slice(2)}`; } -function resolveCurrentBidPointAmount(auction: ApiAuction): number { - const bidPointAmount = Math.max( - Number(auction.currentHighestBidPointAmount), - Number(auction.currentBid), - 0, - ); - return Number.isFinite(bidPointAmount) ? bidPointAmount : 0; -} - // [AUCTION-DEBUG] 아래 텔레메트리 헬퍼들과 호출부는 조사 종료 후 제거 대상. type AuctionTelemetryAttributes = Record< string, diff --git a/apps/web/src/pages/AuctionPage.tsx b/apps/web/src/pages/AuctionPage.tsx index 994a3b5d..c07eeaef 100644 --- a/apps/web/src/pages/AuctionPage.tsx +++ b/apps/web/src/pages/AuctionPage.tsx @@ -37,6 +37,10 @@ import { } from "../config"; import { ApiError } from "../lib/api"; import { mergeAuctionState } from "../api/auction-state"; +import { + AUCTION_SAFETY_POLL_INTERVAL_MS, + startAuctionRecoveryPolling, +} from "../api/auction-polling"; import { profileIconUrl, } from "../lib/ddragon"; @@ -1196,16 +1200,23 @@ export default function AuctionPage() { currentUser?.role === "owner" || currentUser?.role === "admin"; const matchQuery = useMatch(id, DEFAULT_COMMUNITY_ID); + const socket = useAuctionSocket(id); const auctionQuery = useAuction(id, DEFAULT_COMMUNITY_ID, { - refetchInterval: 5000, + refetchInterval: AUCTION_SAFETY_POLL_INTERVAL_MS, }); + const { refetch: refetchAuction } = auctionQuery; + useEffect(() => { + if (!id || socket.isSynchronized) return; + return startAuctionRecoveryPolling(() => { + void refetchAuction({ cancelRefetch: false }); + }); + }, [id, socket.isSynchronized, refetchAuction]); const historyQuery = useAuctionHistory(id, DEFAULT_COMMUNITY_ID, { enabled: canManageAuction, - refetchInterval: 3000, + refetchInterval: AUCTION_SAFETY_POLL_INTERVAL_MS, }); const undoMutation = useUndoAuction(DEFAULT_COMMUNITY_ID); const resetAuctionMutation = useResetAuction(DEFAULT_COMMUNITY_ID); - const socket = useAuctionSocket(id); const savedCaptainTeamIndex = useAuctionCaptainTeamIndex(id); const [assignedPositionDraft, setAssignedPositionDraft] = useState<{ playerId: string | null; @@ -1256,6 +1267,13 @@ export default function AuctionPage() { useEffect(() => { syncPolledAuctionState(auctionQuery.data); }, [auctionQuery.data, syncPolledAuctionState]); + useEffect(() => { + if (!id || !canManageAuction || !socket.historyUpdateCount) return; + void queryClient.invalidateQueries({ + queryKey: ["auction-history", DEFAULT_COMMUNITY_ID, id], + exact: true, + }); + }, [id, canManageAuction, queryClient, socket.historyUpdateCount]); const liveAuction = pickLatestAuction(socket.auction, auctionQuery.data); const auctionState = useMemo( () => @@ -1384,18 +1402,25 @@ export default function AuctionPage() { sequence, }, { - onSuccess: async () => { - await Promise.all([ + onSuccess: async (updatedAuction) => { + const refreshes = [ queryClient.invalidateQueries({ queryKey: ["auction", DEFAULT_COMMUNITY_ID, id], }), - queryClient.invalidateQueries({ - queryKey: ["auction-history", DEFAULT_COMMUNITY_ID, id], - }), queryClient.invalidateQueries({ queryKey: ["match", DEFAULT_COMMUNITY_ID, id], }), - ]); + ]; + // Redis history refreshes on its post-durability event. Legacy Undo + // commits before responding and does not emit that event. + if (updatedAuction.auctionEngineVersion !== "redis-v1") { + refreshes.push( + queryClient.invalidateQueries({ + queryKey: ["auction-history", DEFAULT_COMMUNITY_ID, id], + }), + ); + } + await Promise.all(refreshes); }, }, ); diff --git a/apps/web/test/auction-polling.test.ts b/apps/web/test/auction-polling.test.ts new file mode 100644 index 00000000..a3e0e4b4 --- /dev/null +++ b/apps/web/test/auction-polling.test.ts @@ -0,0 +1,75 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { setImmediate } from 'node:timers/promises'; +import { + environmentManager, + QueryClient, + QueryObserver, +} from '@tanstack/react-query'; +import { + AUCTION_SAFETY_POLL_INTERVAL_MS, + startAuctionRecoveryPolling, +} from '../src/api/auction-polling.ts'; + +test('repeated short recovery periods cannot postpone the safety HTTP poll', async (t) => { + t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: 0 }); + const wasServer = environmentManager.isServer(); + environmentManager.setIsServer(() => false); + const client = new QueryClient({ + defaultOptions: { queries: { retry: false, gcTime: Infinity } }, + }); + const requests: number[] = []; + const observer = new QueryObserver(client, { + queryKey: ['auction', 'match-1'], + queryFn: async () => { + requests.push(Date.now()); + return { revision: 1 }; + }, + refetchInterval: AUCTION_SAFETY_POLL_INTERVAL_MS, + }); + const unsubscribe = observer.subscribe(() => {}); + let stopRecovery: (() => void) | undefined; + t.after(() => { + stopRecovery?.(); + unsubscribe(); + client.clear(); + environmentManager.setIsServer(() => wasServer); + }); + await setImmediate(); + + for (let attempt = 0; attempt < 6; attempt += 1) { + stopRecovery = startAuctionRecoveryPolling( + () => { void observer.refetch({ cancelRefetch: false }); }, + () => true, + ); + t.mock.timers.tick(1000); + await setImmediate(); + stopRecovery(); + t.mock.timers.tick(9000); + await setImmediate(); + } + + assert.deepEqual(requests, [0, 30_000, 60_000]); +}); + +test('recovery polls every five seconds while visible and stops after cleanup', (t) => { + t.mock.timers.enable({ apis: ['setInterval'] }); + let visible = true; + let requests = 0; + const stop = startAuctionRecoveryPolling( + () => { requests += 1; }, + () => visible, + ); + t.after(stop); + t.mock.timers.tick(5000); + assert.equal(requests, 1); + visible = false; + t.mock.timers.tick(10_000); + assert.equal(requests, 1); + visible = true; + t.mock.timers.tick(5000); + assert.equal(requests, 2); + stop(); + t.mock.timers.tick(10_000); + assert.equal(requests, 2); +}); diff --git a/apps/web/test/auction-resync.test.ts b/apps/web/test/auction-resync.test.ts index 1e9dc72c..e5533f1a 100644 --- a/apps/web/test/auction-resync.test.ts +++ b/apps/web/test/auction-resync.test.ts @@ -72,6 +72,24 @@ test('bounds retries and honors retry delay without rejoining the room', async ( assert.deepEqual(waits, [1500, 1500]); }); +test('reports failure when all replies remain behind the required revision', async () => { + let failed = 0; + let applied = 0; + const sync = createAuctionResyncRequester( + async () => ({ ok: true, state: state(10) }), + () => { + applied++; + }, + noWait, + () => { + failed++; + }, + ); + await sync.request(12, 'life'); + assert.equal(applied, 3); + assert.equal(failed, 1); +}); + test('drops an old connection reply after cancellation and permits a new request', async () => { let finish!: (reply: AuctionResyncReply) => void; let calls = 0; diff --git a/apps/web/test/auction-state.test.ts b/apps/web/test/auction-state.test.ts index 039e52dd..ffb6c29b 100644 --- a/apps/web/test/auction-state.test.ts +++ b/apps/web/test/auction-state.test.ts @@ -6,9 +6,192 @@ import { shouldApplyIncomingAuctionState, shouldApplyPolledAuctionState, shouldResyncForBidDelta, + resolveCurrentBidPointAmount, + shouldResyncForAuctionIdentity, + createAuctionHeartbeatMonitor, } from "../src/api/auction-state.ts"; import type { ApiAuction } from "../src/api/types.ts"; +test("Redis-only snapshots retain the bid amount without a legacy alias", () => { + assert.equal( + resolveCurrentBidPointAmount({ + currentHighestBidPointAmount: 310, + } as ApiAuction), + 310, + ); + assert.equal( + resolveCurrentBidPointAmount({ currentBid: 120 } as ApiAuction), + 120, + ); + assert.equal( + resolveCurrentBidPointAmount({ + currentHighestBidPointAmount: 0, + currentBid: 120, + } as ApiAuction), + 0, + ); + assert.equal( + resolveCurrentBidPointAmount({ + currentHighestBidPointAmount: NaN, + } as ApiAuction), + 0, + ); +}); + +test("heartbeats detect missed state and separately versioned side effects", () => { + const state = { + auctionLifecycleId: "life", + revision: 10, + matchSideEffectRevision: 2, + } as ApiAuction; + assert.equal( + shouldResyncForAuctionIdentity(state, { + auctionLifecycleId: "life", + revision: 11, + }), + true, + ); + assert.equal( + shouldResyncForAuctionIdentity(state, { + auctionLifecycleId: "life", + revision: 10, + matchSideEffectRevision: 3, + }), + true, + ); + assert.equal( + shouldResyncForAuctionIdentity(state, { + auctionLifecycleId: "life", + revision: 10, + matchSideEffectRevision: 2, + }), + false, + ); + assert.equal( + shouldResyncForAuctionIdentity(state, { + auctionLifecycleId: "new-life", + revision: 1, + }), + true, + ); +}); + +test("a delayed heartbeat cannot pull an advanced socket backward", () => { + const advanced = { auctionLifecycleId: "life", revision: 12 } as ApiAuction; + assert.equal( + shouldResyncForAuctionIdentity(advanced, { + auctionLifecycleId: "life", + revision: 11, + matchSideEffectRevision: 9, + }), + false, + ); +}); + +test("expired complete runtimes and legacy archives do not repeatedly resync", () => { + assert.equal( + shouldResyncForAuctionIdentity( + { auctionLifecycleId: "life", phase: "complete" } as ApiAuction, + null, + ), + false, + ); + assert.equal( + shouldResyncForAuctionIdentity({ phase: "bidding" } as ApiAuction, null), + false, + ); + assert.equal( + shouldResyncForAuctionIdentity( + { + auctionEngineVersion: "legacy-renamed", + auctionLifecycleId: "legacy-auction:1", + phase: "bidding", + } as ApiAuction, + null, + ), + false, + ); + assert.equal( + shouldResyncForAuctionIdentity( + { + auctionEngineVersion: "redis-v1", + auctionLifecycleId: "life", + phase: "bidding", + } as ApiAuction, + null, + ), + true, + ); +}); + +test("a new in-flight bid does not confirm an earlier gap that already closed", () => { + const monitor = createAuctionHeartbeatMonitor(); + const client = { + auctionEngineVersion: "redis-v1", + auctionLifecycleId: "life", + revision: 10, + } as ApiAuction; + assert.equal( + monitor.inspect(client, { auctionLifecycleId: "life", revision: 11 }) + .needsResync, + false, + ); + const caughtUp = { ...client, revision: 11 }; + assert.equal( + monitor.inspect(caughtUp, { auctionLifecycleId: "life", revision: 12 }) + .needsResync, + false, + ); + assert.equal( + monitor.inspect(caughtUp, { auctionLifecycleId: "life", revision: 13 }) + .needsResync, + true, + ); +}); + +test("heartbeat failures require two misses and reset after a healthy reply", () => { + const monitor = createAuctionHeartbeatMonitor(); + assert.equal(monitor.recordFailure(), false); + assert.equal(monitor.recordFailure(), true); + monitor.inspect( + { auctionEngineVersion: "legacy-renamed" } as ApiAuction, + null, + ); + assert.equal(monitor.recordFailure(), false); + monitor.reset(); + assert.equal(monitor.recordFailure(), false); +}); + +test("heartbeat confirmation covers side effects and missing runtimes", () => { + const monitor = createAuctionHeartbeatMonitor(); + const client = { + auctionEngineVersion: "redis-v1", + auctionLifecycleId: "life", + revision: 10, + matchSideEffectRevision: 1, + } as ApiAuction; + const identity = { + auctionLifecycleId: "life", + revision: 10, + matchSideEffectRevision: 2, + }; + assert.equal(monitor.inspect(client, identity).needsResync, false); + assert.equal( + monitor.inspect({ ...client, matchSideEffectRevision: 2 }, identity) + .isCurrent, + true, + ); + assert.equal(monitor.inspect(client, null).needsResync, false); + assert.equal(monitor.inspect(client, null).needsResync, true); + monitor.reset(); + const legacy = { + ...client, + auctionEngineVersion: "legacy-renamed", + } as ApiAuction; + assert.equal(monitor.inspect(legacy, null).isCurrent, true); + assert.equal(monitor.inspect(legacy, null).needsResync, false); +}); + const current = { auctionLifecycleId: "life", revision: 10, diff --git a/packages/contracts/src/auction.ts b/packages/contracts/src/auction.ts index 5931afa6..40ad9098 100644 --- a/packages/contracts/src/auction.ts +++ b/packages/contracts/src/auction.ts @@ -30,3 +30,16 @@ export interface AuctionReactionReply { reaction?: AuctionReactionEvent; code?: "NOT_JOINED" | "INVALID_REACTION" | "REACTION_RATE_LIMITED"; } + +export interface AuctionStateIdentity { + auctionLifecycleId?: string; + revision?: number; + matchSideEffectRevision?: number; + matchSideEffectTransitionId?: string; +} + +export interface AuctionHistoryUpdatedEvent { + matchId: string; + auctionLifecycleId: string; + auctionStateTransitionId: string; +}