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

import type { OAuthAccount } from '@/lib/api-client';
import { buildAccountVisualGroups } from '@/lib/account-visual-groups';

function makeAccount(overrides: Partial<OAuthAccount> & Pick<OAuthAccount, 'id' | 'tokenFile'>) {
  return {
    id: overrides.id,
    email: 'kaidu.kd@gmail.com',
    provider: 'codex',
    isDefault: false,
    tokenFile: overrides.tokenFile,
    createdAt: '2026-03-30T00:00:00.000Z',
    ...overrides,
  } satisfies OAuthAccount;
}

describe('buildAccountVisualGroups', () => {
  it('preserves grouped codex variant identity details while ordering by audience', () => {
    const groups = buildAccountVisualGroups([
      makeAccount({
        id: 'kaidu.kd@gmail.com#free',
        tokenFile: 'codex-kaidu.kd@gmail.com-free.json',
      }),
      makeAccount({
        id: 'kaidu.kd@gmail.com#04a0f049-team',
        tokenFile: 'codex-04a0f049-kaidu.kd@gmail.com-team.json',
      }),
    ]);

    expect(groups).toHaveLength(1);
    expect(groups[0]?.variants?.map((variant) => variant.audience)).toEqual([
      'business',
      'personal',
    ]);
    expect(groups[0]?.variants?.map((variant) => variant.inlineLabel)).toEqual([
      'Business · Workspace 04a0f049',
      'Personal · Free',
    ]);
    expect(groups[0]?.variants?.map((variant) => variant.compactDetailLabel)).toEqual([
      '04a0f049',
      'Free',
    ]);
    expect(groups[0]?.memberIds).toEqual([
      'kaidu.kd@gmail.com#04a0f049-team',
      'kaidu.kd@gmail.com#free',
    ]);
  });

  it('keeps multiple personal codex plans distinct inside the same grouped card', () => {
    const groups = buildAccountVisualGroups([
      makeAccount({
        id: 'kaidu.kd@gmail.com#plus',
        tokenFile: 'codex-kaidu.kd@gmail.com-plus.json',
      }),
      makeAccount({
        id: 'kaidu.kd@gmail.com#free',
        tokenFile: 'codex-kaidu.kd@gmail.com-free.json',
      }),
    ]);

    expect(groups).toHaveLength(1);
    expect(groups[0]?.variants?.map((variant) => variant.inlineLabel)).toEqual([
      'Personal · Free',
      'Personal · Plus',
    ]);
  });
});
