package store import ( "aeternalis/server/content" "aeternalis/server/internal/game" p "aeternalis/server/internal/protocol" "context" "encoding/json" "errors" "os" "path/filepath" "reflect" "testing" "time" ) func TestCraftDurability(t *testing.T) { testCraftDurability(t, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "craft.db"))) } func TestPostgresCraftDurability(t *testing.T) { dsn := os.Getenv("TEST_DATABASE_URL") if dsn == "" { t.Skip("TEST_DATABASE_URL not configured") } testCraftDurability(t, dsn) } func testCraftDurability(t *testing.T, dsn string) { ctx := context.Background() db, e := Open(ctx, dsn) if e != nil { t.Fatal(e) } defer db.Close() b, e := content.Load() if e != nil { t.Fatal(e) } b.Crafting.Rules.BaseFailure = 0 b.Crafting.Rules.MinFailure = 0 b.Crafting.Rules.MaxFailure = 0 now := time.Now().UTC() w := game.New("craft-"+game.ID(), 1, now, b) l, _, e := db.Acquire(ctx, w) if e != nil { t.Fatal(e) } c, e := w.Join("crafter-"+game.ID(), "Maker") if e != nil { t.Fatal(e) } if e = w.Connect(c.View.Entity.ID); e != nil { t.Fatal(e) } c.View.Inventory["wood"] = 20 c.View.Inventory["ore"] = 20 c.View.Inventory["escape_stone"] = 1 if e = l.Save(ctx, w, ""); e != nil { t.Fatal(e) } req := p.CraftRequest{ID: game.ID(), CharacterID: c.View.Entity.ID, Profession: "weapon_smithing", Ingredients: map[string]int64{"wood": 3, "ore": 2}} before, _ := json.Marshal(c) random := w.RandomCounter for _, state := range []string{"offline", "sheltered", "dead", "capped"} { switch state { case "offline": c.Online = false case "sheltered": c.Sheltered = true case "dead": c.View.Entity.State = "dead" case "capped": c.View.RemainingSeconds = 0 } if _, e = l.Craft(ctx, w, c.Account, req); !errors.Is(e, ErrCraft) { t.Fatalf("accepted %s: %v", state, e) } var restored game.Character if e = json.Unmarshal(before, &restored); e != nil { t.Fatal(e) } *c = restored c.Online = true } stale := *l stale.Revision-- if _, e = stale.Craft(ctx, w, c.Account, req); !errors.Is(e, ErrOwnership) { t.Fatal("stale craft was not fenced", e) } after, _ := json.Marshal(c) if string(after) != string(before) || w.RandomCounter != random { t.Fatal("failed save changed live crafting state") } if receipt, e := db.CraftReceipt(ctx, c.Account, req); e != nil || receipt != nil { t.Fatal("failed save left receipt", e) } result, e := l.Craft(ctx, w, c.Account, req) if e != nil || !result.Success { t.Fatal(result, e) } if w.Characters[c.View.Entity.ID] != c || c.View.Inventory["wood"] != 17 || c.View.Crafting["weapon_smithing"].XP != 20 { t.Fatal("craft did not publish onto live character") } committed, _ := json.Marshal(c) random = w.RandomCounter replay, e := l.Craft(ctx, w, c.Account, req) if e != nil || !reflect.DeepEqual(result, replay) { t.Fatal("retry changed result", e) } after, _ = json.Marshal(c) if string(after) != string(committed) || w.RandomCounter != random { t.Fatal("retry spent resources or rerolled") } changed := req changed.Ingredients = map[string]int64{"wood": 4, "ore": 2} if _, e = l.Craft(ctx, w, c.Account, changed); !errors.Is(e, ErrCraft) { t.Fatal("reused nonce accepted changed ingredients", e) } if _, e = l.Craft(ctx, w, "intruder", req); !errors.Is(e, ErrCraft) { t.Fatal("other account recovered receipt", e) } // Reload from the durable world snapshot, not the live result. if e = l.Release(ctx); e != nil { t.Fatal(e) } restored := game.New(w.ID, 1, now, b) var snapshot []byte l, snapshot, e = db.Acquire(ctx, restored) if e != nil { t.Fatal(e) } if e = json.Unmarshal(snapshot, restored); e != nil { t.Fatal(e) } if e = restored.Restore(now); e != nil { t.Fatal(e) } w = restored c = w.ForAccount(c.Account) if c == nil || len(c.View.DiscoveredRecipes) != 1 || c.View.Crafting["weapon_smithing"].XP != 20 { t.Fatal("restart lost progression") } if e = w.Connect(c.View.Entity.ID); e != nil { t.Fatal(e) } hub := game.New("hub-"+game.ID(), 2, now, b) hub.Kind = "hub" hub.Phase = "hub" hl, _, e := db.Acquire(ctx, hub) if e != nil { t.Fatal(e) } progression, _ := json.Marshal([]any{c.View.Crafting, c.View.DiscoveredRecipes, c.View.Items, c.View.Experience, c.CraftReadyAt}) transfer, e := l.PrepareTravel(ctx, w, c.Account, game.ID(), hub.ID, "escape") if e != nil { t.Fatal(e) } if _, e = hl.AcceptTravel(ctx, hub, c.Account, transfer.ID); e != nil { t.Fatal(e) } arrived := hub.ForAccount(c.Account) after, _ = json.Marshal([]any{arrived.View.Crafting, arrived.View.DiscoveredRecipes, arrived.View.Items, arrived.View.Experience, arrived.CraftReadyAt}) if string(progression) != string(after) { t.Fatal("travel changed crafting progression or provenance") } replay, e = hl.Craft(ctx, hub, c.Account, req) if e != nil || !reflect.DeepEqual(result, replay) { t.Fatal("travel lost original receipt", e) } } func TestCraftRecipeRevision(t *testing.T) { ctx := context.Background() db, e := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "recipes.db"))) if e != nil { t.Fatal(e) } defer db.Close() initial, e := db.Catalog(ctx) if e != nil { t.Fatal(e) } recipe := content.CraftRecipe{Name: "Trial spear", Profession: "weapon_smithing", Level: 2, XP: 25, Inputs: map[string]int64{"wood": 5, "ore": 3}, Outputs: map[string]int64{"spear": 1}} change := CatalogChange{ID: game.ID(), ExpectedVersion: initial.Version, Op: "create-craft-recipe", Reason: "Create a test recipe", CraftRecipe: &recipe} first, e := db.PublishCatalog(ctx, "operator", change) if e != nil { t.Fatal(e) } firstCatalog, e := db.Catalog(ctx) if e != nil { t.Fatal(e) } made := firstCatalog.Crafting.Recipes[len(firstCatalog.Crafting.Recipes)-1] if made.ID == "" || made.Revision != 1 { t.Fatal("server did not assign recipe identity") } replay, e := db.PublishCatalog(ctx, "operator", change) if e != nil || replay.Version != first.Version { t.Fatal("recipe create retry failed", e) } recipe.Inputs = map[string]int64{"wood": 6, "ore": 3} update := CatalogChange{ID: game.ID(), ExpectedVersion: first.Version, Op: "update-craft-recipe", TargetID: made.ID, Reason: "Revise test ingredients", CraftRecipe: &recipe} _, e = db.PublishCatalog(ctx, "operator", update) if e != nil { t.Fatal(e) } revised, e := db.Catalog(ctx) if e != nil { t.Fatal(e) } got := revised.Crafting.Recipes[len(revised.Crafting.Recipes)-1] if len(revised.Crafting.RecipeHistory) != 1 || !reflect.DeepEqual(revised.Crafting.RecipeHistory[0], made) || len(firstCatalog.Crafting.RecipeHistory) != 0 { t.Fatal("recipe publication lost the immutable salvage revision or mutated the old catalog") } if got.ID != made.ID || got.Revision != 2 || len(revised.Crafting.Recipes) != len(firstCatalog.Crafting.Recipes) || made.Inputs["wood"] != 5 { t.Fatal("revision duplicated identity or mutated prior version") } update.ID = game.ID() if _, e = db.PublishCatalog(ctx, "operator", update); !errors.Is(e, ErrCatalogConflict) { t.Fatal("stale edit accepted", e) } }