import { describe, expect, it } from "bun:test"

import { normalizeDeviceRemoteAccess } from "@/components/admin/devices/admin-devices-overview"

describe("normalizeDeviceRemoteAccess", () => {
  it("returns null when value is empty", () => {
    expect(normalizeDeviceRemoteAccess(undefined)).toBeNull()
    expect(normalizeDeviceRemoteAccess(" ")).toBeNull()
  })

  it("parses plain identifier strings", () => {
    const result = normalizeDeviceRemoteAccess("PC-001")
    expect(result).toEqual({
      provider: null,
      identifier: "PC-001",
      username: null,
      password: null,
      url: null,
      notes: null,
      lastVerifiedAt: null,
      metadata: null,
    })
  })

  it("detects URLs in string input", () => {
    const result = normalizeDeviceRemoteAccess("https://remote.example.com/session/123")
    expect(result).toEqual({
      provider: null,
      identifier: null,
      username: null,
      password: null,
      url: "https://remote.example.com/session/123",
      notes: null,
      lastVerifiedAt: null,
      metadata: null,
    })
  })

  it("normalizes object payload with aliases", () => {
    const timestamp = 1_701_234_567_890
    const result = normalizeDeviceRemoteAccess({
      provider: "AnyDesk",
      code: "123-456-789",
      remoteUrl: "https://anydesk.com/session/123",
      note: "Suporte avançado",
      user: "admin",
      secret: "S3nh@",
      verifiedAt: timestamp,
      extraTag: "vip",
    })
    expect(result).toEqual({
      provider: "AnyDesk",
      identifier: "123-456-789",
      username: "admin",
      password: "S3nh@",
      url: "https://anydesk.com/session/123",
      notes: "Suporte avançado",
      lastVerifiedAt: timestamp,
      metadata: {
        provider: "AnyDesk",
        code: "123-456-789",
        remoteUrl: "https://anydesk.com/session/123",
        note: "Suporte avançado",
        user: "admin",
        secret: "S3nh@",
        verifiedAt: timestamp,
        extraTag: "vip",
      },
    })
  })
})
