feat(service): add pagination to GetJobs endpoint
GetJobs now accepts page/page_size query parameters and returns JobListResponse instead of raw array. Uses in-memory pagination matching GetJobHistory pattern. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -188,7 +188,7 @@ func TestGetJobs_Success(t *testing.T) {
|
||||
|
||||
router := setupJobRouter(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/jobs", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/jobs?page=1&page_size=10", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
@@ -202,6 +202,93 @@ func TestGetJobs_Success(t *testing.T) {
|
||||
if !resp["success"].(bool) {
|
||||
t.Fatal("expected success=true")
|
||||
}
|
||||
data := resp["data"].(map[string]interface{})
|
||||
jobs := data["jobs"].([]interface{})
|
||||
if len(jobs) != 2 {
|
||||
t.Fatalf("expected 2 jobs, got %d", len(jobs))
|
||||
}
|
||||
if int(data["total"].(float64)) != 2 {
|
||||
t.Errorf("expected total=2, got %v", data["total"])
|
||||
}
|
||||
if int(data["page"].(float64)) != 1 {
|
||||
t.Errorf("expected page=1, got %v", data["page"])
|
||||
}
|
||||
if int(data["page_size"].(float64)) != 10 {
|
||||
t.Errorf("expected page_size=10, got %v", data["page_size"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetJobs_Pagination(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/slurm/v0.0.40/jobs", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(slurm.OpenapiJobInfoResp{
|
||||
Jobs: []slurm.JobInfo{
|
||||
{JobID: slurm.Ptr(int32(1)), Name: slurm.Ptr("job1")},
|
||||
{JobID: slurm.Ptr(int32(2)), Name: slurm.Ptr("job2")},
|
||||
{JobID: slurm.Ptr(int32(3)), Name: slurm.Ptr("job3")},
|
||||
},
|
||||
})
|
||||
})
|
||||
srv, handler := setupJobHandler(mux)
|
||||
defer srv.Close()
|
||||
|
||||
router := setupJobRouter(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/jobs?page=2&page_size=1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
data := resp["data"].(map[string]interface{})
|
||||
jobs := data["jobs"].([]interface{})
|
||||
if len(jobs) != 1 {
|
||||
t.Fatalf("expected 1 job on page 2, got %d", len(jobs))
|
||||
}
|
||||
if int(data["total"].(float64)) != 3 {
|
||||
t.Errorf("expected total=3, got %v", data["total"])
|
||||
}
|
||||
jobData := jobs[0].(map[string]interface{})
|
||||
if int(jobData["job_id"].(float64)) != 2 {
|
||||
t.Errorf("expected job_id=2 on page 2, got %v", jobData["job_id"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetJobs_DefaultPagination(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/slurm/v0.0.40/jobs", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(slurm.OpenapiJobInfoResp{Jobs: []slurm.JobInfo{}})
|
||||
})
|
||||
srv, handler := setupJobHandler(mux)
|
||||
defer srv.Close()
|
||||
|
||||
router := setupJobRouter(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/jobs", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
data := resp["data"].(map[string]interface{})
|
||||
if int(data["page"].(float64)) != 1 {
|
||||
t.Errorf("expected default page=1, got %v", data["page"])
|
||||
}
|
||||
if int(data["page_size"].(float64)) != 20 {
|
||||
t.Errorf("expected default page_size=20, got %v", data["page_size"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetJob_Success(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user