<commit_before>const {Cu} = require('chrome');
Cu.import('resource://gre/modules/Services.jsm'); /* globals Services: false */
const tabs = require("sdk/tabs");
const testRunner = require('sdk/test');
const {before, after} = require('sdk/test/utils');

const {Heartbeat} = require('../lib/Heartbeat.js');


let targetWindow;
let notificationBox;

exports['test it shows a heartbeat panel'] = assert => {
  assert.equal(notificationBox.childElementCount, 0);
  new Heartbeat(targetWindow, {testing: true});
  assert.equal(notificationBox.childElementCount, 1);
};

exports['test it shows five stars when there is no engagementButtonLabel'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    engagementButtonLabel: undefined,
  });
  assert.equal(hb.notice.querySelectorAll('.star-x').length, 5);
  assert.equal(hb.notice.querySelectorAll('.notification-button').length, 0);
};

exports['test it shows a button when there is an engagementButtonLabel'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    engagementButtonLabel: 'Click me!',
  });
  assert.equal(hb.notice.querySelectorAll('.star-x').length, 0);
  assert.equal(hb.notice.querySelectorAll('.notification-button').length, 1);
};

exports['test it shows a learn more link'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    learnMoreMessage: 'Learn More',
    learnMoreURL: 'https://example.org/learnmore',
  });
  let learnMoreEl = hb.notice.querySelector('.text-link');
  assert.equal(learnMoreEl.href, 'https://example.org/learnmore');
  assert.equal(learnMoreEl.value, 'Learn More');
};

exports['test it shows the message'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    message: 'oh hai',
  });
  let messageEl = targetWindow.document.getAnonymousElementByAttribute(
    hb.notice,
    'anonid',
    'messageText'
  );
  assert.equal(messageEl.textContent, 'oh hai');
};

exports['test it pings telemetry'] = (assert, done) => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    message: 'oh hai',
  });

  hb.events.on('TelemetrySent', payload => {
    assert.ok(isOrdered([0, payload.offeredTS, payload.closedTS]));
    done();
  });

  // triggers sending ping to normandy
  closeAllNotifications();
};

exports['test it includes learnMoreTS if learn more is clicked'] = (assert, done) => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    message: 'oh hai',
    learnMoreMessage: 'Learn More',
    learnMoreURL: 'https://example.org/learnmore',
  });

  hb.events.on('TelemetrySent', payload => {
    assert.ok(isOrdered([0, payload.offeredTS, payload.learnMoreTS, payload.closedTS]));
    // Close learn more tab
    tabs[tabs.length - 1].close();
    done();
  });

  let learnMoreEl = hb.notice.querySelector('.text-link');
  learnMoreEl.click();

  // triggers sending ping to normandy
  closeAllNotifications();
};

//exports['test it opens an engagement page after interaction'] = assert => {
//  assert.ok(false);
//};
//

function closeAllNotifications() {
  if (notificationBox.allNotifications.length === 0) {
    return Promise.resolve();
  }

  let promises = [];<commit_msg>Add engagement page test<commit_after>const {Cu} = require('chrome');
Cu.import('resource://gre/modules/Services.jsm'); /* globals Services: false */
const tabs = require("sdk/tabs");
const testRunner = require('sdk/test');
const {before, after} = require('sdk/test/utils');

const {Heartbeat} = require('../lib/Heartbeat.js');


let targetWindow;
let notificationBox;

exports['test it shows a heartbeat panel'] = assert => {
  assert.equal(notificationBox.childElementCount, 0);
  new Heartbeat(targetWindow, {testing: true});
  assert.equal(notificationBox.childElementCount, 1);
};

exports['test it shows five stars when there is no engagementButtonLabel'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    engagementButtonLabel: undefined,
  });
  assert.equal(hb.notice.querySelectorAll('.star-x').length, 5);
  assert.equal(hb.notice.querySelectorAll('.notification-button').length, 0);
};

exports['test it shows a button when there is an engagementButtonLabel'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    engagementButtonLabel: 'Click me!',
  });
  assert.equal(hb.notice.querySelectorAll('.star-x').length, 0);
  assert.equal(hb.notice.querySelectorAll('.notification-button').length, 1);
};

exports['test it shows a learn more link'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    learnMoreMessage: 'Learn More',
    learnMoreURL: 'https://example.org/learnmore',
  });
  let learnMoreEl = hb.notice.querySelector('.text-link');
  assert.equal(learnMoreEl.href, 'https://example.org/learnmore');
  assert.equal(learnMoreEl.value, 'Learn More');
};

exports['test it shows the message'] = assert => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    message: 'oh hai',
  });
  let messageEl = targetWindow.document.getAnonymousElementByAttribute(
    hb.notice,
    'anonid',
    'messageText'
  );
  assert.equal(messageEl.textContent, 'oh hai');
};

exports['test it pings telemetry'] = (assert, done) => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    message: 'oh hai',
  });

  hb.events.on('TelemetrySent', payload => {
    assert.ok(isOrdered([0, payload.offeredTS, payload.closedTS]));
    done();
  });

  // triggers sending ping to normandy
  closeAllNotifications();
};

exports['test it includes learnMoreTS if learn more is clicked'] = (assert, done) => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    message: 'oh hai',
    learnMoreMessage: 'Learn More',
    learnMoreURL: 'https://example.org/learnmore',
  });

  hb.events.on('TelemetrySent', payload => {
    assert.ok(isOrdered([0, payload.offeredTS, payload.learnMoreTS, payload.closedTS]));
    // Close learn more tab
    tabs[tabs.length - 1].close();
    done();
  });

  let learnMoreEl = hb.notice.querySelector('.text-link');
  learnMoreEl.click();

  // triggers sending ping to normandy
  closeAllNotifications();
};

exports['test it opens an engagement page after interaction'] = (assert, done) => {
  let hb = new Heartbeat(targetWindow, {
    testing: true,
    message: 'oh hai',
    engagementButtonLabel: 'Engage whooo',
    postAnswerURL: 'https://www.example.org/engaging',
  });

  tabs.on('load', (tab) => {
    assert.equal(tabs.activeTab.url, 'https://www.example.org/engaging');
    // Close engagement tab
    tabs[tabs.length - 1].close();
    done();
  });

  let engagementEl = hb.notice.querySelector('.notification-button');
  engagementEl.click();
};

function closeAllNotifications() {
  if (notificationBox.allNotifications.length === 0) {
    return Promise.resolve();
  }

  let promises = [];
{'fraction_non_alphanumeric': 0.15715744929228076, 'fraction_numerical': 0.0030643513789581204, 'mean_word_length': 4.188493565480696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 8, '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': '884607', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2104, 'n_words': 493}