The notify module names the problems of an action, an object inside its payload, by the action's place in the payload. The module routes need the same for a payload sent inside a larger request body, so the naming becomes a method of PayloadError. What notify reports does not change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
3.9 KiB
Go
124 lines
3.9 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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A payload's problems name their field from the place that holds the
|
|
// payload, and a problem with the whole payload names that place.
|
|
func TestPayloadErrorWithin(t *testing.T) {
|
|
err := &modules.PayloadError{Problems: []string{
|
|
"body: expected a JSON object, got array",
|
|
"done[0]: index 5 is out of range",
|
|
`"we ird": unknown field`,
|
|
}}
|
|
want := []string{
|
|
"payload: expected a JSON object, got array",
|
|
"payload.done[0]: index 5 is out of range",
|
|
`payload."we ird": unknown field`,
|
|
}
|
|
if got := err.Within("payload"); strings.Join(got, "\n") != strings.Join(want, "\n") {
|
|
t.Errorf("Within = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|