import { afterEach, describe, expect, it, vi } from 'vitest' import { AutoReservationService } from '../electron/main/auto-reservation' import { CLOCK_CALIBRATION_DEFAULTS, type AutoReservationConfig, type ClockEstimate, type ReservationResult } from '../src/type/reservation' function estimate(): ClockEstimate { const now = Date.now() return { monitoring: true, observedAtMs: now, estimatedServerTimeMs: now, offsetMs: 0, driftPpm: 0, minRttMs: 80, p50RttMs: 82, p95RttMs: 90, oneWayMs: 40, uncertaintyMs: 540, sampleAgeMs: 0, acceptedSamples: 8, rawOffsetMs: 0, boundaryCount: 5, boundaryOffsetMs: 0, boundaryUncertaintyMs: 50, boundarySpreadMs: 20, recommendedCorrectionMs: 0, calibrationReady: true, calibrationApplied: true, calibrationSettings: CLOCK_CALIBRATION_DEFAULTS, remoteIp: '13.125.18.86', quality: 'fair', samples: [], } } function config(targetServerTimeMs: number): AutoReservationConfig { return { targetServerTimeMs, userOffsetMs: 0, captchaPastedAtMs: Date.now(), request: { branchNum: 22, themeNum: 66, themeInfoNum: 44, date: '2026-08-18', timeNum: 2309, timeLabel: '14:25', themeName: '내 방', totalPrice: 60_000, person: 2, name: '테스트', phone: '010-1234-5678', captchaToken: 'x'.repeat(100), }, } } describe('single-shot auto reservation scheduler', () => { afterEach(() => vi.useRealTimers()) it('consumes one token and submits exactly once', async () => { vi.useFakeTimers() vi.setSystemTime(new Date('2026-08-12T06:00:00.000Z')) const execute = vi.fn<() => Promise>().mockResolvedValue({ success: true, message: '완료', reservationNum: 'R-1', transport: { sentAtMs: Date.now(), receivedAtMs: Date.now() + 80, rttMs: 80, httpStatus: 200, remoteIp: '13.125.18.86', reusedSocket: true, }, }) const clock = { ensureSample: async () => estimate(), getEstimate: estimate, setAutoTarget: vi.fn() } const service = new AutoReservationService(clock, execute, vi.fn()) await service.arm(config(Date.now() + 1_000)) await vi.advanceTimersByTimeAsync(1_500) expect(execute).toHaveBeenCalledTimes(1) expect(service.getState().status).toBe('succeeded') expect(service.getState().tokenStatus).toBe('consumed') await vi.advanceTimersByTimeAsync(5_000) expect(execute).toHaveBeenCalledTimes(1) }) it('disarming clears the schedule and prevents submission', async () => { vi.useFakeTimers() vi.setSystemTime(new Date('2026-08-12T06:00:00.000Z')) const execute = vi.fn<() => Promise>() const clock = { ensureSample: async () => estimate(), getEstimate: estimate, setAutoTarget: vi.fn() } const service = new AutoReservationService(clock, execute, vi.fn()) await service.arm(config(Date.now() + 1_000)) service.disarm() await vi.advanceTimersByTimeAsync(2_000) expect(execute).not.toHaveBeenCalled() expect(service.getState().tokenStatus).toBe('empty') }) it('does not fire when the target is already missed by more than one second', async () => { vi.useFakeTimers() vi.setSystemTime(new Date('2026-08-12T06:00:00.000Z')) const execute = vi.fn<() => Promise>() const clock = { ensureSample: async () => estimate(), getEstimate: estimate, setAutoTarget: vi.fn() } const service = new AutoReservationService(clock, execute, vi.fn()) await service.arm(config(Date.now() + 500)) vi.setSystemTime(Date.now() + 2_000) await vi.runOnlyPendingTimersAsync() expect(execute).not.toHaveBeenCalled() expect(service.getState().status).toBe('failed') }) })