package transport import ( "aeternalis/server/content" "aeternalis/server/internal/game" "aeternalis/server/internal/store" "context" "encoding/json" "net/http" "net/http/httptest" "path/filepath" "reflect" "strings" "testing" "time" ) func TestCharacterAppearanceOwnershipAndDurability(t *testing.T) { ctx := context.Background() db, err := store.Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "appearance.db"))) if err != nil { t.Fatal(err) } defer db.Close() b, _ := content.Load() world := game.New("appearance", 1, time.Now(), b) lease, _, err := db.Acquire(ctx, world) if err != nil { t.Fatal(err) } svc := NewService(world, lease) a := *content.DefaultCharacterAppearance() a.Body.HeadWidth = 1.1 a.Body.Chest = "female" view, err := svc.join(ctx, "artist", "Artist", "", &a) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(view.Entity.Appearance, &a) { t.Fatal("new character appearance missing") } other := a other.Primary = "#ffffff" if _, err = svc.join(ctx, "artist", "Artist", "", &other); err != nil { t.Fatal(err) } if !reflect.DeepEqual(world.ForAccount("artist").View.Entity.Appearance, &a) { t.Fatal("join retry overwrote appearance") } auth, _ := NewAuth(ctx, db, true, "http://127.0.0.1", "", "", "") server := httptest.NewServer(NewHTTP(svc, auth, t.TempDir(), nil).Handler()) defer server.Close() auth.base = server.URL token := game.ID() + game.ID() if err = db.CreateSession(ctx, hash(token), "artist"); err != nil { t.Fatal(err) } request := func(value any, cookie, origin string) int { t.Helper() raw, _ := json.Marshal(value) r, _ := http.NewRequest("POST", server.URL+"/api/appearance", strings.NewReader(string(raw))) r.Header.Set("Origin", origin) if cookie != "" { r.AddCookie(&http.Cookie{Name: auth.cookieName(), Value: cookie}) } resp, err := server.Client().Do(r) if err != nil { t.Fatal(err) } defer resp.Body.Close() return resp.StatusCode } if request(other, "", server.URL) != 401 || request(other, token, "https://elsewhere.example") != 403 { t.Fatal("appearance auth boundary bypassed") } bad := other bad.Family = "creature" if request(bad, token, server.URL) != 403 { t.Fatal("player could choose creature") } before := world.ForAccount("artist").View.Stats["moveSpeed"] if request(other, token, server.URL) != 403 { t.Fatal("existing character appearance could be changed") } record, err := db.ActiveCharacter(ctx, "artist") if err != nil || !reflect.DeepEqual(record.Character.View.Entity.Appearance, &a) || record.Character.View.Stats["moveSpeed"] != before { t.Fatal("appearance not durable or changed stats", err) } invalid := *content.DefaultCharacterAppearance() invalid.Body.NoseScale = 200 if _, err := svc.join(ctx, "invalid", "Invalid", "", &invalid); err == nil || world.ForAccount("invalid") != nil { t.Fatal("invalid initial appearance created a character") } plain, err := svc.join(ctx, "plain", "Plain", "") if err != nil || !reflect.DeepEqual(plain.Entity.Appearance, content.DefaultCharacterAppearance()) { t.Fatal("creation without customization did not get the default anatomy", err) } }