<commit_before>/* eslint-disable no-unused-expressions */

import sinon from 'sinon';
import { dedup } from '.';

const delay = (cb, t = 5) => new Promise((res, rej) => {
  setTimeout(() => cb().then(res, rej), t);
});

describe('dedup', () => {
  describe('for operations other than READ', () => {
    it('just returns the original apiFn', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => Promise.resolve({ ...user }));
      apiFn.operation = 'UPDATE';
      const wrappedApiFn = dedup({})({ apiFn });
      expect(wrappedApiFn).to.equal(apiFn);
    });
  });

  describe('for READ operations', () => {
    it('wraps the function', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => Promise.resolve(user));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      expect(wrappedApiFn).not.to.equal(apiFn);
    });

    it('makes several calls when apiFn is called with different args', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => Promise.resolve({ ...user }));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      return Promise.all([
        wrappedApiFn('x'),
        wrappedApiFn('y')
      ]).then(() => {
        expect(apiFn).to.have.been.calledTwice;
      });
    });

    it('only makes one call when apiFn is called in identical args', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      return Promise.all([
        wrappedApiFn('x'),
        wrappedApiFn('x')
      ]).then(() => {
        expect(apiFn).to.have.been.calledOnce;
      });
    });

    it('passes the result of the single call to all callees', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      return Promise.all([
        wrappedApiFn(),
        wrappedApiFn()
      ]).then((res) => {
        expect(res[0]).to.deep.equal(user);
        expect(res[1]).to.deep.equal(user);
        expect(res[0]).to.equal(res[1]);
      });
    });

    it('makes subsequent calls if another calls is made after the first one is resolved', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
      apiFn.operation = 'READ';<commit_msg>Add spec for complex arguments<commit_after>/* eslint-disable no-unused-expressions */

import sinon from 'sinon';
import { dedup } from '.';

const delay = (cb, t = 5) => new Promise((res, rej) => {
  setTimeout(() => cb().then(res, rej), t);
});

describe('dedup', () => {
  describe('for operations other than READ', () => {
    it('just returns the original apiFn', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => Promise.resolve({ ...user }));
      apiFn.operation = 'UPDATE';
      const wrappedApiFn = dedup({})({ apiFn });
      expect(wrappedApiFn).to.equal(apiFn);
    });
  });

  describe('for READ operations', () => {
    it('wraps the function', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => Promise.resolve(user));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      expect(wrappedApiFn).not.to.equal(apiFn);
    });

    it('makes several calls when apiFn is called with different args', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => Promise.resolve({ ...user }));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      return Promise.all([
        wrappedApiFn('x'),
        wrappedApiFn('y')
      ]).then(() => {
        expect(apiFn).to.have.been.calledTwice;
      });
    });

    it('only makes one call when apiFn is called in identical args', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      return Promise.all([
        wrappedApiFn('x'),
        wrappedApiFn('x')
      ]).then(() => {
        expect(apiFn).to.have.been.calledOnce;
      });
    });

    it('detects complex arguments properly', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      return Promise.all([
        wrappedApiFn({ a: 1, b: [2, 3] }, 'a'),
        wrappedApiFn({ a: 1, b: [2, 3] }, 'a')
      ]).then(() => {
        expect(apiFn).to.have.been.calledOnce;
      });
    });

    it('passes the result of the single call to all callees', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
      apiFn.operation = 'READ';
      const wrappedApiFn = dedup({})({ apiFn });
      return Promise.all([
        wrappedApiFn(),
        wrappedApiFn()
      ]).then((res) => {
        expect(res[0]).to.deep.equal(user);
        expect(res[1]).to.deep.equal(user);
        expect(res[0]).to.equal(res[1]);
      });
    });

    it('makes subsequent calls if another calls is made after the first one is resolved', () => {
      const user = { id: 'x' };
      const apiFn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
      apiFn.operation = 'READ';
{'fraction_non_alphanumeric': 0.20510037981551818, 'fraction_numerical': 0.0028938325194429373, 'mean_word_length': 2.4845620667926904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968115', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1762, 'n_words': 429}