fix: align ticket timers to server clock

This commit is contained in:
Esdras Renan 2025-10-19 20:27:11 -03:00
parent 3b5676ed35
commit 090ebb9607
7 changed files with 162 additions and 17 deletions

View file

@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest"
import { reconcileLocalSessionStart } from "@/components/tickets/ticket-timer.utils"
import { deriveServerOffset, reconcileLocalSessionStart, toServerTimestamp } from "@/components/tickets/ticket-timer.utils"
describe("reconcileLocalSessionStart", () => {
it("usa o timestamp remoto quando não há marcador local", () => {
@ -34,3 +34,26 @@ describe("reconcileLocalSessionStart", () => {
expect(result).toEqual({ localStart: 4_000, origin: "remote" })
})
})
describe("deriveServerOffset", () => {
it("calcula deslocamento simples entre relógios", () => {
const offset = deriveServerOffset({ currentOffset: 0, localNow: 1_700_000, serverNow: 1_699_000 })
expect(offset).toBe(1_000)
})
it("ignora serverNow inválido", () => {
const offset = deriveServerOffset({ currentOffset: 500, localNow: 2_000_000, serverNow: Number.NaN })
expect(offset).toBe(500)
})
it("aceita saltos grandes quando necessário", () => {
const offset = deriveServerOffset({ currentOffset: 500, localNow: 2_000_000, serverNow: 1_000_000 })
expect(offset).toBe(1_000_000)
})
})
describe("toServerTimestamp", () => {
it("aplica offset corretamente", () => {
expect(toServerTimestamp(1_000_000, 1_500)).toBe(998_500)
})
})