Skip to content

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

3.5 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 sql

import (
	"testing"

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

func TestRelationPrimaryRenderTemplate(t *testing.T) {
	const firstID = "20260926000000-first01"
	const secondID = "20260926000000-second1"
	for _, templateContent := range []string{
		"<b>.action{.Computed}</b> .action{index .Related 0}",
		".action{if false}hidden.action{end}",
		".action{if}",
		"",
	} {
		t.Run(templateContent, func(t *testing.T) {
			primary := av.NewKey("primary", "Primary", "", av.KeyTypeBlock)
			primary.RenderTemplate = templateContent
			number := av.NewKey("number", "Number", "", av.KeyTypeNumber)
			computed := av.NewKey("computed", "Computed", "", av.KeyTypeTemplate)
			computed.Template = ".action{mulf .Number 2}"
			related := av.NewKey("related", "Related", "", av.KeyTypeRelation)
			related.Relation = &av.Relation{AvID: "target"}
			first := newTemplateErrorTestBlockValue(primary.ID, firstID, "Original")
			second := newTemplateErrorTestBlockValue(primary.ID, secondID, "Other")
			selfRelation := &av.Value{ID: "self", KeyID: related.ID, BlockID: firstID, Type: av.KeyTypeRelation,
				Relation: &av.ValueRelation{BlockIDs: []string{secondID}}}
			target := &av.AttributeView{ID: "target", KeyValues: []*av.KeyValues{
				{Key: primary, Values: []*av.Value{first, second}},
				{Key: number, Values: []*av.Value{{ID: "number-value", KeyID: number.ID, BlockID: firstID,
					Type: av.KeyTypeNumber, Number: &av.ValueNumber{Content: 21, IsNotEmpty: true}}}},
				{Key: computed},
				{Key: related, Values: []*av.Value{selfRelation}},
			}}
			// 目标视图未包含关联条目,也未展示模板依赖列。
			target.Views = []*av.View{{ID: "filtered", LayoutType: av.LayoutTypeTable,
				Table: av.NewLayoutTable(), GroupItemIDs: []string{secondID}, PageSize: 1}}
			context := NewAttributeViewRenderContext()
			depth := 2
			for range 10 {
				relation := &av.Value{Type: av.KeyTypeRelation, KeyID: related.ID, BlockID: firstID,
					Relation: &av.ValueRelation{BlockIDs: []string{firstID}}}
				source := &av.AttributeView{ID: "source", KeyValues: []*av.KeyValues{
					{Key: primary, Values: []*av.Value{first}},
					{Key: related, Values: []*av.Value{relation}},
				}}
				collection := newRenderTemplateTestCollection(av.LayoutTypeTable, firstID, first, relation)
				fillAttributeViewAutoGeneratedValues(source, collection, nil, &depth,
					map[string]*av.AttributeView{target.ID: target, source.ID: source}, context)
				value := relation.Relation.Contents[0]
				want := ""
				if templateContent == "<b>.action{.Computed}</b> .action{index .Related 0}" {
					want = "<b>42</b> Other"
				}
				if value.RenderedContent != want || value.HasRenderTemplate != (templateContent != "") {
					t.Fatalf("unexpected relation display: %+v", value)
				}
				if value.Block.Content != "Original" || value.BlockID != firstID || value.ID != first.ID {
					t.Fatalf("relation identity or stored value changed: %+v", value)
				}
			}
			if depth != 2 || context.renderingRelationTemplates {
				t.Fatal("nested template rendering consumed the outer recursion budget")
			}
			if first.HasRenderTemplate || first.RenderedContent != "" || len(selfRelation.Relation.Contents) != 0 ||
				len(target.KeyValues[2].Values) != 0 {
				t.Fatal("target template rendering changed source values")
			}
			if templateContent == ".action{if}" && len(context.sortedTemplateErrors()) != 1 {
				t.Fatal("relation template errors were not collected")
			}
		})
	}
}