package main

import (
	"os"
	"testing"
)

// TestEkTabloAnahtari: surec tablosu anahtari profil ve eklenti adini ayirir.
func TestEkTabloAnahtari(t *testing.T) {
	if a := ekTabloAnahtari("ahmet", "everything"); a != "ahmet|everything" {
		t.Errorf("beklenmeyen anahtar: %q", a)
	}
	if ekTabloAnahtari("ahmet", "x") == ekTabloAnahtari("ayse", "x") {
		t.Errorf("farkli profiller ayni anahtari uretmemeli")
	}
}

// TestEkSurecProfilIzole: bir profilin baslattigi surec, baska profilde gorunmez ve
// durdurulamaz; kendi profilinde yeniden gorunur. Aktif hesap kavrami kullanilmaz.
func TestEkSurecProfilIzole(t *testing.T) {
	t.Setenv("FIKOYT_EK_YARDIMCI", "1")
	t.Setenv("FIKOYT_EK_MOD", "normal")
	ekTemizle(t)

	if _, err := ekOturumAlProfil("ahmet", "test-izole", os.Args[0], ekYardimciArgs()); err != nil {
		t.Fatal(err)
	}
	if !ekSurecCalisiyorProfil("ahmet", "test-izole") {
		t.Fatal("ahmet kendi surecini gormeliydi")
	}

	if ekSurecCalisiyorProfil("ayse", "test-izole") {
		t.Errorf("ayse, ahmet'in surecini gormemeliydi")
	}
	if err := ekEklentiDurdurProfil("ayse", "test-izole"); err == nil {
		t.Errorf("ayse, ahmet'in surecini durdurabilmemeliydi")
	}
	if !ekTabloDoluMu(ekTabloAnahtari("ahmet", "test-izole")) {
		t.Errorf("ahmet'in sureci ayse islemleriyle kapanmamaliydi")
	}

	if !ekSurecCalisiyorProfil("ahmet", "test-izole") {
		t.Errorf("ahmet'in sureci hala gorunmeliydi")
	}
}

// ekTabloDoluMu: verilen tam tablo anahtarinda acik bir surec var mi (profil on eki dahil).
func ekTabloDoluMu(anahtar string) bool {
	ekMu.Lock()
	o, ok := ekOturumlar[anahtar]
	ekMu.Unlock()
	return ok && ekOturumAcikMi(o)
}
