aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/casino_validate_test.go
blob: a0f44519a7b4cf4463195be79c6a8ee5b151a8d8 (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
package admin

import (
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"thehouseoficarus/internal/world"
)

func TestRoomUpdateRejectsInvalidCasinoBeforeWriting(t *testing.T) {
	dir := t.TempDir()
	rooms := filepath.Join(dir, "rooms")
	if err := os.MkdirAll(rooms, 0755); err != nil {
		t.Fatal(err)
	}
	path := filepath.Join(rooms, "1.yaml")
	original := "name: Original\n"
	if err := os.WriteFile(path, []byte(original), 0644); err != nil {
		t.Fatal(err)
	}
	s := &AdminServer{dataDir: dir, world: world.New(dir), undoStack: NewUndoStack(dir)}
	for _, cfg := range []string{
		`{"id":"slots","game":"slots","payout":-1}`,
		`{"id":"slots","game":"slots","spin_tickz":2}`,
	} {
		r := httptest.NewRequest(http.MethodPut, "/api/rooms/1", strings.NewReader(`{"casino_machines":[`+cfg+`]}`))
		w := httptest.NewRecorder()
		s.handleRoomByID(w, r)
		if w.Code != http.StatusBadRequest {
			t.Fatalf("status %d: %s", w.Code, w.Body.String())
		}
		data, err := os.ReadFile(path)
		if err != nil || string(data) != original || s.undoStack.Info().CanUndo {
			t.Fatal("invalid update changed room or undo history")
		}
	}
}

func TestRoomCreateValidatesBeforeAllocatingOrLinking(t *testing.T) {
	// No world/config needed: rejection must precede all mutation work.
	s := &AdminServer{}
	r := httptest.NewRequest(http.MethodPost, "/api/rooms", strings.NewReader(`{"casino_tables":[{"id":"cards","game":"typo"}],"link_from":1,"link_dir":"n"}`))
	w := httptest.NewRecorder()
	s.handleRooms(w, r)
	if w.Code != http.StatusBadRequest {
		t.Fatalf("status %d: %s", w.Code, w.Body.String())
	}
}