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>
225 lines
7.5 KiB
Go
225 lines
7.5 KiB
Go
package receipt
|
|
|
|
import (
|
|
"errors"
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"reflect"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// enumTag returns the enum values in the jsonschema tag of Element's field.
|
|
func enumTag(t *testing.T, field string) []string {
|
|
t.Helper()
|
|
f, ok := reflect.TypeFor[Element]().FieldByName(field)
|
|
if !ok {
|
|
t.Fatalf("Element has no field %s", field)
|
|
}
|
|
var values []string
|
|
for part := range strings.SplitSeq(f.Tag.Get("jsonschema"), ",") {
|
|
if v, ok := strings.CutPrefix(part, "enum="); ok {
|
|
values = append(values, v)
|
|
}
|
|
}
|
|
return values
|
|
}
|
|
|
|
// problemsWith validates a job of el alone and returns the problems that
|
|
// name field, as "font must be a or b" or "unknown symbology" do.
|
|
func problemsWith(t *testing.T, el Element, field string) []string {
|
|
t.Helper()
|
|
var ve *ValidationError
|
|
err := Job{Elements: []Element{el}}.Validate()
|
|
if err != nil && !errors.As(err, &ve) {
|
|
t.Fatalf("Validate: %v", err)
|
|
}
|
|
var out []string
|
|
if ve != nil {
|
|
for _, p := range ve.Problems {
|
|
if strings.Contains(p, "): "+field+" ") || strings.Contains(p, " "+field+" ") {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// The type enum lists every Type constant, so the schema offers every
|
|
// element type validation accepts.
|
|
func TestTypeEnumListsEveryType(t *testing.T) {
|
|
file, err := parser.ParseFile(token.NewFileSet(), "job.go", nil, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var types []string
|
|
ast.Inspect(file, func(n ast.Node) bool {
|
|
spec, ok := n.(*ast.ValueSpec)
|
|
if !ok {
|
|
return true
|
|
}
|
|
for i, name := range spec.Names {
|
|
if !strings.HasPrefix(name.Name, "Type") || i >= len(spec.Values) {
|
|
continue
|
|
}
|
|
if lit, ok := spec.Values[i].(*ast.BasicLit); ok && lit.Kind == token.STRING {
|
|
v, _ := strconv.Unquote(lit.Value)
|
|
types = append(types, v)
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
enum := enumTag(t, "Type")
|
|
slices.Sort(types)
|
|
slices.Sort(enum)
|
|
if !slices.Equal(types, enum) {
|
|
t.Errorf("Element.Type enum %v, Type constants %v", enum, types)
|
|
}
|
|
}
|
|
|
|
// Each enum value in Element's jsonschema tags passes validation on an
|
|
// element it applies to, and a made-up value does not. Validation also takes
|
|
// other letter case and a few aliases, which the enums leave out.
|
|
func TestEnumTagsMatchValidation(t *testing.T) {
|
|
tests := []struct {
|
|
field, json string
|
|
// elements set the field to v on the elements it applies to; a
|
|
// value is valid when one of them accepts it.
|
|
elements func(v string) []Element
|
|
}{
|
|
{"Font", "font", func(v string) []Element { return []Element{{Type: TypeText, Text: "x", Font: v}} }},
|
|
{"Align", "align", func(v string) []Element { return []Element{{Type: TypeText, Text: "x", Align: v}} }},
|
|
{"Correction", "correction", func(v string) []Element {
|
|
return []Element{{Type: TypeQR, Data: "x", Correction: v}}
|
|
}},
|
|
{"Symbology", "symbology", func(v string) []Element {
|
|
return []Element{{Type: TypeBarcode, Data: "0", Symbology: v}}
|
|
}},
|
|
{"HRI", "hri", func(v string) []Element {
|
|
return []Element{{Type: TypeBarcode, Data: "x", Symbology: "code128", HRI: v}}
|
|
}},
|
|
{"Mode", "mode", func(v string) []Element { return []Element{{Type: TypeCut, Mode: v}} }},
|
|
{"TitleStyle", "title_style", func(v string) []Element {
|
|
return []Element{{Type: TypeBox, Text: "x", Title: "t", TitleStyle: v}}
|
|
}},
|
|
{"Style", "style", func(v string) []Element {
|
|
return []Element{{Type: TypeBox, Text: "x", Style: v}, {Type: TypeRule, Style: v}}
|
|
}},
|
|
{"Leader", "leader", func(v string) []Element {
|
|
return []Element{{Type: TypeKV, Pairs: []Pair{{Key: "k", Value: "v"}}, Leader: v}}
|
|
}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.field, func(t *testing.T) {
|
|
accepted := func(v string) bool {
|
|
for _, el := range tt.elements(v) {
|
|
if len(problemsWith(t, el, tt.json)) == 0 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
for _, v := range enumTag(t, tt.field) {
|
|
if !accepted(v) {
|
|
t.Errorf("%s %q is in the enum but validation refuses it", tt.json, v)
|
|
}
|
|
}
|
|
if accepted("not-a-value") {
|
|
t.Errorf("%s accepts a value outside its enum", tt.json)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// tagBound returns the number after key= in the jsonschema tag of field on
|
|
// typ, e.g. maximum=5.
|
|
func tagBound(t *testing.T, typ reflect.Type, field, key string) (int, bool) {
|
|
t.Helper()
|
|
f, ok := typ.FieldByName(field)
|
|
if !ok {
|
|
t.Fatalf("%s has no field %s", typ.Name(), field)
|
|
}
|
|
for part := range strings.SplitSeq(f.Tag.Get("jsonschema"), ",") {
|
|
if v, ok := strings.CutPrefix(part, key+"="); ok {
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
t.Fatalf("%s.%s: %s=%q is not a number", typ.Name(), field, key, v)
|
|
}
|
|
return n, true
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
// Each numeric bound in the jsonschema tags is a limit validation enforces:
|
|
// a job at the bound passes, and one just beyond it fails with a problem
|
|
// naming the field.
|
|
func TestBoundTagsMatchValidation(t *testing.T) {
|
|
text := Element{Type: TypeText, Text: "x"}
|
|
kv := func(pairs int, key string) Element {
|
|
el := Element{Type: TypeKV}
|
|
for range pairs {
|
|
el.Pairs = append(el.Pairs, Pair{Key: key, Value: "v"})
|
|
}
|
|
return el
|
|
}
|
|
one := func(el Element) Job { return Job{Elements: []Element{el}} }
|
|
tests := []struct {
|
|
typ reflect.Type
|
|
field, json string
|
|
lower, upper string // the tag keys of the bounds
|
|
job func(n int) Job
|
|
}{
|
|
{reflect.TypeFor[Job](), "Copies", "copies", "minimum", "maximum",
|
|
func(n int) Job { return Job{Elements: []Element{text}, Copies: n} }},
|
|
{reflect.TypeFor[Job](), "Elements", "elements", "minItems", "maxItems",
|
|
func(n int) Job { return Job{Elements: slices.Repeat([]Element{text}, n)} }},
|
|
{reflect.TypeFor[Element](), "Size", "size", "minimum", "maximum",
|
|
func(n int) Job { return one(Element{Type: TypeQR, Data: "x", Size: n}) }},
|
|
{reflect.TypeFor[Element](), "Height", "height", "minimum", "maximum",
|
|
func(n int) Job { return one(Element{Type: TypeBarcode, Symbology: "code128", Data: "x", Height: n}) }},
|
|
{reflect.TypeFor[Element](), "Underline", "underline", "minimum", "maximum",
|
|
func(n int) Job { return one(Element{Type: TypeText, Text: "x", Underline: n}) }},
|
|
{reflect.TypeFor[Element](), "Lines", "lines", "minimum", "maximum",
|
|
func(n int) Job { return one(Element{Type: TypeFeed, Lines: n}) }},
|
|
{reflect.TypeFor[Element](), "Padding", "padding", "minimum", "maximum",
|
|
func(n int) Job { return one(Element{Type: TypeBox, Text: "x", Padding: &n}) }},
|
|
{reflect.TypeFor[Element](), "Columns", "columns", "minimum", "maximum",
|
|
func(n int) Job { return one(Element{Type: TypeBox, Text: "x", Columns: n}) }},
|
|
{reflect.TypeFor[Element](), "Pairs", "pairs", "minItems", "maxItems",
|
|
func(n int) Job { return one(kv(n, "k")) }},
|
|
{reflect.TypeFor[Pair](), "Key", "key", "minLength", "",
|
|
func(n int) Job { return one(kv(1, strings.Repeat("k", n))) }},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.json, func(t *testing.T) {
|
|
check := func(key string, at, beyond int) {
|
|
if err := tt.job(at).Validate(); err != nil {
|
|
t.Errorf("%s=%d at the %s bound fails validation: %v", tt.json, at, key, err)
|
|
}
|
|
err := tt.job(beyond).Validate()
|
|
if err == nil || !strings.Contains(err.Error(), tt.json) {
|
|
t.Errorf("%s=%d beyond the %s bound: Validate = %v, want a problem naming %s",
|
|
tt.json, beyond, key, err, tt.json)
|
|
}
|
|
}
|
|
if n, ok := tagBound(t, tt.typ, tt.field, tt.lower); ok {
|
|
check(tt.lower, n, n-1)
|
|
} else {
|
|
t.Errorf("%s has no %s tag", tt.json, tt.lower)
|
|
}
|
|
if tt.upper == "" {
|
|
return
|
|
}
|
|
if n, ok := tagBound(t, tt.typ, tt.field, tt.upper); ok {
|
|
check(tt.upper, n, n+1)
|
|
} else {
|
|
t.Errorf("%s has no %s tag", tt.json, tt.upper)
|
|
}
|
|
})
|
|
}
|
|
}
|