<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// @flow

import React from 'react';
import { Provider } from 'react-redux';
import { render } from '@testing-library/react';

import { ListOfPublishedProfiles } from 'firefox-profiler/components/app/ListOfPublishedProfiles';
import { storeProfileData } from 'firefox-profiler/app-logic/published-profiles-store';
import { blankStore } from '../fixtures/stores';
import { mockDate } from '../fixtures/mocks/date';

import 'fake-indexeddb/auto';
import FDBFactory from 'fake-indexeddb/lib/FDBFactory';

function resetIndexedDb() {
  // This is the recommended way to reset the IDB state between test runs, but
  // neither flow nor eslint like that we assign to indexedDB directly, for
  // different reasons.
  /* $FlowExpectError */ /* eslint-disable-next-line no-global-assign */
  indexedDB = new FDBFactory();
}
beforeEach(resetIndexedDb);
afterEach(resetIndexedDb);

describe('ListOfPublishedProfiles', () => {
  function setup() {
    const store = blankStore();
    const renderResult = render(
      <Provider store={store}>
        <ListOfPublishedProfiles />
      </Provider>
    );
    const { findByText } = renderResult;

    const waitForFirstRender = () => findByText('Uploaded Recordings');
    return { ...renderResult, waitForFirstRender };
  }
  it('matches a snapshot when there is no published profiles', async () => {
    const { container, waitForFirstRender } = setup();
    await waitForFirstRender();
    expect(container.firstChild).toMatchSnapshot();
  });

  it('matches a snapshot when rendering published profiles', async () => {
    mockDate('2020-07-04T15:00'); // Now is 4th of July, at 3pm UTC.

    // 1. Add some profiles to the local indexeddb.
    await storeProfileData({
      profileToken: '0123456789',
      jwtToken: null,
      publishedDate: new Date('2020-07-04T14:00'), // "today" earlier
      name: '',
      preset: null,
      originHostname: null,
      meta: {
        product: 'Fennec',
        // Not more meta information, to test that we can handle profiles that
        // don't have this information.
      },
      urlPath: '/',
      publishedRange: { start: 1000, end: 3000 },
    });

    await storeProfileData({
      profileToken: '<PASSWORD>',
      jwtToken: null,
      publishedDate: new Date('2020-07-03T08:00'), // yesterday
      name: 'Layout profile',
      preset: 'web',
      originHostname: null,
      meta: {
        product: 'Firefox',
        platform: 'X11',
        toolkit: 'gtk',
        oscpu: 'Linux x86_64',
        misc: 'rv:68.0',
      },
      urlPath: '/',
      publishedRange: { start: 1000, end: 1005 },
    });

    await storeProfileData({
      profileToken: '<PASSWORD>',
      jwtToken: null,
      publishedDate: new Date('2018-05-20'), // ancient date
      name: '',
      preset: null,
      originHostname: 'https://www.cnn.com',
      meta: {
        product: 'Firefox Preview',
        platform: 'Android 7.0',
        toolkit: 'android',
        misc: 'rv:80.0',
        oscpu: 'Linux armv7l',
      },
      urlPath:
        '/public/123abc456/calltree/?globalTrackOrder=0-1-2-3-4-5-6-7-8-9-10&hiddenGlobalTracks=1-2-3-4-5-6-7-8-9&localTrackOrderByPid=32027-1-2-0~12629-0~12149-0~11755-0~11945-0~12533-0~11798-0~11862-0~12083-0~12118-0~12298-0-1~&search=bla&thread=11&v=5',
      // This tests ranges that should show up as µs
      publishedRange: { start: 40000, end: 40000.1 },
    });

    await storeProfileData({
      profileToken: '<PASSWORD>',
      jwtToken: null,
      publishedDate: new Date('2020-07-04T13:00'),
      name: 'Another good profile',
      preset: null,
      originHostname: 'https://profiler.firefox.com',
      meta: {
        product: 'Firefox',
        platform: 'Windows',
        toolkit: 'windows',
        misc: 'rv:77.0',
        oscpu: 'Windows NT 10.0; Win64; x64',
      },
      urlPath: '/public/WINDOWS/marker-chart/',
      publishedRange: { start: 2000, end: 40000 },
    });

    await storeProfileData({
      profileToken: '<PASSWORD>',
      jwtToken: null,
      publishedDate: new Date('2020-07-05T11:00'), // This is the future!
      name: 'MacOS X profile',
      preset: null,
      originHostname: 'https://mozilla.org',<commit_msg>Fix timezone issues in the published profiles test (PR #2726)<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// @flow

import React from 'react';
import { Provider } from 'react-redux';
import { render } from '@testing-library/react';

import { ListOfPublishedProfiles } from 'firefox-profiler/components/app/ListOfPublishedProfiles';
import { storeProfileData } from 'firefox-profiler/app-logic/published-profiles-store';
import { blankStore } from '../fixtures/stores';
import { mockDate } from '../fixtures/mocks/date';

import 'fake-indexeddb/auto';
import FDBFactory from 'fake-indexeddb/lib/FDBFactory';

function resetIndexedDb() {
  // This is the recommended way to reset the IDB state between test runs, but
  // neither flow nor eslint like that we assign to indexedDB directly, for
  // different reasons.
  /* $FlowExpectError */ /* eslint-disable-next-line no-global-assign */
  indexedDB = new FDBFactory();
}
beforeEach(resetIndexedDb);
afterEach(resetIndexedDb);

describe('ListOfPublishedProfiles', () => {
  function setup() {
    const store = blankStore();
    const renderResult = render(
      <Provider store={store}>
        <ListOfPublishedProfiles />
      </Provider>
    );
    const { findByText } = renderResult;

    const waitForFirstRender = () => findByText('Uploaded Recordings');
    return { ...renderResult, waitForFirstRender };
  }
  it('matches a snapshot when there is no published profiles', async () => {
    const { container, waitForFirstRender } = setup();
    await waitForFirstRender();
    expect(container.firstChild).toMatchSnapshot();
  });

  it('matches a snapshot when rendering published profiles', async () => {
    // Because the rendering is dependent on the timezone, all dates in this
    // test are using a timezone-dependent format so that the rendering is the
    // same in all timezones.
    mockDate('4 Jul 2020 15:00'); // Now is 4th of July, at 3pm local timezone.

    // 1. Add some profiles to the local indexeddb.
    await storeProfileData({
      profileToken: '0123456789',
      jwtToken: null,
      publishedDate: new Date('4 Jul 2020 14:00'), // "today" earlier
      name: '',
      preset: null,
      originHostname: null,
      meta: {
        product: 'Fennec',
        // Not more meta information, to test that we can handle profiles that
        // don't have this information.
      },
      urlPath: '/',
      publishedRange: { start: 1000, end: 3000 },
    });

    await storeProfileData({
      profileToken: 'ABCDEFGHI',
      jwtToken: null,
      publishedDate: new Date('3 Jul 2020 08:00'), // yesterday
      name: 'Layout profile',
      preset: 'web',
      originHostname: null,
      meta: {
        product: 'Firefox',
        platform: 'X11',
        toolkit: 'gtk',
        oscpu: 'Linux x86_64',
        misc: 'rv:68.0',
      },
      urlPath: '/',
      publishedRange: { start: 1000, end: 1005 },
    });

    await storeProfileData({
      profileToken: '<PASSWORD>',
      jwtToken: null,
      publishedDate: new Date('20 May 2018'), // ancient date
      name: '',
      preset: null,
      originHostname: 'https://www.cnn.com',
      meta: {
        product: 'Firefox Preview',
        platform: 'Android 7.0',
        toolkit: 'android',
        misc: 'rv:80.0',
        oscpu: 'Linux armv7l',
      },
      urlPath:
        '/public/123abc456/calltree/?globalTrackOrder=0-1-2-3-4-5-6-7-8-9-10&hiddenGlobalTracks=1-2-3-4-5-6-7-8-9&localTrackOrderByPid=32027-1-2-0~12629-0~12149-0~11755-0~11945-0~12533-0~11798-0~11862-0~12083-0~12118-0~12298-0-1~&search=bla&thread=11&v=5',
      // This tests ranges that should show up as µs
      publishedRange: { start: 40000, end: 40000.1 },
    });

    await storeProfileData({
      profileToken: 'WINDOWS',
      jwtToken: null,
      publishedDate: new Date('4 Jul 2020 13:00'),
      name: 'Another good profile',
      preset: null,
      originHostname: 'https://profiler.firefox.com',
      meta: {
        product: 'Firefox',
        platform: 'Windows',
        toolkit: 'windows',
        misc: 'rv:77.0',
        oscpu: 'Windows NT 10.0; Win64; x64',
      },
      urlPath: '/public/WINDOWS/marker-chart/',
      publishedRange: { start: 2000, end: 40000 },
    });

    await storeProfileData({
      profileToken: '<PASSWORD>',
      jwtToken: null,
      publishedDate: new Date('5 Jul 2020 11:00'), // This is the future!
      name: 'MacOS X profile',
      preset: null,
      originHostname: 'https://mozilla.org',
{'fraction_non_alphanumeric': 0.13682358117025956, 'fraction_numerical': 0.05246370435547734, 'mean_word_length': 2.784019975031211, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 6, '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': '4734807', 'n_tokens_mistral': 3146, 'n_tokens_neox': 2837, 'n_words': 764}