<commit_before>import { ClientFunction, Selector } from 'testcafe';

const PAGE_FLIP_WAIT_TIME = 1000;

const getHash = ClientFunction(() => document.location.hash);
const getUrl = ClientFunction(() => window.location.href);

/**
 * Check URL page parameter in # and path
 */
const isPageInUrl = ClientFunction(() => {
  const hash = document.location.hash;
  if (hash) {
    return hash.indexOf('#page/') > -1;
  } else {
    return window.location.href.indexOf('/page/') > -1;
  }
});

/**
 * Check URL mode parameter in # and path
 *
 * @param mode '1up', '2up', 'thumb'
 */
const isModeInUrl = ClientFunction((mode) => {
  const hash = document.location.hash;
  if (hash) {
    return hash.indexOf('/mode/' + mode) > -1;
  } else {
    return window.location.href.indexOf('/mode/' + mode) > -1;
  }
});

/**
 * Runs all expected base tests for BookReader
 *
 * @param { import('../models/BookReader').default } br - Model
 */
export function runBaseTests (br) {
  test('On load, pages fit fully inside of the BookReader™', async t => {
    await t.expect(br.shell.visible).ok();
    await t.expect(br.BRcontainer.visible).ok();

    const shellHeight = await br.shell.getBoundingClientRectProperty('height');
    const bookHeight = await br.BRcontainer.getBoundingClientRectProperty('height');
    await t.expect(bookHeight).lte(shellHeight, 'images do not get cropped vertically');

    const shellWidth = await br.shell.getBoundingClientRectProperty('width');
    const bookWidth = await br.BRcontainer.getBoundingClientRectProperty('width');
    await t.expect(bookWidth).lte(shellWidth, 'images do not get cropped horizontally');
  });

  test('nav menu displays properly', async t => {
    const { nav } = br;

    await t.expect(nav.desktop.goLeft.visible).ok();
    await t.expect(nav.desktop.goRight.visible).ok();
    await t.expect(nav.desktop.mode1Up.visible).ok();
    await t.expect(nav.desktop.mode2Up.visible).ok();
    await t.expect(nav.desktop.modeThumb.visible).ok();
    await t.expect(nav.desktop.zoomIn.visible).ok();
    await t.expect(nav.desktop.zoomOut.visible).ok();
    await t.expect(nav.desktop.fullScreen.visible).ok();
  });

  test("Canonical URL has no initial parameters", async t => {
    // Initial URL has no params
    await t.expect(getHash()).eql('');
    // Initial URL has no page/ mode/
    await t.expect(getUrl()).notContains('#page/');
    await t.expect(getUrl()).notContains('/page/');
    await t.expect(getUrl()).notContains('/mode/');
  });

  // Need to disable page caching to have cookies persist in test
  test.disablePageCaching('Canonical URL shows parameters if cookie set', async t => {
    const { nav } = br;

    // Check if uses plugin.resume.js
    const usesResume = ClientFunction(() => {
      const hasResumePlugin = typeof(br.getResumeValue) !== "undefined";
      const hasResumeValue = hasResumePlugin ? br.getResumeValue() : false;
      return hasResumeValue;
    });

    // Store initial URL
    const initialUrl = await getUrl();

    // Set Cookie by page navigation, wait for cookie
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    // reload canonical URL, wait for URL change
    await t.navigateTo(initialUrl);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    if (await usesResume()) {
      await t.expect(isPageInUrl()).eql(true, initialUrl);
      await t.expect(isModeInUrl('2up')).eql(true, initialUrl)
    } else {
      // No plugin, no br-resume cookie
      await t.expect(getUrl()).notContains('#page/');
      await t.expect(getUrl()).notContains('/page/');
      await t.expect(getUrl()).notContains('/mode/');
    }
  });

  test('2up mode - Clicking `Previous page` changes the page', async t => {
    const { nav, BRcontainer} = br;

    // Go to next page, so we can go previous if at front cover
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const onLoadBrState = BRcontainer.child(0);
    const initialImages = onLoadBrState.find('img');
    const origImg1Src = await initialImages.nth(0).getAttribute('src');
    const origImg2Src = await initialImages.nth(-1).getAttribute('src');

    await t.click(nav.desktop.goPrev);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const nextBrState = Selector('.BRcontainer').child(0);
    const prevImages = nextBrState.find('img');
    const prevImg1Src = await prevImages.nth(0).getAttribute('src');
    const prevImg2Src = await prevImages.nth(-1).getAttribute('src');

    // we aren't showing the same image in both leaves
    await t.expect(origImg1Src).notEql(origImg2Src);

    // we are showing new pages
    await t.expect(prevImg1Src).notEql(origImg1Src);
    await t.expect(prevImg1Src).notEql(origImg2Src);
    await t.expect(prevImg2Src).notEql(origImg1Src);
    await t.expect(prevImg2Src).notEql(origImg2Src);

    // we aren't showing the same image in the new pages
    await t.expect(prevImg1Src).notEql(prevImg2Src);
  })

  test('2up mode - Clicking `Next page` changes the page', async t => {
    // Note: this will fail on a R to L book if at front cover
    const { nav, BRcontainer} = br;
    // Flip away from cover
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const onLoadBrState = BRcontainer.child(0);
    const initialImages = onLoadBrState.find('img');
    const origImg1Src = await initialImages.nth(0).getAttribute('src');
    const origImg2Src = await initialImages.nth(-1).getAttribute('src');

    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const nextBrState = Selector('.BRcontainer').child(0);
    const nextImages = nextBrState.find('img');
    const nextImg1Src = await nextImages.nth(0).getAttribute('src');
    const nextImg2Src = await nextImages.nth(-1).getAttribute('src');

    // we are showing new pages
    await t.expect(nextImg1Src).notEql(origImg1Src);
    await t.expect(nextImg1Src).notEql(origImg2Src);
    await t.expect(nextImg2Src).notEql(origImg1Src);
    await t.expect(nextImg2Src).notEql(origImg2Src);

    // we aren't showing the same image in the new pages
    await t.expect(nextImg1Src).notEql(nextImg2Src);
  })

  test('Clicking `page flip buttons` updates location', async t => {
    const { nav } = br;
    // Page navigation creates params
    await t.click(nav.desktop.goNext);
    await t.expect(isPageInUrl()).eql(true);
    await t.expect(isModeInUrl('2up')).eql(true);

    await t.click(nav.desktop.goPrev);
    await t.expect(isPageInUrl()).eql(true);
    await t.expect(isModeInUrl('2up')).eql(true);
  });

  test('Clicking `2 page view` brings up 2 pages at a time', async t => {
    const { nav } = br;
    await t.click(nav.desktop.mode2Up);
    await t.expect(Selector('.BRpagecontainer').count).eql(2);
  });

  test('Clicking `1 page view` brings up 1 at a time', async t => {
    const { nav } = br;
    await t.click(nav.desktop.mode1Up);
    // we usually pre-fetch the page in question & the 2 after it
    await t.expect(Selector('.BRpagecontainer').count).gte(3);
  });

  test('Clicking `thumbnail view` brings up all of the page thumbnails', async t => {
    const { nav } = br;
    await t.click(nav.desktop.modeThumb);
    await t.expect(Selector('.BRpagecontainer').count).gte(3);
  });
<commit_msg>Update e2e tests for Mode1Up larger default zoom<commit_after>import { ClientFunction, Selector } from 'testcafe';

const PAGE_FLIP_WAIT_TIME = 1000;

const getHash = ClientFunction(() => document.location.hash);
const getUrl = ClientFunction(() => window.location.href);

/**
 * Check URL page parameter in # and path
 */
const isPageInUrl = ClientFunction(() => {
  const hash = document.location.hash;
  if (hash) {
    return hash.indexOf('#page/') > -1;
  } else {
    return window.location.href.indexOf('/page/') > -1;
  }
});

/**
 * Check URL mode parameter in # and path
 *
 * @param mode '1up', '2up', 'thumb'
 */
const isModeInUrl = ClientFunction((mode) => {
  const hash = document.location.hash;
  if (hash) {
    return hash.indexOf('/mode/' + mode) > -1;
  } else {
    return window.location.href.indexOf('/mode/' + mode) > -1;
  }
});

/**
 * Runs all expected base tests for BookReader
 *
 * @param { import('../models/BookReader').default } br - Model
 */
export function runBaseTests (br) {
  test('On load, pages fit fully inside of the BookReader™', async t => {
    await t.expect(br.shell.visible).ok();
    await t.expect(br.BRcontainer.visible).ok();

    const shellHeight = await br.shell.getBoundingClientRectProperty('height');
    const bookHeight = await br.BRcontainer.getBoundingClientRectProperty('height');
    await t.expect(bookHeight).lte(shellHeight, 'images do not get cropped vertically');

    const shellWidth = await br.shell.getBoundingClientRectProperty('width');
    const bookWidth = await br.BRcontainer.getBoundingClientRectProperty('width');
    await t.expect(bookWidth).lte(shellWidth, 'images do not get cropped horizontally');
  });

  test('nav menu displays properly', async t => {
    const { nav } = br;

    await t.expect(nav.desktop.goLeft.visible).ok();
    await t.expect(nav.desktop.goRight.visible).ok();
    await t.expect(nav.desktop.mode1Up.visible).ok();
    await t.expect(nav.desktop.mode2Up.visible).ok();
    await t.expect(nav.desktop.modeThumb.visible).ok();
    await t.expect(nav.desktop.zoomIn.visible).ok();
    await t.expect(nav.desktop.zoomOut.visible).ok();
    await t.expect(nav.desktop.fullScreen.visible).ok();
  });

  test("Canonical URL has no initial parameters", async t => {
    // Initial URL has no params
    await t.expect(getHash()).eql('');
    // Initial URL has no page/ mode/
    await t.expect(getUrl()).notContains('#page/');
    await t.expect(getUrl()).notContains('/page/');
    await t.expect(getUrl()).notContains('/mode/');
  });

  // Need to disable page caching to have cookies persist in test
  test.disablePageCaching('Canonical URL shows parameters if cookie set', async t => {
    const { nav } = br;

    // Check if uses plugin.resume.js
    const usesResume = ClientFunction(() => {
      const hasResumePlugin = typeof(br.getResumeValue) !== "undefined";
      const hasResumeValue = hasResumePlugin ? br.getResumeValue() : false;
      return hasResumeValue;
    });

    // Store initial URL
    const initialUrl = await getUrl();

    // Set Cookie by page navigation, wait for cookie
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    // reload canonical URL, wait for URL change
    await t.navigateTo(initialUrl);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    if (await usesResume()) {
      await t.expect(isPageInUrl()).eql(true, initialUrl);
      await t.expect(isModeInUrl('2up')).eql(true, initialUrl)
    } else {
      // No plugin, no br-resume cookie
      await t.expect(getUrl()).notContains('#page/');
      await t.expect(getUrl()).notContains('/page/');
      await t.expect(getUrl()).notContains('/mode/');
    }
  });

  test('2up mode - Clicking `Previous page` changes the page', async t => {
    const { nav, BRcontainer} = br;

    // Go to next page, so we can go previous if at front cover
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const onLoadBrState = BRcontainer.child(0);
    const initialImages = onLoadBrState.find('img');
    const origImg1Src = await initialImages.nth(0).getAttribute('src');
    const origImg2Src = await initialImages.nth(-1).getAttribute('src');

    await t.click(nav.desktop.goPrev);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const nextBrState = Selector('.BRcontainer').child(0);
    const prevImages = nextBrState.find('img');
    const prevImg1Src = await prevImages.nth(0).getAttribute('src');
    const prevImg2Src = await prevImages.nth(-1).getAttribute('src');

    // we aren't showing the same image in both leaves
    await t.expect(origImg1Src).notEql(origImg2Src);

    // we are showing new pages
    await t.expect(prevImg1Src).notEql(origImg1Src);
    await t.expect(prevImg1Src).notEql(origImg2Src);
    await t.expect(prevImg2Src).notEql(origImg1Src);
    await t.expect(prevImg2Src).notEql(origImg2Src);

    // we aren't showing the same image in the new pages
    await t.expect(prevImg1Src).notEql(prevImg2Src);
  })

  test('2up mode - Clicking `Next page` changes the page', async t => {
    // Note: this will fail on a R to L book if at front cover
    const { nav, BRcontainer} = br;
    // Flip away from cover
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const onLoadBrState = BRcontainer.child(0);
    const initialImages = onLoadBrState.find('img');
    const origImg1Src = await initialImages.nth(0).getAttribute('src');
    const origImg2Src = await initialImages.nth(-1).getAttribute('src');

    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    const nextBrState = Selector('.BRcontainer').child(0);
    const nextImages = nextBrState.find('img');
    const nextImg1Src = await nextImages.nth(0).getAttribute('src');
    const nextImg2Src = await nextImages.nth(-1).getAttribute('src');

    // we are showing new pages
    await t.expect(nextImg1Src).notEql(origImg1Src);
    await t.expect(nextImg1Src).notEql(origImg2Src);
    await t.expect(nextImg2Src).notEql(origImg1Src);
    await t.expect(nextImg2Src).notEql(origImg2Src);

    // we aren't showing the same image in the new pages
    await t.expect(nextImg1Src).notEql(nextImg2Src);
  })

  test('Clicking `page flip buttons` updates location', async t => {
    const { nav } = br;
    // Page navigation creates params
    await t.click(nav.desktop.goNext);
    await t.expect(isPageInUrl()).eql(true);
    await t.expect(isModeInUrl('2up')).eql(true);

    await t.click(nav.desktop.goPrev);
    await t.expect(isPageInUrl()).eql(true);
    await t.expect(isModeInUrl('2up')).eql(true);
  });

  test('Clicking `2 page view` brings up 2 pages at a time', async t => {
    const { nav } = br;
    await t.click(nav.desktop.mode2Up);
    await t.expect(Selector('.BRpagecontainer').count).eql(2);
  });

  test('Clicking `1 page view` brings up 1 at a time', async t => {
    const { nav } = br;
    await t.click(nav.desktop.mode1Up);

    // Flip away from cover
    await t.click(nav.desktop.goNext);
    await t.wait(PAGE_FLIP_WAIT_TIME);

    // we usually pre-fetch the page in question & the 2 after it
    await t.expect(Selector('.BRpagecontainer').count).gte(3);
  });

  test('Clicking `thumbnail view` brings up all of the page thumbnails', async t => {
    const { nav } = br;
    await t.click(nav.desktop.modeThumb);
    await t.expect(Selector('.BRpagecontainer').count).gte(3);
  });

{'fraction_non_alphanumeric': 0.14380933144819227, 'fraction_numerical': 0.009425705244731705, 'mean_word_length': 3.605891472868217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '1043616', 'n_tokens_mistral': 4833, 'n_tokens_neox': 4859, 'n_words': 1145}