Skip to content
quarry Artifact [ebda704fb7]
Login

Artifact [ebda704fb7]

Artifact ebda704fb7f03f0fdb98aad78fe6636ae550e3e7188567618495c555fafee982:


package api

import (
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"
)

// An error written without a next renders as it always did: the error
// key alone, and help only on a 401.
func TestAnErrorWithoutNextRendersAsBefore(t *testing.T) {
	w := httptest.NewRecorder()
	writeError(w, http.StatusNotFound, errors.New("gone"))
	var body map[string]any
	json.Unmarshal(w.Body.Bytes(), &body)
	if len(body) != 1 || body["error"] != "gone" {
		t.Errorf("FAIL: a 404 without next rendered %v", body)
	}
	w = httptest.NewRecorder()
	writeCodedError(w, http.StatusConflict, "stale", errors.New("old"))
	body = nil
	json.Unmarshal(w.Body.Bytes(), &body)
	if _, has := body["next"]; has || body["code"] != "stale" || len(body) != 2 {
		t.Errorf("FAIL: a coded 409 without next rendered %v", body)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A next renders its keys literally; cmd is left out until it ships.
func TestNextRendersItsWireKeys(t *testing.T) {
	w := httptest.NewRecorder()
	writeCodedError(w, http.StatusForbidden, "x", errors.New("no"), &Next{Say: "s", Call: "POST /a", Help: "/llms.txt#h"})
	var body struct {
		Next map[string]any `json:"next"`
	}
	json.Unmarshal(w.Body.Bytes(), &body)
	for _, k := range []string{"say", "call", "ask", "help"} {
		if _, ok := body.Next[k]; !ok {
			t.Errorf("FAIL: next lacks %q: %v", k, body.Next)
		}
	}
	if _, has := body.Next["cmd"]; has {
		t.Errorf("FAIL: next carries cmd: %v", body.Next)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}