The server registers its routes from one table in server/routes.go, and tools/genopenapi (run by go generate) writes docs/swagger.json, an OpenAPI 3.1 document, from the same table and the Go types it names. Schemas are reflected with invopop/jsonschema: descriptions from the first paragraph of the doc comments, enums and limits from jsonschema tags. Request schemas are strict, as the decoder is; response schemas allow fields added later. The daemon serves the embedded document at GET /openapi.json without a token, internal/app passes it to the server in its config, and release archives and the Docker build include it. Tests keep it honest: a golden test and a CI step fail when the document is stale, every registered route must be in the table, and the tags are held to validation, to the Go constants and to the values the handlers send. receipt.Element and printqueue.Status gain field doc comments for the descriptions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package printqueue
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"reflect"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The status enum in Status's jsonschema tag lists every State constant.
|
|
func TestStateEnumListsEveryState(t *testing.T) {
|
|
file, err := parser.ParseFile(token.NewFileSet(), "queue.go", nil, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var states []string
|
|
ast.Inspect(file, func(n ast.Node) bool {
|
|
spec, ok := n.(*ast.ValueSpec)
|
|
if !ok {
|
|
return true
|
|
}
|
|
if ident, ok := spec.Type.(*ast.Ident); !ok || ident.Name != "State" {
|
|
return true
|
|
}
|
|
for _, v := range spec.Values {
|
|
if lit, ok := v.(*ast.BasicLit); ok && lit.Kind == token.STRING {
|
|
s, _ := strconv.Unquote(lit.Value)
|
|
states = append(states, s)
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
|
|
f, _ := reflect.TypeFor[Status]().FieldByName("State")
|
|
var enum []string
|
|
for part := range strings.SplitSeq(f.Tag.Get("jsonschema"), ",") {
|
|
if v, ok := strings.CutPrefix(part, "enum="); ok {
|
|
enum = append(enum, v)
|
|
}
|
|
}
|
|
slices.Sort(states)
|
|
slices.Sort(enum)
|
|
if len(states) == 0 || !slices.Equal(states, enum) {
|
|
t.Errorf("Status.State enum %v, State constants %v", enum, states)
|
|
}
|
|
}
|