aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_dig_test.go
blob: 1a96c251b4099c579608b007abafa35f4e9f1bca (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
package game

import (
	"os"
	"path/filepath"
	"testing"
)

// TestFindNextRoomIDSkipsIDsInOtherDirectories: the in-game allocator must
// check every room ID in the world, not just the current room's directory.
// A directory-scoped scan hands out an ID that already exists elsewhere, and
// World.AddRoomPath then silently shadows that room.
func TestFindNextRoomIDSkipsIDsInOtherDirectories(t *testing.T) {
	root := t.TempDir()
	intro := filepath.Join(root, "intro")
	arena := filepath.Join(root, "arena")
	for _, dir := range []string{intro, arena} {
		if err := os.MkdirAll(dir, 0o755); err != nil {
			t.Fatal(err)
		}
	}
	// intro owns 1001 and 1002; arena owns 1003.
	for _, id := range []string{"1001", "1002"} {
		if err := os.WriteFile(filepath.Join(intro, id+".yaml"), []byte("name: intro\n"), 0o644); err != nil {
			t.Fatal(err)
		}
	}
	if err := os.WriteFile(filepath.Join(arena, "1003.yaml"), []byte("name: arena\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	globallyUsed := map[int]bool{1001: true, 1002: true, 1003: true}

	got, err := findNextRoomID(intro, globallyUsed)
	if err != nil {
		t.Fatalf("findNextRoomID: %v", err)
	}
	if got != 1004 {
		t.Fatalf("findNextRoomID = %d, want 1004 (1003 is used by arena)", got)
	}
}

// TestFindNextRoomIDEmptyDirectoryStartsAtOne: an empty directory still
// allocates the globally lowest free ID rather than blindly returning 1.
func TestFindNextRoomIDEmptyDirectoryStartsAtOne(t *testing.T) {
	root := t.TempDir()
	empty := filepath.Join(root, "empty")
	if err := os.MkdirAll(empty, 0o755); err != nil {
		t.Fatal(err)
	}
	got, err := findNextRoomID(empty, map[int]bool{1: true, 2: true})
	if err != nil {
		t.Fatalf("findNextRoomID: %v", err)
	}
	if got != 3 {
		t.Fatalf("findNextRoomID = %d, want 3", got)
	}
}