Skip to content
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

test(integration): add error handling for responses #154

Merged
merged 5 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions internal/api/mock/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"os"
"strings"

"github.com/cli/go-gh/v2/pkg/api"
)

type Call struct {
Expand All @@ -20,7 +22,7 @@ type RawCall struct {
Verb string `json:"verb"`
Endpoint string `json:"endpoint"`
Data any `json:"data"`
Error error `json:"error"`
Error RawError `json:"error"`
Response RawResponse `json:"response"`
}

Expand All @@ -30,6 +32,10 @@ type RawResponse struct {
Body any `json:"body"`
}

type RawError struct {
StatusCode int `json:"status_code"`
}

func LoadCallsFromFile(path string) ([]Call, error) {
rawCalls := []RawCall{}

Expand All @@ -43,23 +49,28 @@ func LoadCallsFromFile(path string) ([]Call, error) {
}

calls := make([]Call, len(rawCalls))
for i, call := range rawCalls {
body, err := json.Marshal(call.Response.Body)
for i, rawCall := range rawCalls {
body, err := json.Marshal(rawCall.Response.Body)
if err != nil {
return nil, err
}

calls[i] = Call{
Verb: call.Verb,
Endpoint: call.Endpoint,
Data: call.Data,
Error: call.Error,
call := Call{
Verb: rawCall.Verb,
Endpoint: rawCall.Endpoint,
Data: rawCall.Data,
Response: &http.Response{
Header: http.Header(call.Response.Headers),
StatusCode: call.Response.StatusCode,
Header: http.Header(rawCall.Response.Headers),
StatusCode: rawCall.Response.StatusCode,
Body: io.NopCloser(strings.NewReader(string(body))),
},
}

if rawCall.Error.StatusCode != 0 {
call.Error = &api.HTTPError{StatusCode: rawCall.Error.StatusCode}
}

calls[i] = call
}

return calls, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/api/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MockError struct {
}

func (e *MockError) Error() string {
return fmt.Sprintf("mock error: %s %s: %s", e.verb, e.endpoint, e.message)
return fmt.Sprintf("mock error for call [%s %s]: %s", e.verb, e.endpoint, e.message)
}

func New(c []Call) api.Requestor {
Expand All @@ -47,7 +47,7 @@ func (m *Mock) call(verb, endpoint string) (Call, error) {
return Call{}, &MockError{
verb,
endpoint,
fmt.Sprintf("unexpected call: mismatch, expected %s %s", call.Verb, call.Endpoint),
fmt.Sprintf("unexpected call: mismatch, expected [%s %s]", call.Verb, call.Endpoint),
}
}

Expand Down
4 changes: 1 addition & 3 deletions internal/gh/enrichments.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Client) Enrich(n *notifications.Notification) error {
return nil
}

slog.Debug("enriching", "url", n.Subject.URL)
slog.Debug("enriching", "id", n.Id, "url", n.Subject.URL)
resp, err := c.API.Request(http.MethodGet, n.Subject.URL, nil)
if err != nil {
return err
Expand All @@ -38,8 +38,6 @@ func (c *Client) Enrich(n *notifications.Notification) error {
return err
}

slog.Debug("enriching", "id", n.Id)

n.Author = extra.User
n.Subject.State = extra.State
n.Subject.HtmlUrl = extra.HtmlUrl
Expand Down
2 changes: 2 additions & 0 deletions internal/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package notifications
import (
"encoding/json"
"fmt"
"log/slog"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -82,6 +83,7 @@ func (n Notifications) Equal(others Notifications) bool {

for i, n := range n {
if !n.Equal(others[i]) {
slog.Info("notification not equal", "n", n.Debug(), "other", others[i].Debug())
return false
}
}
Expand Down
39 changes: 20 additions & 19 deletions internal/notifications/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,56 @@ state if the remote notification is newer than the local one.
TODO: refactor this to `func (n Notifications) Sync(remote Notifications) {}`
*/
func Sync(local, remote Notifications) Notifications {
// TODO: do we need to have the whole map?
remoteMap := remote.Map()
localMap := local.Map()

n := Notifications{}

// Add any new notifications to the list
for remoteId, remote := range remoteMap {
if _, ok := localMap[remoteId]; !ok {
for i := range remote {
if _, ok := localMap[remote[i].Id]; !ok {
// (1) Insert
slog.Debug("sync", "action", "insert", "id", remote.Id)
slog.Debug("sync", "action", "insert", "id", remote[i].Id)

remote.Meta.RemoteExists = true
n = append(n, remote)
remote[i].Meta.RemoteExists = true
n = append(n, remote[i])
}
}

for localId, local := range localMap {
remote, remoteExist := remoteMap[localId]
for i := range local {
remote, remoteExist := remoteMap[local[i].Id]

local.Meta.RemoteExists = remoteExist
local[i].Meta.RemoteExists = remoteExist

if remoteExist {
// (3) Keep
if local.Meta.Hidden {
slog.Debug("sync", "action", "keeping hidden", "id", local.Id)
n = append(n, local)
if local[i].Meta.Hidden {
slog.Debug("sync", "action", "keeping hidden", "id", local[i].Id)
n = append(n, local[i])
continue
}

// (2) Update
slog.Debug("sync", "action", "update", "id", remote.Id)

if local.Meta.Done && remote.UpdatedAt.After(local.UpdatedAt) {
slog.Debug("sync", "action", "reseting done", "id", local.Id)
local.Meta.Done = false
if local[i].Meta.Done && remote.UpdatedAt.After(local[i].UpdatedAt) {
slog.Debug("sync", "action", "reseting done", "id", local[i].Id)
local[i].Meta.Done = false
}

remote.Meta = local.Meta
remote.Meta = local[i].Meta
n = append(n, remote)
} else {
if local.Meta.Done || local.Meta.Hidden {
if local[i].Meta.Done || local[i].Meta.Hidden {
// (4) Drop
slog.Debug("sync", "action", "drop", "id", local.Id)
slog.Debug("sync", "action", "drop", "id", local[i].Id)
continue
}

// (3) Keep
slog.Debug("sync", "action", "keep", "id", local.Id)
n = append(n, local)
slog.Debug("sync", "action", "keep", "id", local[i].Id)
n = append(n, local[i])
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/integration/001/calls.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
"body": [{ "id": "1", "subject": { "url": "enrichment#1" } }]
}
},
{
"verb": "GET",
"endpoint": "https://api.github.com/notifications?all=true&page=2",
"error": {
"status_code": 404
}
},
{
"verb": "GET",
"endpoint": "https://api.github.com/notifications?all=true&page=2",
Expand Down
74 changes: 74 additions & 0 deletions test/integration/002/cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[
{
"id": "1 exists",
"subject": {
"url": "enrichment#1",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": false,
"hidden": false
}
},
{
"id": "1 missing",
"subject": {
"url": "enrichment#1",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": false,
"hidden": false
}
},
{
"id": "2 exists",
"subject": {
"url": "enrichment#2",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": true,
"hidden": false
}
},
{
"id": "2 missing",
"subject": {
"url": "enrichment#2",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": true,
"hidden": false
}
},
{
"id": "3 exists",
"subject": {
"url": "enrichment#3",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": false,
"hidden": true
}
},
{
"id": "3 missing",
"subject": {
"url": "enrichment#3",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": false,
"hidden": true
}
}
]
39 changes: 39 additions & 0 deletions test/integration/002/calls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
{
"verb": "GET",
"endpoint": "https://api.github.com/notifications?all=true",
"response": {
"status_code": 200,
"headers": {},
"body": [
{ "id": "1 exists", "subject": { "url": "enrichment#1" } },
{ "id": "2 exists", "subject": { "url": "enrichment#2" } },
{ "id": "3 exists", "subject": { "url": "enrichment#3" } }
]
}
},
{
"verb": "GET",
"endpoint": "enrichment#1",
"response": {
"status_code": 200,
"body": { "state": "open" }
}
},
{
"verb": "GET",
"endpoint": "enrichment#1",
"response": {
"status_code": 200,
"body": { "state": "open" }
}
},
{
"verb": "GET",
"endpoint": "enrichment#3",
"response": {
"status_code": 200,
"body": { "state": "open" }
}
}
]
Empty file.
50 changes: 50 additions & 0 deletions test/integration/002/want.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"id": "1 exists",
"subject": {
"url": "enrichment#1",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": false,
"hidden": false
}
},
{
"id": "1 missing",
"subject": {
"url": "enrichment#1",
"state": "open"
},
"meta": {
"remote_exists": false,
"done": false,
"hidden": false
}
},
{
"id": "2 exists",
"subject": {
"url": "enrichment#2",
"state": ""
},
"meta": {
"remote_exists": true,
"done": true,
"hidden": false
}
},
{
"id": "3 exists",
"subject": {
"url": "enrichment#3",
"state": "open"
},
"meta": {
"remote_exists": true,
"done": false,
"hidden": true
}
}
]
Loading
Loading