Skip to content
quarry Artifact [05654f3f98]
Login

Artifact [05654f3f98]

Artifact 05654f3f98c6ffc732d564c8da4c23d0ebfdbcd047a6d2043c3af2b5e1244d03:


package api

import (
	"context"
	"errors"
	"strings"
	"testing"

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

const twoPollBody = "BEFORE.\n\n```poll\nquestion: Lunch?\n- ramen\n- salad\n```\n\nAFTER.\n"

// #376: a poll draws where its fence is, not after all of the text.
func TestAPollPreviewsWhereItsFenceIs(t *testing.T) {
	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", Repos: testRepos(wikiIndex(t))}); err != nil {
		t.Fatal(err)
	}
	out, err := r.Invoke(context.Background(), "markup.preview", markupPreviewIn{Repo: "test", Body: twoPollBody})
	if err != nil {
		t.Fatal(err)
	}
	html := out.(markupPreviewOut).HTML
	b, p, a := strings.Index(html, "BEFORE"), strings.Index(html, "f-pollprev"), strings.Index(html, "AFTER")
	if b < 0 || p < 0 || a < 0 || !(b < p && p < a) {
		t.Errorf("FAIL: the poll is not between the text around it: %s", html)
	}
	if strings.Contains(html, pollSlot) {
		t.Error("FAIL: the slot marker leaked into the page")
	}
}

func TestASecondPollIsRefused(t *testing.T) {
	if err := refuseSecondPoll(twoPollBody); err != nil {
		t.Errorf("FAIL: one poll refused: %v", err)
	}
	two := twoPollBody + "\n```poll\nquestion: Dinner?\n- a\n- b\n```\n"
	if err := refuseSecondPoll(two); !errors.Is(err, ops.ErrInvalid) {
		t.Errorf("FAIL: two polls in one post: %v, want invalid", err)
	}
}