import { describe, it, expect } from "vitest"
import { parseUserAgent, formatRelativeTime } from "./security-utils"

describe("security-utils", () => {
  describe("parseUserAgent", () => {
    it("should return Unknown for null input", () => {
      expect(parseUserAgent(null)).toEqual({ device: "Unknown", browser: "Unknown" })
    })

    it("should return Unknown for empty string", () => {
      expect(parseUserAgent("")).toEqual({ device: "Unknown", browser: "Unknown" })
    })

    it("should detect Mobile device for iPhone", () => {
      const ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15"
      const result = parseUserAgent(ua)
      expect(result.device).toBe("Mobile")
    })

    it("should detect Mobile device for Android", () => {
      const ua = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"
      const result = parseUserAgent(ua)
      expect(result.device).toBe("Mobile")
    })

    it("should detect Tablet device for iPad", () => {
      const ua = "Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15"
      const result = parseUserAgent(ua)
      expect(result.device).toBe("Tablet")
    })

    it("should detect Desktop device for Windows", () => {
      const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
      const result = parseUserAgent(ua)
      expect(result.device).toBe("Desktop")
    })

    it("should detect Edge browser", () => {
      const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
      const result = parseUserAgent(ua)
      expect(result.browser).toBe("Edge")
    })

    it("should detect Chrome browser", () => {
      const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
      const result = parseUserAgent(ua)
      expect(result.browser).toBe("Chrome")
    })

    it("should detect Firefox browser", () => {
      const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0"
      const result = parseUserAgent(ua)
      expect(result.browser).toBe("Firefox")
    })

    it("should detect Safari browser (without Chrome)", () => {
      const ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15"
      const result = parseUserAgent(ua)
      expect(result.browser).toBe("Safari")
    })

    it("should return Unknown browser for unrecognized UA", () => {
      const ua = "SomeCustomBrowser/1.0"
      const result = parseUserAgent(ua)
      expect(result.browser).toBe("Unknown")
    })
  })

  describe("formatRelativeTime", () => {
    it("should return '刚刚' for zh locale when less than 1 minute ago", () => {
      const now = new Date()
      expect(formatRelativeTime(now.toISOString(), "zh")).toBe("刚刚")
    })

    it("should return 'just now' for en locale when less than 1 minute ago", () => {
      const now = new Date()
      expect(formatRelativeTime(now.toISOString(), "en")).toBe("just now")
    })

    it("should return minutes ago for zh locale", () => {
      const past = new Date(Date.now() - 5 * 60 * 1000) // 5 minutes ago
      const result = formatRelativeTime(past.toISOString(), "zh")
      expect(result).toContain("5")
      expect(result).toContain("分钟")
    })

    it("should return hours ago for en locale", () => {
      const past = new Date(Date.now() - 3 * 60 * 60 * 1000) // 3 hours ago
      const result = formatRelativeTime(past.toISOString(), "en")
      expect(result).toContain("3")
      expect(result).toContain("hour")
    })

    it("should return days ago for en locale", () => {
      const past = new Date(Date.now() - 5 * 24 * 60 * 60 * 1000) // 5 days ago
      const result = formatRelativeTime(past.toISOString(), "en")
      expect(result).toContain("5")
      expect(result).toContain("day")
    })

    it("should return formatted date for dates older than 30 days", () => {
      const past = new Date(Date.now() - 60 * 24 * 60 * 60 * 1000) // 60 days ago
      const result = formatRelativeTime(past.toISOString(), "en")
      // Should be a date string, not a relative time
      expect(result).toMatch(/\d/)
    })
  })
})
