The canvas is centred, so on wide screens there is margin to its left that boxes could not be dragged into: x was clamped to 0 in the drag geometry, the arrow-key nudge and validateBox. Only y stays clamped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
49 lines
1.9 KiB
Go
49 lines
1.9 KiB
Go
package graph
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"adam-french.co.uk/backend/models"
|
|
)
|
|
|
|
func TestValidateBox(t *testing.T) {
|
|
str := func(s string) *string { return &s }
|
|
one := uint(1)
|
|
|
|
for _, c := range []struct {
|
|
name string
|
|
box models.Box
|
|
wantErr bool
|
|
}{
|
|
{"widget with a name", models.Box{Page: "stp", Kind: "widget", Widget: str("Chat")}, false},
|
|
{"widget without a name", models.Box{Page: "stp", Kind: "widget"}, true},
|
|
{"widget with an empty name", models.Box{Page: "stp", Kind: "widget", Widget: str("")}, true},
|
|
{"html with html", models.Box{Page: "stp", Kind: "html", HTML: str("<p>hi</p>")}, false},
|
|
{"html with empty html", models.Box{Page: "stp", Kind: "html", HTML: str("")}, false},
|
|
{"html without html", models.Box{Page: "stp", Kind: "html"}, true},
|
|
{"linked html without html", models.Box{Page: "stp", Kind: "html", ComponentID: &one}, false},
|
|
{"linked widget", models.Box{Page: "stp", Kind: "widget", Widget: str("Chat"), ComponentID: &one}, true},
|
|
{"no page", models.Box{Kind: "widget", Widget: str("Chat")}, true},
|
|
{"bad page", models.Box{Page: "Shrines/GTO", Kind: "widget", Widget: str("Chat")}, true},
|
|
{"unknown kind", models.Box{Page: "stp", Kind: "iframe", HTML: str("x")}, true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
err := validateBox(&c.box)
|
|
if (err != nil) != c.wantErr {
|
|
t.Fatalf("err = %v, wantErr %v", err, c.wantErr)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Geometry is clamped, not rejected: x may go negative, into the margin
|
|
// left of a centred canvas, but nothing goes above the top or below a
|
|
// grabbable size.
|
|
box := models.Box{Page: "stp", Kind: "widget", Widget: str("Chat"), X: -30, Y: -1, W: 5, H: 0}
|
|
if err := validateBox(&box); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if box.X != -30 || box.Y != 0 || box.W != minBoxSize || box.H != minBoxSize {
|
|
t.Fatalf("clamped to %d,%d %dx%d; want -30,0 %dx%d", box.X, box.Y, box.W, box.H, minBoxSize, minBoxSize)
|
|
}
|
|
}
|