-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1062 from wakatime/develop
Release v1.94.1
- Loading branch information
Showing
28 changed files
with
828 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,147 @@ | ||
package offlinesync | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
func TestSyncOfflineActivityLegacy(t *testing.T) { | ||
testServerURL, router, tearDown := setupTestServer() | ||
defer tearDown() | ||
|
||
var ( | ||
plugin = "plugin/0.0.1" | ||
numCalls int | ||
) | ||
|
||
router.HandleFunc("/users/current/heartbeats.bulk", func(w http.ResponseWriter, req *http.Request) { | ||
numCalls++ | ||
|
||
// check request | ||
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.True(t, strings.HasSuffix(req.Header["User-Agent"][0], plugin), fmt.Sprintf( | ||
"%q should have suffix %q", | ||
req.Header["User-Agent"][0], | ||
plugin, | ||
)) | ||
|
||
expectedBody, err := os.ReadFile("testdata/api_heartbeats_request_template.json") | ||
require.NoError(t, err) | ||
|
||
body, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.JSONEq(t, string(expectedBody), string(body)) | ||
|
||
// send response | ||
w.WriteHeader(http.StatusCreated) | ||
|
||
f, err := os.Open("testdata/api_heartbeats_response.json") | ||
require.NoError(t, err) | ||
defer f.Close() | ||
|
||
_, err = io.Copy(w, f) | ||
require.NoError(t, err) | ||
}) | ||
|
||
// setup offline queue | ||
f, err := os.CreateTemp(t.TempDir(), "") | ||
require.NoError(t, err) | ||
|
||
// early close to avoid file locking in Windows | ||
f.Close() | ||
|
||
db, err := bolt.Open(f.Name(), 0600, nil) | ||
require.NoError(t, err) | ||
|
||
dataGo, err := os.ReadFile("testdata/heartbeat_go.json") | ||
require.NoError(t, err) | ||
|
||
dataPy, err := os.ReadFile("testdata/heartbeat_py.json") | ||
require.NoError(t, err) | ||
|
||
dataJs, err := os.ReadFile("testdata/heartbeat_js.json") | ||
require.NoError(t, err) | ||
|
||
insertHeartbeatRecords(t, db, "heartbeats", []heartbeatRecord{ | ||
{ | ||
ID: "1592868367.219124-file-coding-wakatime-cli-heartbeat-/tmp/main.go-true", | ||
Heartbeat: string(dataGo), | ||
}, | ||
{ | ||
ID: "1592868386.079084-file-debugging-wakatime-summary-/tmp/main.py-false", | ||
Heartbeat: string(dataPy), | ||
}, | ||
{ | ||
ID: "1592868394.084354-file-building-wakatime-todaygoal-/tmp/main.js-false", | ||
Heartbeat: string(dataJs), | ||
}, | ||
}) | ||
|
||
err = db.Close() | ||
require.NoError(t, err) | ||
|
||
v := viper.New() | ||
v.Set("api-url", testServerURL) | ||
v.Set("key", "00000000-0000-4000-8000-000000000000") | ||
v.Set("sync-offline-activity", 100) | ||
v.Set("plugin", plugin) | ||
|
||
err = syncOfflineActivityLegacy(v, f.Name()) | ||
require.NoError(t, err) | ||
|
||
assert.NoFileExists(t, f.Name()) | ||
|
||
assert.Eventually(t, func() bool { return numCalls == 1 }, time.Second, 50*time.Millisecond) | ||
} | ||
|
||
func setupTestServer() (string, *http.ServeMux, func()) { | ||
router := http.NewServeMux() | ||
srv := httptest.NewServer(router) | ||
|
||
return srv.URL, router, func() { srv.Close() } | ||
} | ||
|
||
type heartbeatRecord struct { | ||
ID string | ||
Heartbeat string | ||
} | ||
|
||
func insertHeartbeatRecords(t *testing.T, db *bolt.DB, bucket string, hh []heartbeatRecord) { | ||
for _, h := range hh { | ||
insertHeartbeatRecord(t, db, bucket, h) | ||
} | ||
} | ||
|
||
func insertHeartbeatRecord(t *testing.T, db *bolt.DB, bucket string, h heartbeatRecord) { | ||
t.Helper() | ||
|
||
err := db.Update(func(tx *bolt.Tx) error { | ||
b, err := tx.CreateBucketIfNotExists([]byte(bucket)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create bucket: %s", err) | ||
} | ||
|
||
err = b.Put([]byte(h.ID), []byte(h.Heartbeat)) | ||
if err != nil { | ||
return fmt.Errorf("failed put heartbeat: %s", err) | ||
} | ||
|
||
return nil | ||
}) | ||
require.NoError(t, err) | ||
} |
This file contains 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 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
Oops, something went wrong.