import { beforeEach, describe, expect, it, vi } from "vitest" import { isValidSession } from "./session" vi.mock("@scandic-hotels/common/logger/createLogger", () => { return { createLogger: (_name: string) => ({ error: vi.fn(), debug: vi.fn(), }), } }) describe("isValidSession", () => { beforeEach(() => { vi.restoreAllMocks() }) it("returns false for null session and does not log", () => { expect(isValidSession(null)).toBe(false) }) it("returns false and logs when session.error is present", () => { const s: any = { error: "bad" } expect(isValidSession(s)).toBe(false) }) it("returns false and logs when session.token.error is present", () => { const s: any = { token: { error: "tokfail" } } expect(isValidSession(s)).toBe(false) }) it("returns false and logs when token.expires_at is in the past", () => { const now = 1_700_000_000_000 vi.spyOn(Date, "now").mockReturnValue(now) const s: any = { token: { expires_at: now - 1000 } } expect(isValidSession(s)).toBe(false) }) it("returns true when token.expires_at equals Date.now()", () => { const now = 1_700_000_000_000 vi.spyOn(Date, "now").mockReturnValue(now) const s: any = { token: { expires_at: now } } expect(isValidSession(s)).toBe(true) }) it("returns true for session without token.expires_at", () => { const s: any = { token: { access: "ok" } } expect(isValidSession(s)).toBe(true) }) it("returns true for a fully valid session with future expires_at", () => { const now = 1_700_000_000_000 vi.spyOn(Date, "now").mockReturnValue(now) const s: any = { token: { expires_at: now + 10_000 } } expect(isValidSession(s)).toBe(true) }) })