import { describe, expect, test } from 'vitest';
import { RemoteFetchError } from '../infrastructure/remote-data';
import { remoteFetchErrorMessage } from './remote-data-errors';

describe('remoteFetchErrorMessage', () => {
  test('a network failure hedges offline vs cross-origin and offers the inline fallback', () => {
    const msg = remoteFetchErrorMessage(new RemoteFetchError('network', 'x'));
    expect(msg).toMatch(/offline/i);
    expect(msg).toMatch(/cross-origin/i);
    expect(msg).toMatch(/paste the data inline/i);
  });

  test('an HTTP 404 explains "not found" without a raw error code', () => {
    const msg = remoteFetchErrorMessage(new RemoteFetchError('http', 'x', 404));
    expect(msg).toMatch(/couldn't find/i);
    expect(msg).not.toMatch(/404/);
  });

  test('an HTTP 403 explains refused access', () => {
    expect(remoteFetchErrorMessage(new RemoteFetchError('http', 'x', 403))).toMatch(
      /refused access/i,
    );
  });

  test('an empty body says it returned no data', () => {
    expect(remoteFetchErrorMessage(new RemoteFetchError('empty', 'x'))).toMatch(/no data/i);
  });

  test('the retry recovery points at refreshing, not pasting inline', () => {
    const msg = remoteFetchErrorMessage(new RemoteFetchError('timeout', 'x'), 'retry');
    expect(msg).toMatch(/refreshing again/i);
    expect(msg).not.toMatch(/inline/i);
  });

  test('a too-large body names the size limit and a real fix, not the inline fallback', () => {
    const msg = remoteFetchErrorMessage(new RemoteFetchError('too-large', 'x'));
    expect(msg).toMatch(/too large/i);
    expect(msg).toMatch(/MB/);
    expect(msg).not.toMatch(/inline/i);
  });

  test('an unknown error still produces a sensible fallback message', () => {
    expect(remoteFetchErrorMessage(new Error('boom'))).toMatch(/couldn't fetch this url/i);
  });
});
