package ogcard
import (
"bytes"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/idn"
)
func sample(name, owner, desc string) Card {
return Card{Owner: owner, Name: name, Description: desc,
Stats: []Stat{{21, "check-in", "check-ins"}, {2, "contributor", "contributors"}, {3, "open ticket", "open tickets"}},
Grid: idn.Grid(name), Color: ParseHex(idn.Hex(name, 0.66)), Site: "zeitforge.app"}
}
// A card is a 1200x630 PNG whatever its text: a name too long to fit
// shrinks and is cut, a description too long is cut at three lines.
func TestACardIsTheUnfurlSize(t *testing.T) {
for _, c := range []Card{
sample("pikchr-studio", "dmestas", "Canvas authoring for Pikchr diagrams, where the Pikchr source stays canonical"),
sample(strings.Repeat("very-long-repository-name-", 4), "", ""),
func() Card {
c := sample("with-avatar", "dmestas", "A repository with an uploaded picture")
a := image.NewRGBA(image.Rect(0, 0, 64, 64))
for y := 0; y < 64; y++ {
for x := 0; x < 64; x++ {
a.Set(x, y, color.RGBA{uint8(x * 4), 120, uint8(y * 4), 0xff})
}
}
c.Avatar = a
return c
}(),
sample("halation", "crustyrustacean", strings.Repeat("An early-Instagram-style photo sharing server, chronological and calm. ", 8)),
} {
b, err := Render(c)
if err != nil {
t.Fatal(err)
}
img, err := png.Decode(bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
if r := img.Bounds(); r.Dx() != W || r.Dy() != H {
t.Errorf("FAIL: %s is %v", c.Name, r)
}
if dir := os.Getenv("OGCARD_OUT"); dir != "" {
os.WriteFile(filepath.Join(dir, c.Name[:min(len(c.Name), 20)]+".png"), b, 0o644)
}
}
}