<commit_before>import { createInMemoryCache } from '@algolia/cache-in-memory';
import { version } from '@algolia/client-common';
import { CallEnum, createStatelessHost, createUserAgent } from '@algolia/transporter';

import { TestSuite } from '../../../client-common/src/__tests__/TestSuite';

const algoliasearch = new TestSuite('search').algoliasearch;
const client = algoliasearch('appId', 'apiKey');

describe('default preset', () => {
  it('sets default headers', () => {
    expect(client.transporter.headers).toEqual({
      'content-type': 'application/x-www-form-urlencoded',
      'x-algolia-application-id': 'appId',
      'x-algolia-api-key': 'apiKey',
    });
  });

  it('sets default query pameters', () => {
    expect(client.transporter.queryParameters).toEqual({});
  });

  it('sets default user agent', () => {
    if (testing.isBrowser()) {
      expect(client.transporter.userAgent.value).toEqual(
        `Algolia for JavaScript (${version}); Browser`
      );
    } else {
      const nodeVersion = process.versions.node;

      expect(client.transporter.userAgent.value).toEqual(
        `Algolia for JavaScript (${version}); Node.js (${nodeVersion})`
      );
    }
  });

  it('allows to customize options', () => {
    const cache = createInMemoryCache();
    const userAgent = createUserAgent('0.2.0');

    const customClient = algoliasearch('appId', 'apiKey', {
      hostsCache: cache,
      requestsCache: cache,
      userAgent,
      timeouts: {
        connect: 45,
        read: 46,
        write: 47,
      },
      queryParameters: {
        queryParameter: 'bar',
      },
      headers: {
        header: 'foo',
      },
      hosts: [{ accept: CallEnum.Any, url: 'foo.com' }],
    });

    // First, all should be the same, except hosts cache.
    const analytics = client.initAnalytics();
    expect(client.transporter.timeouts).toBe(analytics.transporter.timeouts);
    expect(client.transporter.userAgent).toBe(analytics.transporter.userAgent);
    expect(client.transporter.responsesCache).toBe(analytics.transporter.responsesCache);
    expect(client.transporter.hostsCache).not.toBe(analytics.transporter.hostsCache);

    // Then, on custom options, only the search client is impacted
    expect(client.transporter.hostsCache).not.toBe(cache);
    expect(customClient.transporter.hostsCache).toBe(cache);
    expect(customClient.initAnalytics().transporter.hostsCache).not.toBe(cache);
    expect(customClient.initAnalytics({ hostsCache: cache }).transporter.hostsCache).not.toBe(
      cache // no retry on analytics
    );

    expect(client.transporter.requestsCache).not.toBe(cache);
    expect(customClient.transporter.requestsCache).toBe(cache);
    expect(customClient.initRecommendation().transporter.requestsCache).not.toBe(cache);
    expect(customClient.initAnalytics({ requestsCache: cache }).transporter.requestsCache).toBe(
      cache
    );
    expect(customClient.transporter.timeouts).toEqual({
      connect: 45,
      read: 46,
      write: 47,
    });
    expect(customClient.initRecommendation().transporter.timeouts).toEqual({
      connect: testing.isBrowser() ? 1 : 2,
      read: testing.isBrowser() ? 2 : 5,
      write: 30,
    });
<commit_msg>chore: fixes test<commit_after>import { createInMemoryCache } from '@algolia/cache-in-memory';
import { version } from '@algolia/client-common';
import { CallEnum, createStatelessHost, createUserAgent } from '@algolia/transporter';

import { TestSuite } from '../../../client-common/src/__tests__/TestSuite';

const algoliasearch = new TestSuite('search').algoliasearch;
const client = algoliasearch('appId', 'apiKey');

describe('default preset', () => {
  it('sets default headers', () => {
    expect(client.transporter.headers).toEqual({
      'content-type': 'application/x-www-form-urlencoded',
      'x-algolia-application-id': 'appId',
      'x-algolia-api-key': 'apiKey',
    });
  });

  it('sets default query pameters', () => {
    expect(client.transporter.queryParameters).toEqual({});
  });

  it('sets default user agent', () => {
    if (testing.isBrowser()) {
      expect(client.transporter.userAgent.value).toEqual(
        `Algolia for JavaScript (${version}); Browser`
      );
    } else {
      const nodeVersion = process.versions.node;

      expect(client.transporter.userAgent.value).toEqual(
        `Algolia for JavaScript (${version}); Node.js (${nodeVersion})`
      );
    }
  });

  it('allows to customize options', () => {
    const cache = createInMemoryCache();
    const userAgent = createUserAgent('0.2.0');

    const customClient = algoliasearch('appId', 'apiKey', {
      hostsCache: cache,
      requestsCache: cache,
      userAgent,
      timeouts: {
        connect: 45,
        read: 46,
        write: 47,
      },
      queryParameters: {
        queryParameter: 'bar',
      },
      headers: {
        header: 'foo',
      },
      hosts: [{ accept: CallEnum.Any, url: 'foo.com' }],
    });

    // First, all should be the same, except hosts cache.
    const analytics = client.initAnalytics();
    expect(client.transporter.timeouts).toBe(analytics.transporter.timeouts);
    expect(client.transporter.userAgent).toBe(analytics.transporter.userAgent);
    expect(client.transporter.responsesCache).toBe(analytics.transporter.responsesCache);
    expect(client.transporter.hostsCache).toBe(analytics.transporter.hostsCache);

    // Then, on custom options, only the search client is impacted
    expect(client.transporter.hostsCache).not.toBe(cache);
    expect(customClient.transporter.hostsCache).toBe(cache);
    expect(customClient.initAnalytics().transporter.hostsCache).not.toBe(cache);
    expect(customClient.initAnalytics({ hostsCache: cache }).transporter.hostsCache).toBe(cache);

    expect(client.transporter.requestsCache).not.toBe(cache);
    expect(customClient.transporter.requestsCache).toBe(cache);
    expect(customClient.initRecommendation().transporter.requestsCache).not.toBe(cache);
    expect(customClient.initAnalytics({ requestsCache: cache }).transporter.requestsCache).toBe(
      cache
    );
    expect(customClient.transporter.timeouts).toEqual({
      connect: 45,
      read: 46,
      write: 47,
    });
    expect(customClient.initRecommendation().transporter.timeouts).toEqual({
      connect: testing.isBrowser() ? 1 : 2,
      read: testing.isBrowser() ? 2 : 5,
      write: 30,
    });

{'fraction_non_alphanumeric': 0.14825808467426965, 'fraction_numerical': 0.006561474769567255, 'mean_word_length': 3.546875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4038351', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1877, 'n_words': 331}