aboutsummaryrefslogtreecommitdiff
path: root/internal/game/map_test.go
blob: dded62e91303a8e94a413608651ae708a06439c8 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package game

import (
	"strings"
	"testing"

	"thehouseoficarus/internal/color"
	"thehouseoficarus/internal/world"
)

func TestBuildTinyMap(t *testing.T) {
	g := &Game{
		World: world.New("../../data"),
	}

	mg := mapGlyphsForPlayer(true)

	tests := []struct {
		name   string
		roomID int
		want   []string // expected lines, or nil to just check count
	}{
		{
			name:   "room 999991 has east exit",
			roomID: 999991,
		},
		{
			name:   "room 999992 has west/north/east",
			roomID: 999992,
		},
		{
			name:   "room 999994 has west/north/east",
			roomID: 999994,
		},
		{
			name:   "room 9999921 has west only",
			roomID: 9999921,
		},
		{
			name:   "room 9999922 west of town square",
			roomID: 9999922,
		},
		{
			name:   "room 9999925 east of south of west",
			roomID: 9999925,
		},
		{
			name:   "room 999998 hacking lab",
			roomID: 999998,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			lines := buildTinyMap(g, nil, tt.roomID, mg)
			if len(lines) != 7 {
				t.Fatalf("expected 7 lines, got %d", len(lines))
			}
			if lines[0] != "╔═════╗" {
				t.Errorf("line 0: want ╔═════╗, got %s", lines[0])
			}
			if lines[6] != "╚═════╝" {
				t.Errorf("line 6: want ╚═════╝, got %s", lines[6])
			}
			for i := 1; i <= 5; i++ {
				if !strings.HasPrefix(lines[i], "║") || !strings.HasSuffix(lines[i], "║") {
					t.Errorf("line %d: should have ║ borders, got %s", i, lines[i])
				}
				if color.VisibleLen(lines[i]) != 7 {
					t.Errorf("line %d: expected 7 visible runes, got %d in %q", i, color.VisibleLen(lines[i]), lines[i])
				}
			}
			t.Logf("Room %d map:\n%s", tt.roomID, strings.Join(lines, "\n"))
		})
	}
}

func TestWrapText(t *testing.T) {
	tests := []struct {
		text  string
		width int
		want  int // expected number of lines
	}{
		{"hello world", 70, 1},
		{"hello world", 5, 2},
		{"", 70, 0},
		{"a b c d e f g h i j", 5, 4},
	}

	for _, tt := range tests {
		result := wrapText(tt.text, tt.width)
		if len(result) != tt.want {
			t.Errorf("wrapText(%q, %d) = %d lines, want %d: %v", tt.text, tt.width, len(result), tt.want, result)
		}
	}
}

func TestBuildFullMap(t *testing.T) {
	g := &Game{
		World: world.New("../../data"),
	}

	mg := mapGlyphsForPlayer(true)

	tests := []struct {
		name   string
		roomID int
		width  int
		height int
	}{
		{"room 999991 30x20", 999991, 30, 20},
		{"room 999991 20x10", 999991, 20, 10},
		{"room 9999925 30x20", 9999925, 30, 20},
		{"min size 5x5", 999991, 5, 5},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			lines := buildFullMap(g, nil, tt.roomID, tt.width, tt.height, mg)
			if len(lines) != tt.height {
				t.Errorf("expected %d lines, got %d", tt.height, len(lines))
			}
			for i, line := range lines {
				if color.VisibleLen(line) != tt.width {
					t.Errorf("line %d: expected %d visible runes, got %d in %q", i, tt.width, color.VisibleLen(line), line)
				}
			}
			t.Logf("Room %d %dx%d map:\n%s", tt.roomID, tt.width, tt.height, strings.Join(lines, "\n"))
		})
	}
}

func TestStripBlankRows(t *testing.T) {
	input := []string{"  ", "hello", "", "  world  ", "   "}
	want := []string{"hello", "  world  "}
	got := stripBlankRows(input)
	if len(got) != len(want) {
		t.Fatalf("expected %d lines, got %d", len(want), len(got))
	}
	for i := range want {
		if got[i] != want[i] {
			t.Errorf("line %d: want %q, got %q", i, want[i], got[i])
		}
	}
}

func TestLeftTrimCommon(t *testing.T) {
	input := []string{"    hello", "    world", "  ", "  foo"}
	got := leftTrimCommon(input)
	// min leading spaces across non-blank lines: "    hello"=4, "    world"=4, "  foo"=2 → min=2
	// trim 2 from all lines
	if got[0] != "  hello" {
		t.Errorf("line 0: want %q, got %q", "  hello", got[0])
	}
	if got[1] != "  world" {
		t.Errorf("line 1: want %q, got %q", "  world", got[1])
	}
	if got[2] != "" {
		t.Errorf("line 2: want %q, got %q", "", got[2])
	}
	if got[3] != "foo" {
		t.Errorf("line 3: want %q, got %q", "foo", got[3])
	}
}

func TestLeftTrimCommonEmpty(t *testing.T) {
	input := []string{"   ", "", "   "}
	got := leftTrimCommon(input)
	if len(got) != 3 {
		t.Fatalf("expected 3 lines, got %d", len(got))
	}
}

func TestLeftTrimCommonZero(t *testing.T) {
	input := []string{"hello", "world"}
	got := leftTrimCommon(input)
	if got[0] != "hello" {
		t.Errorf("line 0: want %q, got %q", "hello", got[0])
	}
	if got[1] != "world" {
		t.Errorf("line 1: want %q, got %q", "world", got[1])
	}
}