package readRawGenomes_test


import "seekia/internal/genetics/readRawGenomes"

import "seekia/internal/genetics/geneticReferences/locusMetadata"
import "seekia/internal/genetics/createRawGenomes"
import "seekia/internal/helpers"

import "testing"

import "strings"


//TODO: Add some of the locusMetadata rsids to the fake files

func TestAncestryDNAFileReading(t *testing.T){

	err := locusMetadata.InitializeLocusMetadataVariables()
	if (err != nil){
		t.Fatal("InitializeLocusMetadataVariables failed: " + err.Error())
	}

	fileString, expectedFileTimeUnix, numberOfAddedLoci, fileRSIDsMap, err := createRawGenomes.CreateFakeRawGenome_AncestryDNA()
	if (err != nil){
		t.Fatal("Failed to create fake AncestryDNA genome: " + err.Error())
	}

	fileReader := strings.NewReader(fileString)

	companyName, importVersion, fileGenerationTime, fileNumberOfLoci, isPhased, genomeMap, err := readRawGenomes.ReadRawGenomeFile(fileReader)
	if (err != nil){
		t.Fatal("Failed to read AncestryDNA file: " + err.Error())
	}
	if (companyName != "AncestryDNA"){
		t.Fatal("Failed to read AncestryDNA file: Invalid companyName: " + companyName)
	}
	expectedImportVersion, err := readRawGenomes.GetCurrentCompanyImportVersion("AncestryDNA")
	if (err != nil){
		t.Fatal("Failed to get AncestryDNA import version: " + err.Error())
	}
	if (importVersion != expectedImportVersion){
		t.Fatal("AncestryDNA import version does not match.")
	}
	if (fileGenerationTime != expectedFileTimeUnix){
		t.Fatal("AncestryDNA file generation time does not match.")
	}

	if (fileNumberOfLoci != numberOfAddedLoci){
		fileNumberOfLociString := helpers.ConvertInt64ToString(fileNumberOfLoci)
		numberOfAddedLociString := helpers.ConvertInt64ToString(numberOfAddedLoci)
		t.Fatal("AncestryDNA number of loci does not match: " + fileNumberOfLociString + " != " + numberOfAddedLociString)
	}
	if (isPhased != false){
		t.Fatal("AncestryDNA file isPhased status is unexpected.")
	}

	for rsid, locusValue := range genomeMap{

		expectedLocusValue, exists := fileRSIDsMap[rsid]
		if (exists == false){
			rsidString := helpers.ConvertInt64ToString(rsid)
			t.Fatal("genomeMap contains unexpected rsid: " + rsidString)
		}
		if (locusValue != expectedLocusValue){
			t.Fatal("genomeMap contains unexpected rsid locus value.")
		}
	}
}


func Test23andMeFileReading(t *testing.T){

	err := locusMetadata.InitializeLocusMetadataVariables()
	if (err != nil){
		t.Fatal("InitializeLocusMetadataVariables failed: " + err.Error())
	}

	newRawGenome, fileCreationTime, fileNumberOfLoci, fileRSIDsMap, err := createRawGenomes.CreateFakeRawGenome_23andMe()
	if (err != nil){
		t.Fatal("Failed to create fake 23andMe Genome: " + err.Error())
	}

	fileReader := strings.NewReader(newRawGenome)

	companyName, importVersion, genomeGenerationTime, genomeNumberOfLoci, isPhased, genomeMap, err := readRawGenomes.ReadRawGenomeFile(fileReader)
	if (err != nil){
		t.Fatal("Failed to read 23andMe file: " + err.Error())
	}
	if (companyName != "23andMe"){
		t.Fatal("Failed to read 23andMe file: Invalid companyName: " + companyName)
	}
	expectedImportVersion, err := readRawGenomes.GetCurrentCompanyImportVersion("23andMe")
	if (err != nil){
		t.Fatal("Failed to get 23andMe import version: " + err.Error())
	}
	if (importVersion != expectedImportVersion){
		t.Fatal("23andMe import version does not match.")
	}
	if (fileCreationTime != genomeGenerationTime){
		t.Fatal("23andMe file generation time does not match.")
	}

	if (fileNumberOfLoci != genomeNumberOfLoci){
		fileNumberOfLociString := helpers.ConvertInt64ToString(fileNumberOfLoci)
		genomeNumberOfLociString := helpers.ConvertInt64ToString(genomeNumberOfLoci)
		t.Fatal("23andMe number of loci does not match: " + fileNumberOfLociString + " != " + genomeNumberOfLociString)
	}
	if (isPhased != false){
		t.Fatal("23andMe file isPhased status is unexpected.")
	}

	for rsid, locusValue := range genomeMap{

		expectedLocusValue, exists := fileRSIDsMap[rsid]
		if (exists == false){
			rsidString := helpers.ConvertInt64ToString(rsid)
			t.Fatal("genomeMap contains unexpected rsid: " + rsidString)
		}
		if (locusValue != expectedLocusValue){
			t.Fatal("genomeMap contains unexpected rsid locus value.")
		}
	}
}


