<commit_before>/* eslint-env jest */
/* globals chrome */

afterEach(() => {
  jest.clearAllMocks()
  jest.resetModules()
})

test('opens a welcome tab on install', () => {
  require('../ext-background')

  // Mock the onInstalled event
  const onInstalledCallback = chrome.runtime.onInstalled.addListener.mock.calls[0][0]
  onInstalledCallback({
    reason: 'install',
    id: 'abc-123'
  })
  expect(chrome.tabs.create).toHaveBeenCalledWith({
    url: 'https://tab.gladly.io/newtab/first-tab/'
  })
})

test('does not open a tab on extension update', () => {
  require('../ext-background')

  // Mock the onInstalled event
  const onInstalledCallback = chrome.runtime.onInstalled.addListener.mock.calls[0][0]
  onInstalledCallback({
    reason: 'update',
    previousVersion: '5.18'
  })
  expect(chrome.tabs.create).not.toHaveBeenCalled()
})

test('gracefully handles any error with opening the welcome page', () => {
  require('../ext-background')

  chrome.tabs.create.mockImplementationOnce(() => {
    throw new Error('Whoops!')
  })

  // Suppress expected console error.
  jest.spyOn(console, 'error').mockImplementationOnce(() => {})

  // Mock the onInstalled event
  const onInstalledCallback = chrome.runtime.onInstalled.addListener.mock.calls[0][0]
  onInstalledCallback({
    reason: 'install',
    previousVersion: '5.18'
  })
})

test('sets the post-uninstall URL', () => {
  require('../ext-background')
  expect(chrome.runtime.setUninstallURL)
    .toHaveBeenCalledWith('https://tab.gladly.io/newtab/uninstalled/')
})

test('gracefully handles errors with setting the post-uninstall URL', () => {
  chrome.runtime.setUninstallURL.mockImplementationOnce(() => {
    throw new Error('Whoops!')
  })

  // Suppress expected console error.
  jest.spyOn(console, 'error').mockImplementationOnce(() => {})

  // Should not throw.
  require('../ext-background')
})
<commit_msg>added action icon click error testing<commit_after>/* eslint-env jest */
/* globals chrome */

afterEach(() => {
  jest.clearAllMocks()
  jest.resetModules()
})

test('opens a welcome tab on install', () => {
  require('../ext-background')

  // Mock the onInstalled event
  const onInstalledCallback = chrome.runtime.onInstalled.addListener.mock.calls[0][0]
  onInstalledCallback({
    reason: 'install',
    id: 'abc-123'
  })
  expect(chrome.tabs.create).toHaveBeenCalledWith({
    url: 'https://tab.gladly.io/newtab/first-tab/'
  })
})

test('does not open a tab on extension update', () => {
  require('../ext-background')

  // Mock the onInstalled event
  const onInstalledCallback = chrome.runtime.onInstalled.addListener.mock.calls[0][0]
  onInstalledCallback({
    reason: 'update',
    previousVersion: '5.18'
  })
  expect(chrome.tabs.create).not.toHaveBeenCalled()
})

test('gracefully handles any error with opening the welcome page', () => {
  require('../ext-background')

  chrome.tabs.create.mockImplementationOnce(() => {
    throw new Error('Whoops!')
  })

  // Suppress expected console error.
  jest.spyOn(console, 'error').mockImplementationOnce(() => {})

  // Mock the onInstalled event
  const onInstalledCallback = chrome.runtime.onInstalled.addListener.mock.calls[0][0]
  onInstalledCallback({
    reason: 'install',
    previousVersion: '5.18'
  })
})

test('sets the post-uninstall URL', () => {
  require('../ext-background')
  expect(chrome.runtime.setUninstallURL)
    .toHaveBeenCalledWith('https://tab.gladly.io/newtab/uninstalled/')
})

test('gracefully handles errors with registering the action click handler', () => {
  chrome.runtime.setUninstallURL.mockImplementationOnce(() => {
    throw new Error('Whoops!')
  })

  // Suppress expected console error.
  jest.spyOn(console, 'error').mockImplementationOnce(() => {})

  // Should not throw.
  require('../ext-background')
})

test('gracefully handles any error with handling an action icon click', () => {
  require('../ext-background')

  chrome.tabs.create.mockImplementationOnce(() => {
    throw new Error('Whoops!')
  })

  // Suppress expected console error.
  jest.spyOn(console, 'error').mockImplementationOnce(() => {})

  // Mock the action icon click event
  const tab = chrome.action.onClicked.addListener.mock.calls[0][0]
  tab({
    id: 1
  })

  expect(chrome.tabs.create).toHaveBeenCalledWith({
    url: 'chrome://newtab'
  })
})

{'fraction_non_alphanumeric': 0.16354816354816354, 'fraction_numerical': 0.007623007623007623, 'mean_word_length': 3.971297359357061, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 4, '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': '6951455', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1378, 'n_words': 268}