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

test('collection builds a shortcode -> reader map', () => {
  const images: PackImages = {
    cat: { url: 'mxc://x/cat', usage: [ImageUsage.Emoticon] },
    dog: { url: 'mxc://x/dog' },
  };
  const reader = new PackImagesReader(images);
  const collection = reader.collection;

  assert.equal(collection.size, 2);
  assert.equal(collection.get('cat')?.url, 'mxc://x/cat');
  assert.equal(collection.get('cat')?.shortcode, 'cat');
  assert.equal(collection.get('dog')?.url, 'mxc://x/dog');
});

test('collection drops invalid images (missing/non-string url)', () => {
  const images = {
    good: { url: 'mxc://x/good' },
    noUrl: {},
    badUrl: { url: 5 },
  } as unknown as PackImages;
  const collection = new PackImagesReader(images).collection;

  assert.equal(collection.size, 1);
  assert.ok(collection.has('good'));
  assert.equal(collection.has('noUrl'), false);
  assert.equal(collection.has('badUrl'), false);
});

test('collection of an empty image set is an empty map', () => {
  const collection = new PackImagesReader({}).collection;
  assert.equal(collection.size, 0);
});

test('collection is memoized (same Map instance returned)', () => {
  const reader = new PackImagesReader({ cat: { url: 'u' } });
  const first = reader.collection;
  const second = reader.collection;
  assert.equal(first, second);
});
