import { hasPlatformErrors, getSingleTab } from "../utils";
import { DEVICE_SUPPORTED, ANDROID } from "../constants";

describe("hasPlatformErrors", () => {
  it("Happy case for hasPlatformErrors", () => {
    const result = hasPlatformErrors();
    expect(result).toBe(false);
  });
  it("returns false when called with no arguments", () => {
    expect(hasPlatformErrors()).toBe(false);
  });

  it("returns false when given null or undefined", () => {
    expect(hasPlatformErrors(null)).toBe(false);
    expect(hasPlatformErrors(undefined)).toBe(false);
  });

  it("returns false for empty error groups", () => {
    const emptyGroup = { ANDROID: [], IOS: [], GENERIC: [] };
    expect(hasPlatformErrors(emptyGroup)).toBe(false);
  });

  it("detects Android errors", () => {
    const data = { ANDROID: ["e1"], IOS: [], GENERIC: [] };
    expect(hasPlatformErrors(data)).toBe(true);
  });

  it("detects IOS errors", () => {
    const data = { ANDROID: [], IOS: ["e1"], GENERIC: [] };
    expect(hasPlatformErrors(data)).toBe(true);
  });

  it("detects generic errors", () => {
    const data = { ANDROID: [], IOS: [], GENERIC: ["e1"] };
    expect(hasPlatformErrors(data)).toBe(true);
  });

  it("detects mixed platform errors", () => {
    const data = { ANDROID: ["a"], IOS: ["i"], GENERIC: [] };
    expect(hasPlatformErrors(data)).toBe(true);
  });
});

describe("getSingleTab", () => {
  const baseAccount = (android, ios) => ({
    selectedWeChatAccount: { configs: { android, ios } },
  });

  it("returns null if both Android and IOS are supported", () => {
    const accountData = baseAccount(DEVICE_SUPPORTED, DEVICE_SUPPORTED);
    expect(getSingleTab(accountData)).toBeNull();
  });

  it("returns ANDROID if only Android is supported", () => {
    const accountData = baseAccount(DEVICE_SUPPORTED, undefined);
    expect(getSingleTab(accountData)).toBe(ANDROID);
  });

  it("returns IOS if only IOS is supported", () => {
    const accountData = baseAccount(undefined, DEVICE_SUPPORTED);
    expect(getSingleTab(accountData)).toBe('IOS');
  });

  it("returns null if neither Android nor IOS is supported", () => {
    const accountData = baseAccount(undefined, undefined);
    expect(getSingleTab(accountData)).toBeNull();
  });

  it("returns null if android/ios are not DEVICE_SUPPORTED", () => {
    const accountData = baseAccount('0', '0');
    expect(getSingleTab(accountData)).toBeNull();
  });

  it("returns null if selectedWeChatAccount is missing", () => {
    expect(getSingleTab({})).toBeNull();
  });

  it("returns null if configs is missing", () => {
    expect(getSingleTab({ selectedWeChatAccount: {} })).toBeNull();
  });

  it("returns null if accountData is undefined", () => {
    expect(getSingleTab(undefined)).toBeNull();
  });
});
