Restructure omni services and add Chatwoot research snapshot

This commit is contained in:
Ruslan Bakiev
2026-02-21 11:11:27 +07:00
parent edea7a0034
commit b73babbbf6
7732 changed files with 978203 additions and 32 deletions

View File

@@ -0,0 +1,117 @@
require 'rails_helper'
RSpec.describe Tiktok::MessageService do
let(:account) { create(:account) }
let(:channel) { create(:channel_tiktok, account: account, business_id: 'biz-123') }
let(:inbox) { channel.inbox }
let(:contact) { create(:contact, account: account) }
let(:contact_inbox) { create(:contact_inbox, inbox: inbox, contact: contact, source_id: 'tt-conv-1') }
describe '#perform' do
it 'creates an incoming text message' do
content = {
type: 'text',
message_id: 'tt-msg-1',
timestamp: 1_700_000_000_000,
conversation_id: 'tt-conv-1',
text: { body: 'Hello from TikTok' },
from: 'Alice',
from_user: { id: 'user-1' },
to: 'Biz',
to_user: { id: 'biz-123' }
}.deep_symbolize_keys
expect do
service = described_class.new(channel: channel, content: content)
allow(service).to receive(:create_contact_inbox).and_return(contact_inbox)
service.perform
end.to change(Message, :count).by(1)
message = Message.last
expect(message.inbox).to eq(inbox)
expect(message.message_type).to eq('incoming')
expect(message.content).to eq('Hello from TikTok')
expect(message.source_id).to eq('tt-msg-1')
expect(message.sender).to eq(contact)
expect(message.content_attributes['is_unsupported']).to be_nil
end
it 'creates an incoming unsupported message for non-supported types' do
content = {
type: 'sticker',
message_id: 'tt-msg-2',
timestamp: 1_700_000_000_000,
conversation_id: 'tt-conv-1',
from: 'Alice',
from_user: { id: 'user-1' },
to: 'Biz',
to_user: { id: 'biz-123' }
}.deep_symbolize_keys
service = described_class.new(channel: channel, content: content)
allow(service).to receive(:create_contact_inbox).and_return(contact_inbox)
service.perform
message = Message.last
expect(message.content).to be_nil
expect(message.content_attributes['is_unsupported']).to be true
end
it 'creates an incoming embed attachment for share_post messages' do
content = {
type: 'share_post',
message_id: 'tt-msg-3',
timestamp: 1_700_000_000_000,
conversation_id: 'tt-conv-1',
share_post: { embed_url: 'https://www.tiktok.com/embed/123' },
from: 'Alice',
from_user: { id: 'user-1' },
to: 'Biz',
to_user: { id: 'biz-123' }
}.deep_symbolize_keys
service = described_class.new(channel: channel, content: content)
allow(service).to receive(:create_contact_inbox).and_return(contact_inbox)
service.perform
message = Message.last
expect(message.attachments.count).to eq(1)
attachment = message.attachments.last
expect(attachment.file_type).to eq('embed')
expect(attachment.external_url).to eq('https://www.tiktok.com/embed/123')
end
it 'creates an incoming image attachment when media is present' do
content = {
type: 'image',
message_id: 'tt-msg-4',
timestamp: 1_700_000_000_000,
conversation_id: 'tt-conv-1',
image: { media_id: 'media-1' },
from: 'Alice',
from_user: { id: 'user-1' },
to: 'Biz',
to_user: { id: 'biz-123' }
}.deep_symbolize_keys
tempfile = Tempfile.new(['tiktok', '.png'])
tempfile.write('fake-image')
tempfile.rewind
tempfile.define_singleton_method(:original_filename) { 'tiktok.png' }
tempfile.define_singleton_method(:content_type) { 'image/png' }
service = described_class.new(channel: channel, content: content)
allow(service).to receive(:create_contact_inbox).and_return(contact_inbox)
allow(service).to receive(:fetch_attachment).and_return(tempfile)
service.perform
message = Message.last
expect(message.attachments.count).to eq(1)
expect(message.attachments.last.file_type).to eq('image')
expect(message.attachments.last.file).to be_attached
ensure
tempfile.close!
end
end
end

View File

@@ -0,0 +1,35 @@
require 'rails_helper'
RSpec.describe Tiktok::ReadStatusService do
let(:account) { create(:account) }
let(:channel) { create(:channel_tiktok, account: account, business_id: 'biz-123') }
let(:inbox) { channel.inbox }
let(:contact) { create(:contact, account: account) }
let(:contact_inbox) { create(:contact_inbox, inbox: inbox, contact: contact, source_id: 'tt-conv-1') }
let!(:conversation) do
create(
:conversation,
account: account,
inbox: inbox,
contact: contact,
contact_inbox: contact_inbox,
additional_attributes: { conversation_id: 'tt-conv-1' }
)
end
describe '#perform' do
it 'enqueues Conversations::UpdateMessageStatusJob for inbound read events' do
allow(Conversations::UpdateMessageStatusJob).to receive(:perform_later)
content = {
conversation_id: 'tt-conv-1',
read: { last_read_timestamp: 1_700_000_000_000 },
from_user: { id: 'user-1' }
}.deep_symbolize_keys
described_class.new(channel: channel, content: content).perform
expect(Conversations::UpdateMessageStatusJob).to have_received(:perform_later).with(conversation.id, kind_of(Time))
end
end
end

View File

@@ -0,0 +1,71 @@
require 'rails_helper'
RSpec.describe Tiktok::SendOnTiktokService do
let(:tiktok_client) { instance_double(Tiktok::Client) }
let(:status_update_service) { instance_double(Messages::StatusUpdateService, perform: true) }
let(:channel) { create(:channel_tiktok, business_id: 'biz-123') }
let(:inbox) { channel.inbox }
let(:contact) { create(:contact, account: inbox.account) }
let(:contact_inbox) { create(:contact_inbox, inbox: inbox, contact: contact, source_id: 'tt-conv-1') }
let(:conversation) do
create(
:conversation,
inbox: inbox,
contact: contact,
contact_inbox: contact_inbox,
additional_attributes: { conversation_id: 'tt-conv-1' }
)
end
before do
allow(channel).to receive(:validated_access_token).and_return('valid-access-token')
allow(Tiktok::Client).to receive(:new).and_return(tiktok_client)
allow(Messages::StatusUpdateService).to receive(:new).and_return(status_update_service)
end
describe '#perform' do
it 'sends outgoing text message and updates source_id' do
allow(tiktok_client).to receive(:send_text_message).and_return('tt-msg-123')
message = create(:message, message_type: :outgoing, inbox: inbox, conversation: conversation, account: inbox.account, content: 'Hello')
message.update!(source_id: nil)
described_class.new(message: message).perform
expect(tiktok_client).to have_received(:send_text_message).with('tt-conv-1', 'Hello', referenced_message_id: nil)
expect(message.reload.source_id).to eq('tt-msg-123')
expect(Messages::StatusUpdateService).to have_received(:new).with(message, 'delivered')
end
it 'sends outgoing image message when a single attachment is present' do
allow(tiktok_client).to receive(:send_media_message).and_return('tt-msg-124')
message = build(:message, message_type: :outgoing, inbox: inbox, conversation: conversation, account: inbox.account, content: nil)
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
message.save!
described_class.new(message: message).perform
expect(tiktok_client).to have_received(:send_media_message).with('tt-conv-1', message.attachments.first, referenced_message_id: nil)
expect(message.reload.source_id).to eq('tt-msg-124')
end
it 'marks message as failed when sending multiple attachments' do
allow(tiktok_client).to receive(:send_media_message)
message = build(:message, message_type: :outgoing, inbox: inbox, conversation: conversation, account: inbox.account, content: nil)
a1 = message.attachments.new(account_id: message.account_id, file_type: :image)
a1.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
a2 = message.attachments.new(account_id: message.account_id, file_type: :image)
a2.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
message.save!
described_class.new(message: message).perform
expect(Messages::StatusUpdateService).to have_received(:new).with(message, 'failed', kind_of(String))
expect(tiktok_client).not_to have_received(:send_media_message)
end
end
end

View File

@@ -0,0 +1,57 @@
require 'rails_helper'
RSpec.describe Tiktok::TokenService do
let(:channel) do
create(
:channel_tiktok,
access_token: 'old-access-token',
refresh_token: 'old-refresh-token',
expires_at: 1.minute.ago,
refresh_token_expires_at: 1.day.from_now
)
end
describe '#access_token' do
it 'returns current token when it is still valid' do
channel.update!(expires_at: 10.minutes.from_now)
allow(Tiktok::AuthClient).to receive(:renew_short_term_access_token)
token = described_class.new(channel: channel).access_token
expect(token).to eq('old-access-token')
expect(Tiktok::AuthClient).not_to have_received(:renew_short_term_access_token)
end
it 'refreshes access token when expired and refresh token is valid' do
lock_manager = instance_double(Redis::LockManager, lock: true, unlock: true)
allow(Redis::LockManager).to receive(:new).and_return(lock_manager)
allow(Tiktok::AuthClient).to receive(:renew_short_term_access_token).and_return(
{
access_token: 'new-access-token',
refresh_token: 'new-refresh-token',
expires_at: 1.day.from_now,
refresh_token_expires_at: 30.days.from_now
}.with_indifferent_access
)
token = described_class.new(channel: channel).access_token
expect(token).to eq('new-access-token')
expect(channel.reload.access_token).to eq('new-access-token')
expect(channel.refresh_token).to eq('new-refresh-token')
end
it 'prompts reauthorization when both access and refresh tokens are expired' do
channel.update!(expires_at: 1.day.ago, refresh_token_expires_at: 1.minute.ago)
allow(channel).to receive(:reauthorization_required?).and_return(false)
allow(channel).to receive(:prompt_reauthorization!)
token = described_class.new(channel: channel).access_token
expect(token).to eq('old-access-token')
expect(channel).to have_received(:prompt_reauthorization!)
end
end
end