import { describe, it, expect } from 'vitest'
import { formatLine, composeMessage, logger } from '../src/main/logger'

// The logger's file sink needs Electron's `app` (userData path), which is absent in
// the node test env — so here we cover the pure formatting helpers and assert that
// the impure entry points degrade to a safe no-op rather than throwing (CC8).
describe('logger (CC8)', () => {
  const now = new Date('2026-07-01T08:30:00.000Z')

  it('formatLine renders ISO timestamp + upper-case level + message', () => {
    expect(formatLine('error', 'boom', now)).toBe('2026-07-01T08:30:00.000Z [ERROR] boom')
    expect(formatLine('info', 'hi', now)).toBe('2026-07-01T08:30:00.000Z [INFO] hi')
  })

  it('composeMessage passes the label through when there is no detail', () => {
    expect(composeMessage('settings write failed')).toBe('settings write failed')
  })

  it('composeMessage appends an Error message', () => {
    expect(composeMessage('write failed', new Error('ENOSPC'))).toBe('write failed: ENOSPC')
  })

  it('composeMessage stringifies a non-Error detail', () => {
    expect(composeMessage('op', 42)).toBe('op: 42')
    expect(composeMessage('op', 'bad input')).toBe('op: bad input')
  })

  it('logger.error is a safe no-op when app/userData is unavailable', () => {
    expect(() => logger.error('some op', new Error('x'))).not.toThrow()
  })
})
