79 lines
2.8 KiB
Go
79 lines
2.8 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestBiomeRevisionsReuseResourcesAndRejectStaleEdits(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, err := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "biomes.db")))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
initial, err := db.Catalog(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
draft := CatalogChange{ID: game.ID(), ExpectedVersion: initial.Version, Reason: "Reuse existing trees", Op: "create-biome", Biome: &content.BiomeDefinition{Name: "Wooded valley", Color: "#445566", Weight: 2, Resources: []string{"tree"}}}
|
|
made, err := db.PublishCatalog(ctx, "admin", draft)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
edit := CatalogChange{ID: game.ID(), ExpectedVersion: made.Version, Reason: "Add existing ore", Op: "update-biome", TargetID: made.ID, Biome: &content.BiomeDefinition{Name: "Wooded valley", Color: "#334455", Weight: 3, Resources: []string{"tree", "ore"}}}
|
|
preview, err := db.PreviewCatalog(ctx, edit)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if preview.Resolved.Biome.ID != made.ID || preview.Resolved.Biome.Revision != 2 {
|
|
t.Fatal("preview replaced permanent identity")
|
|
}
|
|
unchanged, _ := db.Catalog(ctx)
|
|
if unchanged.Version != made.Version || len(unchanged.Gathering.BiomeHistory) != 0 {
|
|
t.Fatal("preview mutated history")
|
|
}
|
|
revised, err := db.PublishCatalog(ctx, "admin", edit)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
replay, err := db.PublishCatalog(ctx, "admin", edit)
|
|
if err != nil || replay != revised {
|
|
t.Fatal("retry changed revision", err)
|
|
}
|
|
current, err := db.Catalog(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(current.Gathering.Resources) != len(initial.Gathering.Resources) || len(current.Gathering.Biomes) != len(initial.Gathering.Biomes)+1 {
|
|
t.Fatal("revision duplicated resource or biome")
|
|
}
|
|
if len(current.Gathering.BiomeHistory) != 1 || current.Gathering.BiomeHistory[0].Revision != 1 || len(current.Gathering.BiomeHistory[0].Resources) != 1 {
|
|
t.Fatal("old revision not preserved")
|
|
}
|
|
edit.ID = game.ID()
|
|
if _, err = db.PublishCatalog(ctx, "admin", edit); !errors.Is(err, ErrCatalogConflict) {
|
|
t.Fatal("stale edit accepted", err)
|
|
}
|
|
edit.ExpectedVersion = current.Version
|
|
edit.Biome.Resources = []string{"missing"}
|
|
if _, err = db.PublishCatalog(ctx, "admin", edit); !errors.Is(err, ErrCatalogInvalid) {
|
|
t.Fatal("unknown resource accepted", err)
|
|
}
|
|
edit.ID = game.ID()
|
|
edit.Biome.Resources = []string{}
|
|
if _, err = db.PublishCatalog(ctx, "admin", edit); err != nil {
|
|
t.Fatal("empty biome rejected", err)
|
|
}
|
|
current, _ = db.Catalog(ctx)
|
|
for _, b := range current.Gathering.Biomes {
|
|
if b.ID == made.ID && (b.Resources == nil || len(b.Resources) != 0 || b.Revision != 3) {
|
|
t.Fatal("empty resource selection lost on save")
|
|
}
|
|
}
|
|
}
|