Files
uberbringer/internal/pkg/raster/raster_test.go
T
57_WolveandClaude Opus 5.5 9b91109550
Test / Lint, test and build (push) Successful in 3m17s
Adopt norn's CI, release, API and docs practices; bare module path
Module
- The module path is now the bare "uberbringer"; imports read
  uberbringer/internal/... The banner keeps the Gitea URL.

CI and releases
- Workflows move to .gitea/workflows: test.yml (every branch push:
  gofmt, goimports, tidy, vet, staticcheck, race tests, goreleaser
  snapshot), security.yml (govulncheck weekly and on main) and a hardened
  release.yml (v* tags on main only, tags fetched, goreleaser pinned to
  2.20.3). The broken labeler workflow is removed.
- go.mod pins toolchain go1.27.4, which fixes GO-2026-6859; workflows
  check that the running Go matches it.
- .goreleaser.yaml: tidy check that never writes, reproducible file times,
  prerelease: auto, a release header linking CHANGELOG.md, and archives
  that ship CHANGELOG.md, the example config and the systemd unit.
- Makefile mirrors the CI gate; .dockerignore is an allowlist.

API and tests
- Every error answers {"error", "code", "details"} with a stable code from
  server/errors.go; the framework's own error text is never sent.
- Panics and unexpected errors are logged and reported to Sentry once;
  refusals are not logged as failures. /healthz sends no-store.
- TestRoutesConform requires auth, rate limiting and the body limit on
  every /api/v1 route.
- Race-prone tests no longer rely on freed ports or sleeps; serve takes
  an injected listener.
- The test config uses a placeholder secret instead of a real one.

Install and packaging
- install.sh resolves the latest release through the Gitea API, verifies
  checksums, requests a certificate only when none exists, installs the
  hardened packaging/uberbringer.service and writes the first token to a
  0600 file instead of printing it. It is now executable in git.

Docs
- CLAUDE.md, CHANGELOG.md (0.2.0 with breaking changes and upgrade
  steps), docs/design.md, and docs/plans/formatters.md for the planned
  CP437 blocks, modules and ntfy printing. README updated throughout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 23:27:48 -06:00

134 lines
3.1 KiB
Go

package raster_test
import (
"bytes"
"errors"
"image"
"image/color"
"image/png"
"testing"
"uberbringer/internal/pkg/raster"
)
func encodePNG(t *testing.T, img image.Image) []byte {
t.Helper()
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func gradient(w, h int) *image.Gray {
img := image.NewGray(image.Rect(0, 0, w, h))
for y := range h {
for x := range w {
img.SetGray(x, y, color.Gray{Y: uint8(x * 255 / max(1, w-1))})
}
}
return img
}
func TestDecode(t *testing.T) {
data := encodePNG(t, gradient(40, 20))
img, err := raster.Decode(data, 1000)
if err != nil {
t.Fatal(err)
}
if img.Bounds().Dx() != 40 || img.Bounds().Dy() != 20 {
t.Errorf("bounds = %v", img.Bounds())
}
}
func TestDecodeRejectsOversizedBeforeDecoding(t *testing.T) {
data := encodePNG(t, gradient(40, 20))
if _, err := raster.Decode(data, 799); !errors.Is(err, raster.ErrTooLarge) {
t.Errorf("err = %v, want ErrTooLarge", err)
}
}
func TestDecodeRejectsGarbage(t *testing.T) {
if _, err := raster.Decode([]byte("not an image"), 1000); err == nil {
t.Error("expected an error")
}
}
func TestMonochromeScalesAndKeepsAspect(t *testing.T) {
out, err := raster.Monochrome(gradient(1152, 400), 576)
if err != nil {
t.Fatal(err)
}
if got := out.Bounds(); got.Dx() != 576 || got.Dy() != 200 {
t.Errorf("bounds = %v, want 576x200", got)
}
}
func TestMonochromeIsBlackAndWhiteAndDithers(t *testing.T) {
out, err := raster.Monochrome(gradient(200, 50), 200)
if err != nil {
t.Fatal(err)
}
if len(out.Palette) != 2 {
t.Fatalf("palette has %d colours, want 2", len(out.Palette))
}
// A dither of a left-to-right gradient has mixed pixels in the middle,
// unlike a hard threshold which would split it into two solid halves.
black := 0
for y := range 50 {
for x := 90; x < 110; x++ {
if out.ColorIndexAt(x, y) == 1 {
black++
}
}
}
if total := 50 * 20; black == 0 || black == total {
t.Errorf("mid-gradient has %d/%d black pixels, want a mix", black, total)
}
}
func TestMonochromeTransparentBecomesWhite(t *testing.T) {
src := image.NewNRGBA(image.Rect(0, 0, 16, 16)) // fully transparent
out, err := raster.Monochrome(src, 16)
if err != nil {
t.Fatal(err)
}
for i, idx := range out.Pix {
if idx != 0 {
t.Fatalf("pixel %d is black, want white paper", i)
}
}
}
func TestMonochromeInvalidWidth(t *testing.T) {
if _, err := raster.Monochrome(gradient(10, 10), 0); !errors.Is(err, raster.ErrInvalidSize) {
t.Errorf("err = %v, want ErrInvalidSize", err)
}
}
func TestPlace(t *testing.T) {
img, err := raster.Monochrome(image.NewGray(image.Rect(0, 0, 8, 2)), 8) // all black
if err != nil {
t.Fatal(err)
}
out, err := raster.Place(img, 24, 8)
if err != nil {
t.Fatal(err)
}
if out.Bounds().Dx() != 24 {
t.Fatalf("width = %d", out.Bounds().Dx())
}
for x := range 24 {
want := uint8(0)
if x >= 8 && x < 16 {
want = 1
}
if got := out.ColorIndexAt(x, 0); got != want {
t.Errorf("x=%d index=%d, want %d", x, got, want)
}
}
if _, err := raster.Place(img, 12, 8); !errors.Is(err, raster.ErrInvalidSize) {
t.Errorf("overflowing place err = %v", err)
}
}