import { describe, it, expect, vi, beforeEach } from 'vitest'
import { useToasts, toast } from '../src/renderer/src/store/toasts'

describe('toasts store', () => {
  beforeEach(() => {
    useToasts.setState({ toasts: [] })
    vi.useRealTimers()
  })

  it('show() adds a toast with the given message and tone', () => {
    toast('could not open file', 'error')
    const ts = useToasts.getState().toasts
    expect(ts).toHaveLength(1)
    expect(ts[0]?.message).toBe('could not open file')
    expect(ts[0]?.tone).toBe('error')
  })

  it('defaults the tone to info', () => {
    toast('heads up')
    expect(useToasts.getState().toasts[0]?.tone).toBe('info')
  })

  it('dismiss() removes only the matching toast', () => {
    toast('a')
    toast('b')
    const firstId = useToasts.getState().toasts[0]?.id ?? ''
    useToasts.getState().dismiss(firstId)
    const ts = useToasts.getState().toasts
    expect(ts).toHaveLength(1)
    expect(ts[0]?.message).toBe('b')
  })

  it('auto-dismisses after the timeout elapses', () => {
    vi.useFakeTimers()
    toast('temporary')
    expect(useToasts.getState().toasts).toHaveLength(1)
    vi.advanceTimersByTime(5000)
    expect(useToasts.getState().toasts).toHaveLength(0)
  })
})
