184 lines
6.1 KiB
Go
184 lines
6.1 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
p "aeternalis/server/internal/protocol"
|
|
"context"
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAdminContentTransactionsAndTitleRevocation(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, err := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "admin-content.db")))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
b, _ := content.Load()
|
|
b.Titles = append(b.Titles, content.Title{ID: "admin_trial", Name: "Admin Trial", Scope: "global", Requirements: []content.TitleRequirement{{Metric: "level", AtLeast: 999}}, Effects: content.TitleEffects{Modifiers: []content.StatModifier{{Stat: "strength", Flat: 4, Percent: 50}}}})
|
|
w := game.New("admin-content", 1, time.Now(), b)
|
|
lease, _, err := db.Acquire(ctx, w)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer lease.Release(ctx)
|
|
c, err := w.Join("account", "Admin target")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.DailySeconds = 123
|
|
c.View.Entity.HP = 20
|
|
if err = lease.Save(ctx, w, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run := func(op, value string) Command {
|
|
t.Helper()
|
|
cmd := Command{ID: game.ID(), WorldID: w.ID, Actor: "operator", Target: c.View.Entity.ID, Op: op, Value: value, Reason: "Administration test"}
|
|
if err := db.Enqueue(ctx, cmd); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := lease.SaveCommand(ctx, w, "", &cmd, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
commands, err := db.Commands(ctx, w.ID, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, result := range commands {
|
|
if result.ID == cmd.ID {
|
|
return result
|
|
}
|
|
}
|
|
t.Fatal("missing audit")
|
|
return Command{}
|
|
}
|
|
if cmd := run("grant-item", "spear:2"); cmd.Status != "completed" {
|
|
t.Fatal(cmd)
|
|
}
|
|
var spear string
|
|
count := 0
|
|
for _, i := range c.View.Items {
|
|
if i.DefinitionID == "spear" {
|
|
spear = i.ID
|
|
count++
|
|
}
|
|
}
|
|
if count != 2 {
|
|
t.Fatal("wrong grant quantity")
|
|
}
|
|
c.View.Equipment["weapon"] = spear
|
|
w.RefreshStats(c)
|
|
before := c.View.Stats["meleeDamage"].Final
|
|
if cmd := run("remove-item", spear+":1"); cmd.Status != "completed" {
|
|
t.Fatal(cmd)
|
|
}
|
|
if c.View.Equipment["weapon"] != "" || c.View.Stats["meleeDamage"].Final >= before {
|
|
t.Fatal("removed equipment still active")
|
|
}
|
|
cmd := run("grant-item", "wood:3")
|
|
if c.View.Inventory["wood"] != 3 {
|
|
t.Fatal("stack grant missing")
|
|
}
|
|
if err := lease.SaveCommand(ctx, w, "", &cmd, ""); err == nil || c.View.Inventory["wood"] != 3 {
|
|
t.Fatal("replayed command duplicated items")
|
|
}
|
|
if cmd := run("remove-item", "wood:4"); cmd.Status != "rejected" || c.View.Inventory["wood"] != 3 {
|
|
t.Fatal("over-removal accepted", cmd)
|
|
}
|
|
if cmd := run("grant-item", "invented:1"); cmd.Status != "rejected" {
|
|
t.Fatal("unknown item accepted", cmd)
|
|
}
|
|
if cmd := run("grant-title", "admin_trial"); cmd.Status != "completed" || c.View.Stats["strength"].Final != 6 {
|
|
t.Fatal("admin title requirement bypass failed", cmd)
|
|
}
|
|
award := c.View.Titles[0]
|
|
for _, a := range c.View.Titles {
|
|
if a.ID == "admin_trial" {
|
|
award = a
|
|
}
|
|
}
|
|
ring := p.ItemInstance{ID: game.ID(), DefinitionID: "trial_ring", Name: "Trial ring", Slot: "ring", Weight: 1, Requirements: p.ItemRequirements{Level: 1, Titles: []string{"admin_trial"}}, Modifiers: []p.StatModifier{{Stat: "luck", Flat: 5}}}
|
|
c.View.Items = append(c.View.Items, ring)
|
|
c.View.Equipment["ring1"] = ring.ID
|
|
w.RefreshStats(c)
|
|
if cmd := run("revoke-title", "admin_trial"); cmd.Status != "completed" || c.View.Stats["strength"].Final != 0 || c.View.Equipment["ring1"] != "" {
|
|
t.Fatal("revocation did not remove effects/eligibility", cmd)
|
|
}
|
|
w.PendingTitles = []game.TitleCandidate{{CharacterID: c.View.Entity.ID, Award: award}}
|
|
if err = lease.Save(ctx, w, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, a := range c.View.Titles {
|
|
if a.ID == award.ID {
|
|
t.Fatal("revoked title automatically re-awarded")
|
|
}
|
|
}
|
|
other, err := w.Join("other-account", "Other")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
otherCommand := Command{ID: game.ID(), WorldID: w.ID, Actor: "operator", Target: other.View.Entity.ID, Op: "grant-title", Value: award.ID, Reason: "First claim test"}
|
|
if err = db.Enqueue(ctx, otherCommand); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = lease.SaveCommand(ctx, w, "", &otherCommand, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(other.View.Titles) != 0 {
|
|
t.Fatal("revocation erased original first claim")
|
|
}
|
|
if cmd := run("grant-title", "admin_trial"); cmd.Status != "completed" || c.View.Stats["strength"].Final != 6 {
|
|
t.Fatal("restore did not retain original title", cmd)
|
|
}
|
|
if c.DailySeconds != 123 || c.View.Entity.HP != 20 {
|
|
t.Fatal("admin content changed budget or healed")
|
|
}
|
|
c.View.Entity.State = "eligible"
|
|
w.Phase = "won"
|
|
if cmd := run("grant-item", "wood:1"); cmd.Status != "completed" {
|
|
t.Fatal("survivor change failed", cmd)
|
|
}
|
|
survivor, err := db.ReadPassage(ctx, c.View.Entity.ID, c.Account)
|
|
if err != nil || survivor.View.Inventory["wood"] != 4 {
|
|
t.Fatal("survivor passage was not updated", err)
|
|
}
|
|
destination := game.New("admin-destination", 2, time.Now(), b)
|
|
dl, _, err := db.Acquire(ctx, destination)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer dl.Release(ctx)
|
|
if err = destination.Import(survivor); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = dl.Save(ctx, destination, survivor.View.Entity.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cmd := run("grant-item", "wood:1"); cmd.Status != "rejected" {
|
|
t.Fatal("stale source command accepted", cmd)
|
|
}
|
|
record, err := db.ActiveCharacter(ctx, c.Account)
|
|
if err != nil || record.Character.View.Inventory["wood"] != 4 {
|
|
t.Fatal("stale source changed destination", err)
|
|
}
|
|
raw, _ := json.Marshal(record.Character.View)
|
|
if err = dl.Release(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
failed := Command{ID: game.ID(), WorldID: destination.ID, Actor: "operator", Target: survivor.View.Entity.ID, Op: "remove-item", Value: "wood:1", Reason: "Rollback test"}
|
|
if err = db.Enqueue(ctx, failed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = dl.SaveCommand(ctx, destination, "", &failed, ""); err == nil {
|
|
t.Fatal("lost owner committed")
|
|
}
|
|
after, _ := json.Marshal(survivor.View)
|
|
if string(raw) != string(after) {
|
|
t.Fatal("failed transaction mutated live view")
|
|
}
|
|
}
|