|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +type mockCheckinsAnswerCreateTransport struct { |
| 16 | + recordedPath string |
| 17 | + recordedBody map[string]any |
| 18 | +} |
| 19 | + |
| 20 | +func (m *mockCheckinsAnswerCreateTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 21 | + header := make(http.Header) |
| 22 | + header.Set("Content-Type", "application/json") |
| 23 | + |
| 24 | + switch { |
| 25 | + case req.Method == "GET" && strings.Contains(req.URL.Path, "/projects.json"): |
| 26 | + return &http.Response{ |
| 27 | + StatusCode: 200, |
| 28 | + Body: io.NopCloser(strings.NewReader(`[{"id":123,"name":"Test Project"}]`)), |
| 29 | + Header: header, |
| 30 | + }, nil |
| 31 | + case req.Method == "POST" && strings.Contains(req.URL.Path, "/questions/456/answers.json"): |
| 32 | + m.recordedPath = req.URL.Path |
| 33 | + if req.Body != nil { |
| 34 | + defer req.Body.Close() |
| 35 | + } |
| 36 | + body, err := io.ReadAll(req.Body) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + if err := json.Unmarshal(body, &m.recordedBody); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return &http.Response{ |
| 44 | + StatusCode: 201, |
| 45 | + Body: io.NopCloser(strings.NewReader(`{ |
| 46 | + "id": 789, |
| 47 | + "content": "<p>hello world</p>", |
| 48 | + "group_on": "2026-03-25", |
| 49 | + "creator": {"name": "Rob Zolkos"}, |
| 50 | + "parent": {"id": 456, "title": "What did you work on today?", "type": "Question", "url": "https://example.test/questions/456", "app_url": "https://example.test/questions/456"}, |
| 51 | + "bucket": {"id": 123, "name": "Test Project", "type": "Project"}, |
| 52 | + "status": "active", |
| 53 | + "type": "Question::Answer", |
| 54 | + "title": "Answer" |
| 55 | + }`)), |
| 56 | + Header: header, |
| 57 | + }, nil |
| 58 | + default: |
| 59 | + return &http.Response{ |
| 60 | + StatusCode: 404, |
| 61 | + Body: io.NopCloser(strings.NewReader(`{"error":"Not Found"}`)), |
| 62 | + Header: header, |
| 63 | + }, nil |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestCheckinsAnswerCreateDefaultsDateToToday(t *testing.T) { |
| 68 | + originalNow := checkinsNow |
| 69 | + checkinsNow = func() time.Time { |
| 70 | + return time.Date(2026, 3, 25, 9, 30, 0, 0, time.Local) |
| 71 | + } |
| 72 | + t.Cleanup(func() { |
| 73 | + checkinsNow = originalNow |
| 74 | + }) |
| 75 | + |
| 76 | + transport := &mockCheckinsAnswerCreateTransport{} |
| 77 | + app, _ := newTestAppWithTransport(t, transport) |
| 78 | + app.Config.ProjectID = "123" |
| 79 | + |
| 80 | + project := "" |
| 81 | + cmd := newCheckinsAnswerCreateCmd(&project) |
| 82 | + |
| 83 | + err := executeCommand(cmd, app, "456", "hello world") |
| 84 | + require.NoError(t, err) |
| 85 | + require.NotNil(t, transport.recordedBody) |
| 86 | + assert.Equal(t, "/99999/questions/456/answers.json", transport.recordedPath) |
| 87 | + assert.Equal(t, "<p>hello world</p>", transport.recordedBody["content"]) |
| 88 | + assert.Equal(t, "2026-03-25", transport.recordedBody["group_on"]) |
| 89 | +} |
| 90 | + |
| 91 | +func TestCheckinsAnswerCreatePreservesExplicitDate(t *testing.T) { |
| 92 | + transport := &mockCheckinsAnswerCreateTransport{} |
| 93 | + app, _ := newTestAppWithTransport(t, transport) |
| 94 | + app.Config.ProjectID = "123" |
| 95 | + |
| 96 | + project := "" |
| 97 | + cmd := newCheckinsAnswerCreateCmd(&project) |
| 98 | + |
| 99 | + err := executeCommand(cmd, app, "456", "hello world", "--date", "2026-03-25") |
| 100 | + require.NoError(t, err) |
| 101 | + require.NotNil(t, transport.recordedBody) |
| 102 | + assert.Equal(t, "2026-03-25", transport.recordedBody["group_on"]) |
| 103 | +} |
0 commit comments