- Introduced .editorconfig for consistent coding styles across the project. - Added .golangci.yml for Go linting configuration. - Updated AGENTS.md to clarify project structure and components. - Enhanced CONTRIBUTING.md with Makefile usage for common tasks. - Updated Dockerfiles to use Go 1.24 and improved build instructions. - Refined README.md and deployment documentation for clarity. - Added testing documentation in testing.md for backend and frontend tests. - Introduced Makefile for streamlined development commands and tasks.
109 lines
2.3 KiB
Go
109 lines
2.3 KiB
Go
package app_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/andyleap/hnh-map/internal/app"
|
|
)
|
|
|
|
func TestCoordName(t *testing.T) {
|
|
tests := []struct {
|
|
coord app.Coord
|
|
want string
|
|
}{
|
|
{app.Coord{X: 0, Y: 0}, "0_0"},
|
|
{app.Coord{X: 5, Y: -3}, "5_-3"},
|
|
{app.Coord{X: -1, Y: -1}, "-1_-1"},
|
|
}
|
|
for _, tt := range tests {
|
|
got := tt.coord.Name()
|
|
if got != tt.want {
|
|
t.Errorf("Coord{%d,%d}.Name() = %q, want %q", tt.coord.X, tt.coord.Y, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCoordParent(t *testing.T) {
|
|
tests := []struct {
|
|
coord app.Coord
|
|
parent app.Coord
|
|
}{
|
|
{app.Coord{X: 0, Y: 0}, app.Coord{X: 0, Y: 0}},
|
|
{app.Coord{X: 2, Y: 4}, app.Coord{X: 1, Y: 2}},
|
|
{app.Coord{X: 3, Y: 5}, app.Coord{X: 1, Y: 2}},
|
|
{app.Coord{X: -1, Y: -1}, app.Coord{X: -1, Y: -1}},
|
|
{app.Coord{X: -2, Y: -3}, app.Coord{X: -1, Y: -2}},
|
|
}
|
|
for _, tt := range tests {
|
|
got := tt.coord.Parent()
|
|
if got != tt.parent {
|
|
t.Errorf("Coord{%d,%d}.Parent() = %v, want %v", tt.coord.X, tt.coord.Y, got, tt.parent)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAuthsHas(t *testing.T) {
|
|
auths := app.Auths{"admin", "map", "upload"}
|
|
|
|
if !auths.Has("admin") {
|
|
t.Error("expected Has(admin)=true")
|
|
}
|
|
if !auths.Has("map") {
|
|
t.Error("expected Has(map)=true")
|
|
}
|
|
if auths.Has("markers") {
|
|
t.Error("expected Has(markers)=false")
|
|
}
|
|
}
|
|
|
|
func TestAuthsHasEmpty(t *testing.T) {
|
|
var auths app.Auths
|
|
if auths.Has("anything") {
|
|
t.Error("expected nil auths to return false")
|
|
}
|
|
}
|
|
|
|
func TestTopicSendAndWatch(t *testing.T) {
|
|
topic := &app.Topic[string]{}
|
|
ch := make(chan *string, 10)
|
|
topic.Watch(ch)
|
|
|
|
msg := "hello"
|
|
topic.Send(&msg)
|
|
|
|
select {
|
|
case got := <-ch:
|
|
if *got != "hello" {
|
|
t.Fatalf("expected hello, got %s", *got)
|
|
}
|
|
default:
|
|
t.Fatal("expected message on channel")
|
|
}
|
|
}
|
|
|
|
func TestTopicClose(t *testing.T) {
|
|
topic := &app.Topic[int]{}
|
|
ch := make(chan *int, 10)
|
|
topic.Watch(ch)
|
|
topic.Close()
|
|
|
|
_, ok := <-ch
|
|
if ok {
|
|
t.Fatal("expected channel to be closed")
|
|
}
|
|
}
|
|
|
|
func TestTopicDropsSlowSubscriber(t *testing.T) {
|
|
topic := &app.Topic[int]{}
|
|
slow := make(chan *int) // unbuffered, will block
|
|
topic.Watch(slow)
|
|
|
|
val := 42
|
|
topic.Send(&val) // should drop the slow subscriber
|
|
|
|
_, ok := <-slow
|
|
if ok {
|
|
t.Fatal("expected slow subscriber channel to be closed")
|
|
}
|
|
}
|