11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst { resolve, join } = require('path')
31cb0ef41Sopenharmony_ciconst fs = require('graceful-fs')
41cb0ef41Sopenharmony_ciconst mockLogs = require('../../fixtures/mock-logs')
51cb0ef41Sopenharmony_ciconst tmock = require('../../fixtures/tmock')
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst mockTimers = (t, options) => {
81cb0ef41Sopenharmony_ci  const { logs, logMocks } = mockLogs()
91cb0ef41Sopenharmony_ci  const Timers = tmock(t, '{LIB}/utils/timers', {
101cb0ef41Sopenharmony_ci    ...logMocks,
111cb0ef41Sopenharmony_ci  })
121cb0ef41Sopenharmony_ci  const timers = new Timers(options)
131cb0ef41Sopenharmony_ci  t.teardown(() => timers.off())
141cb0ef41Sopenharmony_ci  return { timers, logs }
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cit.test('getters', async (t) => {
181cb0ef41Sopenharmony_ci  const { timers } = mockTimers(t)
191cb0ef41Sopenharmony_ci  t.match(timers.unfinished, new Map())
201cb0ef41Sopenharmony_ci  t.match(timers.finished, {})
211cb0ef41Sopenharmony_ci})
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cit.test('listens/stops on process', async (t) => {
241cb0ef41Sopenharmony_ci  const { timers } = mockTimers(t)
251cb0ef41Sopenharmony_ci  process.emit('time', 'foo')
261cb0ef41Sopenharmony_ci  process.emit('time', 'bar')
271cb0ef41Sopenharmony_ci  process.emit('timeEnd', 'bar')
281cb0ef41Sopenharmony_ci  t.match(timers.unfinished, new Map([['foo', Number]]))
291cb0ef41Sopenharmony_ci  t.match(timers.finished, { bar: Number })
301cb0ef41Sopenharmony_ci  timers.off()
311cb0ef41Sopenharmony_ci  process.emit('time', 'baz')
321cb0ef41Sopenharmony_ci  t.notOk(timers.unfinished.get('baz'))
331cb0ef41Sopenharmony_ci})
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cit.test('convenience time method', async (t) => {
361cb0ef41Sopenharmony_ci  const { timers } = mockTimers(t)
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const end = timers.time('later')
391cb0ef41Sopenharmony_ci  timers.time('sync', () => {})
401cb0ef41Sopenharmony_ci  await timers.time('async', () => new Promise(r => setTimeout(r, 10)))
411cb0ef41Sopenharmony_ci  end()
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  t.match(timers.finished, { later: Number, sync: Number, async: Number })
441cb0ef41Sopenharmony_ci})
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cit.test('initial timer', async (t) => {
471cb0ef41Sopenharmony_ci  const { timers } = mockTimers(t, { start: 'foo' })
481cb0ef41Sopenharmony_ci  process.emit('timeEnd', 'foo')
491cb0ef41Sopenharmony_ci  t.match(timers.finished, { foo: Number })
501cb0ef41Sopenharmony_ci})
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cit.test('initial listener', async (t) => {
531cb0ef41Sopenharmony_ci  const events = []
541cb0ef41Sopenharmony_ci  const listener = (...args) => events.push(args)
551cb0ef41Sopenharmony_ci  const { timers } = mockTimers(t, { listener })
561cb0ef41Sopenharmony_ci  process.emit('time', 'foo')
571cb0ef41Sopenharmony_ci  process.emit('time', 'bar')
581cb0ef41Sopenharmony_ci  process.emit('timeEnd', 'bar')
591cb0ef41Sopenharmony_ci  timers.off(listener)
601cb0ef41Sopenharmony_ci  process.emit('timeEnd', 'foo')
611cb0ef41Sopenharmony_ci  t.equal(events.length, 1)
621cb0ef41Sopenharmony_ci  t.match(events, [['bar', Number]])
631cb0ef41Sopenharmony_ci})
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cit.test('finish unstarted timer', async (t) => {
661cb0ef41Sopenharmony_ci  const { logs } = mockTimers(t)
671cb0ef41Sopenharmony_ci  process.emit('timeEnd', 'foo')
681cb0ef41Sopenharmony_ci  t.match(logs.silly, [['timing', /^Tried to end timer/, 'foo']])
691cb0ef41Sopenharmony_ci})
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_cit.test('writes file', async (t) => {
721cb0ef41Sopenharmony_ci  const { timers } = mockTimers(t)
731cb0ef41Sopenharmony_ci  const dir = t.testdir()
741cb0ef41Sopenharmony_ci  process.emit('time', 'foo')
751cb0ef41Sopenharmony_ci  process.emit('timeEnd', 'foo')
761cb0ef41Sopenharmony_ci  timers.load({ path: resolve(dir, `TIMING_FILE-`) })
771cb0ef41Sopenharmony_ci  timers.writeFile({ some: 'data' })
781cb0ef41Sopenharmony_ci  const data = JSON.parse(fs.readFileSync(resolve(dir, 'TIMING_FILE-timing.json')))
791cb0ef41Sopenharmony_ci  t.match(data, {
801cb0ef41Sopenharmony_ci    metadata: { some: 'data' },
811cb0ef41Sopenharmony_ci    timers: { foo: Number },
821cb0ef41Sopenharmony_ci    unfinishedTimers: {
831cb0ef41Sopenharmony_ci      npm: [Number, Number],
841cb0ef41Sopenharmony_ci    },
851cb0ef41Sopenharmony_ci  })
861cb0ef41Sopenharmony_ci})
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cit.test('fails to write file', async (t) => {
891cb0ef41Sopenharmony_ci  const { logs, timers } = mockTimers(t)
901cb0ef41Sopenharmony_ci  const dir = t.testdir()
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  timers.load({ path: join(dir, 'does', 'not', 'exist') })
931cb0ef41Sopenharmony_ci  timers.writeFile()
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  t.match(logs.warn, [['timing', 'could not write timing file']])
961cb0ef41Sopenharmony_ci  t.equal(timers.file, null)
971cb0ef41Sopenharmony_ci})
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_cit.test('no dir and no file', async (t) => {
1001cb0ef41Sopenharmony_ci  const { logs, timers } = mockTimers(t)
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  timers.load()
1031cb0ef41Sopenharmony_ci  timers.writeFile()
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  t.strictSame(logs, [])
1061cb0ef41Sopenharmony_ci  t.equal(timers.file, null)
1071cb0ef41Sopenharmony_ci})
108