import { describe, it, expect } from 'vitest'
// Pure module — no electron import chain (L37), same as lib/formatters.
import { orphanPartials } from '../src/main/lib/partials'

// The resolved FINAL output path the before_dl `dest|` print gives download.ts.
const OUT = 'C:\\Users\\me\\Videos\\Despacito.webm'

describe('orphanPartials (R4 cancel cleanup)', () => {
  it('deletes the in-progress .part stream file', () => {
    expect(orphanPartials(OUT, ['Despacito.f399.mp4.part'])).toEqual(['Despacito.f399.mp4.part'])
  })

  it('deletes a finished per-stream intermediate (no .part) awaiting merge', () => {
    // e.g. the video half of a video+audio download whose audio was still in flight.
    expect(orphanPartials(OUT, ['Despacito.f251.webm'])).toEqual(['Despacito.f251.webm'])
  })

  it('deletes fragment pieces and the .ytdl fragment-state file', () => {
    const entries = [
      'Despacito.f140.m4a.part-Frag0',
      'Despacito.f140.m4a.part-Frag12',
      'Despacito.f140.m4a.ytdl'
    ]
    expect(orphanPartials(OUT, entries)).toEqual(entries)
  })

  it('KEEPS a pre-existing same-title completed file (video and audio)', () => {
    // The audit's "delete the wrong file" risk: a bare <stem>.<ext> is never ours.
    expect(orphanPartials(OUT, ['Despacito.webm', 'Despacito.mp3', 'Despacito.mkv'])).toEqual([])
  })

  it('KEEPS a different download in the same folder (different stem)', () => {
    expect(orphanPartials(OUT, ['Other Video.f399.mp4.part', 'Other Video.f251.webm'])).toEqual([])
  })

  it('does not match a stem that is only a prefix of a longer name', () => {
    // "Despacito 2.f399.mp4.part" must NOT be swept by the "Despacito" download —
    // startsWith('Despacito.') excludes it (the dot boundary matters).
    expect(orphanPartials(OUT, ['Despacito 2.f399.mp4.part', 'Despacito2.webm.part'])).toEqual([])
  })

  it('handles a title containing dots (only the final extension is the boundary)', () => {
    const out = 'C:\\Users\\me\\Music\\Track 3.14 - Pi.mp3'
    const entries = [
      'Track 3.14 - Pi.webm.part',
      'Track 3.14 - Pi.mp3',
      'Track 3.14 - Pi.f251.webm'
    ]
    // .part + intermediate deleted; the completed .mp3 kept.
    expect(orphanPartials(out, entries)).toEqual([
      'Track 3.14 - Pi.webm.part',
      'Track 3.14 - Pi.f251.webm'
    ])
  })

  it('sorts nothing / preserves order and mixes correctly in a realistic listing', () => {
    const entries = [
      'Despacito.f399.mp4.part', // delete: in-progress video
      'Despacito.f251.webm', // delete: finished audio intermediate
      'Despacito.webm', // keep: pre-existing final
      'Unrelated.f140.m4a.part', // keep: other download
      'Despacito.f140.m4a.ytdl' // delete: fragment state
    ]
    expect(orphanPartials(OUT, entries)).toEqual([
      'Despacito.f399.mp4.part',
      'Despacito.f251.webm',
      'Despacito.f140.m4a.ytdl'
    ])
  })

  it('returns [] when the path has no usable stem', () => {
    expect(orphanPartials('', ['x.part'])).toEqual([])
  })
})
