import { describe, it, expect } from 'vitest'
import { isArchivedLogs } from '../sse-executions'

describe('isArchivedLogs', () => {
  it('returns true for a valid archived pointer object', () => {
    const pointer = {
      __archived: true,
      storage_path: 'org-123/executions/exec-456/logs.json',
      archived_at: '2026-01-15T10:00:00Z',
      original_count: 1500
    }
    expect(isArchivedLogs(pointer)).toBe(true)
  })

  it('returns false for null', () => {
    expect(isArchivedLogs(null)).toBe(false)
  })

  it('returns false for undefined', () => {
    expect(isArchivedLogs(undefined)).toBe(false)
  })

  it('returns false for an empty array (normal logs state)', () => {
    expect(isArchivedLogs([])).toBe(false)
  })

  it('returns false for a non-empty array of log messages', () => {
    const logs = [
      { level: 'info', message: 'Starting execution', timestamp: 1700000000000 },
      { level: 'info', message: 'Execution complete', timestamp: 1700000001000 }
    ]
    expect(isArchivedLogs(logs)).toBe(false)
  })

  it('returns false for an empty object', () => {
    expect(isArchivedLogs({})).toBe(false)
  })

  it('returns false for an object with __archived: false', () => {
    const obj = {
      __archived: false,
      storage_path: 'org-123/executions/exec-456/logs.json',
      archived_at: '2026-01-15T10:00:00Z',
      original_count: 42
    }
    expect(isArchivedLogs(obj)).toBe(false)
  })

  it('returns false for a string', () => {
    expect(isArchivedLogs('archived')).toBe(false)
  })

  it('returns false for a number', () => {
    expect(isArchivedLogs(42)).toBe(false)
  })

  it('narrows the type so storage_path and original_count are accessible after the guard', () => {
    const value: unknown = {
      __archived: true,
      storage_path: 'org-123/executions/exec-789/logs.json',
      archived_at: '2026-03-01T08:30:00Z',
      original_count: 300
    }

    if (isArchivedLogs(value)) {
      expect(value.storage_path).toBe('org-123/executions/exec-789/logs.json')
      expect(value.original_count).toBe(300)
    } else {
      throw new Error('Expected isArchivedLogs to return true')
    }
  })
})
