import { describe, expect, it } from 'vitest'
import type { HarnessPlanRun } from '../../lib/panel-data'
import { abandonedPlanItemForRun } from './AbandonedContent'

const run: HarnessPlanRun = {
  runId: 'run-abandoned',
  seq: 42,
  title: 'Provider probe',
  status: 'failed',
  state: 'error',
  owner: 'runner',
  currentTask: null,
  tasksTotal: 3,
  tasksCompleted: 1,
  pendingDecisions: 0,
  degradedReason: 'runner-not-live',
  updatedAt: null,
  repoRoot: '/home/user/Projects/overdeck',
  registry: { slug: 'provider-probe', projectName: 'Overdeck' },
  failClass: 'gate-failed',
  abandoned: true,
  abandonedAt: '2026-07-19T12:00:00.000Z',
  waves: [],
}

describe('abandonedPlanItemForRun', () => {
  it('maps persisted collector facts and prefers failClass', () => {
    expect(abandonedPlanItemForRun(run)).toEqual({
      abandonedAtMs: Date.parse('2026-07-19T12:00:00.000Z'),
      failLabel: 'gate-failed',
      href: '/plans/run-abandoned',
      projectLabel: 'Overdeck',
      runId: 'run-abandoned',
      slug: 'provider-probe',
      title: 'Provider probe',
    })
  })

  it('falls back to degraded reason and rejects missing timestamps', () => {
    expect(abandonedPlanItemForRun({ ...run, failClass: undefined })).toMatchObject({
      failLabel: 'runner-not-live',
    })
    expect(abandonedPlanItemForRun({ ...run, abandonedAt: null })).toBeNull()
  })
})
