import { describe, expect, it } from 'vitest'
import { buildKbxHomeAttention, buildKbxHomeAttentionQueue } from '@kbx/ui'

describe('KBX Home attention queue',()=>{
  const entries=[{screenId:'OMS-ORD-001',path:'/oms/orders',section:'주문',title:'주문관리',module:'OMS' as const}]

  it('prioritizes dirty, failed work, urgent notifications, running work, then ordinary notifications',()=>{
    const tabs=[{key:'tab-1',screenId:'OMS-ORD-001',title:'주문관리',path:'/oms/orders',dirty:true,openedAt:'2026-08-08T00:00:00Z',lastActivatedAt:'2026-08-08T00:05:00Z'}]
    const operations=[{id:'op-f',type:'import',title:'주문 반영',status:'failed' as const,requestedAt:'2026-08-08T00:00:00Z'},{id:'op-r',type:'export',title:'주문 내보내기',status:'running' as const,requestedAt:'2026-08-08T00:00:00Z'}]
    const notifications=[{id:'n-w',severity:'warning' as const,title:'확인 필요',createdAt:'2026-08-08T00:00:00Z'},{id:'n-i',severity:'info' as const,title:'완료 안내',createdAt:'2026-08-08T00:00:00Z'}]
    expect(buildKbxHomeAttention(entries,tabs,operations,notifications).map(item=>item.source)).toEqual(['dirty','operation-failed','notification-urgent','operation-running','notification'])
  })

  it('sorts newest-first within priority and reports overflow without hiding the real total',()=>{
    const operations=[
      {id:'old',type:'import',title:'이전 실패',status:'failed' as const,requestedAt:'2026-08-08T00:00:00Z',completedAt:'2026-08-08T00:10:00Z'},
      {id:'new',type:'import',title:'최근 실패',status:'failed' as const,requestedAt:'2026-08-08T00:20:00Z',completedAt:'2026-08-08T00:30:00Z'},
    ]
    const queue=buildKbxHomeAttentionQueue(entries,[],operations,[],1)
    expect(queue.items.map(item=>item.title)).toEqual(['최근 실패'])
    expect(queue.totalCount).toBe(2)
    expect(queue.overflowCount).toBe(1)
    expect(queue.sourceCounts['operation-failed']).toBe(2)
  })
})
