import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { mkdtempSync, writeFileSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import { listSpotVideos } from '../../src/spot-utils.js'

describe('listSpotVideos (spot-utils.js)', () => {
    let dir

    beforeEach(() => {
        dir = mkdtempSync(join(tmpdir(), 'spot-test-'))
    })

    afterEach(() => {
        rmSync(dir, { recursive: true, force: true })
    })

    it('ritorna [] se la cartella non esiste', () => {
        expect(listSpotVideos(join(dir, 'inesistente'))).toEqual([])
    })

    it('ritorna [] se la cartella è vuota', () => {
        expect(listSpotVideos(dir)).toEqual([])
    })

    it('elenca solo i file .mp4, ordinati alfabeticamente', () => {
        writeFileSync(join(dir, 'b.mp4'), '')
        writeFileSync(join(dir, 'a.mp4'), '')
        writeFileSync(join(dir, 'note.txt'), '')
        writeFileSync(join(dir, 'foto.png'), '')
        expect(listSpotVideos(dir)).toEqual(['a.mp4', 'b.mp4'])
    })

    it('riconosce l\'estensione .mp4 senza distinzione di maiuscole', () => {
        writeFileSync(join(dir, 'SPOT.MP4'), '')
        expect(listSpotVideos(dir)).toEqual(['SPOT.MP4'])
    })
})
