81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package sdkserver
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"gac-hkrpg-go/internal/testutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func chdirRepoRootSDK(t *testing.T) {
|
|
t.Helper()
|
|
_, source, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("cannot locate test helper source")
|
|
}
|
|
root := filepath.Clean(filepath.Join(filepath.Dir(source), "..", ".."))
|
|
if _, err := os.Stat(filepath.Join(root, "go.mod")); err != nil {
|
|
t.Fatalf("cannot locate repository root from %s: %v", source, err)
|
|
}
|
|
temporary := t.TempDir()
|
|
if err := testutil.CopyRuntime(root, temporary); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Chdir(temporary)
|
|
}
|
|
|
|
func TestApiCommandUnlockAllWarp(t *testing.T) {
|
|
chdirRepoRootSDK(t)
|
|
before, err := os.ReadFile("freesr-data.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
runCmd := func(command string) (bool, string) {
|
|
body, _ := json.Marshal(map[string]string{"command": command})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/command", strings.NewReader(string(body)))
|
|
rec := httptest.NewRecorder()
|
|
apiCommand(rec, req)
|
|
var res struct {
|
|
Success bool `json:"success"`
|
|
Output string `json:"output"`
|
|
}
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &res)
|
|
return res.Success, res.Output
|
|
}
|
|
|
|
// 1. /unlockall warp all
|
|
ok, out := runCmd("/unlockall warp all")
|
|
if !ok || !strings.Contains(out, "Set all warps") {
|
|
t.Fatalf("unexpected output for /unlockall warp all: ok=%v, out=%s", ok, out)
|
|
}
|
|
|
|
// 2. /unlockall warp off
|
|
ok, out = runCmd("/unlockall warp off")
|
|
if !ok || !strings.Contains(out, "default active warps") {
|
|
t.Fatalf("unexpected output for /unlockall warp off: ok=%v, out=%s", ok, out)
|
|
}
|
|
|
|
// 3. /unlockall warp avatar
|
|
ok, out = runCmd("/unlockall warp avatar")
|
|
if !ok || !strings.Contains(out, "character warps") {
|
|
t.Fatalf("unexpected output for /unlockall warp avatar: ok=%v, out=%s", ok, out)
|
|
}
|
|
|
|
// 4. /reload banner
|
|
ok, out = runCmd("/reload banner")
|
|
if !ok || !strings.Contains(out, "Banner configurations reloaded") {
|
|
t.Fatalf("unexpected output for /reload banner: ok=%v, out=%s", ok, out)
|
|
}
|
|
after, err := os.ReadFile("freesr-data.json")
|
|
if err != nil || !bytes.Equal(before, after) {
|
|
t.Fatalf("banner commands rewrote avatar inventory: %v", err)
|
|
}
|
|
}
|