Skip to content

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

2.1 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 (
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testing"

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

func TestTabsUserGuideExamplesRoundTrip(t *testing.T) {
	ids := map[string]bool{"20200825162036-4dx365o.sy": true, "20200924093441-ft2rhps.sy": true,
		"20211226121319-emrk2yy.sy": true, "20240530101000-3qhz7br.sy": true}
	l := util.NewLute()
	count := 0
	err := filepath.WalkDir("../../app/guide", func(path string, entry os.DirEntry, err error) error {
		if err != nil || entry.IsDir() || !ids[entry.Name()] {
			return err
		}
		count++
		data, err := os.ReadFile(path)
		if err != nil {
			return err
		}
		tree, err := dataparser.ParseJSONWithoutFix(data, l.ParseOptions)
		if err != nil {
			return err
		}
		spec, err := strconv.Atoi(tree.Root.Spec)
		if err != nil || spec < 3 {
			t.Fatalf("%s: tabs require spec 3 or newer, got %q", path, tree.Root.Spec)
		}
		ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
			if !entering || n.Type != ast.NodeTabs {
				return ast.WalkContinue
			}
			if err := treenode.ValidateBlockSubtree(n); err != nil {
				t.Fatal(err)
			}
			fragment := &parse.Tree{Root: n}
			dom := l.Tree2BlockDOM(fragment, l.RenderOptions, l.ParseOptions)
			markdown := l.BlockDOM2Md(dom)
			if !strings.Contains(markdown, ":::: tabs\n") || !strings.Contains(markdown, "\n::: tabs\n") ||
				strings.Count(markdown, "@tab:active ") != 2 || strings.Contains(markdown, ":::tab") {
				t.Fatalf("%s: unexpected nested tabs syntax\n%s", path, markdown)
			}
			spun := l.SpinBlockDOM(dom)
			for _, id := range n.BlockIDs() {
				if !strings.Contains(spun, id) {
					t.Fatalf("%s: lost block %s", path, id)
				}
			}
			return ast.WalkSkipChildren
		})
		return nil
	})
	if err != nil {
		t.Fatal(err)
	}
	if count != len(ids) {
		t.Fatalf("found %d guide examples", count)
	}
}