// DO NOT TOUCH THIS
require("../../requires.js")

mapFunctions = new Map()
// Loading the Function Map
var path = `${mainDir}/services/modules`
var folders = fs.readdirSync(path).filter(function (file) {
    return fs.statSync(path+'/'+file).isDirectory();
});
folders.forEach(element => {
    var commandFiles = fs.readdirSync(`${path}/${element}`).filter(file => file.endsWith('.js') && !file.startsWith("index"));
    for (const file of commandFiles) {
        delete require.cache[require.resolve(`${path}/${element}/${file}`)];
        const command = require(`${path}/${element}/${file}`);
        mapFunctions.set(command.name, command);
    }
});
// You can touch beyond this point

let audiopath
let transcriptPath
let summarizePath
let llmpath
let speakers

describe("Unit Tests", function() {

    describe('Audio Extraction', function () {
        this.slow(1000)
        this.timeout(3000)
        it('Extract .mp4 to .mp3', function (done) {

            mapFunctions.get("extraction-video-to-audio").function({inputVideoPath: __dirname.replaceAll("\\","/")+"/testvideo.mp4", outputType: "mp3"}).then(resp => {
                audiopath = resp
                // console.log(resp);
                done()
            }).catch(err => {
                done(err);
            })
        })
        it('Extract .mp4 to .flac', function (done) {

            mapFunctions.get("extraction-video-to-audio").function({inputVideoPath: __dirname.replaceAll("\\","/")+"/testvideo.mp4", outputType: "flac"}).then(resp => {
                // console.log(resp);
                done()
            }).catch(err => {
                done(err);
            })
        })
        it('Extracting to a nonexistant format', function (done) {

            mapFunctions.get("extraction-video-to-audio").function({inputVideoPath: __dirname.replaceAll("\\","/")+"/testvideo.mp4", outputType: "qqq"}).then(resp => {
                // console.log(resp);
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
        it('Extracting from nonexistant file', function (done) {

            mapFunctions.get("extraction-video-to-audio").function({inputVideoPath: "a", outputType: "mp3"}).then(resp => {
                // console.log(resp);
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
        it('Extracting from nonexistant file to nonexistant format', function (done) {

            mapFunctions.get("extraction-video-to-audio").function({inputVideoPath: "a", outputType: "qqq"}).then(resp => {
                // console.log(resp);
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
    });


    describe("Audio Transcription", function() {
        this.slow(20000)
        this.timeout(120000)

        it('Assembly', function (done) {
            mapFunctions.get("assembly").function(audiopath).then(resp => {
                // console.log(resp);
                transcriptPath = resp
                done()
            }).catch(err => {
                done(err)
            })
        })

        it('Assembly Wrong file', function (done) {
            mapFunctions.get("assembly").function("a").then(resp => {
                // console.log(resp);
                // transcriptPath = resp
                done("Didnt crash")
            }).catch(err => {
                // console.log(err);
                done()
            })
        })
        // TODO add more Transcription Tool tests here
    })

    describe("Transcript Summarizer", function() {
        this.slow(100)
        this.timeout(1000)

        it("Summarizer 1", function (done){
            mapFunctions.get("summarize-transcription").function(transcriptPath).then(resp => {
                done()
            }).catch(err => {
                done(err)
            })
        })

        it("Summarizer 1 Wrong File", function (done){
            mapFunctions.get("summarize-transcription").function("a").then(resp => {
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })

        it("Summarizer 2 (Main)", function (done){
            mapFunctions.get("summarize-transcription2").function(transcriptPath).then(resp => {
                summarizePath = resp
                done()
            }).catch(err => {
                done(err)
            })
        })

        it("Summarizer 2 (Main) Wrong File", function (done){
            mapFunctions.get("summarize-transcription2").function("a").then(resp => {
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
    })

    describe("Large Language Model", function() {
        this.slow(30000)
        this.timeout(120000)

        it("ChatGPT", function (done){
            mapFunctions.get("llm-saia_openai_gpt").function({inputTranscriptPath: summarizePath, documentTypePath: "./storage/documentType/followup_report.txt", language: "en"}).then(resp => {
                llmpath = resp
                done()
            }).catch(err => {
                done(err)
            })
        })


        it("Gemini", function (done){
            mapFunctions.get("llm-gemini").function({inputTranscriptPath: summarizePath, documentTypePath: "./storage/documentType/followup_report.txt", language: "en"}).then(resp => {
                done()
            }).catch(err => {
                if(err.includes("(503)")){done()} // Error 503 is gemini overload, so an Error that they can at any time throw at us which would crash the pipeline, so we just ignore it and we just imagine that the test passed
                else{
                    console.log(err);
                    done(err)
                }
            })
        })

        it("Qwen3", function (done){
            mapFunctions.get("qwen3-235b-a22b").function({inputTranscriptPath: summarizePath, documentTypePath: "./storage/documentType/followup_report.txt", language: "en"}).then(resp => {
                done()
            }).catch(err => {
                done(err)
            })
        })


        it("ChatGPT (Nonexistant Type File)", function (done){
            mapFunctions.get("llm-saia_openai_gpt").function({inputTranscriptPath: summarizePath, documentTypePath: "a", language: "en"}).then(resp => {
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
        it("Gemini (Nonexistant Type File)", function (done){
            mapFunctions.get("llm-gemini").function({inputTranscriptPath: summarizePath, documentTypePath: "a", language: "en"}).then(resp => {
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
        it("Qwen3 (Nonexistant Type File)", function (done){
            mapFunctions.get("qwen3-235b-a22b").function({inputTranscriptPath: summarizePath, documentTypePath: "a", language: "en"}).then(resp => {
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
    })

    describe("Audio Snippet", function() {
        this.slow(1000)
        this.timeout(5000)

        // transcriptPath = "A:\\programing\\@projects\\video2document\\storage\\transcriptionSummaries\\testvideo-1765900665001.json"
        // audiopath = "A:\\programing\\@projects\\video2document\\storage\\audio\\testvideo.mp3"


        it("Audio Snipper Generator", function (done){
            mapFunctions.get("extract-speaker-snippets").function({audioPath: audiopath, jsonPath: summarizePath }).then(resp => {
                speakers = resp
                done()
            }).catch(err => {
                done(err)
            })
        })

        it("Audio Snipper Generator (Nonexistant Transcript File)", function (done){
            mapFunctions.get("extract-speaker-snippets").function({audioPath: audiopath, jsonPath: "a" }).then(resp => {
                speakers = resp
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })

        it("Audio Snipper Generator (Nonexistant Audio File)", function (done){
            mapFunctions.get("extract-speaker-snippets").function({audioPath: "a", jsonPath: summarizePath }).then(resp => {
                speakers = resp
                done("Didnt crash")
            }).catch(err => {
                done()
            })
        })
    })
    after(function() {
        console.log(`\n\n\n${audiopath} \n${transcriptPath} \n${summarizePath} \n${llmpath}`);
    })
})


