package store import ( "aeternalis/server/content" "aeternalis/server/internal/game" "context" "errors" "path/filepath" "testing" ) func TestCombatCatalogReferencesAndReplay(t *testing.T) { ctx := context.Background() db, err := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "combat.db"))) if err != nil { t.Fatal(err) } defer db.Close() catalog, _ := db.Catalog(ctx) publish := func(change CatalogChange) string { t.Helper() change.ID = game.ID() change.ExpectedVersion = catalog.Version change.Reason = "Combat content test" if _, err := db.PreviewCatalog(ctx, change); err != nil { t.Fatal(err) } r, err := db.PublishCatalog(ctx, "admin", change) if err != nil { t.Fatal(err) } again, err := db.PublishCatalog(ctx, "admin", change) if err != nil || r != again { t.Fatal("changed replay", err) } catalog, _ = db.Catalog(ctx) return r.ID } attack := catalog.Combat.Attacks[0] attack.ID = "" attack.Name = "Test shot" id := publish(CatalogChange{Op: "create-attack", Attack: &attack}) loot := publish(CatalogChange{Op: "create-loot", Loot: &content.LootTable{Name: "Loot", Entries: []content.LootEntry{{Item: "herb", Amount: 1, Chance: 100, Weight: 1}}}}) mob := catalog.Combat.Mobs[0] mob.ID = "" mob.AttackID = id mob.LootID = loot mid := publish(CatalogChange{Op: "create-mob", Mob: &mob}) publish(CatalogChange{Op: "create-spawn", Spawn: &content.MobSpawn{Name: "Region", MobID: mid, Count: 2, Respawn: 60, Biomes: []string{"forest"}}}) publish(CatalogChange{Op: "create-item", Item: &content.ItemDefinition{Name: "Bow", Slot: "weapon", Requirements: content.ItemRequirements{Level: 1}, AttackID: id}}) attack.Duration = 2 attack.Radius = 0 bad := CatalogChange{ID: game.ID(), ExpectedVersion: catalog.Version, Reason: "Invalid area test", Op: "create-attack", Attack: &attack} if _, err := db.PublishCatalog(ctx, "admin", bad); !errors.Is(err, ErrCatalogInvalid) { t.Fatal("invalid area accepted", err) } mob.AttackID = "missing" bad.Op = "create-mob" bad.Attack = nil bad.Mob = &mob if _, err := db.PublishCatalog(ctx, "admin", bad); !errors.Is(err, ErrCatalogInvalid) { t.Fatal("missing attack reference accepted", err) } }