Files
leadchat/spec/presenters/message_content_presenter_spec.rb
Muhsin Keloth 608be1036b fix: Send raw content in webhook payloads instead of channel-rendered markdown (#13896)
Webhook payloads (`message_created`, `message_updated`) started sending
channel-rendered HTML in the `content` field instead of the original raw
message content after PR #12878. This broke downstream agent bots and
integrations that expected plain text or markdown. 
Closes https://linear.app/chatwoot/issue/PLA-109/webhook-payloads-send-channel-rendered-html-instead-of-raw-content

## How to reproduce

1. Connect an agent bot to a WebWidget inbox
2. Send a message with markdown formatting (e.g. `**bold**`) from the
widget
3. Observe the agent bot webhook payload — `content` field contains
`<p><strong>bold</strong></p>` instead of `**bold**`

## What changed

Split `MessageContentPresenter` into two public methods:
- `outgoing_content` — renders markdown for the target channel (used by
channel delivery services)
- `webhook_content` — returns raw content with CSAT survey URL when
applicable, no markdown rendering (used by `webhook_data`)

Updated `Message#webhook_data` to use `webhook_content` instead of
`outgoing_content`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
2026-03-25 16:56:22 +04:00

97 lines
3.5 KiB
Ruby

require 'rails_helper'
RSpec.describe MessageContentPresenter do
let(:conversation) { create(:conversation) }
let(:message) { create(:message, conversation: conversation, content_type: content_type, content: content) }
let(:presenter) { described_class.new(message) }
describe '#outgoing_content' do
context 'when message is not input_csat' do
let(:content_type) { 'text' }
let(:content) { 'Regular message' }
it 'returns content transformed for channel (HTML for WebWidget)' do
expect(presenter.outgoing_content).to eq("<p>Regular message</p>\n")
end
end
context 'when message is input_csat and inbox is web widget' do
let(:content_type) { 'input_csat' }
let(:content) { 'Rate your experience' }
before do
allow(message.inbox).to receive(:web_widget?).and_return(true)
end
it 'returns content without survey URL (HTML for WebWidget)' do
expect(presenter.outgoing_content).to eq("<p>Rate your experience</p>\n")
end
end
context 'when message is input_csat and inbox is not web widget' do
let(:content_type) { 'input_csat' }
let(:content) { 'Rate your experience' }
before do
allow(message.inbox).to receive(:web_widget?).and_return(false)
end
it 'returns I18n default message when no CSAT config and dynamically generates survey URL (HTML format)' do
with_modified_env 'FRONTEND_URL' => 'https://app.chatwoot.com' do
expected_url = "https://app.chatwoot.com/survey/responses/#{conversation.uuid}"
expect(presenter.outgoing_content).to include(expected_url)
expect(presenter.outgoing_content).to include('<p>')
end
end
it 'returns CSAT config message when config exists and dynamically generates survey URL (HTML format)' do
with_modified_env 'FRONTEND_URL' => 'https://app.chatwoot.com' do
allow(message.inbox).to receive(:csat_config).and_return({ 'message' => 'Custom CSAT message' })
expected_url = "https://app.chatwoot.com/survey/responses/#{conversation.uuid}"
expected_content = "<p>Custom CSAT message #{expected_url}</p>\n"
expect(presenter.outgoing_content).to eq(expected_content)
end
end
end
end
describe '#webhook_content' do
context 'when message is not input_csat' do
let(:content_type) { 'text' }
let(:content) { 'Regular **bold** message' }
it 'returns raw content without markdown rendering' do
expect(presenter.webhook_content).to eq('Regular **bold** message')
end
end
context 'when message is input_csat and inbox is not web widget' do
let(:content_type) { 'input_csat' }
let(:content) { 'Rate your experience' }
before do
allow(message.inbox).to receive(:web_widget?).and_return(false)
end
it 'includes CSAT survey URL without markdown rendering' do
with_modified_env 'FRONTEND_URL' => 'https://app.chatwoot.com' do
expected_url = "https://app.chatwoot.com/survey/responses/#{conversation.uuid}"
expect(presenter.webhook_content).to include(expected_url)
expect(presenter.webhook_content).not_to include('<p>')
end
end
end
end
describe 'delegation' do
let(:content_type) { 'text' }
let(:content) { 'Test message' }
it 'delegates model methods to the wrapped message' do
expect(presenter.content).to eq('Test message')
expect(presenter.content_type).to eq('text')
expect(presenter.conversation).to eq(conversation)
end
end
end