aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_entity_path_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/admin/api_entity_path_test.go')
-rw-r--r--internal/admin/api_entity_path_test.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/internal/admin/api_entity_path_test.go b/internal/admin/api_entity_path_test.go
index 7abf65f..27ecbe8 100644
--- a/internal/admin/api_entity_path_test.go
+++ b/internal/admin/api_entity_path_test.go
@@ -8,6 +8,7 @@ import (
"strings"
"testing"
+ "thehouseoficarus/internal/behavior"
"thehouseoficarus/internal/config"
"thehouseoficarus/internal/player"
)
@@ -60,3 +61,35 @@ func TestEntityAPICannotEscapeData(t *testing.T) {
t.Fatal("entity API escaped the data directory")
}
}
+
+// TestEntityRenameRejectsDuplicateIDInOtherDirectory: entity IDs are global
+// across the type's subdirectory tree (the runtime path indexes resolve by
+// filename anywhere under data/<subdir>), so renaming into an ID that
+// exists in a DIFFERENT directory must be rejected instead of creating a
+// duplicate ID shadowed by whichever file the index finds first.
+func TestEntityRenameRejectsDuplicateIDInOtherDirectory(t *testing.T) {
+ dir := t.TempDir()
+ for _, sub := range []string{"items/a", "items/b"} {
+ if err := os.MkdirAll(filepath.Join(dir, sub), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := os.WriteFile(filepath.Join(dir, "items/a/old.yaml"), []byte("name: Original\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "items/b/taken.yaml"), []byte("name: Existing\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ if _, _, _, err := renameEntityFile(dir, "items", "old", "taken"); err == nil {
+ t.Fatal("rename accepted an ID that already exists in another directory")
+ }
+ if dups := behavior.CheckDuplicateIDs(filepath.Join(dir, "items")); len(dups) != 0 {
+ t.Fatalf("rename produced duplicate IDs: %+v", dups)
+ }
+
+ // Renaming within the same directory to a free name still works.
+ if _, _, _, err := renameEntityFile(dir, "items", "old", "moved"); err != nil {
+ t.Fatalf("ordinary rename rejected: %v", err)
+ }
+}