import { describe, it } from 'node:test';
import { strict as assert } from 'node:assert';

import { htmlReferencesBundle } from '../BundleFreshness';

describe('htmlReferencesBundle', () => {
  it('accepts the currently served Vite entry module', () => {
    const html = '<script type="module" crossorigin src="/apps/director/assets/index-new.js"></script>';
    assert.equal(
      htmlReferencesBundle(
        html,
        'https://example.invalid/apps/director/assets/index-new.js',
        'https://example.invalid/apps/director/',
      ),
      true,
    );
  });

  it('accepts Vite dev HTML that imports the entrypoint from an inline module script', () => {
    const html = '<script type="module">import "./index.ts";</script>';
    assert.equal(
      htmlReferencesBundle(
        html,
        'https://local.example.invalid/apps/web/index.ts',
        'https://local.example.invalid/apps/web/',
      ),
      true,
    );
  });

  it('rejects an older content-hashed bundle', () => {
    const html = '<script type="module" crossorigin src="/apps/director/assets/index-new.js"></script>';
    assert.equal(
      htmlReferencesBundle(
        html,
        'https://example.invalid/apps/director/assets/index-old.js',
        'https://example.invalid/apps/director/',
      ),
      false,
    );
  });
});
