import { describe, expect, it, vi } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import { useUserStore } from '../users'

describe('useUserStore invites', () => {
  it('expires invite', () => {
    setActivePinia(createPinia())
    const store = useUserStore()
    store.init([], [{ id: 'invite-1', email: 'a@demo.com', role: 'operator', status: '待接受', invitedAt: '2026-02-01T00:00:00Z' }], [])

    store.expireInvite('invite-1')

    const invite = store.invites[0]
    expect(invite.status).toBe('已过期')
    expect(invite.expiredAt).toBeTruthy()
  })

  it('resends invite by resetting status and invitedAt', () => {
    setActivePinia(createPinia())
    const store = useUserStore()
    store.init([], [{ id: 'invite-2', email: 'b@demo.com', role: 'viewer', status: '已过期', invitedAt: '2026-02-01T00:00:00Z', expiredAt: '2026-02-02T00:00:00Z' }], [])

    vi.useFakeTimers()
    vi.setSystemTime(new Date('2026-02-10T00:00:00Z'))

    store.resendInvite('invite-2')

    const invite = store.invites[0]
    expect(invite.status).toBe('待接受')
    expect(invite.invitedAt).toBe('2026-02-10T00:00:00.000Z')
    expect(invite.expiredAt).toBeUndefined()

    vi.useRealTimers()
  })
})
