From 2f44308d4208844dc8a18d30bdf6b4aebeec09de Mon Sep 17 00:00:00 2001 From: Oliver Jakoubek Date: Wed, 14 Jan 2026 13:47:23 +0100 Subject: [PATCH] Add unit tests for Notes Create notes_test.go with tests: - TestNotes_List: list all notes on a task - TestNotes_Create: create new note - TestNotes_Update: update note comment - TestNotes_Delete: delete note Add testdata/notes/ fixtures: - list.json: sample notes list All 4 tests pass using httptest.Server mocking. Closes checkvist-api-bbx --- .beads/issues.jsonl | 2 +- notes_test.go | 172 +++++++++++++++++++++++++++++++++++++++ testdata/notes/list.json | 16 ++++ 3 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 notes_test.go create mode 100644 testdata/notes/list.json diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index 745290f..8e7875d 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -12,7 +12,7 @@ {"id":"checkvist-api-8q3","title":"Set up Mage build targets","description":"Create magefiles/magefile.go with:\n- Test() - run go test -v ./...\n- Coverage() - run go test -coverprofile=coverage.out ./...\n- Lint() - run staticcheck ./...\n- Fmt() - run gofmt -w .\n- Check() - run all quality checks (fmt, vet, staticcheck, test)\nEnsure magefiles has its own go.mod importing magefile.org/mage","status":"closed","priority":0,"issue_type":"task","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:31:09.228450637+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T13:33:08.511791793+01:00","closed_at":"2026-01-14T13:33:08.511791793+01:00","close_reason":"Closed","dependencies":[{"issue_id":"checkvist-api-8q3","depends_on_id":"checkvist-api-5wr","type":"blocks","created_at":"2026-01-14T12:32:48.556022687+01:00","created_by":"Oliver Jakoubek"}]} {"id":"checkvist-api-8u6","title":"Implement HTTP request helper with retry logic","description":"Add internal HTTP helper to client.go:\n- doRequest(ctx, method, path, body) helper for all API calls\n- Automatic authentication check before requests\n- JSON marshaling/unmarshaling\n- Exponential backoff retry for:\n - HTTP 429 (Too Many Requests)\n - HTTP 5xx (Server Errors)\n - Network errors (timeout, connection reset)\n- Respect context cancellation\n- Optional debug logging of requests/responses via slog","status":"closed","priority":0,"issue_type":"task","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:31:08.780244392+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T13:27:52.914675409+01:00","closed_at":"2026-01-14T13:27:52.914675409+01:00","close_reason":"Closed","dependencies":[{"issue_id":"checkvist-api-8u6","depends_on_id":"checkvist-api-ymg","type":"blocks","created_at":"2026-01-14T12:32:47.973194416+01:00","created_by":"Oliver Jakoubek"},{"issue_id":"checkvist-api-8u6","depends_on_id":"checkvist-api-mnh","type":"blocks","created_at":"2026-01-14T12:32:48.268500727+01:00","created_by":"Oliver Jakoubek"}]} {"id":"checkvist-api-93m","title":"Create CHANGELOG","description":"Create CHANGELOG.md following Keep a Changelog format:\n- [Unreleased] section for ongoing work\n- Initial release preparation notes\n- Document all features implemented","status":"closed","priority":0,"issue_type":"task","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:31:39.009748936+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T13:37:00.644317886+01:00","closed_at":"2026-01-14T13:37:00.644317886+01:00","close_reason":"Closed"} -{"id":"checkvist-api-bbx","title":"Write unit tests for Notes","description":"Create notes_test.go with tests:\n- TestNotes_List\n- TestNotes_Create\n- TestNotes_Update\n- TestNotes_Delete\nUse table-driven tests. Create testdata/notes/ fixtures.","status":"open","priority":0,"issue_type":"task","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:31:37.829382141+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T12:31:37.829382141+01:00","dependencies":[{"issue_id":"checkvist-api-bbx","depends_on_id":"checkvist-api-5ab","type":"blocks","created_at":"2026-01-14T12:33:14.119755191+01:00","created_by":"Oliver Jakoubek"}]} +{"id":"checkvist-api-bbx","title":"Write unit tests for Notes","description":"Create notes_test.go with tests:\n- TestNotes_List\n- TestNotes_Create\n- TestNotes_Update\n- TestNotes_Delete\nUse table-driven tests. Create testdata/notes/ fixtures.","status":"closed","priority":0,"issue_type":"task","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:31:37.829382141+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T13:47:17.141162679+01:00","closed_at":"2026-01-14T13:47:17.141162679+01:00","close_reason":"Closed","dependencies":[{"issue_id":"checkvist-api-bbx","depends_on_id":"checkvist-api-5ab","type":"blocks","created_at":"2026-01-14T12:33:14.119755191+01:00","created_by":"Oliver Jakoubek"}]} {"id":"checkvist-api-br3","title":"Core API Operations","description":"Phase 2: Implement CRUD operations for Checklists, Tasks, and Notes. All P0 (must-have) features for the library.","status":"open","priority":0,"issue_type":"epic","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:30:53.20627925+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T12:30:53.20627925+01:00"} {"id":"checkvist-api-c2k","title":"Implement Checklist operations","description":"Create checklists.go with ChecklistService:\n- client.Checklists() returns ChecklistService\n- List(ctx) ([]Checklist, error) - GET /checklists.json\n- Get(ctx, id) (*Checklist, error) - GET /checklists/{id}.json\n- Create(ctx, name) (*Checklist, error) - POST /checklists.json\n- Update(ctx, id, name) (*Checklist, error) - PUT /checklists/{id}.json\n- Delete(ctx, id) error - DELETE /checklists/{id}.json\n- Support archived filter in List\nContext support for all methods.","status":"closed","priority":0,"issue_type":"feature","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:30:53.566197933+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T13:38:06.162666425+01:00","closed_at":"2026-01-14T13:38:06.162666425+01:00","close_reason":"Closed","dependencies":[{"issue_id":"checkvist-api-c2k","depends_on_id":"checkvist-api-8u6","type":"blocks","created_at":"2026-01-14T12:32:54.533462004+01:00","created_by":"Oliver Jakoubek"},{"issue_id":"checkvist-api-c2k","depends_on_id":"checkvist-api-lpn","type":"blocks","created_at":"2026-01-14T12:32:54.859645166+01:00","created_by":"Oliver Jakoubek"}]} {"id":"checkvist-api-cb8","title":"Extended Features","description":"Phase 3: Implement P1 (should-have) features including client-side filtering and builder patterns for fluent interfaces.","status":"open","priority":1,"issue_type":"epic","owner":"mail@oliverjakoubek.de","created_at":"2026-01-14T12:30:55.624242123+01:00","created_by":"Oliver Jakoubek","updated_at":"2026-01-14T12:30:55.624242123+01:00"} diff --git a/notes_test.go b/notes_test.go new file mode 100644 index 0000000..d1c22b0 --- /dev/null +++ b/notes_test.go @@ -0,0 +1,172 @@ +package checkvist + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +func TestNotes_List(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + switch r.URL.Path { + case "/auth/login.json": + json.NewEncoder(w).Encode(map[string]string{"token": "test-token"}) + case "/checklists/1/tasks/101/comments.json": + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + w.Write(loadFixture(t, "testdata/notes/list.json")) + default: + t.Errorf("unexpected path: %s", r.URL.Path) + } + })) + defer server.Close() + + client := NewClient("user@example.com", "api-key", WithBaseURL(server.URL)) + notes, err := client.Notes(1, 101).List(context.Background()) + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(notes) != 2 { + t.Fatalf("expected 2 notes, got %d", len(notes)) + } + if notes[0].ID != 501 { + t.Errorf("expected ID 501, got %d", notes[0].ID) + } + if notes[0].Comment != "First comment on task" { + t.Errorf("expected comment 'First comment on task', got %s", notes[0].Comment) + } + if notes[0].TaskID != 101 { + t.Errorf("expected TaskID 101, got %d", notes[0].TaskID) + } +} + +func TestNotes_Create(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + switch r.URL.Path { + case "/auth/login.json": + json.NewEncoder(w).Encode(map[string]string{"token": "test-token"}) + case "/checklists/1/tasks/101/comments.json": + if r.Method != http.MethodPost { + t.Errorf("expected POST, got %s", r.Method) + } + + var req createNoteRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + t.Fatalf("failed to decode request: %v", err) + } + if req.Comment != "New note content" { + t.Errorf("expected comment 'New note content', got %s", req.Comment) + } + + response := Note{ + ID: 600, + TaskID: 101, + Comment: req.Comment, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + } + json.NewEncoder(w).Encode(response) + default: + t.Errorf("unexpected path: %s", r.URL.Path) + } + })) + defer server.Close() + + client := NewClient("user@example.com", "api-key", WithBaseURL(server.URL)) + note, err := client.Notes(1, 101).Create(context.Background(), "New note content") + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if note.ID != 600 { + t.Errorf("expected ID 600, got %d", note.ID) + } + if note.Comment != "New note content" { + t.Errorf("expected comment 'New note content', got %s", note.Comment) + } +} + +func TestNotes_Update(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + switch r.URL.Path { + case "/auth/login.json": + json.NewEncoder(w).Encode(map[string]string{"token": "test-token"}) + case "/checklists/1/tasks/101/comments/501.json": + if r.Method != http.MethodPut { + t.Errorf("expected PUT, got %s", r.Method) + } + + var req updateNoteRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + t.Fatalf("failed to decode request: %v", err) + } + if req.Comment != "Updated comment" { + t.Errorf("expected comment 'Updated comment', got %s", req.Comment) + } + + response := Note{ + ID: 501, + TaskID: 101, + Comment: req.Comment, + UpdatedAt: time.Now(), + } + json.NewEncoder(w).Encode(response) + default: + t.Errorf("unexpected path: %s", r.URL.Path) + } + })) + defer server.Close() + + client := NewClient("user@example.com", "api-key", WithBaseURL(server.URL)) + note, err := client.Notes(1, 101).Update(context.Background(), 501, "Updated comment") + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if note.Comment != "Updated comment" { + t.Errorf("expected comment 'Updated comment', got %s", note.Comment) + } +} + +func TestNotes_Delete(t *testing.T) { + var deleteCalled bool + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + switch r.URL.Path { + case "/auth/login.json": + json.NewEncoder(w).Encode(map[string]string{"token": "test-token"}) + case "/checklists/1/tasks/101/comments/501.json": + if r.Method != http.MethodDelete { + t.Errorf("expected DELETE, got %s", r.Method) + } + deleteCalled = true + w.WriteHeader(http.StatusOK) + default: + t.Errorf("unexpected path: %s", r.URL.Path) + } + })) + defer server.Close() + + client := NewClient("user@example.com", "api-key", WithBaseURL(server.URL)) + err := client.Notes(1, 101).Delete(context.Background(), 501) + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !deleteCalled { + t.Error("expected DELETE to be called") + } +} diff --git a/testdata/notes/list.json b/testdata/notes/list.json new file mode 100644 index 0000000..2c17c14 --- /dev/null +++ b/testdata/notes/list.json @@ -0,0 +1,16 @@ +[ + { + "id": 501, + "task_id": 101, + "comment": "First comment on task", + "updated_at": "2026-01-14T10:00:00Z", + "created_at": "2026-01-13T09:00:00Z" + }, + { + "id": 502, + "task_id": 101, + "comment": "Second comment with more details", + "updated_at": "2026-01-14T11:30:00Z", + "created_at": "2026-01-14T11:00:00Z" + } +]