Skip to content
quarry Artifact [3883bcc2e4]
Login

Artifact [3883bcc2e4]

Artifact 3883bcc2e4e2f04f9b6945c14593ed22021c5869603012d21ab8a1c340aac89d:


package api

import (
	"strings"
	"testing"
	"time"
)

func TestParsePoll(t *testing.T) {
	body := "Voting time!\n\n```poll\nquestion: Which default?\n- trunk\n- main\ncloses: 2026-09-01\n```\n\nDiscuss below.\n"
	def, rest := parsePoll(body)
	if def == nil {
		t.Fatal("well-formed poll not parsed")
	}
	if def.Question != "Which default?" || len(def.Options) != 2 ||
		def.Options[1] != "main" || def.Closes != "2026-09-01" {
		t.Fatalf("def = %+v", def)
	}
	if strings.Contains(rest, "```") || !strings.Contains(rest, "Discuss below.") {
		t.Fatalf("rest = %q", rest)
	}
	// Ballots accepted through the close day, frozen after it.
	if def.closed(time.Date(2026, 9, 1, 23, 0, 0, 0, time.UTC)) {
		t.Fatal("closed during its final day")
	}
	if !def.closed(time.Date(2026, 9, 2, 1, 0, 0, 0, time.UTC)) {
		t.Fatal("open past its close")
	}

	// Malformed polls degrade to plain markdown: body untouched.
	for _, bad := range []string{
		"```poll\nquestion: x\n- only one option\n```",
		"```poll\n- a\n- b\n```",            // no question
		"```poll\nquestion: x\n- a\n- b\n", // unterminated
		"no poll here",
	} {
		if def, rest := parsePoll(bad); def != nil || rest != bad {
			t.Fatalf("malformed %q parsed as %+v", bad, def)
		}
	}
}