The list block prints items with a dot, dash, number or checkbox bullet,
wrapping a long item with a hanging indent; a checkbox list ticks its
done items as [√], or [x] on code pages without √. It stands alone or
inside a box, and its items, bullet and done fields carry their enums
and limits as jsonschema tags.
internal/pkg/modules holds modules that build ordinary jobs from their
own payloads: a registry by name, an Env with the clock, time zone and
time layout, strict payload decoding that reports every problem, ErrSkip
and PayloadError. The note module prints a boxed note with an optional
checklist and a time stamp in the footer.
Three routes join the route table: POST /api/v1/print/{module} and
POST /api/v1/preview/{module}, which take ?copies and a reserved
?printer, and GET /api/v1/modules. Print and the module route share one
submit path, and preview shares one too. New error codes:
invalid_payload, invalid_query, unknown_printer, module_not_found and
module_failed; a module that builds an invalid receipt is logged and
reported to Sentry once. [format] gains timezone and time_layout, the
binary embeds the time zone database, and uberbringer preview takes
--module.
The note receipt passed on the TM-T88IV, and its bytes are pinned in
TestPaperReceipts. docs/api.md describes the list block, the module
routes and the note payload; docs/swagger.json is regenerated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
3.3 KiB
Go
106 lines
3.3 KiB
Go
package modules_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"uberbringer/internal/pkg/modules"
|
|
"uberbringer/internal/pkg/receipt"
|
|
)
|
|
|
|
// stubModule is a module that prints one line of text.
|
|
type stubModule struct{ name, description string }
|
|
|
|
func (m stubModule) Name() string { return m.name }
|
|
func (m stubModule) Description() string { return m.description }
|
|
|
|
func (m stubModule) Build(context.Context, []byte, modules.Env) (receipt.Job, error) {
|
|
return receipt.Job{Elements: []receipt.Element{{Type: "text", Text: m.name}}}, nil
|
|
}
|
|
|
|
func TestRegistryLookup(t *testing.T) {
|
|
r, err := modules.NewRegistry(stubModule{"b", "B"}, modules.Note(), stubModule{"a-2", "A"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m, ok := r.Get("note")
|
|
if !ok || m.Name() != "note" {
|
|
t.Errorf("Get(note) = %v, %v", m, ok)
|
|
}
|
|
for _, name := range []string{"Note", "nope", ""} {
|
|
if _, ok := r.Get(name); ok {
|
|
t.Errorf("Get(%q) found a module", name)
|
|
}
|
|
}
|
|
got := r.List()
|
|
want := []modules.Info{{"a-2", "A"}, {"b", "B"}, {"note", "Boxed note with an optional checklist"}}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("List() = %v, want %v", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Errorf("List()[%d] = %v, want %v", i, got[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRegistryListIsNeverNil(t *testing.T) {
|
|
r, err := modules.NewRegistry()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := r.List(); got == nil || len(got) != 0 {
|
|
t.Errorf("List() = %#v, want an empty list", got)
|
|
}
|
|
}
|
|
|
|
func TestRegistryRefusals(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mods []modules.Module
|
|
wantErr string
|
|
}{
|
|
{"duplicate name", []modules.Module{stubModule{"note", ""}, modules.Note()}, `module "note" is registered twice`},
|
|
{"upper case", []modules.Module{stubModule{"Note", ""}}, `module name "Note"`},
|
|
{"empty", []modules.Module{stubModule{"", ""}}, `module name ""`},
|
|
{"slash", []modules.Module{stubModule{"a/b", ""}}, `module name "a/b"`},
|
|
{"leading digit", []modules.Module{stubModule{"1a", ""}}, `module name "1a"`},
|
|
{"too long", []modules.Module{stubModule{strings.Repeat("a", 33), ""}}, "module name"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := modules.NewRegistry(tt.mods...)
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Errorf("err = %v, want it to contain %q", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnvStamp(t *testing.T) {
|
|
london, err := time.LoadLocation("Europe/London")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
summer := time.Date(2026, 7, 14, 20, 4, 5, 0, time.UTC)
|
|
tests := []struct {
|
|
env modules.Env
|
|
want string
|
|
}{
|
|
{modules.Env{Now: summer, Location: london, TimeLayout: "02 Jan 15:04"}, "14 Jul 21:04"},
|
|
{modules.Env{Now: summer, Location: time.UTC, TimeLayout: "02 Jan 15:04"}, "14 Jul 20:04"},
|
|
{modules.Env{Now: summer, Location: london, TimeLayout: "2006-01-02 15:04 MST"}, "2026-07-14 21:04 BST"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := tt.env.Stamp(); got != tt.want {
|
|
t.Errorf("Stamp() in %v with %q = %q, want %q", tt.env.Location, tt.env.TimeLayout, got, tt.want)
|
|
}
|
|
}
|
|
local := modules.Env{Now: summer, TimeLayout: time.RFC3339}
|
|
if got, want := local.Stamp(), summer.In(time.Local).Format(time.RFC3339); got != want {
|
|
t.Errorf("Stamp() without a location = %q, want local time %q", got, want)
|
|
}
|
|
}
|