import { test, expect, beforeEach, afterEach, afterAll } from 'vitest'

const rows = []
let attempts = 0
const original = new Error('original synchronous retry')
original.stack = ''
beforeEach(({ task }) => rows.push(['before', task.name]))
afterEach(({ task }) => rows.push(['after', task.name]))

test('native and subclass Promises in the actual document', async () => {
  expect(await Promise.resolve(7)).toBe(7)
  await expect(Promise.resolve(11)).resolves.toBe(11)
  class Other extends Promise {}
  await expect(new Other((resolve) => resolve(13))).resolves.toBe(13)
  rows.push(['promises', 7, 11, 13])
})

test('actual document and native event dispatch', () => {
  const button = document.createElement('button')
  button.textContent = 'actual WebKit'
  document.body.append(button)
  let clicks = 0
  button.addEventListener('click', () => clicks++)
  button.click()
  expect(button.textContent).toBe('actual WebKit')
  expect(clicks).toBe(1)
  button.remove()
  rows.push(['document', clicks])
})

test('original synchronous retry', { retry: 1 }, () => {
  rows.push(['retry', ++attempts])
  if (attempts === 1) throw original
  expect(attempts).toBe(2)
})

afterAll(() => console.log('WEBKIT_RUNTIME:' + JSON.stringify(rows)))
