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

import {
  applyEntitlementSnapshotToView,
  toView,
} from "@/stores/user/user-machine.helpers";

describe("toView", () => {
  it("fills optional user fields with stable defaults", () => {
    expect(toView({ id: "user-1", username: "Luna" })).toEqual({
      id: "user-1",
      username: "Luna",
      countryCode: "",
      creditBalance: 0,
      dailyFreeChatLimit: 0,
      dailyFreeChatRemaining: 0,
      dailyFreePrivateLimit: 0,
      dailyFreePrivateRemaining: 0,
      isVip: false,
    });
  });

  it("preserves provided user fields", () => {
    expect(
      toView({
        id: "user-2",
        username: "Elio",
        countryCode: "HK",
        dolBalance: 12,
        creditBalance: 120,
        dailyFreeChatLimit: 30,
        dailyFreeChatRemaining: 26,
        dailyFreePrivateLimit: 2,
        dailyFreePrivateRemaining: 1,
        isVip: true,
      }),
    ).toEqual({
      id: "user-2",
      username: "Elio",
      countryCode: "HK",
      creditBalance: 120,
      dailyFreeChatLimit: 30,
      dailyFreeChatRemaining: 26,
      dailyFreePrivateLimit: 2,
      dailyFreePrivateRemaining: 1,
      isVip: true,
    });
  });

  it("merges entitlement snapshot into user view fields", () => {
    const user = toView({ id: "user-3", username: "Nora", isVip: false });

    expect(
      applyEntitlementSnapshotToView(user, {
        isVip: true,
        creditBalance: 120,
      }),
    ).toMatchObject({
      isVip: true,
      creditBalance: 120,
    });
  });
});
