1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

test: impl StoreStub.MarkSeen (#443)

* test: impl StoreStub.MarkSeen

Signed-off-by: James Hillyerd <james@hillyerd.com>

* continue to Message interface in StoreStub

Signed-off-by: James Hillyerd <james@hillyerd.com>

* test errors

Signed-off-by: James Hillyerd <james@hillyerd.com>

---------

Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
James Hillyerd
2023-11-18 11:21:09 -08:00
committed by GitHub
parent 208d20582e
commit 1b5a783dbd
2 changed files with 134 additions and 15 deletions

View File

@@ -9,8 +9,8 @@ import (
// StoreStub stubs storage.Store for testing.
type StoreStub struct {
storage.Store
mailboxes map[string][]storage.Message
deleted map[storage.Message]struct{}
mailboxes map[string][]storage.Message // Stored messages, by mailbox.
deleted map[storage.Message]struct{} // Deleted message references.
}
// NewStore creates a new StoreStub.
@@ -25,7 +25,7 @@ func NewStore() *StoreStub {
func (s *StoreStub) AddMessage(m storage.Message) (id string, err error) {
mb := m.Mailbox()
msgs := s.mailboxes[mb]
s.mailboxes[mb] = append(msgs, m)
s.mailboxes[mb] = append(msgs, &MessageStub{Message: m})
return m.ID(), nil
}
@@ -50,34 +50,57 @@ func (s *StoreStub) GetMessages(mailbox string) ([]storage.Message, error) {
return s.mailboxes[mailbox], nil
}
// MarkSeen marks the message as having been seen.
func (s *StoreStub) MarkSeen(mailbox, id string) error {
if mailbox == "messageerr" {
return errors.New("internal error")
}
for _, m := range s.mailboxes[mailbox] {
if m.ID() == id {
if stub, ok := m.(*MessageStub); ok {
stub.seen = true
return nil
}
return errors.New("unexpected type in StoreStub.mailboxes")
}
}
return storage.ErrNotExist
}
// RemoveMessage deletes a message by ID from the specified mailbox.
func (s *StoreStub) RemoveMessage(mailbox, id string) error {
mb, ok := s.mailboxes[mailbox]
if ok {
var msg storage.Message
if mb, ok := s.mailboxes[mailbox]; ok {
var removed storage.Message
for i, m := range mb {
if m.ID() == id {
msg = m
removed = m
s.mailboxes[mailbox] = append(mb[:i], mb[i+1:]...)
break
}
}
if msg != nil {
s.deleted[msg] = struct{}{}
return nil
if removed != nil {
// Clients will be checking for their original storage.Message, not our wrapper.
if stub, ok := removed.(*MessageStub); ok {
s.deleted[stub.Message] = struct{}{}
return nil
}
return errors.New("unexpected type in StoreStub.mailboxes")
}
}
return storage.ErrNotExist
}
// VisitMailboxes accepts a function that will be called with the messages in each mailbox while it
// continues to return true.
func (s *StoreStub) VisitMailboxes(f func([]storage.Message) (cont bool)) error {
for _, v := range s.mailboxes {
if !f(v) {
for _, msgs := range s.mailboxes {
if !f(msgs) {
return nil
}
}
return nil
}
@@ -86,3 +109,14 @@ func (s *StoreStub) MessageDeleted(m storage.Message) bool {
_, ok := s.deleted[m]
return ok
}
// MessageStub wraps a storage.Message with "seen" functionality.
type MessageStub struct {
storage.Message
seen bool
}
// Seen returns true if the message has been marked as seen previously.
func (m *MessageStub) Seen() bool {
return m.seen
}