Files
aeternalis/server/internal/gitops/worlds_test.go
T
dingoandClaude Sonnet 5 5df2f1b681 Merge control portal work from RPGTheory/aeternalis-portal
RPGTheory/aeternalis-portal had diverged from this repo (the git/CI/CD
copy) with an in-progress control portal feature that was never synced
back here, so none of it had gone through CI or been deployed:

- New site/game/control routing split: index.html becomes a world
  directory landing page (site.ts), game.html is the renamed former
  index.html (main.ts), control.html is a new admin panel
  (control.ts, portal-shared.ts, portal.css).
- Server: new internal/store/portal.go (character/world registry,
  command queue, admin grants) and internal/transport/control.go
  (portal API routes), plus a new internal/gitops package that lets
  the portal provision new worlds via a GitOps repo commit.
- game.World gains BornAt/DiedAt/DeathReason and an operator Control()
  path (close/open admissions, awaken boss, end world, per-character
  rename/heal/unstuck/retire); death no longer reuses the same
  character id on rejoin, matching the "new life" permadeath model in
  Plan.md rather than the old bypass-prevention test's expectations.
- deploy/: new control-egress network policy and portal/control
  values (admin role, GitOps repo/branch/values file/CIDRs).
- New e2e coverage: tests/portal.spec.ts, tools/capture-world.mjs.

Verified this repo's own independent commits (800ms movement-staleness
window, 20s boss-attack poll, SRS/TLS deploy values, ubuntu-22.04
runner pin, DOCKER_HOST fix) were already present in the portal copy
before merging, so nothing here regresses them. `tsc --noEmit`, the
vite client build, and the protocol contract check all pass; no go.mod
changes. Could not run `go build`/`go test` locally (no Go toolchain
on PATH, Docker daemon not running) — every new/changed Go symbol was
manually cross-referenced and resolves, but this should still be built
and tested in CI before merging to master.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-06 11:08:44 -04:00

90 lines
3.1 KiB
Go

package gitops
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"go.yaml.in/yaml/v3"
)
func TestCreateWorldPreservesValuesAndRetries(t *testing.T) {
current := "# retained operator comment\nimage:\n digest: sha256:keep\nportal:\n host: srs.example.test\nworlds:\n - id: world-001\n seed: \"12\"\n host: srs.example.test\n"
puts := 0
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "token test-token" {
t.Error("missing token")
}
if r.URL.Path != "/api/v1/repos/owner/game/contents/deploy/values.yaml" {
t.Error("wrong path", r.URL.Path)
}
if r.Method == "GET" {
if r.URL.Query().Get("ref") != "master" {
t.Error("wrong branch")
}
_ = json.NewEncoder(w).Encode(map[string]string{"sha": "revision", "content": base64.StdEncoding.EncodeToString([]byte(current))})
return
}
var body map[string]string
_ = json.NewDecoder(r.Body).Decode(&body)
if body["sha"] != "revision" || body["branch"] != "master" {
t.Error("missing optimistic revision")
}
puts++
if puts == 1 {
w.WriteHeader(409)
return
}
raw, err := base64.StdEncoding.DecodeString(body["content"])
if err != nil {
t.Fatal(err)
}
current = string(raw)
w.WriteHeader(200)
_, _ = w.Write([]byte(`{}`))
}))
defer server.Close()
c, err := New(Config{server.URL + "/owner/game.git", "master", "deploy/values.yaml", "test-token", "https://srs.example.test"})
if err != nil {
t.Fatal(err)
}
c.http = server.Client()
if _, err = c.CreateWorld(context.Background(), "world-002", "42"); err != nil {
t.Fatal(err)
}
if puts != 2 || !strings.Contains(current, "retained operator comment") || !strings.Contains(current, "sha256:keep") {
t.Fatal("unrelated values changed", current)
}
var values struct {
Worlds []struct{ ID, Seed, Host string }
}
if err = yaml.Unmarshal([]byte(current), &values); err != nil || len(values.Worlds) != 2 || values.Worlds[1].Seed != "42" {
t.Fatal("bad world definition", err, current)
}
if _, err = c.CreateWorld(context.Background(), "world-002", "42"); err != nil || puts != 2 {
t.Fatal("retry duplicated world", err)
}
if _, err = c.CreateWorld(context.Background(), "world-002", "43"); err == nil {
t.Fatal("existing ID reused")
}
if _, err = c.CreateWorld(context.Background(), "../bad", "42"); err == nil {
t.Fatal("invalid ID accepted")
}
}
func TestGitOpsConfigurationValidation(t *testing.T) {
for _, repository := range []string{"http://example.test/o/r", "https://token@example.test/o/r", "https://example.test/only-one", "https://example.test/../repo"} {
if _, err := New(Config{repository, "master", "deploy/values.yaml", "token", "https://srs.example.test"}); err == nil {
t.Fatal("unsafe repository accepted", repository)
}
}
for _, file := range []string{"../secret.yaml", "/absolute.yaml", "values.json", "a/../values.yaml"} {
if _, err := New(Config{"https://example.test/o/r", "master", file, "token", "https://srs.example.test"}); err == nil {
t.Fatal("unsafe file accepted", file)
}
}
}