import { describe, expect, it } from "vitest";

import {
  isFavoriteEntryRequest,
  resolveFavoriteEntryMode,
} from "../favorite_entry";

describe("favorite external entry", () => {
  it("keeps the favorite action inside an in-app browser", () => {
    expect(
      resolveFavoriteEntryMode({
        hasFavoriteIntent: true,
        isAndroid: true,
        isIOS: false,
        isInAppBrowser: true,
        isInstalled: false,
      }),
    ).toBe("favorite");
  });

  it("turns the external Android action into Download", () => {
    expect(
      resolveFavoriteEntryMode({
        hasFavoriteIntent: true,
        isAndroid: true,
        isIOS: false,
        isInAppBrowser: false,
        isInstalled: false,
      }),
    ).toBe("download");
  });

  it("turns the external iOS action into Download until standalone mode is detected", () => {
    expect(
      resolveFavoriteEntryMode({
        hasFavoriteIntent: true,
        isAndroid: false,
        isIOS: true,
        isInAppBrowser: false,
        isInstalled: false,
      }),
    ).toBe("download");
  });

  it("treats an installed PWA as saved", () => {
    expect(
      resolveFavoriteEntryMode({
        hasFavoriteIntent: false,
        isAndroid: true,
        isIOS: false,
        isInAppBrowser: false,
        isInstalled: true,
      }),
    ).toBe("saved");
  });

  it("accepts only the explicit favorite entry marker", () => {
    expect(isFavoriteEntryRequest("1")).toBe(true);
    expect(isFavoriteEntryRequest("true")).toBe(false);
    expect(isFavoriteEntryRequest(null)).toBe(false);
  });
});
