Skip to content

Commit 2c3e672

Browse files
heavyscientistheavyscientist
authored andcommitted
export the map specs on download
1 parent 25ad390 commit 2c3e672

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

pkg/api/generate_test.go

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,30 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) {
2626

2727
var response *RustMapsGenerateResponse
2828
if req.MapParameters.Seed == "1" {
29-
w.WriteHeader(http.StatusOK)
3029
response = &RustMapsGenerateResponse{
3130
Meta: RustMapsGenerateResponseMeta{
3231
Status: "complete",
3332
StatusCode: 200,
3433
Errors: []string{},
3534
},
3635
}
36+
} else if req.MapParameters.Seed == "1111" {
37+
response = &RustMapsGenerateResponse{
38+
Meta: RustMapsGenerateResponseMeta{
39+
Status: "complete",
40+
StatusCode: 201,
41+
Errors: []string{},
42+
},
43+
}
44+
} else if req.MapParameters.Seed == "11" {
45+
response = &RustMapsGenerateResponse{
46+
Meta: RustMapsGenerateResponseMeta{
47+
Status: "complete",
48+
StatusCode: 400,
49+
Errors: []string{},
50+
},
51+
}
3752
} else if req.MapParameters.Seed == "2" {
38-
w.WriteHeader(http.StatusUnauthorized)
3953
response = &RustMapsGenerateResponse{
4054
Meta: RustMapsGenerateResponseMeta{
4155
Status: "complete",
@@ -44,7 +58,6 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) {
4458
},
4559
}
4660
} else if req.MapParameters.Seed == "3" {
47-
w.WriteHeader(http.StatusForbidden)
4861
response = &RustMapsGenerateResponse{
4962
Meta: RustMapsGenerateResponseMeta{
5063
Status: "complete",
@@ -53,7 +66,6 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) {
5366
},
5467
}
5568
} else if req.MapParameters.Seed == "4" {
56-
w.WriteHeader(http.StatusConflict)
5769
response = &RustMapsGenerateResponse{
5870
Meta: RustMapsGenerateResponseMeta{
5971
Status: "complete",
@@ -62,7 +74,6 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) {
6274
},
6375
}
6476
} else {
65-
w.WriteHeader(http.StatusInternalServerError)
6677
response = &RustMapsGenerateResponse{
6778
Meta: RustMapsGenerateResponseMeta{
6879
Status: "error",
@@ -71,6 +82,7 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) {
7182
},
7283
}
7384
}
85+
w.WriteHeader(response.Meta.StatusCode)
7486
json.NewEncoder(w).Encode(response)
7587
return
7688
}
@@ -118,6 +130,55 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) {
118130
},
119131
},
120132
},
133+
{
134+
name: "GenerateCustom 201",
135+
fields: fields{
136+
apiURL: mockServer.URL,
137+
apiKey: "test",
138+
rateLimiter: &RateLimiter{},
139+
},
140+
args: args{
141+
log: zap.NewNop(),
142+
m: &types.Map{
143+
Size: 3500,
144+
Seed: "1111",
145+
Staging: false,
146+
SavedConfig: "default",
147+
},
148+
},
149+
want: &RustMapsGenerateResponse{
150+
Meta: RustMapsGenerateResponseMeta{
151+
Status: "complete",
152+
StatusCode: 201,
153+
Errors: []string{},
154+
},
155+
},
156+
},
157+
{
158+
name: "GenerateCustom 400",
159+
fields: fields{
160+
apiURL: mockServer.URL,
161+
apiKey: "test",
162+
rateLimiter: &RateLimiter{},
163+
},
164+
args: args{
165+
log: zap.NewNop(),
166+
m: &types.Map{
167+
Size: 3500,
168+
Seed: "11",
169+
Staging: false,
170+
SavedConfig: "default",
171+
},
172+
},
173+
want: &RustMapsGenerateResponse{
174+
Meta: RustMapsGenerateResponseMeta{
175+
Status: "complete",
176+
StatusCode: 400,
177+
Errors: []string{},
178+
},
179+
},
180+
wantErr: true,
181+
},
121182
{
122183
name: "GenerateCustom 401",
123184
fields: fields{
@@ -189,6 +250,31 @@ func TestRustMapsClient_GenerateCustom(t *testing.T) {
189250
},
190251
{
191252
name: "GenerateCustom 500",
253+
fields: fields{
254+
apiURL: mockServer.URL,
255+
apiKey: "test",
256+
rateLimiter: &RateLimiter{},
257+
},
258+
args: args{
259+
log: zap.NewNop(),
260+
m: &types.Map{
261+
Size: 3500,
262+
Seed: "111",
263+
Staging: false,
264+
SavedConfig: "default",
265+
},
266+
},
267+
want: &RustMapsGenerateResponse{
268+
Meta: RustMapsGenerateResponseMeta{
269+
Status: "complete",
270+
StatusCode: 400,
271+
Errors: []string{},
272+
},
273+
},
274+
wantErr: true,
275+
},
276+
{
277+
name: "GenerateCustom 500 part 2",
192278
fields: fields{
193279
apiURL: "http://localhost",
194280
apiKey: "test",

pkg/rustmaps/download.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func (g *Generator) Download(log *zap.Logger, version string) error {
143143
imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_icons.png", prefix))
144144
thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_thumbnail.png", prefix))
145145
downloadLinksTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_download_links.json", prefix))
146+
mapSpecsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_specs.json", prefix))
146147
// create a json file next to the rest that contains the download urls
147148
log.Info("Downloading assets", zap.String("seed", m.Seed), zap.String("map_id", m.MapID))
148149
links := DownloadLinks{
@@ -162,6 +163,17 @@ func (g *Generator) Download(log *zap.Logger, version string) error {
162163
return err
163164
}
164165

166+
mapSpecsData, err := json.MarshalIndent(status, "", " ")
167+
if err != nil {
168+
log.Error("Error marshalling JSON", zap.Error(err))
169+
return err
170+
}
171+
log.Info("Writing map specs", zap.String("target", mapSpecsTarget))
172+
if err := os.WriteFile(mapSpecsTarget, mapSpecsData, 0644); err != nil {
173+
log.Error("Error writing JSON file", zap.Error(err))
174+
return err
175+
}
176+
165177
if err := g.DownloadFile(log, status.Data.DownloadURL, mapTarget); err != nil {
166178
log.Error("Error downloading map", zap.String("seed", m.Seed), zap.Error(err))
167179
return err

0 commit comments

Comments
 (0)