import { describe, it, expect } from 'vitest';
import { RemoteData, map2, map } from './remote-data';

const loading: RemoteData<string, number> = { tag: 'Loading' };
const failure: RemoteData<string, number> = { tag: 'Failure', error: 'x' };
const ok = (n: number): RemoteData<string, number> => ({ tag: 'Success', value: n });

describe('RemoteData combinators', () => {
  it('map only touches Success', () => {
    const times10 = (n: number) => n * 10;
    expect(map(ok(2), times10)).toEqual({ tag: 'Success', value: 20 });
    expect(map(loading, times10)).toEqual(loading);
  });

  it('map2 precedence: Failure > Loading > Success', () => {
    const add = (a: number, b: number) => a + b;
    expect(map2(failure, ok(1), add)).toEqual(failure); // a failed
    expect(map2(ok(1), failure, add)).toEqual(failure); // b failed
    expect(map2(loading, ok(1), add)).toEqual({ tag: 'Loading' });
    expect(map2(ok(2), ok(3), add)).toEqual({ tag: 'Success', value: 5 });
  });
});
