package admin // Regression: renaming a room must rewrite structural room references in // non-room content files (courses, modules, triggers, items, objects, mobs), // and undo must restore those files too. Before this, only data/rooms was // scanned, so a course obstacle or transport destination silently kept the // old ID. import ( "net/http" "os" "path/filepath" "strings" "testing" ) func writeContent(t *testing.T, s *AdminServer, sub, name, body string) string { t.Helper() dir := filepath.Join(s.dataDir, sub) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } path := filepath.Join(dir, name) if err := os.WriteFile(path, []byte(body), 0o644); err != nil { t.Fatal(err) } return path } func readFile(t *testing.T, path string) string { t.Helper() data, err := os.ReadFile(path) if err != nil { t.Fatal(err) } return string(data) } func TestRoomRenameRewritesNonRoomContent(t *testing.T) { s := newTestAdminServer(t, map[int]string{ 5: "id: 5\nname: Lab\nexits:\n north:\n room: 6\n", 6: "id: 6\nname: Hall\nexits:\n south:\n room: 5\n", }) s.rewriteRoomIDs = func(map[int]int) {} modulePath := writeContent(t, s, "modules", "transport_home.yaml", "name: Home\ncategory: transport\ndestination: 5\n") coursePath := writeContent(t, s, "courses", "agility.yaml", "name: Agility\nstart_room: 6\nobstacles:\n - room_id: 5\n verb: climb\n") // An author value equal to a room ID must survive untouched. itemPath := writeContent(t, s, "items", "gem.yaml", "name: gem\nvalue: 5\n") if rec := renameRoom(t, s, 5, 7); rec.Code != http.StatusOK { t.Fatalf("rename failed: %d %s", rec.Code, rec.Body.String()) } if got := readFile(t, modulePath); !strings.Contains(got, "destination: 7") { t.Errorf("module destination not rewritten: %s", got) } course := readFile(t, coursePath) if !strings.Contains(course, "room_id: 7") { t.Errorf("course obstacle room_id not rewritten: %s", course) } // start_room: 6 is not the renamed ID, so it stays. if !strings.Contains(course, "start_room: 6") { t.Errorf("unrelated course start_room changed: %s", course) } if got := readFile(t, itemPath); !strings.Contains(got, "value: 5") { t.Errorf("author value equal to the renamed ID was rewritten: %s", got) } // Undo restores the content files alongside the room. if s.undoStack.Undo() == nil { t.Fatal("undo failed") } if got := readFile(t, modulePath); !strings.Contains(got, "destination: 5") { t.Errorf("undo did not restore module destination: %s", got) } if got := readFile(t, coursePath); !strings.Contains(got, "room_id: 5") { t.Errorf("undo did not restore course obstacle: %s", got) } }