package config

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestDevModeRefusedOnProductionHost(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "config.yml")
	body := []byte("base_url: https://git.timfalken.com\nstorage_path: /mnt/storage/bluegit\nauth:\n  mode: dev\n")
	if err := os.WriteFile(path, body, 0o600); err != nil {
		t.Fatal(err)
	}
	_, err := Load(path)
	if err == nil || !strings.Contains(err.Error(), "dev") {
		t.Fatalf("verwacht weigering, kreeg %v", err)
	}
}

func TestSecretFromEnv(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "config.yml")
	body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nauth:\n  mode: oauth\n  client_id: git.example.test\n")
	if err := os.WriteFile(path, body, 0o600); err != nil {
		t.Fatal(err)
	}
	t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "uit-de-omgeving")
	cfg, err := Load(path)
	if err != nil {
		t.Fatal(err)
	}
	if cfg.Auth.ClientSecret != "uit-de-omgeving" || cfg.Git.DefaultBranch != "main" {
		t.Fatalf("%+v", cfg.Auth)
	}
	if cfg.RedirectURI() != "https://git.example.test/auth/callback" {
		t.Fatal(cfg.RedirectURI())
	}
}
