mirror of
https://github.com/Yuyi-Oak/BlueArchiveToolkit.git
synced 2026-09-18 07:24:55 +08:00
113 lines
4.3 KiB
Go
113 lines
4.3 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"bat-api/internal/backendrpc"
|
|
)
|
|
|
|
func readContractFixture(t *testing.T, name string) []byte {
|
|
t.Helper()
|
|
data, err := os.ReadFile(filepath.Join("testdata", "contract", name))
|
|
if err != nil {
|
|
t.Fatalf("read contract fixture %s: %v", name, err)
|
|
}
|
|
return data
|
|
}
|
|
|
|
func TestRustContractFixturesPreserveGoMirror(t *testing.T) {
|
|
availableRaw := readContractFixture(t, "catalog-status.available.json")
|
|
unavailableRaw := readContractFixture(t, "catalog-status.unavailable.json")
|
|
manifestRaw := readContractFixture(t, "resource-manifest.page0.json")
|
|
snapshotRaw := readContractFixture(t, "official-sync-snapshot.json")
|
|
|
|
for name, raw := range map[string][]byte{
|
|
"catalog available": availableRaw,
|
|
"catalog unavailable": unavailableRaw,
|
|
"resource manifest": manifestRaw,
|
|
"snapshot": snapshotRaw,
|
|
} {
|
|
if bytes.Contains(raw, []byte("/tmp/")) {
|
|
t.Fatalf("%s contains an absolute temporary path", name)
|
|
}
|
|
if bytes.Contains(raw, []byte("r93_fixture")) {
|
|
t.Fatalf("%s contains a concrete release token", name)
|
|
}
|
|
}
|
|
|
|
summary, resourceRoot, available := parseCatalogStatus(availableRaw)
|
|
if !available || summary == nil {
|
|
t.Fatal("available catalog fixture was not accepted")
|
|
}
|
|
if summary.Status != "published" || summary.StatusCode != "official.published" {
|
|
t.Fatalf("catalog status=%+v", summary)
|
|
}
|
|
if summary.DistributionStatusCode != "distribution.ready" {
|
|
t.Fatalf("distribution status=%q", summary.DistributionStatusCode)
|
|
}
|
|
if resourceRoot != "${RESOURCE_ROOT}" || summary.VersionID != "${VERSION_ID}" {
|
|
t.Fatalf("catalog root/version=%q/%q", resourceRoot, summary.VersionID)
|
|
}
|
|
if summary.LauncherMetadata == nil || summary.GameMainConfig == nil {
|
|
t.Fatalf("catalog optional objects missing: %+v", summary)
|
|
}
|
|
if summary.LauncherMetadata.GameLowestVersion != "" {
|
|
t.Fatalf("null optional field became non-empty: %q", summary.LauncherMetadata.GameLowestVersion)
|
|
}
|
|
if summary.GameMainConfig.DefaultConnectionGroup != "${CONNECTION_GROUP}" {
|
|
t.Fatalf("game config=%+v", summary.GameMainConfig)
|
|
}
|
|
|
|
if got, _, ok := parseCatalogStatus(unavailableRaw); got != nil || ok {
|
|
t.Fatalf("unavailable catalog parsed as available: got=%+v ok=%v", got, ok)
|
|
}
|
|
|
|
var manifest backendrpc.ResourceManifestPage
|
|
if err := json.Unmarshal(manifestRaw, &manifest); err != nil {
|
|
t.Fatalf("decode resource manifest: %v", err)
|
|
}
|
|
if !manifest.Available || manifest.ManifestVersion != 1 || manifest.TotalEntries != 2 {
|
|
t.Fatalf("manifest header=%+v", manifest)
|
|
}
|
|
if len(manifest.Entries) != 2 {
|
|
t.Fatalf("manifest entries=%d", len(manifest.Entries))
|
|
}
|
|
if manifest.Entries[0].Bytes == nil || *manifest.Entries[0].Bytes != 21 {
|
|
t.Fatalf("manifest first bytes=%v", manifest.Entries[0].Bytes)
|
|
}
|
|
if manifest.Entries[0].BLAKE3 == "" || !strings.Contains(manifest.Entries[0].Destination, "{addressables-root}") {
|
|
t.Fatalf("manifest first entry=%+v", manifest.Entries[0])
|
|
}
|
|
|
|
var snapshot struct {
|
|
AppVersion string `json:"app_version"`
|
|
BundleVersion string `json:"bundle_version"`
|
|
GameMainConfigBootstrap *GameMainConfigSummary `json:"game_main_config_bootstrap"`
|
|
LauncherMetadata *LauncherMetadataSummary `json:"launcher_metadata"`
|
|
LegacyGameMainConfig json.RawMessage `json:"game_main_config"`
|
|
}
|
|
if err := json.Unmarshal(snapshotRaw, &snapshot); err != nil {
|
|
t.Fatalf("decode snapshot: %v", err)
|
|
}
|
|
if snapshot.AppVersion != "${APP_VERSION}" || snapshot.BundleVersion != "${BUNDLE_VERSION}" {
|
|
t.Fatalf("snapshot versions=%q/%q", snapshot.AppVersion, snapshot.BundleVersion)
|
|
}
|
|
if snapshot.GameMainConfigBootstrap == nil || snapshot.GameMainConfigBootstrap.ServerInfoDataURL == "" {
|
|
t.Fatalf("snapshot game config=%+v", snapshot.GameMainConfigBootstrap)
|
|
}
|
|
if snapshot.GameMainConfigBootstrap.ServerInfoDataURL != "https://yostar-serverinfo.bluearchiveyostar.com/{server-info-path}" {
|
|
t.Fatalf("snapshot server info=%q", snapshot.GameMainConfigBootstrap.ServerInfoDataURL)
|
|
}
|
|
if snapshot.LauncherMetadata == nil || snapshot.LauncherMetadata.ManifestSource == "" {
|
|
t.Fatalf("snapshot launcher metadata=%+v", snapshot.LauncherMetadata)
|
|
}
|
|
if len(snapshot.LegacyGameMainConfig) != 0 {
|
|
t.Fatalf("legacy game_main_config field unexpectedly present: %s", snapshot.LegacyGameMainConfig)
|
|
}
|
|
}
|