package modules_test import ( "context" "encoding/json" "errors" "fmt" "os" "path/filepath" "strings" "testing" "time" "unicode/utf8" "uberbringer/internal/pkg/escpos" "uberbringer/internal/pkg/modules" "uberbringer/internal/pkg/ntfy" "uberbringer/internal/pkg/receipt" ) // TestNtfyBuildsThePaperJobs checks that the ntfy module still builds the // jobs whose printed result was checked on the TM-T88IV, and whose bytes // the receipt package pins: the messages in testdata/paper, built on a // host at UTC-6 with the default card options. func TestNtfyBuildsThePaperJobs(t *testing.T) { for _, name := range []string{"high", "urgent", "default", "min", "cutfeed"} { t.Run(name, func(t *testing.T) { body, err := os.ReadFile(filepath.Join("testdata", "paper", "ntfy-"+name+".json")) if err != nil { t.Fatal(err) } env := modules.Env{ Now: time.Unix(1796063393, 0), Location: time.FixedZone("UTC-6", -6*60*60), TimeLayout: "02 Jan 15:04", } job, err := modules.Ntfy().Build(context.Background(), body, env) if err != nil { t.Fatal(err) } data, err := os.ReadFile(filepath.Join("..", "receipt", "testdata", "paper", "ntfy-"+name+".json")) if err != nil { t.Fatal(err) } paper, err := receipt.DecodeJob(data) if err != nil { t.Fatal(err) } got, err := json.Marshal(job) if err != nil { t.Fatal(err) } want, err := json.Marshal(paper) if err != nil { t.Fatal(err) } if string(got) != string(want) { t.Errorf("the module builds\n%s\nbut the job checked on paper is\n%s", got, want) } }) } } // ntfyEnv is the time the example ntfy messages were sent, 29 Nov 2025 // 21:04:05 UTC, in Europe/London, which is on GMT then. func ntfyEnv(t *testing.T) modules.Env { t.Helper() env := fixedEnv(t) env.Now = time.Unix(1764450245, 0) return env } // checkGolden compares got with the file at path, or rewrites the file // with -update. func checkGolden(t *testing.T, path, got string) { t.Helper() if *update { if err := os.WriteFile(path, []byte(got), 0o644); err != nil { t.Fatal(err) } return } want, err := os.ReadFile(path) if err != nil { t.Fatalf("%v (run go test -run %s -update)", err, strings.Split(t.Name(), "/")[0]) } if got != string(want) { t.Errorf("preview differs from %s:\ngot:\n%s\nwant:\n%s", path, got, want) } } // buildGoldens builds each payload in testdata/DIR/*.json with module and // compares its preview with NAME.txt. func buildGoldens(t *testing.T, dir string, module modules.Module, env modules.Env) { t.Helper() paths, err := filepath.Glob(filepath.Join("testdata", dir, "*.json")) if err != nil { t.Fatal(err) } if len(paths) == 0 { t.Fatalf("no payloads in testdata/%s", dir) } for _, path := range paths { name := strings.TrimSuffix(filepath.Base(path), ".json") t.Run(name, func(t *testing.T) { body, err := os.ReadFile(path) if err != nil { t.Fatal(err) } job, err := module.Build(context.Background(), body, env) if err != nil { t.Fatalf("Build: %v", err) } if err := job.Validate(); err != nil { t.Fatalf("built an invalid job: %v", err) } checkGolden(t, filepath.Join("testdata", dir, name+".txt"), previewText(t, job)) }) } } // TestNtfyGoldens previews each testdata/ntfy/*.json message at the time // it was sent. Run with -update to rewrite the .txt files, then review the // diff. func TestNtfyGoldens(t *testing.T) { buildGoldens(t, "ntfy", modules.Ntfy(), ntfyEnv(t)) } // TestNotifyGoldens previews each testdata/notify/*.json payload. func TestNotifyGoldens(t *testing.T) { buildGoldens(t, "notify", modules.Notify(), fixedEnv(t)) } // TestCardStyles previews the example message at every priority in every // style, in testdata/card/p-