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 model

import (
	"slices"
	"strings"
	"testing"

	"github.com/88250/lute/ast"
	"github.com/88250/lute/parse"
	"github.com/siyuan-note/siyuan/kernel/util"
)

func TestCanonicalBlockKramdownIAL(t *testing.T) {
	ial := testBlockKramdownIAL(
		"custom-z", "title-img", "fold", "style", "icon", "tags", "bookmark", "memo", "alias", "name", "title",
		"type", "updated", "id", "custom-sy-readonly", "custom-riff-decks", "custom-reminder-wechat",
		"custom-heading-mode", "custom-heading-level", "custom-avs", "unknown", "heading-fold", "custom-a",
	)
	originalNames := blockKramdownIALAttrNames(ial)

	canonical := canonicalBlockKramdownIAL(ial)

	assertBlockKramdownIALAttrNames(t, canonical, []string{
		"id", "updated", "type", "title", "name", "alias", "memo", "bookmark", "tags", "icon", "title-img", "style",
		"fold", "custom-avs", "custom-heading-level", "custom-heading-mode", "custom-reminder-wechat", "custom-riff-decks", "custom-sy-readonly",
		"custom-a", "custom-z", "heading-fold", "unknown",
	})
	assertBlockKramdownIALAttrNames(t, ial, originalNames)
}

func TestGetBlockKramdownCanonicalBlockIAL(t *testing.T) {
	for _, mode := range []string{"md", "textmark"} {
		t.Run(mode, func(t *testing.T) {
			id := "20260730230437-v4c93el"
			ial := [][]string{
				{"custom-z", "z"},
				{"updated", "20260731125712"},
				{"custom-sy-readonly", "true"},
				{"id", id},
				{"custom-a", "a"},
			}
			originalNames := blockKramdownIALAttrNames(ial)
			root := &ast.Node{Type: ast.NodeDocument}
			paragraph := &ast.Node{Type: ast.NodeParagraph, ID: id, KramdownIAL: ial}
			paragraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("content")})
			root.AppendChild(paragraph)
			tree := &parse.Tree{Root: root}

			kramdown := getBlockKramdown0(tree, id, mode, util.NewLute())

			expectedIAL := `{: id="20260730230437-v4c93el" updated="20260731125712" ` +
				`custom-sy-readonly="true" custom-a="a" custom-z="z"}`
			if !strings.Contains(kramdown, expectedIAL) {
				t.Fatalf("unexpected Kramdown IAL: %q", kramdown)
			}
			assertBlockKramdownIALAttrNames(t, paragraph.KramdownIAL, originalNames)
		})
	}
}

func TestAddCanonicalBlockIALNodesIncludesChildBlocks(t *testing.T) {
	root := &ast.Node{Type: ast.NodeDocument}
	blockquote := &ast.Node{Type: ast.NodeBlockquote, KramdownIAL: testBlockKramdownIAL("custom-z", "updated", "id")}
	paragraph := &ast.Node{Type: ast.NodeParagraph, KramdownIAL: testBlockKramdownIAL("custom-b", "updated", "id", "custom-a")}
	blockquote.AppendChild(paragraph)
	root.AppendChild(blockquote)
	tree := &parse.Tree{Root: root}

	addCanonicalBlockIALNodes(tree, false)

	assertBlockKramdownIALAttrNames(t, parse.Tokens2IAL(blockquote.Next.Tokens), []string{"id", "updated", "custom-z"})
	assertBlockKramdownIALAttrNames(t, parse.Tokens2IAL(paragraph.Next.Tokens), []string{"id", "updated", "custom-a", "custom-b"})
}

func testBlockKramdownIAL(names ...string) (ret [][]string) {
	for _, name := range names {
		ret = append(ret, []string{name, name + "-value"})
	}
	return
}

func blockKramdownIALAttrNames(ial [][]string) (ret []string) {
	for _, attr := range ial {
		ret = append(ret, attr[0])
	}
	return
}

func assertBlockKramdownIALAttrNames(t *testing.T, ial [][]string, expected []string) {
	t.Helper()
	actual := blockKramdownIALAttrNames(ial)
	if !slices.Equal(actual, expected) {
		t.Fatalf("unexpected IAL attribute order: got %v, want %v", actual, expected)
	}
}