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")
}
}
// The forge's storage section has to say whose bucket is in effect,
// because a repository inheriting the forge's looks identical to one
// that bound its own — and the choice the substrate model offers is
// then one nobody can see they have.
func TestFleetSettingsSaysWhetherItSuppliesStorage(t *testing.T) {
src := fleetSettingsSource
for _, hook := range []string{`data-hook="forge-bucket-bound"`, `data-hook="forge-bucket-unbound"`} {
if !strings.Contains(src, hook) {
t.Errorf("FAIL: the page cannot say %s", hook)
continue
}
t.Logf("ok: the page can say %s", hook)
}
if !strings.Contains(src, `action="/fleet/settings/bucket"`) {
t.Error("FAIL: no form posts the forge's binding")
}
if !strings.Contains(src, `action="/fleet/settings/bucket/unbind"`) {
t.Error("FAIL: the forge cannot stop supplying storage")
}
}
// And a repository has to be able to say it is using the forge's.
func TestARepositoryCanSayItUsesTheForgesStorage(t *testing.T) {
if !strings.Contains(settingsSource, `data-hook="bucket-forge"`) {
t.Fatal("FAIL: a repository inheriting the forge's storage cannot say so")
}
t.Log("ok: a repository says whose storage it uses")
}