import test from "node:test";
import assert from "node:assert/strict";
import { accountDisplayName, accountDisplayParts, accountIdentityParts, accountStatusLabel, visibleAccountOptions } from "./account-display.js";

test("account display prefers safe email over configured label", () => {
  assert.equal(accountDisplayName({ label: "Account A", email: "person@example.com" }), "person@example.com");
  assert.equal(accountDisplayName({ label: "Account A", displayName: "person@example.com" }), "person@example.com");
  assert.equal(accountDisplayName({ label: "Account A" }), "Account A");
  assert.equal(accountDisplayName(null), "No account");
});

test("account display parts preserve the at-sign and domain outside the mosaic target", () => {
  assert.deepEqual(accountDisplayParts({ email: "person@example.com" }), { local: "person", domain: "@example.com" });
  assert.deepEqual(accountDisplayParts({ displayName: "Personal" }), { local: "Personal", domain: "" });
  assert.deepEqual(accountIdentityParts("person@example.com · desktop"), { local: "person", domain: "@example.com · desktop" });
});

test("account display keeps the safe identity even when multiple accounts share it", () => {
  const accounts = [
    { id: "new", label: "New account", displayName: "ansrb4134" },
    { id: "desktop", label: "Desktop Codex", displayName: "ansrb4134" }
  ];

  assert.equal(accountDisplayName(accounts[0], accounts), "ansrb4134");
  assert.equal(accountDisplayName(accounts[1], accounts), "ansrb4134");
});

test("visible account options preserve every configured account when login identities match", () => {
  const accounts = [
    { id: "new", label: "New account", displayName: "ansrb4134" },
    { id: "desktop", label: "Desktop Codex", displayName: "ansrb4134" },
    { id: "work", label: "Work", displayName: "work-user" }
  ];

  assert.deepEqual(visibleAccountOptions(accounts).map((account) => account.id), ["new", "desktop", "work"]);
  assert.deepEqual(visibleAccountOptions(accounts, "desktop").map((account) => account.id), ["new", "desktop", "work"]);
});

test("visible account options do not merge account IDs after identity normalization", () => {
  const accounts = [
    { id: "managed", displayName: " Person@Example.com " },
    { id: "desktop", email: "person@example.com" },
    { id: "work", email: "work@example.com" }
  ];

  assert.deepEqual(visibleAccountOptions(accounts).map((account) => account.id), ["managed", "desktop", "work"]);
  assert.deepEqual(visibleAccountOptions(accounts, "desktop").map((account) => account.id), ["managed", "desktop", "work"]);
});

test("account status shows plan when Codex reports it", () => {
  assert.equal(accountStatusLabel({ status: "logged_in", planType: "plus" }), "Plus");
  assert.equal(accountStatusLabel({ status: "logged_in", planType: "self_serve_business_usage_based" }), "Business");
  assert.equal(accountStatusLabel({ status: "logged_in", planType: "unknown" }), "Logged in");
  assert.equal(accountStatusLabel({ status: "needs_login" }), "Needs login");
});
