package initialize

import (
	"encoding/json"
	"testing"

	"kra/internal/conf"
	"kra/pkg/protoutil"

	"google.golang.org/protobuf/encoding/protojson"
)

func TestMergeProtoJSONPreservesOmittedFieldsAndNormalizesKeys(t *testing.T) {
	target := &conf.AdminBackend{
		RouterPrefix: "/api",
		System:       &conf.AdminBackend_System{UseRedis: true, IplimitCount: 3},
	}
	patch := json.RawMessage(`{"router_prefix":"/admin","system":{"iplimit_count":9}}`)

	if err := protoutil.MergeProtoJSON(target, patch, protojson.UnmarshalOptions{DiscardUnknown: true}); err != nil {
		t.Fatal(err)
	}
	if target.RouterPrefix != "/admin" || target.System.IplimitCount != 9 {
		t.Fatalf("patch was not applied: %#v", target)
	}
	if !target.System.UseRedis {
		t.Fatal("an omitted nested field was cleared")
	}
}
