b1482b554b
ParseOutput returned only the text after the outcome value, so a stage that wrote its result first and "# outcome" last handed the next stage "(no output)" — the probe's Compose and Critique turns were both lost this way. The body is now the whole document with just the outcome heading and value line removed, in order, byte for byte. Tests cover a trailing and a mid-document outcome.
151 lines
4.4 KiB
Go
151 lines
4.4 KiB
Go
package turn
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseOutput(t *testing.T) {
|
|
allowed := []string{"success", "blocked", "rejected"}
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
outcome string
|
|
body string
|
|
err error
|
|
}{
|
|
{
|
|
name: "text before and after the outcome section is the body",
|
|
input: "Checking the work first.\n\n## outcome\nsuccess\n\n# Result\n\nThe work is complete.\n",
|
|
outcome: "success",
|
|
body: "Checking the work first.\n\n\n# Result\n\nThe work is complete.\n",
|
|
},
|
|
{
|
|
name: "a trailing outcome keeps the whole body written before it",
|
|
input: "# Composition\n\nAll stages listed.\n\n# outcome\nsuccess\n",
|
|
outcome: "success",
|
|
body: "# Composition\n\nAll stages listed.\n\n",
|
|
},
|
|
{
|
|
name: "an outcome in the middle keeps both sides in order",
|
|
input: "Before.\n\n# outcome\nblocked\n\nAfter.\n",
|
|
outcome: "blocked",
|
|
body: "Before.\n\n\nAfter.\n",
|
|
},
|
|
{
|
|
name: "diff embedding an outcome line cannot confuse the parser",
|
|
input: "# outcome\nblocked\n\n```diff\n+# outcome success\n+unrelated change\n```\n",
|
|
outcome: "blocked",
|
|
body: "\n```diff\n+# outcome success\n+unrelated change\n```\n",
|
|
},
|
|
{
|
|
name: "fenced headings before the real outcome are ignored",
|
|
input: "```md\n# outcome\nfailed\n```\n\n~~~\n## outcome\nrejected\n~~~\n\n# outcome\nSUCCESS\n\nbody",
|
|
outcome: "success",
|
|
body: "```md\n# outcome\nfailed\n```\n\n~~~\n## outcome\nrejected\n~~~\n\n\nbody",
|
|
},
|
|
{
|
|
name: "CRLF line endings",
|
|
input: "leading\r\n\r\n## outcome\r\n\r\nsuccess\r\n\r\nbody\r\n",
|
|
outcome: "success",
|
|
body: "leading\r\n\r\n\r\nbody\r\n",
|
|
},
|
|
{
|
|
name: "arbitrary markdown body is preserved exactly",
|
|
input: "# outcome\nsuccess\n\n## Heading\n\n- item\n\n```go\nfmt.Println(\"hi\")\n```\n\n# outcome is discussed here\n",
|
|
outcome: "success",
|
|
body: "\n## Heading\n\n- item\n\n```go\nfmt.Println(\"hi\")\n```\n\n# outcome is discussed here\n",
|
|
},
|
|
{
|
|
name: "duplicate outcome",
|
|
input: "# outcome\nsuccess\n\n# outcome\nblocked\n",
|
|
err: ErrDuplicateOutcome,
|
|
},
|
|
{
|
|
name: "duplicate inside a fence is ignored",
|
|
input: "```\n# outcome\nsuccess\n```\n\n# outcome\nblocked\n",
|
|
outcome: "blocked",
|
|
body: "```\n# outcome\nsuccess\n```\n\n",
|
|
},
|
|
{
|
|
name: "unknown outcome word",
|
|
input: "# outcome\nmaybe\n",
|
|
err: ErrOutcomeNotAllowed,
|
|
},
|
|
{
|
|
name: "extra text on the outcome line",
|
|
input: "# outcome\nsuccess please\n",
|
|
err: ErrOutcomeNotAllowed,
|
|
},
|
|
{
|
|
name: "value inside a fence is not an outcome value",
|
|
input: "# outcome\n```\nsuccess\n```\n",
|
|
err: ErrOutcomeNotAllowed,
|
|
},
|
|
{
|
|
name: "empty output",
|
|
input: "",
|
|
err: ErrEmptyOutput,
|
|
},
|
|
{
|
|
name: "whitespace-only output",
|
|
input: " \r\n\t\n",
|
|
err: ErrEmptyOutput,
|
|
},
|
|
{
|
|
name: "missing outcome heading",
|
|
input: "# Result\nsuccess\n",
|
|
err: ErrMissingOutcome,
|
|
},
|
|
{
|
|
name: "outcome heading without a value",
|
|
input: "# outcome\n\n",
|
|
err: ErrMissingOutcome,
|
|
},
|
|
{
|
|
name: "fenced outcome is not a real outcome",
|
|
input: "```\n# outcome\nsuccess\n```\n",
|
|
err: ErrMissingOutcome,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
outcome, body, err := ParseOutput(test.input, allowed)
|
|
if test.err != nil {
|
|
if !errors.Is(err, test.err) {
|
|
t.Fatalf("error = %v, want %v", err, test.err)
|
|
}
|
|
if outcome != "" || body != "" {
|
|
t.Fatalf("error result = (%q, %q), want empty", outcome, body)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if outcome != test.outcome || body != test.body {
|
|
t.Fatalf("ParseOutput() = (%q, %q), want (%q, %q)", outcome, body, test.outcome, test.body)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseOutputReturnsCanonicalOutcome(t *testing.T) {
|
|
outcome, body, err := ParseOutput("#### Outcome\n\n Success \n\nrest", []string{" success ", "blocked"})
|
|
if err != nil {
|
|
t.Fatalf("ParseOutput() error: %v", err)
|
|
}
|
|
if outcome != "success" {
|
|
t.Fatalf("outcome = %q, want canonical %q", outcome, "success")
|
|
}
|
|
if body != "\nrest" {
|
|
t.Fatalf("body = %q, want %q", body, "\nrest")
|
|
}
|
|
}
|
|
|
|
func TestParseOutputRequiresWhitespaceAfterHeadingMarker(t *testing.T) {
|
|
if _, _, err := ParseOutput("#outcome\nsuccess\n", []string{"success"}); !errors.Is(err, ErrMissingOutcome) {
|
|
t.Fatalf("error = %v, want missing outcome", err)
|
|
}
|
|
}
|