import { describe, it, expect } from "vitest";
import { unifyDeviceFields } from "../mappers.js";

describe("unifyDeviceFields", () => {
  it("maps snake_case fields to camelCase", () => {
    const device = {
      device_name: "Relay 1",
      device_hard_id: "abc123",
      device_ip: "192.168.1.10",
      device_type: "relay",
      ip_address: "192.168.1.10",
      mac_address: "A4:CF:12:9B:3F:D2",
      device_mac: "A4:CF:12:9B:3F:D2",
      core_version: "1.0",
    };

    const result = unifyDeviceFields(device);

    expect(result.name).toBe("Relay 1");
    expect(result.device_id).toBe("abc123");
    expect(result.ip).toBe("192.168.1.10");
    expect(result.device_type).toBe("relay");
    expect(result.mac).toBe("A4:CF:12:9B:3F:D2");
    expect(result.firmware_core_version).toBe("1.0");
  });

  it("passes through unknown fields unchanged", () => {
    const device = {
      id: 42,
      unknown_field: "value",
    };

    const result = unifyDeviceFields(device);

    expect(result.id).toBe(42);
    expect(result.unknown_field).toBe("value");
  });

  it("returns empty object for null input", () => {
    const result = unifyDeviceFields(null);

    expect(result).toEqual({});
  });

  it("returns empty object for undefined input", () => {
    const result = unifyDeviceFields(undefined);

    expect(result).toEqual({});
  });

  it("handles mixed known and unknown fields", () => {
    const device = {
      device_name: "Button 1",
      alias: "kitchen_btns",
      extra: { a: 1 },
    };

    const result = unifyDeviceFields(device);

    expect(result.name).toBe("Button 1");
    expect(result.alias).toBe("kitchen_btns");
    expect(result.extra).toEqual({ a: 1 });
  });

  it("maps legacy 'type' to 'device_type' for backwards compatibility", () => {
    const device = {
      type: "relay",
      name: "Relay 1",
    };

    const result = unifyDeviceFields(device);

    expect(result.device_type).toBe("relay");
    expect(result.name).toBe("Relay 1");
  });
});
