Skip to content

Commit 0c4e130

Browse files
committed
refactor(tests): restructure BulkStatusResponse to use Data and Metadata fields for better organization
The BulkStatusResponse structure is updated to encapsulate the main data within a Data field and additional metadata within a Metadata field. This change enhances the clarity and organization of the response, making it easier to manage and understand the status of bulk operations. The tests are also updated accordingly to reflect this new structure.
1 parent aefd4ae commit 0c4e130

File tree

2 files changed

+218
-178
lines changed

2 files changed

+218
-178
lines changed

internal/api/client_bulk_test.go

Lines changed: 116 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -247,34 +247,33 @@ func TestClient_GetBulkStatus(t *testing.T) {
247247
name: "successful status retrieval",
248248
batchID: "batch-123",
249249
mockResponse: dto.BulkStatusResponse{
250-
BatchID: "batch-123",
251-
Status: "processing",
252-
Runs: []dto.RunStatusItem{
253-
{
254-
ID: 1,
255-
Title: "Task 1",
256-
Status: "completed",
257-
Progress: 100,
258-
RunURL: "https://repobird.ai/runs/1",
250+
Data: dto.BulkStatusData{
251+
BatchID: "batch-123",
252+
Status: "processing",
253+
Runs: []dto.RunStatusItem{
254+
{
255+
ID: 1,
256+
Title: "Task 1",
257+
Status: "completed",
258+
Progress: 100,
259+
PRURL: &[]string{"https://repobird.ai/runs/1"}[0],
260+
},
261+
{
262+
ID: 2,
263+
Title: "Task 2",
264+
Status: "processing",
265+
Progress: 50,
266+
},
259267
},
260-
{
261-
ID: 2,
262-
Title: "Task 2",
263-
Status: "processing",
264-
Progress: 50,
265-
Message: "Analyzing code...",
268+
Metadata: dto.BulkStatusMetadata{
269+
TotalRuns: 2,
270+
Queued: 0,
271+
Processing: 1,
272+
Completed: 1,
273+
Failed: 0,
274+
StartedAt: time.Now().Add(-5 * time.Minute).Format(time.RFC3339),
266275
},
267276
},
268-
Statistics: dto.BulkStatistics{
269-
Total: 2,
270-
Queued: 0,
271-
Processing: 1,
272-
Completed: 1,
273-
Failed: 0,
274-
Cancelled: 0,
275-
},
276-
CreatedAt: time.Now().Add(-5 * time.Minute),
277-
UpdatedAt: time.Now(),
278277
},
279278
mockStatusCode: http.StatusOK,
280279
expectedError: false,
@@ -283,29 +282,31 @@ func TestClient_GetBulkStatus(t *testing.T) {
283282
name: "batch completed",
284283
batchID: "batch-456",
285284
mockResponse: dto.BulkStatusResponse{
286-
BatchID: "batch-456",
287-
Status: "completed",
288-
Runs: []dto.RunStatusItem{
289-
{
290-
ID: 1,
291-
Title: "Task 1",
292-
Status: "completed",
293-
Progress: 100,
294-
CompletedAt: &[]time.Time{time.Now()}[0],
285+
Data: dto.BulkStatusData{
286+
BatchID: "batch-456",
287+
Status: "completed",
288+
Runs: []dto.RunStatusItem{
289+
{
290+
ID: 1,
291+
Title: "Task 1",
292+
Status: "completed",
293+
Progress: 100,
294+
CompletedAt: &[]string{time.Now().Format(time.RFC3339)}[0],
295+
},
296+
{
297+
ID: 2,
298+
Title: "Task 2",
299+
Status: "failed",
300+
CompletedAt: &[]string{time.Now().Format(time.RFC3339)}[0],
301+
},
295302
},
296-
{
297-
ID: 2,
298-
Title: "Task 2",
299-
Status: "failed",
300-
Error: "Build failed",
301-
CompletedAt: &[]time.Time{time.Now()}[0],
303+
Metadata: dto.BulkStatusMetadata{
304+
TotalRuns: 2,
305+
Completed: 1,
306+
Failed: 1,
307+
StartedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339),
302308
},
303309
},
304-
Statistics: dto.BulkStatistics{
305-
Total: 2,
306-
Completed: 1,
307-
Failed: 1,
308-
},
309310
},
310311
mockStatusCode: http.StatusOK,
311312
expectedError: false,
@@ -354,9 +355,9 @@ func TestClient_GetBulkStatus(t *testing.T) {
354355
require.NoError(t, err)
355356
assert.NotNil(t, result)
356357
if response, ok := tt.mockResponse.(dto.BulkStatusResponse); ok {
357-
assert.Equal(t, response.BatchID, result.BatchID)
358-
assert.Equal(t, response.Status, result.Status)
359-
assert.Equal(t, len(response.Runs), len(result.Runs))
358+
assert.Equal(t, response.Data.BatchID, result.Data.BatchID)
359+
assert.Equal(t, response.Data.Status, result.Data.Status)
360+
assert.Equal(t, len(response.Data.Runs), len(result.Data.Runs))
360361
}
361362
}
362363
})
@@ -445,32 +446,41 @@ func TestClient_PollBulkStatus(t *testing.T) {
445446
case 1:
446447
// First call: processing
447448
response = dto.BulkStatusResponse{
448-
BatchID: batchID,
449-
Status: "processing",
450-
Statistics: dto.BulkStatistics{
451-
Total: 2,
452-
Processing: 2,
449+
Data: dto.BulkStatusData{
450+
BatchID: batchID,
451+
Status: "processing",
452+
Metadata: dto.BulkStatusMetadata{
453+
TotalRuns: 2,
454+
Processing: 2,
455+
StartedAt: "2024-01-01T10:00:00Z",
456+
},
453457
},
454458
}
455459
case 2:
456460
// Second call: still processing
457461
response = dto.BulkStatusResponse{
458-
BatchID: batchID,
459-
Status: "processing",
460-
Statistics: dto.BulkStatistics{
461-
Total: 2,
462-
Processing: 1,
463-
Completed: 1,
462+
Data: dto.BulkStatusData{
463+
BatchID: batchID,
464+
Status: "processing",
465+
Metadata: dto.BulkStatusMetadata{
466+
TotalRuns: 2,
467+
Processing: 1,
468+
Completed: 1,
469+
StartedAt: "2024-01-01T10:00:00Z",
470+
},
464471
},
465472
}
466473
default:
467474
// Final call: completed
468475
response = dto.BulkStatusResponse{
469-
BatchID: batchID,
470-
Status: "completed",
471-
Statistics: dto.BulkStatistics{
472-
Total: 2,
473-
Completed: 2,
476+
Data: dto.BulkStatusData{
477+
BatchID: batchID,
478+
Status: "completed",
479+
Metadata: dto.BulkStatusMetadata{
480+
TotalRuns: 2,
481+
Completed: 2,
482+
StartedAt: "2024-01-01T10:00:00Z",
483+
},
474484
},
475485
}
476486
}
@@ -493,25 +503,32 @@ func TestClient_PollBulkStatus(t *testing.T) {
493503
var updates []dto.BulkStatusResponse
494504
for status := range statusChan {
495505
updates = append(updates, status)
496-
if status.Status == "completed" {
506+
if status.Data.Status == "completed" {
497507
break
498508
}
499509
}
500510

501511
// Verify we got updates
502512
assert.Greater(t, len(updates), 0)
503513
lastUpdate := updates[len(updates)-1]
504-
assert.Equal(t, "completed", lastUpdate.Status)
505-
assert.Equal(t, 2, lastUpdate.Statistics.Completed)
514+
assert.Equal(t, "completed", lastUpdate.Data.Status)
515+
assert.Equal(t, 2, lastUpdate.Data.Metadata.Completed)
506516
})
507517

508518
t.Run("polling with context cancellation", func(t *testing.T) {
509519
batchID := "batch-456"
510520

511521
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
512522
response := dto.BulkStatusResponse{
513-
BatchID: batchID,
514-
Status: "processing",
523+
Data: dto.BulkStatusData{
524+
BatchID: batchID,
525+
Status: "processing",
526+
Metadata: dto.BulkStatusMetadata{
527+
TotalRuns: 2,
528+
Processing: 2,
529+
StartedAt: "2024-01-01T10:00:00Z",
530+
},
531+
},
515532
}
516533
w.WriteHeader(http.StatusOK)
517534
_ = json.NewEncoder(w).Encode(response)
@@ -528,7 +545,7 @@ func TestClient_PollBulkStatus(t *testing.T) {
528545
// Get first update
529546
select {
530547
case status := <-statusChan:
531-
assert.Equal(t, "processing", status.Status)
548+
assert.Equal(t, "processing", status.Data.Status)
532549
case <-time.After(1 * time.Second):
533550
t.Fatal("timeout waiting for status")
534551
}
@@ -550,11 +567,14 @@ func TestClient_PollBulkStatus(t *testing.T) {
550567

551568
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
552569
response := dto.BulkStatusResponse{
553-
BatchID: batchID,
554-
Status: "failed",
555-
Statistics: dto.BulkStatistics{
556-
Total: 2,
557-
Failed: 2,
570+
Data: dto.BulkStatusData{
571+
BatchID: batchID,
572+
Status: "FAILED",
573+
Metadata: dto.BulkStatusMetadata{
574+
TotalRuns: 2,
575+
Failed: 2,
576+
StartedAt: "2024-01-01T10:00:00Z",
577+
},
558578
},
559579
}
560580
w.WriteHeader(http.StatusOK)
@@ -570,7 +590,7 @@ func TestClient_PollBulkStatus(t *testing.T) {
570590

571591
// Should get failed status and then channel closes
572592
status := <-statusChan
573-
assert.Equal(t, "failed", status.Status)
593+
assert.Equal(t, "FAILED", status.Data.Status)
574594

575595
// Channel should be closed
576596
_, ok := <-statusChan
@@ -598,8 +618,15 @@ func TestClient_PollBulkStatus(t *testing.T) {
598618
} else {
599619
// Subsequent calls succeed
600620
response := dto.BulkStatusResponse{
601-
BatchID: batchID,
602-
Status: "completed",
621+
Data: dto.BulkStatusData{
622+
BatchID: batchID,
623+
Status: "completed",
624+
Metadata: dto.BulkStatusMetadata{
625+
TotalRuns: 2,
626+
Completed: 2,
627+
StartedAt: "2024-01-01T10:00:00Z",
628+
},
629+
},
603630
}
604631
w.WriteHeader(http.StatusOK)
605632
_ = json.NewEncoder(w).Encode(response)
@@ -618,7 +645,7 @@ func TestClient_PollBulkStatus(t *testing.T) {
618645
// Should eventually get a successful response despite initial error
619646
select {
620647
case status := <-statusChan:
621-
assert.Equal(t, "completed", status.Status)
648+
assert.Equal(t, "completed", status.Data.Status)
622649
case <-time.After(3 * time.Second):
623650
t.Fatal("timeout waiting for successful status after error")
624651
}
@@ -671,8 +698,15 @@ func TestClient_BulkRetryBehavior(t *testing.T) {
671698
// Succeed on second attempt
672699
w.WriteHeader(http.StatusOK)
673700
_ = json.NewEncoder(w).Encode(dto.BulkStatusResponse{
674-
BatchID: "batch-123",
675-
Status: "processing",
701+
Data: dto.BulkStatusData{
702+
BatchID: "batch-123",
703+
Status: "processing",
704+
Metadata: dto.BulkStatusMetadata{
705+
TotalRuns: 2,
706+
Processing: 2,
707+
StartedAt: "2024-01-01T10:00:00Z",
708+
},
709+
},
676710
})
677711
}))
678712
defer server.Close()
@@ -682,7 +716,7 @@ func TestClient_BulkRetryBehavior(t *testing.T) {
682716
result, err := client.GetBulkStatus(context.Background(), "batch-123")
683717

684718
require.NoError(t, err)
685-
assert.Equal(t, "batch-123", result.BatchID)
719+
assert.Equal(t, "batch-123", result.Data.BatchID)
686720
assert.Equal(t, 2, callCount)
687721
})
688722
}

0 commit comments

Comments
 (0)