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

// Regression: multi-file room/entity edits must be journaled transactions so
// a failure cannot half-apply them. These tests make the transaction journal
// uncreatable (a regular file sits at the journal path), which fails Commit
// before any operation is applied, and assert that no partial mutation
// survived.

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

	"thehouseoficarus/internal/world"
)

// blockJournal makes the transaction journal path unusable by placing a
// regular file where persistence.Transaction.Commit wants a directory.
func blockJournal(t *testing.T, dataDir string) {
	t.Helper()
	if err := os.WriteFile(filepath.Join(dataDir, ".journal"), []byte("x"), 0o600); err != nil {
		t.Fatal(err)
	}
}

func TestRoomMoveAtomicWhenJournalUnavailable(t *testing.T) {
	s := newTestAdminServer(t, map[int]string{1: "id: 1\nname: Origin\n"})
	if err := os.Mkdir(filepath.Join(s.dataDir, "rooms", "sub"), 0o755); err != nil {
		t.Fatal(err)
	}
	blockJournal(t, s.dataDir)

	rec := httptest.NewRecorder()
	s.handleRoomMove(rec, httptest.NewRequest(http.MethodPost, "/api/rooms/move",
		strings.NewReader(`{"id":1,"dir":"sub"}`)))
	if !strings.Contains(rec.Body.String(), "error") {
		t.Fatalf("move should fail with an unusable journal; got %s", rec.Body.String())
	}
	if _, err := os.Stat(filepath.Join(s.dataDir, "rooms", "1.yaml")); err != nil {
		t.Fatalf("source room disappeared after failed move: %v", err)
	}
	if _, err := os.Stat(filepath.Join(s.dataDir, "rooms", "sub", "1.yaml")); !os.IsNotExist(err) {
		t.Fatalf("destination created after failed move (stat err=%v)", err)
	}
}

func TestRoomUnlinkAtomicWhenJournalUnavailable(t *testing.T) {
	s := newTestAdminServer(t, map[int]string{
		1: "id: 1\nname: A\nexits:\n  north:\n    room: 2\n",
		2: "id: 2\nname: B\nexits:\n  south:\n    room: 1\n",
	})
	blockJournal(t, s.dataDir)

	rec := httptest.NewRecorder()
	s.handleRoomLink(rec, httptest.NewRequest(http.MethodDelete, "/api/rooms/link",
		strings.NewReader(`{"from":1,"to":2}`)))
	if !strings.Contains(rec.Body.String(), "error") {
		t.Fatalf("unlink should fail with an unusable journal; got %s", rec.Body.String())
	}
	if got := roomExitTarget(s, 1, world.North); got != 2 {
		t.Fatalf("unlink half-applied: room 1 north now points at %d", got)
	}
	if got := roomExitTarget(s, 2, world.South); got != 1 {
		t.Fatalf("unlink half-applied: room 2 south now points at %d", got)
	}
	if info := s.undoStack.Info(); info.StackSize != 0 {
		t.Fatalf("failed unlink pushed an undo entry: %+v", info)
	}
}

func TestEntityMoveAtomicWhenJournalUnavailable(t *testing.T) {
	dir := t.TempDir()
	if err := os.MkdirAll(filepath.Join(dir, "items", "a"), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(dir, "items", "a", "one.yaml"), []byte("name: One\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	blockJournal(t, dir)

	if _, _, _, err := moveEntityFile(dir, "items", "one", ""); err == nil {
		t.Fatal("move succeeded despite an unusable journal")
	}
	if _, err := os.Stat(filepath.Join(dir, "items", "a", "one.yaml")); err != nil {
		t.Fatalf("source entity disappeared after failed move: %v", err)
	}
	if _, err := os.Stat(filepath.Join(dir, "items", "one.yaml")); !os.IsNotExist(err) {
		t.Fatalf("destination created after failed move (stat err=%v)", err)
	}
}

func TestDeleteEntityDirAtomicWhenJournalUnavailable(t *testing.T) {
	dir := t.TempDir()
	if err := os.MkdirAll(filepath.Join(dir, "items", "pack"), 0o755); err != nil {
		t.Fatal(err)
	}
	for _, name := range []string{"a.yaml", "b.yaml"} {
		if err := os.WriteFile(filepath.Join(dir, "items", "pack", name), []byte("name: X\n"), 0o644); err != nil {
			t.Fatal(err)
		}
	}
	blockJournal(t, dir)

	if _, err := deleteEntityDir(dir, "items", "pack"); err == nil {
		t.Fatal("deleteEntityDir succeeded despite an unusable journal")
	}
	for _, name := range []string{"a.yaml", "b.yaml"} {
		if _, err := os.Stat(filepath.Join(dir, "items", "pack", name)); err != nil {
			t.Fatalf("file %s lost after failed dir move: %v", name, err)
		}
	}
}