package web
import (
"errors"
"fmt"
"strings"
"testing"
"craftdesign.group/quarry/internal/ops"
)
// A refusal carries an instruction, and the instruction is the whole
// point of the page. This is the real one that went missing: a stacked
// landing whose target had already landed refused with the command that
// fixes it, and the reader was shown a blank "something went wrong".
func TestRefusalKeepsTheInstructionAndDropsTheSentinel(t *testing.T) {
err := fmt.Errorf("retarget it with 'zit landing retarget 63f3a7ba5c18 --to trunk': %w", ops.ErrInvalid)
got := refusalOf(err)
if !strings.Contains(got, "--to trunk") {
t.Fatalf("refusal = %q, want it to keep the instruction", got)
}
if strings.HasSuffix(got, ops.ErrInvalid.Error()) {
t.Fatalf("refusal = %q, want the sentinel suffix trimmed — the status code already says it", got)
}
}
// Only the trailing sentinel goes. A refusal that happens to mention
// the same words mid-sentence keeps them.
func TestRefusalTrimsOnlyTheTrailingSentinel(t *testing.T) {
err := fmt.Errorf("an invalid request is not the same as a refusal: %w", ops.ErrInvalid)
got := refusalOf(err)
if !strings.HasPrefix(got, "an invalid request is not the same as a refusal") {
t.Fatalf("refusal = %q, want the sentence intact", got)
}
if strings.Count(got, ops.ErrInvalid.Error()) != 1 {
t.Fatalf("refusal = %q, want exactly the mid-sentence occurrence left", got)
}
}
// A bare sentinel has nothing to say, and must not render as an empty
// page with a stray colon.
func TestRefusalOfABareSentinelIsNotEmptyPunctuation(t *testing.T) {
got := refusalOf(ops.ErrInvalid)
if got == "" || strings.HasPrefix(got, ":") {
t.Fatalf("refusal = %q, want readable text", got)
}
if !errors.Is(ops.ErrInvalid, ops.ErrInvalid) {
t.Fatal("sanity")
}
}