From d79a2d57303ba3610a68c93da1b0eb4a1628d6e7 Mon Sep 17 00:00:00 2001 From: heavyscientist Date: Mon, 17 Feb 2025 23:18:32 -0800 Subject: [PATCH] export the map specs on download --- pkg/api/generate_test.go | 96 +++++++++++++++++++++++++++++++++++++--- pkg/rustmaps/download.go | 12 +++++ 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/pkg/api/generate_test.go b/pkg/api/generate_test.go index 1f57c93..a152751 100644 --- a/pkg/api/generate_test.go +++ b/pkg/api/generate_test.go @@ -26,7 +26,6 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { var response *RustMapsGenerateResponse if req.MapParameters.Seed == "1" { - w.WriteHeader(http.StatusOK) response = &RustMapsGenerateResponse{ Meta: RustMapsGenerateResponseMeta{ Status: "complete", @@ -34,8 +33,23 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { Errors: []string{}, }, } + } else if req.MapParameters.Seed == "1111" { + response = &RustMapsGenerateResponse{ + Meta: RustMapsGenerateResponseMeta{ + Status: "complete", + StatusCode: 201, + Errors: []string{}, + }, + } + } else if req.MapParameters.Seed == "11" { + response = &RustMapsGenerateResponse{ + Meta: RustMapsGenerateResponseMeta{ + Status: "complete", + StatusCode: 400, + Errors: []string{}, + }, + } } else if req.MapParameters.Seed == "2" { - w.WriteHeader(http.StatusUnauthorized) response = &RustMapsGenerateResponse{ Meta: RustMapsGenerateResponseMeta{ Status: "complete", @@ -44,7 +58,6 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { }, } } else if req.MapParameters.Seed == "3" { - w.WriteHeader(http.StatusForbidden) response = &RustMapsGenerateResponse{ Meta: RustMapsGenerateResponseMeta{ Status: "complete", @@ -53,7 +66,6 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { }, } } else if req.MapParameters.Seed == "4" { - w.WriteHeader(http.StatusConflict) response = &RustMapsGenerateResponse{ Meta: RustMapsGenerateResponseMeta{ Status: "complete", @@ -62,7 +74,6 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { }, } } else { - w.WriteHeader(http.StatusInternalServerError) response = &RustMapsGenerateResponse{ Meta: RustMapsGenerateResponseMeta{ Status: "error", @@ -71,6 +82,7 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { }, } } + w.WriteHeader(response.Meta.StatusCode) json.NewEncoder(w).Encode(response) return } @@ -118,6 +130,55 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { }, }, }, + { + name: "GenerateCustom 201", + fields: fields{ + apiURL: mockServer.URL, + apiKey: "test", + rateLimiter: &RateLimiter{}, + }, + args: args{ + log: zap.NewNop(), + m: &types.Map{ + Size: 3500, + Seed: "1111", + Staging: false, + SavedConfig: "default", + }, + }, + want: &RustMapsGenerateResponse{ + Meta: RustMapsGenerateResponseMeta{ + Status: "complete", + StatusCode: 201, + Errors: []string{}, + }, + }, + }, + { + name: "GenerateCustom 400", + fields: fields{ + apiURL: mockServer.URL, + apiKey: "test", + rateLimiter: &RateLimiter{}, + }, + args: args{ + log: zap.NewNop(), + m: &types.Map{ + Size: 3500, + Seed: "11", + Staging: false, + SavedConfig: "default", + }, + }, + want: &RustMapsGenerateResponse{ + Meta: RustMapsGenerateResponseMeta{ + Status: "complete", + StatusCode: 400, + Errors: []string{}, + }, + }, + wantErr: true, + }, { name: "GenerateCustom 401", fields: fields{ @@ -189,6 +250,31 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) { }, { name: "GenerateCustom 500", + fields: fields{ + apiURL: mockServer.URL, + apiKey: "test", + rateLimiter: &RateLimiter{}, + }, + args: args{ + log: zap.NewNop(), + m: &types.Map{ + Size: 3500, + Seed: "111", + Staging: false, + SavedConfig: "default", + }, + }, + want: &RustMapsGenerateResponse{ + Meta: RustMapsGenerateResponseMeta{ + Status: "complete", + StatusCode: 400, + Errors: []string{}, + }, + }, + wantErr: true, + }, + { + name: "GenerateCustom 500 part 2", fields: fields{ apiURL: "http://localhost", apiKey: "test", diff --git a/pkg/rustmaps/download.go b/pkg/rustmaps/download.go index 3ae463d..c8628ec 100644 --- a/pkg/rustmaps/download.go +++ b/pkg/rustmaps/download.go @@ -143,6 +143,7 @@ func (g *Generator) Download(log *zap.Logger, version string) error { imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_icons.png", prefix)) thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_thumbnail.png", prefix)) downloadLinksTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_download_links.json", prefix)) + mapSpecsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_specs.json", prefix)) // create a json file next to the rest that contains the download urls log.Info("Downloading assets", zap.String("seed", m.Seed), zap.String("map_id", m.MapID)) links := DownloadLinks{ @@ -162,6 +163,17 @@ func (g *Generator) Download(log *zap.Logger, version string) error { return err } + mapSpecsData, err := json.MarshalIndent(status, "", " ") + if err != nil { + log.Error("Error marshalling JSON", zap.Error(err)) + return err + } + log.Info("Writing map specs", zap.String("target", mapSpecsTarget)) + if err := os.WriteFile(mapSpecsTarget, mapSpecsData, 0644); err != nil { + log.Error("Error writing JSON file", zap.Error(err)) + return err + } + if err := g.DownloadFile(log, status.Data.DownloadURL, mapTarget); err != nil { log.Error("Error downloading map", zap.String("seed", m.Seed), zap.Error(err)) return err