import { describe, expect, it } from 'vitest' import { appendClockSample, computePlannedSendAtMs, estimateBoundaryOffset, estimateClock, observationToSample, quantile } from '../electron/main/server-clock-core' import { CLOCK_CALIBRATION_DEFAULTS, type ClockSample } from '../src/type/reservation' function boundarySamples(offsets: number[]): ClockSample[] { const base = 1_800_000_000_000 return offsets.flatMap((offset, index) => { const serverBoundary = base + index * 2_000 const beforeMidpoint = serverBoundary - offset - 25 const afterMidpoint = serverBoundary - offset + 25 return [ { id: index * 2 + 1, sentAtMs: beforeMidpoint - 20, receivedAtMs: beforeMidpoint + 20, midpointMs: beforeMidpoint, serverDateMs: serverBoundary - 1_000, rttMs: 40, offsetEstimateMs: serverBoundary - 500 - beforeMidpoint, offsetMinMs: 0, offsetMaxMs: 0, remoteIp: '10.0.0.1', reusedSocket: true, accepted: true, }, { id: index * 2 + 2, sentAtMs: afterMidpoint - 20, receivedAtMs: afterMidpoint + 20, midpointMs: afterMidpoint, serverDateMs: serverBoundary, rttMs: 40, offsetEstimateMs: serverBoundary + 500 - afterMidpoint, offsetMinMs: 0, offsetMaxMs: 0, remoteIp: '10.0.0.1', reusedSocket: true, accepted: true, }, ] }) } describe('server clock estimator', () => { it('rejects cached and high-RTT observations', () => { const cached = observationToSample({ id: 1, sentAtMs: 1_000, receivedAtMs: 1_080, rttMs: 80, serverDateHeader: new Date(1_000).toUTCString(), remoteIp: '127.0.0.1', reusedSocket: true, ageHeader: '2', statusCode: 200, }) const slow = observationToSample({ id: 2, sentAtMs: 1_000, receivedAtMs: 4_000, rttMs: 3_000, serverDateHeader: new Date(1_000).toUTCString(), remoteIp: '127.0.0.1', reusedSocket: false, statusCode: 200, }) expect(cached.accepted).toBe(false) expect(slow.accepted).toBe(false) }) it('uses low-latency samples and reports distribution metrics', () => { const base = 1_800_000_000_000 const trueOffset = 240 const rtts = [82, 78, 85, 81, 900, 79, 84, 80] const samples = rtts.map((rtt, index) => { const sentAtMs = base + index * 237 const receivedAtMs = sentAtMs + rtt const midpoint = (sentAtMs + receivedAtMs) / 2 const serverDateMs = Math.floor((midpoint + trueOffset) / 1_000) * 1_000 return observationToSample({ id: index + 1, sentAtMs, receivedAtMs, rttMs: rtt, serverDateHeader: new Date(serverDateMs).toUTCString(), remoteIp: '13.125.18.86', reusedSocket: index > 0, statusCode: 200, }) }) const estimate = estimateClock(samples, base + 2_000, true) expect(estimate.acceptedSamples).toBe(7) expect(estimate.minRttMs).toBe(78) expect(estimate.p95RttMs).toBeGreaterThan(80) expect(Math.abs(estimate.offsetMs - trueOffset)).toBeLessThan(550) expect(estimate.remoteIp).toBe('13.125.18.86') }) it('computes a server-arrival-based send time', () => { expect(computePlannedSendAtMs(10_000, -25, { offsetMs: 200, oneWayMs: 40 })).toBe(9_735) expect(quantile([1, 2, 3, 4], 0.5)).toBe(2.5) }) it('resets the estimator window when the L4/L7 route address changes', () => { const first = observationToSample({ id: 1, sentAtMs: 1_000, receivedAtMs: 1_080, rttMs: 80, serverDateHeader: new Date(1_000).toUTCString(), remoteIp: '10.0.0.1', reusedSocket: false, statusCode: 200, }) const second = observationToSample({ id: 2, sentAtMs: 2_000, receivedAtMs: 2_080, rttMs: 80, serverDateHeader: new Date(2_000).toUTCString(), remoteIp: '10.0.0.2', reusedSocket: false, statusCode: 200, }) expect(appendClockSample([first], second)).toEqual([second]) }) it('marks old estimates as poor quality', () => { const sample = observationToSample({ id: 1, sentAtMs: 1_000, receivedAtMs: 1_080, rttMs: 80, serverDateHeader: new Date(1_000).toUTCString(), remoteIp: '10.0.0.1', reusedSocket: false, statusCode: 200, }) expect(estimateClock([sample], 20_000).quality).toBe('poor') }) it('combines multiple second boundaries and rejects a phase outlier', () => { const samples = boundarySamples([240, 245, 235, 242, 238, 610]) const boundary = estimateBoundaryOffset(samples) expect(boundary?.count).toBe(5) expect(boundary?.offsetMs).toBeGreaterThanOrEqual(235) expect(boundary?.offsetMs).toBeLessThanOrEqual(245) expect(boundary?.spreadMs).toBeLessThan(10) const estimate = estimateClock(samples, samples.at(-1)!.receivedAtMs, true) expect(estimate.calibrationReady).toBe(true) expect(estimate.calibrationApplied).toBe(true) expect(estimate.offsetMs).toBe(boundary?.offsetMs) expect(estimate.uncertaintyMs).toBeLessThan(50) }) it('reports a recommendation but does not apply it when auto apply is disabled', () => { const samples = boundarySamples([240, 245, 235, 242, 238]) const estimate = estimateClock(samples, samples.at(-1)!.receivedAtMs, true, { ...CLOCK_CALIBRATION_DEFAULTS, autoApply: false, }) expect(estimate.calibrationReady).toBe(true) expect(estimate.calibrationApplied).toBe(false) expect(estimate.offsetMs).toBe(estimate.rawOffsetMs) expect(estimate.recommendedCorrectionMs).not.toBe(0) }) })