Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

1.8 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package model

import (
	"encoding/json"
	"testing"

	"github.com/siyuan-note/siyuan/kernel/conf"
)

func TestMergeConfSectionJSONFieldAndMapKeys(t *testing.T) {
	current := conf.NewAI()
	current.Decision.Profiles["typesafe"].APIKey = "keep-key"
	merged, err := MergeConfSectionJSON(current, []byte(`{"Decision":{"Profiles":{"typesafe":{"Timeout":45},"Typesafe":{"APIKey":"separate-key"}}}}`))
	if err != nil {
		t.Fatal(err)
	}
	var next conf.AI
	if err = json.Unmarshal(merged, &next); err != nil {
		t.Fatal(err)
	}
	profiles := next.Decision.Profiles
	if profiles["typesafe"].Timeout != 45 || profiles["typesafe"].APIKey != "keep-key" ||
		profiles["Typesafe"] == nil || profiles["Typesafe"].APIKey != "separate-key" {
		t.Fatalf("struct fields and dictionary keys did not retain their matching rules: %s", merged)
	}
}

func TestMergeConfSectionJSONFieldCaseDeterministic(t *testing.T) {
	for range 20 {
		merged, err := MergeConfSectionJSON(conf.NewEditor(), []byte(`{"BlockGutterMode":false,"blockGutterMode":true}`))
		if err != nil {
			t.Fatal(err)
		}
		var next conf.Editor
		if err = json.Unmarshal(merged, &next); err != nil || !next.BlockGutterMode {
			t.Fatalf("canonical field did not win consistently: %s (%v)", merged, err)
		}
	}
}

func TestMergeConfJSONKeepsExactKeys(t *testing.T) {
	// 没有结构体类型的通用合并只按精确键名匹配,由调用方决定是否需要大小写兼容。
	merged, err := MergeConfJSON([]byte(`{"openFile":"F1"}`), []byte(`{"openfile":"F2"}`))
	if err != nil {
		t.Fatal(err)
	}
	if string(merged) != `{"openFile":"F1","openfile":"F2"}` {
		t.Fatalf("untyped merge conflated independently named keys: %s", merged)
	}
}