Skip to content
quarry Artifact [95569ecf10]
Login

Artifact [95569ecf10]

Artifact 95569ecf10c50b71a08663b39cca5da12c09b24922ae5d4494bc9ba8d5bef0cf:


package api

import (
	"context"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/ops"
)

// The block editor's read: every block's source joins back to the
// draft byte for byte, and each block is rendered on its own through
// the preview's pipeline, polls included.
func TestMarkupBlocksSplitAndRenderEachBlock(t *testing.T) {
	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", Repos: testRepos(wikiIndex(t))}); err != nil {
		t.Fatal(err)
	}
	body := "# Title\n\nSome *words*.\n\n```poll\nquestion: Ship it?\n- yes\n- no\n```\n"
	out, err := r.Invoke(context.Background(), "markup.blocks", markupPreviewIn{Repo: "test", Body: body})
	if err != nil {
		t.Fatal(err)
	}
	blocks := out.(markupBlocksOut).Blocks
	var joined strings.Builder
	for _, b := range blocks {
		joined.WriteString(b.Source)
	}
	if joined.String() != body {
		t.Fatalf("FAIL: the blocks do not join back to the draft: %q", joined.String())
	}
	if len(blocks) != 3 {
		t.Fatalf("FAIL: %d blocks; want heading, paragraph, poll", len(blocks))
	}
	if !strings.Contains(blocks[0].HTML, "<h") || !strings.Contains(blocks[1].HTML, "<em>") {
		t.Errorf("FAIL: blocks are not rendered: %q %q", blocks[0].HTML, blocks[1].HTML)
	}
	if strings.Contains(blocks[2].HTML, "<pre") {
		t.Errorf("FAIL: the poll block rendered as a code fence, not as its widget: %q", blocks[2].HTML)
	}
}

// A reference link resolves in its block even when its definition sits
// in another block, and a loose list is one block, as in the post.
func TestMarkupBlocksResolveAcrossBlocks(t *testing.T) {
	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", Repos: testRepos(wikiIndex(t))}); err != nil {
		t.Fatal(err)
	}
	body := "See [the docs][d].\n\n- one\n\n- two\n\n[d]: https://example.org/docs\n"
	out, err := r.Invoke(context.Background(), "markup.blocks", markupPreviewIn{Repo: "test", Body: body})
	if err != nil {
		t.Fatal(err)
	}
	blocks := out.(markupBlocksOut).Blocks
	if len(blocks) != 3 {
		t.Fatalf("FAIL: %d blocks; want the paragraph, one list, the definition", len(blocks))
	}
	if !strings.Contains(blocks[0].HTML, `href="https://example.org/docs"`) {
		t.Errorf("FAIL: the reference link did not resolve across blocks: %q", blocks[0].HTML)
	}
}