-
Notifications
You must be signed in to change notification settings - Fork 15
Return fresh project after update #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+162
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/basecamp/basecamp-sdk/go/pkg/basecamp" | ||
|
|
||
| "github.com/basecamp/basecamp-cli/internal/appctx" | ||
| "github.com/basecamp/basecamp-cli/internal/auth" | ||
| "github.com/basecamp/basecamp-cli/internal/config" | ||
| "github.com/basecamp/basecamp-cli/internal/names" | ||
| "github.com/basecamp/basecamp-cli/internal/output" | ||
| ) | ||
|
|
||
| type mockProjectUpdateTransport struct { | ||
| getCount int | ||
| putCount int | ||
| putName string | ||
| putDescription string | ||
| failRefetch bool | ||
| } | ||
|
|
||
| func (t *mockProjectUpdateTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| header := make(http.Header) | ||
| header.Set("Content-Type", "application/json") | ||
|
|
||
| if !strings.Contains(req.URL.Path, "/projects/123") { | ||
| return nil, fmt.Errorf("unexpected request path: %s", req.URL.Path) | ||
| } | ||
|
|
||
| switch req.Method { | ||
| case http.MethodGet: | ||
| t.getCount++ | ||
| if t.getCount > 1 && t.failRefetch { | ||
| return jsonResponse(400, `{"error":"boom"}`, header), nil | ||
| } | ||
| description := "Old description" | ||
| updatedAt := "2026-06-01T00:00:00.000Z" | ||
| if t.getCount > 1 { | ||
| description = "New description" | ||
| updatedAt = "2026-06-02T00:00:00.000Z" | ||
| } | ||
| return jsonResponse(200, fmt.Sprintf(`{"id":123,"name":"Test Project","description":%q,"updated_at":%q}`, description, updatedAt), header), nil | ||
| case http.MethodPut: | ||
| t.putCount++ | ||
| var body map[string]any | ||
| if err := json.NewDecoder(req.Body).Decode(&body); err != nil { | ||
| return nil, fmt.Errorf("decode update body: %w", err) | ||
| } | ||
| if name, ok := body["name"].(string); ok { | ||
| t.putName = name | ||
| } | ||
| if description, ok := body["description"].(string); ok { | ||
| t.putDescription = description | ||
| } | ||
| return jsonResponse(200, `{"id":123,"name":"Test Project","description":"Old description","updated_at":"2026-06-01T00:00:00.000Z"}`, header), nil | ||
| default: | ||
| return nil, fmt.Errorf("unexpected method: %s", req.Method) | ||
| } | ||
| } | ||
|
|
||
| func jsonResponse(status int, body string, header http.Header) *http.Response { | ||
| return &http.Response{ | ||
| StatusCode: status, | ||
| Body: io.NopCloser(strings.NewReader(body)), | ||
| Header: header, | ||
| } | ||
| } | ||
|
|
||
| func setupProjectsMockApp(t *testing.T, transport http.RoundTripper) (*appctx.App, *bytes.Buffer) { | ||
| t.Helper() | ||
| t.Setenv("BASECAMP_NO_KEYRING", "1") | ||
|
|
||
| cfg := &config.Config{AccountID: "99999"} | ||
| sdkClient := basecamp.NewClient(&basecamp.Config{}, &testTokenProvider{}, | ||
| basecamp.WithTransport(transport), | ||
| basecamp.WithMaxRetries(1), | ||
| ) | ||
| authMgr := auth.NewManager(cfg, nil) | ||
| buf := &bytes.Buffer{} | ||
|
|
||
| return &appctx.App{ | ||
| Config: cfg, | ||
| Auth: authMgr, | ||
| SDK: sdkClient, | ||
| Names: names.NewResolver(sdkClient, authMgr, cfg.AccountID), | ||
| Output: output.New(output.Options{Format: output.FormatJSON, Writer: buf}), | ||
| }, buf | ||
| } | ||
|
|
||
| func TestProjectsUpdateReturnsFreshProjectAfterDescriptionChange(t *testing.T) { | ||
| transport := &mockProjectUpdateTransport{} | ||
| app, out := setupProjectsMockApp(t, transport) | ||
|
|
||
| cmd := NewProjectsCmd() | ||
| err := executeCommand(cmd, app, "update", "123", "--description", "New description") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, 1, transport.putCount) | ||
| assert.Equal(t, "Test Project", transport.putName) | ||
| assert.Equal(t, "New description", transport.putDescription) | ||
| assert.Equal(t, 2, transport.getCount, "description-only update should fetch the current name, then refetch the fresh project after update") | ||
|
|
||
| var envelope projectUpdateEnvelope | ||
| require.NoError(t, json.Unmarshal(out.Bytes(), &envelope)) | ||
| assert.True(t, envelope.OK) | ||
| assert.Equal(t, int64(123), envelope.Data.ID) | ||
| assert.Equal(t, "Test Project", envelope.Data.Name) | ||
| assert.Equal(t, "New description", envelope.Data.Description) | ||
| assert.Equal(t, "2026-06-02T00:00:00Z", envelope.Data.UpdatedAt) | ||
| assert.Empty(t, envelope.Notice) | ||
| } | ||
|
|
||
| func TestProjectsUpdateFallsBackToUpdateResponseWhenRefetchFails(t *testing.T) { | ||
| transport := &mockProjectUpdateTransport{failRefetch: true} | ||
| app, out := setupProjectsMockApp(t, transport) | ||
|
|
||
| cmd := NewProjectsCmd() | ||
| err := executeCommand(cmd, app, "update", "123", "--description", "New description") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, 1, transport.putCount) | ||
| assert.Equal(t, 2, transport.getCount) | ||
|
|
||
| var envelope projectUpdateEnvelope | ||
| require.NoError(t, json.Unmarshal(out.Bytes(), &envelope)) | ||
| assert.True(t, envelope.OK) | ||
| assert.Equal(t, int64(123), envelope.Data.ID) | ||
| assert.Equal(t, "Test Project", envelope.Data.Name) | ||
| assert.Equal(t, "Old description", envelope.Data.Description) | ||
| assert.Contains(t, envelope.Notice, "Project updated, but fetching the latest project state failed") | ||
| } | ||
|
|
||
| type projectUpdateEnvelope struct { | ||
| OK bool `json:"ok"` | ||
| Notice string `json:"notice"` | ||
| Data struct { | ||
| ID int64 `json:"id"` | ||
| Name string `json:"name"` | ||
| Description string `json:"description"` | ||
| UpdatedAt string `json:"updated_at"` | ||
| } `json:"data"` | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.