import { describe, expect, it } from 'vitest'
import { addSeen } from '../logic/storage'

describe('addSeen', () => {
  it('appends new keys to the end', () => {
    expect(addSeen(['a', 'b'], ['c', 'd'])).toEqual(['a', 'b', 'c', 'd'])
  })

  it('deduplicates existing keys', () => {
    expect(addSeen(['a', 'b'], ['b', 'c'])).toEqual(['a', 'b', 'c'])
  })

  it('handles all-duplicate addition', () => {
    expect(addSeen(['a', 'b'], ['a', 'b'])).toEqual(['a', 'b'])
  })

  it('respects MAX_SEEN cap (5000) by trimming oldest', () => {
    const existing = Array.from({ length: 5000 }, (_, i) => `k${i}`)
    const out = addSeen(existing, ['new1', 'new2'])
    expect(out).toHaveLength(5000)
    expect(out[out.length - 1]).toBe('new2')
    expect(out[out.length - 2]).toBe('new1')
    // 最旧的 'k0' 和 'k1' 被挤出
    expect(out.includes('k0')).toBe(false)
    expect(out.includes('k1')).toBe(false)
  })

  it('empty input', () => {
    expect(addSeen([], [])).toEqual([])
    expect(addSeen([], ['a'])).toEqual(['a'])
    expect(addSeen(['a'], [])).toEqual(['a'])
  })
})
