import { assert } from '@open-wc/testing'
import { Har } from 'har-format'
import { sortEntires } from '../../../src/elements/har/HarUtils.js'

describe('HarUtils', () => {
  const demo1file = 'har1.har'

  async function readHarFile(name: string): Promise<Har> {
    const url = new URL(`/test/elements/har/test-data/${name}`, window.location.href).toString()
    const response = await fetch(url)
    return response.json()
  }

  describe('sortEntires()', () => {
    let model: Har

    before(async () => {
      model = await readHarFile(demo1file)
    })

    it('sorts entries', () => {
      const d = new Date()
      const e1 = {
        ...model.log.entries[0],
        id: '1',
        startedDateTime: d.toISOString(),
      }
      d.setSeconds(d.getSeconds() - 10)
      const e2 = {
        ...model.log.entries[1],
        id: '2',
        startedDateTime: d.toISOString(),
      }
      const result = sortEntires([e1, e2])

      assert.equal(result[0].id, '2')
    })

    it('sets the timestamp property on the items', () => {
      const entry = { ...model.log.entries[0] }
      const result = sortEntires([entry])
      assert.typeOf(result[0].timestamp, 'number')
    })
  })
})
