Skip to content

Commit

Permalink
Merge pull request #975 from wakatime/misc/file-experts-integration-test
Browse files Browse the repository at this point in the history
Add integration test for --file-experts
  • Loading branch information
gandarez authored Oct 31, 2023
2 parents 93c7e6b + 264c48b commit 5d5e9bb
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
82 changes: 82 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,88 @@ func TestSendHeartbeats_MalformedInternalConfig(t *testing.T) {
assert.Equal(t, 1, count)
}

func TestFileExperts(t *testing.T) {
t.Skip()

apiURL, router, close := setupTestServer()
defer close()

projectFolder, err := filepath.Abs(".")
require.NoError(t, err)

subfolders := project.CountSlashesInProjectFolder(projectFolder)

var numCalls int

router.HandleFunc("/users/current/file_experts",
func(w http.ResponseWriter, req *http.Request) {
numCalls++

// check headers
assert.Equal(t, http.MethodPost, req.Method)
assert.Equal(t, []string{"application/json"}, req.Header["Accept"])
assert.Equal(t, []string{"application/json"}, req.Header["Content-Type"])
assert.Equal(t, []string{"Basic MDAwMDAwMDAtMDAwMC00MDAwLTgwMDAtMDAwMDAwMDAwMDAw"}, req.Header["Authorization"])
assert.Equal(t, []string{heartbeat.UserAgent("")}, req.Header["User-Agent"])

// check body
expectedBodyTpl, err := os.ReadFile("testdata/api_file_experts_request_template.json")
require.NoError(t, err)

entityPath, err := realpath.Realpath("testdata/main.go")
require.NoError(t, err)

entityPath = strings.ReplaceAll(entityPath, `\`, `/`)
expectedBody := fmt.Sprintf(
string(expectedBodyTpl),
entityPath,
"wakatime-cli",
subfolders,
)

body, err := io.ReadAll(req.Body)
require.NoError(t, err)

assert.JSONEq(t, expectedBody, string(body))

// write response
f, err := os.Open("testdata/api_file_experts_response.json")
require.NoError(t, err)

w.WriteHeader(http.StatusOK)
_, err = io.Copy(w, f)
require.NoError(t, err)
})

tmpDir := t.TempDir()

tmpConfigFile, err := os.CreateTemp(tmpDir, "wakatime.cfg")
require.NoError(t, err)

defer tmpConfigFile.Close()

tmpInternalConfigFile, err := os.CreateTemp(tmpDir, "wakatime-internal.cfg")
require.NoError(t, err)

defer tmpInternalConfigFile.Close()

out := runWakatimeCli(
t,
&bytes.Buffer{},
"--api-url", apiURL,
"--key", "00000000-0000-4000-8000-000000000000",
"--config", tmpConfigFile.Name(),
"--internal-config", tmpInternalConfigFile.Name(),
"--entity", "testdata/main.go",
"--file-experts",
"--verbose",
)

assert.Equal(t, "You: 4 hrs 15 mins | Steve: 22 mins\n", out)

assert.Eventually(t, func() bool { return numCalls == 1 }, time.Second, 50*time.Millisecond)
}

func TestTodayGoal(t *testing.T) {
apiURL, router, close := setupTestServer()
defer close()
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/fileexperts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Client) FileExperts(heartbeats []heartbeat.Heartbeat) ([]heartbeat.Resu
return nil, fmt.Errorf("failed to json encode body: %s", err)
}

log.Debugf("fileexpert: %s", string(data))
log.Debugf("file-experts: %s", string(data))

req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions testdata/api_file_experts_request_template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"entity": "%s",
"project": "%s",
"project_root_count": %d
}
60 changes: 60 additions & 0 deletions testdata/api_file_experts_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"data": [
{
"user": {
"id": "a8d9322f-78f0-4c6f-b4ee-91daa7d97495",
"name": "John",
"long_name": "John Papa",
"is_current_user": true
},
"total": {
"digital": "4:15",
"decimal": "4.25",
"text": "4 hrs 15 mins",
"total_seconds": 15314
}
},
{
"user": {
"id": "1ddebdf7-d97c-4e4f-8c4a-7f942597d430",
"name": "Steve",
"long_name": "Steve Wonder",
"is_current_user": false
},
"total": {
"digital": "0:22",
"decimal": "0.37",
"text": "22 mins",
"total_seconds": 1352
}
},
{
"user": {
"id": "dc62fedf-d3ef-4277-90f7-ba3dd80d6780",
"name": "Alex",
"long_name": "Alex Bowen",
"is_current_user": false
},
"total": {
"digital": "0:00",
"decimal": "0.00",
"text": "0 secs",
"total_seconds": 0
}
},
{
"user": {
"id": "bdad5d80-a9bc-4e3b-978c-657c9be65b3f",
"name": "Mike",
"long_name": "Mike Bispo",
"is_current_user": false
},
"total": {
"digital": "0:00",
"decimal": "0.00",
"text": "0 secs",
"total_seconds": 0
}
}
]
}

0 comments on commit 5d5e9bb

Please sign in to comment.