package main

import (
	"testing"
	"time"

	"github.com/go-sql-driver/mysql"
)

func TestNormalizedOneOSDSNForcesTimeParsingAndTimeouts(t *testing.T) {
	dsn, err := normalizedDSN("ONEOS_MYSQL_DSN", "reader:secret@tcp(mysql:3306)/ln_asset_management", true)
	if err != nil {
		t.Fatal(err)
	}
	config, err := mysql.ParseDSN(dsn)
	if err != nil {
		t.Fatal(err)
	}
	if !config.ParseTime || config.Loc == nil || config.Loc.String() != "Asia/Shanghai" {
		t.Fatalf("time settings not normalized: %#v", config)
	}
	if config.Timeout != 10*time.Second || config.ReadTimeout != 30*time.Second || config.WriteTimeout != 10*time.Second {
		t.Fatalf("timeouts not normalized: %#v", config)
	}
}

func TestNormalizedOneOSDSNRejectsWrongDatabase(t *testing.T) {
	if _, err := normalizedDSN("ONEOS_MYSQL_DSN", "reader:secret@tcp(mysql:3306)/other", true); err == nil {
		t.Fatal("wrong OneOS database should be rejected")
	}
}

func TestEnvFloatValidation(t *testing.T) {
	t.Setenv("RATIO_TEST", "1.1")
	if _, err := envFloat("RATIO_TEST", 0.1); err == nil {
		t.Fatal("ratio above one should be rejected")
	}
}
