Skip to content

Commit acb4be3

Browse files
committed
tests: add ActionMailer metadata/event logging tests; fix Sorbet issues in test stubs; ensure coverage > 80%
1 parent 9318d85 commit acb4be3

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

lefthook.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
colors: true
2-
skip_output:
3-
- summary
42

53
pre-commit:
64
parallel: true
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require "test_helper"
5+
require "action_mailer"
6+
7+
class ActionMailerEventLoggingTest < ActiveSupport::TestCase
8+
EventLogging = LogStruct::Integrations::ActionMailer::EventLogging
9+
Callbacks = LogStruct::Integrations::ActionMailer::Callbacks
10+
11+
DummyMessage = Struct.new(:to, :cc, :bcc, :attachments, :subject, :from, :message_id)
12+
13+
class DummyMailer < ActionMailer::Base
14+
include Callbacks
15+
include EventLogging
16+
17+
def initialize(msg)
18+
super()
19+
@__msg = msg
20+
end
21+
22+
def message = @__msg
23+
24+
def action_name = "sample"
25+
end
26+
27+
setup do
28+
@msg = DummyMessage.new(%w[a@b.com], nil, %w[x@y.com], [], "Welcome", ["noreply@example.com"], "m-1")
29+
@mailer = DummyMailer.new(@msg)
30+
# Capture logs as JSON
31+
@orig_logger = ::Rails.logger
32+
@buf = StringIO.new
33+
logger = ::Logger.new(@buf)
34+
logger.formatter = LogStruct::Formatter.new
35+
::Rails.logger = logger
36+
end
37+
38+
teardown do
39+
::Rails.logger = @orig_logger
40+
end
41+
42+
test "log_email_delivery builds ActionMailer log with Delivery event" do
43+
@mailer.send(:log_email_delivery)
44+
parsed = JSON.parse(@buf.string)
45+
46+
assert_equal "mailer", parsed["src"]
47+
assert_equal "delivery", parsed["evt"]
48+
assert_kind_of Array, parsed["to"]
49+
assert_match(/\[EMAIL:/, parsed["to"].first)
50+
assert_match(/\[EMAIL:/, parsed["from"])
51+
assert_equal "Welcome", parsed["subject"]
52+
assert_match(/DummyMailer\z/, parsed["mailer_class"])
53+
assert_equal "sample", parsed["mailer_action"]
54+
assert_equal "m-1", parsed["message_id"]
55+
end
56+
57+
test "log_email_delivered builds ActionMailer log with Delivered event" do
58+
@mailer.send(:log_email_delivered)
59+
parsed = JSON.parse(@buf.string)
60+
61+
assert_equal "delivered", parsed["evt"]
62+
end
63+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require "test_helper"
5+
require "action_mailer"
6+
7+
class ActionMailerMetadataCollectionTest < ActiveSupport::TestCase
8+
MetadataCollection = LogStruct::Integrations::ActionMailer::MetadataCollection
9+
10+
DummyMessage = Struct.new(:to, :cc, :bcc, :attachments, :subject, :from, :message_id)
11+
12+
test "add_message_metadata counts recipients and attachments when message present" do
13+
msg = DummyMessage.new(%w[a@b.com], nil, %w[x@y.com], [1, 2], "S", ["from@example.com"], "mid")
14+
data = {}
15+
MetadataCollection.add_message_metadata(OpenStruct.new(message: msg), data)
16+
17+
assert_equal 2, data[:recipient_count]
18+
assert data[:has_attachments]
19+
assert_equal 2, data[:attachment_count]
20+
end
21+
22+
test "add_message_metadata sets defaults when message missing" do
23+
data = {}
24+
MetadataCollection.add_message_metadata(OpenStruct.new, data)
25+
26+
assert_equal 0, data[:recipient_count]
27+
refute data[:has_attachments]
28+
assert_equal 0, data[:attachment_count]
29+
end
30+
31+
test "add_context_metadata gathers tags and request_id when available" do
32+
data = {}
33+
34+
# Stub ActiveSupport::TaggedLogging.current_tags
35+
ActiveSupport::TaggedLogging.define_singleton_method(:current_tags) { ["req:abc"] }
36+
37+
# Stub ActionDispatch::Request.current_request_id
38+
ActionDispatch::Request.define_singleton_method(:current_request_id) { "rid-123" }
39+
40+
MetadataCollection.add_context_metadata(Object.new, data)
41+
42+
assert_equal ["req:abc"], data[:tags]
43+
assert_equal "rid-123", data[:request_id]
44+
end
45+
46+
test "add_context_metadata includes job_id when ActiveJob::Logging.job_id is available" do
47+
data = {}
48+
# Ensure module exists
49+
unless defined?(::ActiveJob::Logging)
50+
module ::ActiveJob
51+
module Logging; end
52+
end
53+
end
54+
# Define job_id method on the module's singleton class
55+
::ActiveJob::Logging.define_singleton_method(:job_id) { "jid-1" }
56+
MetadataCollection.add_context_metadata(Object.new, data)
57+
58+
assert_equal "jid-1", data[:job_id]
59+
end
60+
end

0 commit comments

Comments
 (0)