aboutsummaryrefslogtreecommitdiff
path: root/internal/validate/grid_test.go
blob: 9a8ef02ee921c4555011c4946d649b31131dfb68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package validate

import (
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testing"

	"thehouseoficarus/internal/world"
)

func writeGridRoom(t *testing.T, dir string, id int, body string) {
	t.Helper()
	rooms := filepath.Join(dir, "rooms")
	if err := os.MkdirAll(rooms, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(rooms, strconv.Itoa(id)+".yaml"), []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
}

func runGridCheck(t *testing.T, rooms map[int]string, roots ...int) []Issue {
	t.Helper()
	dir := t.TempDir()
	for id, body := range rooms {
		writeGridRoom(t, dir, id, body)
	}
	return validateRoomGrid(Source{World: world.New(dir), RootRooms: roots})
}

func containsMsg(issues []Issue, substr string) bool {
	for _, iss := range issues {
		if strings.Contains(iss.Message, substr) {
			return true
		}
	}
	return false
}

func TestGridCleanLayout(t *testing.T) {
	// A consistent 2x2 block: 4 is reached the same way from 2 and from 3.
	rooms := map[int]string{
		1: "exits:\n  south: 3\n  east: 2\n",
		2: "exits:\n  south: 4\n",
		3: "exits:\n  east: 4\n",
		4: "name: corner\n",
	}
	if issues := runGridCheck(t, rooms, 1); len(issues) != 0 {
		t.Errorf("expected no grid issues, got: %+v", issues)
	}
}

func TestGridOverlap(t *testing.T) {
	// rooms 4 and 5 both resolve to grid (1,1).
	rooms := map[int]string{
		1: "exits:\n  south: 3\n  east: 2\n",
		2: "exits:\n  south: 5\n",
		3: "exits:\n  east: 4\n",
		4: "name: four\n",
		5: "name: five\n",
	}
	issues := runGridCheck(t, rooms, 1)
	if !containsMsg(issues, "Grid overlap") {
		t.Errorf("expected a grid overlap, got: %+v", issues)
	}
	for _, iss := range issues {
		if iss.Level != "ERROR" {
			t.Errorf("grid issues should be ERROR, got %q", iss.Level)
		}
	}
}

func TestGridTwist(t *testing.T) {
	// room 3 is forced onto two different cells.
	rooms := map[int]string{
		1: "exits:\n  south: 4\n  east: 2\n",
		2: "exits:\n  east: 3\n",
		4: "exits:\n  east: 3\n",
		3: "name: three\n",
	}
	issues := runGridCheck(t, rooms, 1)
	if !containsMsg(issues, "Grid twist") {
		t.Errorf("expected a grid twist, got: %+v", issues)
	}
}

func TestGridMultiPlaneViaUpDown(t *testing.T) {
	// Plane A is just room 1. Going up seeds plane B (origin 10), which has an
	// overlap. This proves up/down crosses planes and frames are independent
	// (room 1 and room 10 both sit at (0,0) without conflicting).
	rooms := map[int]string{
		1:  "exits:\n  up: 10\n",
		10: "exits:\n  south: 12\n  east: 11\n",
		11: "exits:\n  south: 14\n",
		12: "exits:\n  east: 13\n",
		13: "name: thirteen\n",
		14: "name: fourteen\n",
	}
	issues := runGridCheck(t, rooms, 1)
	if !containsMsg(issues, "plane origin 10") {
		t.Errorf("expected an overlap in plane origin 10, got: %+v", issues)
	}
}