aboutsummaryrefslogtreecommitdiff
path: root/internal/validate/p2_hardening_test.go
blob: 68ae48fa68116fe110e8332849ac320018c6f0c4 (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
package validate

import (
	"path/filepath"
	"testing"
)

// TestValidateRejectsNonPositiveExit: an exit with room <= 0 renders as a
// blocked direction but passes every other check, so it must be reported
// unless always_blocked: true expresses the intent.
func TestValidateRejectsNonPositiveExit(t *testing.T) {
	dir := t.TempDir()
	writeFile(t, filepath.Join(dir, "rooms", "1.yaml"), "name: Blocked\nexits:\n  north:\n    room: 0\n")
	issues := validateRooms(newSource(dir))
	if !containsMsg(issues, "non-positive") {
		t.Fatalf("non-positive exit not reported: %+v", issues)
	}
}

// TestValidateRejectsEmptyObjectEntry: an objects: entry that is neither a
// local definition nor a file reference disappears at runtime, so it must be
// an error instead of a silent no-op.
func TestValidateRejectsEmptyObjectEntry(t *testing.T) {
	dir := t.TempDir()
	writeFile(t, filepath.Join(dir, "rooms", "1.yaml"),
		"name: Empty\nobjects:\n  - aliases: []\n    description: []\n    hidden: false\n    name: \"\"\n")
	issues := validateRooms(newSource(dir))
	if !containsMsg(issues, "resolves to nothing") {
		t.Fatalf("empty object entry not reported: %+v", issues)
	}
}

// TestValidateRejectsDropTableInRoomTrigger: room on_enter/on_exit and
// on_traverse steps consume drop_table like every other trigger block, so a
// typo'd table id must be reported rather than silently awarding nothing.
func TestValidateRejectsDropTableInRoomTrigger(t *testing.T) {
	dir := t.TempDir()
	writeFile(t, filepath.Join(dir, "rooms", "1.yaml"),
		"name: Loot\nexits:\n  north:\n    room: 1\n    on_traverse:\n      - steps:\n          - drop_table:\n              - table: no_such_table\n                weight: 100\n")
	if issues := validateRooms(newSource(dir)); !containsMsg(issues, "no_such_table") {
		t.Fatalf("unknown drop table in an exit trigger not reported: %+v", issues)
	}

	// on_enter is validated by validateRoomTriggers.
	writeFile(t, filepath.Join(dir, "rooms", "2.yaml"),
		"name: Loot2\non_enter:\n  - steps:\n      - drop_table:\n          - table: no_such_table\n            weight: 100\n")
	if issues := validateRoomTriggers(newSource(dir)); !containsMsg(issues, "no_such_table") {
		t.Fatalf("unknown drop table in on_enter not reported: %+v", issues)
	}
}