import { test } from 'node:test';
import assert from 'node:assert/strict';
import { PackMetaReader } from './PackMetaReader';
import { ImageUsage, PackMeta } from './types';

const reader = (meta: PackMeta) => new PackMetaReader(meta);

test('name reads display_name only when it is a string', () => {
  assert.equal(reader({ display_name: 'Cats' }).name, 'Cats');
  assert.equal(reader({}).name, undefined);
  assert.equal(reader({ display_name: 42 } as unknown as PackMeta).name, undefined);
});

test('avatar reads avatar_url only when it is a string', () => {
  assert.equal(reader({ avatar_url: 'mxc://x/y' }).avatar, 'mxc://x/y');
  assert.equal(reader({}).avatar, undefined);
  assert.equal(reader({ avatar_url: {} } as unknown as PackMeta).avatar, undefined);
});

test('attribution reads attribution only when it is a string', () => {
  assert.equal(reader({ attribution: 'me' }).attribution, 'me');
  assert.equal(reader({}).attribution, undefined);
  assert.equal(reader({ attribution: 0 } as unknown as PackMeta).attribution, undefined);
});

test('usage falls back when missing, non-array, or empty/unknown', () => {
  const fallback = [ImageUsage.Emoticon, ImageUsage.Sticker];
  assert.deepEqual(reader({}).usage, fallback);
  assert.deepEqual(reader({ usage: 'emoticon' } as unknown as PackMeta).usage, fallback);
  assert.deepEqual(reader({ usage: [] }).usage, fallback);
  assert.deepEqual(reader({ usage: ['bogus'] as unknown as ImageUsage[] }).usage, fallback);
});

test('usage filters to only known values', () => {
  assert.deepEqual(reader({ usage: [ImageUsage.Emoticon] }).usage, [ImageUsage.Emoticon]);
  assert.deepEqual(reader({ usage: [ImageUsage.Sticker] }).usage, [ImageUsage.Sticker]);
  assert.deepEqual(
    reader({
      usage: [ImageUsage.Emoticon, 'bogus', ImageUsage.Sticker] as unknown as ImageUsage[],
    }).usage,
    [ImageUsage.Emoticon, ImageUsage.Sticker],
  );
});

test('content returns the original meta object', () => {
  const meta: PackMeta = { display_name: 'Cats', usage: [ImageUsage.Emoticon] };
  assert.equal(reader(meta).content, meta);
});
