import { describe, expect, it } from 'vitest'

import { createVisibilityAwarePoller } from './visibilityRefresh'

describe('createVisibilityAwarePoller', () => {
  it('runs the task immediately on schedule and stops when cancelled', async () => {
    let count = 0
    const stop = createVisibilityAwarePoller({
      intervalMs: 50,
      task: () => {
        count += 1
      },
    })

    await new Promise((resolve) => setTimeout(resolve, 120))
    stop()

    expect(count).toBeGreaterThanOrEqual(2)
  })
})
