Skip to content
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
58 changes: 58 additions & 0 deletions lib/email/chime/chimeadapters/email_worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chimeadapters

import (
"context"
"errors"

"github.com/GoogleChrome/webstatus.dev/lib/email/chime"
"github.com/GoogleChrome/webstatus.dev/lib/workertypes"
)

type EmailSender interface {
Send(ctx context.Context, id string, to string, subject string, htmlBody string) error
}

type EmailWorkerChimeAdapter struct {
chimeSender EmailSender
}

// NewEmailWorkerChimeAdapter creates a new adapter for the email worker to use Chime.
func NewEmailWorkerChimeAdapter(chimeSender EmailSender) *EmailWorkerChimeAdapter {
return &EmailWorkerChimeAdapter{
chimeSender: chimeSender,
}
}

// Send implements the EmailSender interface for the email worker.
func (a *EmailWorkerChimeAdapter) Send(ctx context.Context, id string, to string,
subject string, htmlBody string) error {
err := a.chimeSender.Send(ctx, id, to, subject, htmlBody)
if err != nil {
if errors.Is(err, chime.ErrPermanentUser) {
return errors.Join(workertypes.ErrUnrecoverableUserFailureEmailSending, err)
} else if errors.Is(err, chime.ErrPermanentSystem) {
return errors.Join(workertypes.ErrUnrecoverableSystemFailureEmailSending, err)
} else if errors.Is(err, chime.ErrDuplicate) {
return errors.Join(workertypes.ErrUnrecoverableSystemFailureEmailSending, err)
}

// Will be recorded as a transient error
return err
}

return nil
}
100 changes: 100 additions & 0 deletions lib/email/chime/chimeadapters/email_worker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chimeadapters

import (
"context"
"errors"
"testing"

"github.com/GoogleChrome/webstatus.dev/lib/email/chime"
"github.com/GoogleChrome/webstatus.dev/lib/workertypes"
)

// mockChimeSender is a mock implementation of the EmailSender for testing.
type mockChimeSender struct {
sendErr error
}

func (m *mockChimeSender) Send(_ context.Context, _ string, _ string, _ string, _ string) error {
return m.sendErr
}

var errTest = errors.New("test error")

func TestEmailWorkerChimeAdapter_Send(t *testing.T) {
ctx := context.Background()
testCases := []struct {
name string
chimeError error
expectedError error
}{
{
name: "Success",
chimeError: nil,
expectedError: nil,
},
{
name: "Permanent User Error",
chimeError: chime.ErrPermanentUser,
expectedError: workertypes.ErrUnrecoverableUserFailureEmailSending,
},
{
name: "Permanent System Error",
chimeError: chime.ErrPermanentSystem,
expectedError: workertypes.ErrUnrecoverableSystemFailureEmailSending,
},
{
name: "Duplicate Error",
chimeError: chime.ErrDuplicate,
expectedError: workertypes.ErrUnrecoverableSystemFailureEmailSending,
},
{
name: "Transient Error",
chimeError: chime.ErrTransient,
expectedError: chime.ErrTransient, // Should be passed through
},
{
name: "Other Error",
chimeError: errTest,
expectedError: errTest, // Should be passed through
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup
mockSender := &mockChimeSender{sendErr: tc.chimeError}
adapter := NewEmailWorkerChimeAdapter(mockSender)

// Execute
err := adapter.Send(ctx, "test-id", "to@example.com", "Test Subject", "<p>Hello</p>")

// Verify
if tc.expectedError != nil {
if err == nil {
t.Fatal("Expected an error, but got nil")
}
if !errors.Is(err, tc.expectedError) {
t.Errorf("Expected error wrapping %v, but got %v", tc.expectedError, err)
}
} else {
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
}
})
}
}
Loading
Loading