| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestInstallIDStableAcrossCalls(t *testing.T) { |
| 13 | isolateDesktopUserDirs(t) |
| 14 | first, err := installID() |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | if !installIDPattern.MatchString(first) { |
| 19 | t.Fatalf("installID() = %q, want 32 hex chars", first) |
| 20 | } |
| 21 | second, err := installID() |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if second != first { |
| 26 | t.Errorf("second call returned %q, want stable %q", second, first) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestPostStartupPing(t *testing.T) { |
| 31 | var got startupPing |
| 32 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | body, _ := io.ReadAll(r.Body) |
| 34 | if err := json.Unmarshal(body, &got); err != nil { |
| 35 | t.Errorf("body not JSON: %v", err) |
| 36 | } |
| 37 | w.WriteHeader(http.StatusAccepted) |
| 38 | })) |
| 39 | defer srv.Close() |
| 40 | |
| 41 | p := startupPing{InstallID: "0123456789abcdef0123456789abcdef", Version: "v9.9.9", OS: "windows", Arch: "amd64", OSVersion: "Windows 10.0 build 26200"} |
| 42 | if err := postStartupPing(context.Background(), srv.Client(), srv.URL, p); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if got != p { |
| 46 | t.Errorf("server received %+v, want %+v", got, p) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestSendStartupPingSkipsDevBuild(t *testing.T) { |
| 51 | isolateDesktopUserDirs(t) |
| 52 | hits := 0 |
| 53 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 54 | hits++ |
| 55 | w.WriteHeader(http.StatusAccepted) |
| 56 | })) |
| 57 | defer srv.Close() |
| 58 | old := pingEndpoint |
| 59 | pingEndpoint = srv.URL |
| 60 | defer func() { pingEndpoint = old }() |
| 61 | |
| 62 | NewApp().sendStartupPing() |
| 63 | if hits != 0 { |
| 64 | t.Errorf("dev build sent %d pings, want 0", hits) |
| 65 | } |
| 66 | } |
| 67 |