import { expect } from 'vitest'
import { isBrowser, isNativeRunner, isV8Provider, readCoverageMap, runVitest, test } from '../utils'

test('vi.importActual() collects coverage of original module', async () => {
  await runVitest({
    include: ['fixtures/test/mock-importActual-fixture.test.ts'],
    coverage: {
      reporter: 'json',
      include: ['fixtures/src/mock-target.ts'],
    },
  })

  const coverageMap = await readCoverageMap()

  // v8-browser and native runner report 100% due to different coverage collection behavior
  if ((isBrowser() && isV8Provider()) || isNativeRunner()) {
    expect(coverageMap).toMatchInlineSnapshot(`
      {
        "branches": "0/0 (100%)",
        "functions": "2/2 (100%)",
        "lines": "2/2 (100%)",
        "statements": "2/2 (100%)",
      }
    `)
  }
  else {
    expect(coverageMap).toMatchInlineSnapshot(`
      {
        "branches": "0/0 (100%)",
        "functions": "1/2 (50%)",
        "lines": "1/2 (50%)",
        "statements": "1/2 (50%)",
      }
    `)

    const coverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/mock-target.ts')
    const functionCoverage = Object.keys(coverage.fnMap)
      .map(index => ({ name: coverage.fnMap[index].name, hits: coverage.f[index] }))
      .sort((a, b) => a.name.localeCompare(b.name))

    expect(functionCoverage).toMatchInlineSnapshot(`
      [
        {
          "hits": 1,
          "name": "double",
        },
        {
          "hits": 0,
          "name": "triple",
        },
      ]
    `)
  }
})
