Skip to content

Commit

Permalink
test: Add test for GetMailboxMessageCounts
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshoulahan committed Nov 11, 2022
1 parent c99ec61 commit 1d8260c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
41 changes: 41 additions & 0 deletions tests/counts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tests

import (
"testing"
"time"

"github.com/ProtonMail/gluon/events"
goimap "github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/stretchr/testify/require"
)

func TestCounts(t *testing.T) {
dir := t.TempDir()

runOneToOneTestClientWithAuth(t, defaultServerOptions(t, withDataDir(dir)), func(client *client.Client, s *testSession) {
for _, count := range getEvent[events.UserAdded](s.eventCh).Counts {
require.Equal(t, 0, count)
}

for i := 0; i < 10; i++ {
require.NoError(t, doAppendWithClientFromFile(t, client, "INBOX", "testdata/afternoon-meeting.eml", time.Now(), goimap.SeenFlag))
}
})

runOneToOneTestClientWithAuth(t, defaultServerOptions(t, withDataDir(dir)), func(_ *client.Client, s *testSession) {
for _, count := range getEvent[events.UserAdded](s.eventCh).Counts {
require.Equal(t, 10, count)
}
})
}

func getEvent[T any](eventCh <-chan events.Event) T {
for event := range eventCh {
if event, ok := event.(T); ok {
return event
}
}

panic("no event")
}
9 changes: 5 additions & 4 deletions tests/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ func runServer(tb testing.TB, options *serverOptions, tests func(session *testSe
}

// Create a new gluon server.
server, err := gluon.New(
gluonOptions...,
)
server, err := gluon.New(gluonOptions...)
require.NoError(tb, err)

// Watch server events.
eventCh := server.AddWatcher()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -272,7 +273,7 @@ func runServer(tb testing.TB, options *serverOptions, tests func(session *testSe

// Run the test against the server.
logging.DoAnnotated(ctx, func(ctx context.Context) {
tests(newTestSession(tb, listener, server, userIDs, conns, dbPaths, options))
tests(newTestSession(tb, listener, server, eventCh, userIDs, conns, dbPaths, options))
}, logging.Labels{
"Action": "Running gluon tests",
})
Expand Down
4 changes: 4 additions & 0 deletions tests/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"entgo.io/ent/dialect"
"github.com/ProtonMail/gluon"
"github.com/ProtonMail/gluon/connector"
"github.com/ProtonMail/gluon/events"
"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/gluon/internal/db/ent"
"github.com/ProtonMail/gluon/internal/utils"
Expand Down Expand Up @@ -53,6 +54,7 @@ type testSession struct {

listener net.Listener
server *gluon.Server
eventCh <-chan events.Event
userIDs map[string]string
conns map[string]Connector
userDBPaths map[string]string
Expand All @@ -63,6 +65,7 @@ func newTestSession(
tb testing.TB,
listener net.Listener,
server *gluon.Server,
eventCh <-chan events.Event,
userIDs map[string]string,
conns map[string]Connector,
userDBPaths map[string]string,
Expand All @@ -72,6 +75,7 @@ func newTestSession(
tb: tb,
listener: listener,
server: server,
eventCh: eventCh,
userIDs: userIDs,
conns: conns,
userDBPaths: userDBPaths,
Expand Down

0 comments on commit 1d8260c

Please sign in to comment.