package config

import (
	"reflect"
	"testing"
	"time"
)

func TestLoadConfigYAML(t *testing.T) {
	cfg, err := Load("../../configs/config.yaml")
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}

	if cfg.HTTPAddr != ":13100" {
		t.Fatalf("HTTPAddr = %q", cfg.HTTPAddr)
	}
	if cfg.MySQLDSN != "hyapp:hyapp@tcp(127.0.0.1:23306)/hyapp_admin?parseTime=true&charset=utf8mb4&loc=UTC" {
		t.Fatalf("MySQLDSN = %q", cfg.MySQLDSN)
	}
	if cfg.UserMySQLDSN != "hyapp:hyapp@tcp(127.0.0.1:23306)/hyapp_user?parseTime=true&charset=utf8mb4&loc=UTC" {
		t.Fatalf("UserMySQLDSN = %q", cfg.UserMySQLDSN)
	}
	if cfg.AccessTokenTTL != 30*time.Minute {
		t.Fatalf("AccessTokenTTL = %s", cfg.AccessTokenTTL)
	}
	if cfg.RefreshTokenTTL != 7*24*time.Hour {
		t.Fatalf("RefreshTokenTTL = %s", cfg.RefreshTokenTTL)
	}
	if !cfg.Migrations.AllowChecksumRepair {
		t.Fatalf("Migrations.AllowChecksumRepair = false, want true for local config")
	}
	if !cfg.TencentCOS.Enabled || cfg.TencentCOS.BucketName != "yumi-assets-1420526837" || cfg.TencentCOS.ObjectPrefix != "admin" {
		t.Fatalf("TencentCOS = %#v", cfg.TencentCOS)
	}
}

func TestLoadEmptyPathUsesDefault(t *testing.T) {
	cfg, err := Load("")
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}

	if !reflect.DeepEqual(cfg, Default()) {
		t.Fatalf("Load(\"\") = %#v, want default", cfg)
	}
}
