<commit_before>import sinon from 'sinon'
import Client from '../../../src/core/client'
import { merge, mergeNotice, objectIsEmpty, makeBacktrace, runBeforeNotifyHandlers, runAfterNotifyHandlers, isIgnored, newObject, sanitize, logger } from '../../../src/core/util'
import { nullLogger } from '../helpers'

describe('utils', function () {
  describe('logger', function() {
    it('skips debug logging by default', function () {
      const mockDebug = jest.fn()
      const console = nullLogger()
      console.debug = mockDebug
      const client = new Client({ logger: console })

      logger(client).debug('expected')

      expect(mockDebug.mock.calls.length).toBe(0)
    })

    it('logs debug to console when enabled', function () {
      const mockDebug = jest.fn()
      const console = nullLogger()
      console.debug = mockDebug
      const client = new Client({ logger: console, debug: true })

      logger(client).debug('expected')

      expect(mockDebug.mock.calls.length).toBe(1)
      expect(mockDebug.mock.calls[0][0]).toBe('[Honeybadger]')
      expect(mockDebug.mock.calls[0][1]).toBe('expected')
    })
  })

  describe('merge', function () {
    it('combines two objects', function () {
      expect(merge({ foo: 'foo' }, { bar: 'bar' })).toEqual({ foo: 'foo', bar: 'bar' })
    })
  })

  describe('mergeNotice', function () {
    it('combines two objects', function () {
      expect(mergeNotice({ foo: 'foo' }, { bar: 'bar' })).toEqual({ foo: 'foo', bar: 'bar' })
    })

    it('combines context properties', function () {
      expect(mergeNotice({ context: { foo: 'foo' } }, { context: { bar: 'bar' } })).toEqual({ context: { foo: 'foo', bar: 'bar' } })
    })
  })

  describe('objectIsEmpty', function () {
    it('returns true when empty', function () {
      expect(objectIsEmpty({})).toEqual(true)
    })

    it('returns false when not empty', function () {
      expect(objectIsEmpty({ foo: 'bar' })).toEqual(false)
    })
  })

  describe('makeBacktrace', function () {
    it('returns a parsed stacktrace in Honeybadger format', function () {
      const stack = 'Error: Something unexpected has occurred.\n\tat bar (foo.js:1:2)'
      expect(makeBacktrace(stack)).toEqual([
        {
          file: 'foo.js',
          method: 'bar',
          number: 1,
          column: 2
        }
      ])
    })
  })

  describe('runBeforeNotifyHandlers', function () {
    it('returns false when any handler returns false', function () {
      const handlers = [
        () => false,
        () => true
      ]
      expect(runBeforeNotifyHandlers({}, handlers)).toEqual(false)
    })

    it('returns true when all handlers return true', function () {
      const handlers = [
        () => true,
        () => true
      ]
      expect(runBeforeNotifyHandlers({}, handlers)).toEqual(true)
    })

    it('passes the notice to handlers', function () {<commit_msg>Accidentally deleted backtrace test<commit_after>import sinon from 'sinon'
import Client from '../../../src/core/client'
import { merge, mergeNotice, objectIsEmpty, makeBacktrace, runBeforeNotifyHandlers, runAfterNotifyHandlers, isIgnored, newObject, sanitize, logger } from '../../../src/core/util'
import { nullLogger } from '../helpers'

describe('utils', function () {
  describe('logger', function() {
    it('skips debug logging by default', function () {
      const mockDebug = jest.fn()
      const console = nullLogger()
      console.debug = mockDebug
      const client = new Client({ logger: console })

      logger(client).debug('expected')

      expect(mockDebug.mock.calls.length).toBe(0)
    })

    it('logs debug to console when enabled', function () {
      const mockDebug = jest.fn()
      const console = nullLogger()
      console.debug = mockDebug
      const client = new Client({ logger: console, debug: true })

      logger(client).debug('expected')

      expect(mockDebug.mock.calls.length).toBe(1)
      expect(mockDebug.mock.calls[0][0]).toBe('[Honeybadger]')
      expect(mockDebug.mock.calls[0][1]).toBe('expected')
    })
  })

  describe('merge', function () {
    it('combines two objects', function () {
      expect(merge({ foo: 'foo' }, { bar: 'bar' })).toEqual({ foo: 'foo', bar: 'bar' })
    })
  })

  describe('mergeNotice', function () {
    it('combines two objects', function () {
      expect(mergeNotice({ foo: 'foo' }, { bar: 'bar' })).toEqual({ foo: 'foo', bar: 'bar' })
    })

    it('combines context properties', function () {
      expect(mergeNotice({ context: { foo: 'foo' } }, { context: { bar: 'bar' } })).toEqual({ context: { foo: 'foo', bar: 'bar' } })
    })
  })

  describe('objectIsEmpty', function () {
    it('returns true when empty', function () {
      expect(objectIsEmpty({})).toEqual(true)
    })

    it('returns false when not empty', function () {
      expect(objectIsEmpty({ foo: 'bar' })).toEqual(false)
    })
  })

  describe('makeBacktrace', function () {
    it('returns a parsed stacktrace in Honeybadger format', function () {
      const stack = 'Error: Something unexpected has occurred.\n\tat bar (foo.js:1:2)'
      expect(makeBacktrace(stack)).toEqual([
        {
          file: 'foo.js',
          method: 'bar',
          number: 1,
          column: 2
        }
      ])
    })

    it('returns and empty array when no stack is undefined', function () {
      expect(makeBacktrace(undefined)).toEqual([])
    })
  })

  describe('runBeforeNotifyHandlers', function () {
    it('returns false when any handler returns false', function () {
      const handlers = [
        () => false,
        () => true
      ]
      expect(runBeforeNotifyHandlers({}, handlers)).toEqual(false)
    })

    it('returns true when all handlers return true', function () {
      const handlers = [
        () => true,
        () => true
      ]
      expect(runBeforeNotifyHandlers({}, handlers)).toEqual(true)
    })

    it('passes the notice to handlers', function () {
{'fraction_non_alphanumeric': 0.16616212579885636, 'fraction_numerical': 0.003363605785401951, 'mean_word_length': 2.928005284015852, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3800771', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1719, 'n_words': 432}