/*
 * Copyright (c) 2026 Huawei Technologies Co., Ltd.
 * openFuyao is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

package controller

import (
	"errors"
	"testing"

	"github.com/go-logr/logr"
	olog "gopkg.openfuyao.cn/common-modules/ologger/log"
)

func TestNormalizeLogArgs(t *testing.T) {
	got := normalizeLogArgs([]interface{}{"key", "value", 123, "bad", "lonely"})
	if len(got) != 6 || got[0] != "key" || got[2] != "arg" || got[5] != "" {
		t.Fatalf("normalized=%v", got)
	}
	if normalizeLogArgs(nil) != nil {
		t.Fatal("nil args should stay nil")
	}
}

func TestOloggerLogSink(t *testing.T) {
	logger := NewControllerRuntimeLogger(olog.With("test", "controller"))
	sink := logger.GetSink()
	if !sink.Enabled(10) {
		t.Fatal("sink should be enabled")
	}
	sink.Init(logr.RuntimeInfo{})
	sink.Info(1, "info", "k", "v")
	sink.Error(errors.New("boom"), "error", "k", "v")
	withValues := sink.WithValues("base", "value")
	withName := withValues.WithName("controller").WithName("child")
	withName.Info(0, "named")
}