Fix Close/Invalidate/Reopen to handle API array response

The Checkvist API returns an array of tasks (containing the modified task
and potentially its subtasks) for close, reopen, and invalidate operations.
The code was incorrectly trying to decode into a single Task struct.

Changes:
- Decode response into []Task instead of Task
- Return first element (the modified task)
- Add defensive error handling for empty arrays
- Update tests to mock array responses

Fixes: checkvist-api-2zr
This commit is contained in:
Oliver Jakoubek 2026-01-15 10:48:38 +01:00
commit b716d4d0fe
3 changed files with 37 additions and 18 deletions

View file

@ -277,10 +277,11 @@ func TestTasks_Close(t *testing.T) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
response := Task{
// API returns an array containing the modified task and potentially subtasks
response := []Task{{
ID: 101,
Status: StatusClosed,
}
}}
json.NewEncoder(w).Encode(response)
default:
t.Errorf("unexpected path: %s", r.URL.Path)
@ -310,10 +311,11 @@ func TestTasks_Reopen(t *testing.T) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
response := Task{
// API returns an array containing the modified task and potentially subtasks
response := []Task{{
ID: 101,
Status: StatusOpen,
}
}}
json.NewEncoder(w).Encode(response)
default:
t.Errorf("unexpected path: %s", r.URL.Path)
@ -343,10 +345,11 @@ func TestTasks_Invalidate(t *testing.T) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
response := Task{
// API returns an array containing the modified task and potentially subtasks
response := []Task{{
ID: 101,
Status: StatusInvalidated,
}
}}
json.NewEncoder(w).Encode(response)
default:
t.Errorf("unexpected path: %s", r.URL.Path)