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

import { parseRecipientConfig } from '@/lib/services/notification-recipients';

describe('parseRecipientConfig', () => {
  it('treats a legacy string[] as explicit emails (backward-compat)', () => {
    expect(parseRecipientConfig(['a@x.com', 'b@x.com'])).toEqual({
      emails: ['a@x.com', 'b@x.com'],
      userIds: [],
      roleIds: [],
      everyone: false,
    });
  });

  it('reads the structured object shape', () => {
    expect(
      parseRecipientConfig({
        emails: ['a@x.com'],
        userIds: ['u1', 'u2'],
        roleIds: ['r1'],
        everyone: true,
      }),
    ).toEqual({
      emails: ['a@x.com'],
      userIds: ['u1', 'u2'],
      roleIds: ['r1'],
      everyone: true,
    });
  });

  it('filters non-string / empty entries and coerces everyone defensively', () => {
    expect(
      parseRecipientConfig({
        emails: ['a@x.com', 2, '', null],
        userIds: 'nope',
        everyone: 'yes',
      }),
    ).toEqual({ emails: ['a@x.com'], userIds: [], roleIds: [], everyone: false });
  });

  it('returns empty for null / garbage', () => {
    const empty = { emails: [], userIds: [], roleIds: [], everyone: false };
    expect(parseRecipientConfig(null)).toEqual(empty);
    expect(parseRecipientConfig('nope')).toEqual(empty);
    expect(parseRecipientConfig(42)).toEqual(empty);
  });
});
