2023-11-27 11:03:24 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::AiHelper::ChatThreadTitler do
|
|
|
|
subject(:titler) { described_class.new(thread) }
|
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
before { assign_fake_provider_to(:ai_helper_model) }
|
2024-01-29 14:04:25 -05:00
|
|
|
|
2023-11-27 11:03:24 -05:00
|
|
|
fab!(:thread) { Fabricate(:chat_thread) }
|
2024-02-07 13:05:50 -05:00
|
|
|
fab!(:chat_message) { Fabricate(:chat_message, thread: thread) }
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:user)
|
2023-11-27 11:03:24 -05:00
|
|
|
|
2024-08-30 16:17:14 -04:00
|
|
|
describe "#suggested_title" do
|
|
|
|
it "bails early if thread has no content" do
|
|
|
|
empty_thread = Chat::Thread.new
|
|
|
|
|
|
|
|
result = described_class.new(empty_thread).suggested_title
|
|
|
|
|
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-07 13:05:50 -05:00
|
|
|
describe "#cleanup" do
|
|
|
|
it "picks the first when there are multiple" do
|
|
|
|
titles = "The solitary horse\nThe horse etched in gold"
|
2023-11-28 10:52:22 -05:00
|
|
|
expected_title = "The solitary horse"
|
2024-02-07 13:05:50 -05:00
|
|
|
|
|
|
|
result = titler.cleanup(titles)
|
2023-11-27 11:03:24 -05:00
|
|
|
|
|
|
|
expect(result).to eq(expected_title)
|
|
|
|
end
|
2024-02-07 13:05:50 -05:00
|
|
|
|
|
|
|
it "cleans up double quotes enclosing the whole title" do
|
|
|
|
titles = '"The solitary horse"'
|
|
|
|
expected_title = "The solitary horse"
|
|
|
|
|
|
|
|
result = titler.cleanup(titles)
|
|
|
|
|
|
|
|
expect(result).to eq(expected_title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "cleans up single quotes enclosing the whole title" do
|
|
|
|
titles = "'The solitary horse'"
|
|
|
|
expected_title = "The solitary horse"
|
|
|
|
|
|
|
|
result = titler.cleanup(titles)
|
|
|
|
|
|
|
|
expect(result).to eq(expected_title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "leaves quotes in the middle of title" do
|
|
|
|
titles = "The 'solitary' horse"
|
|
|
|
expected_title = "The 'solitary' horse"
|
|
|
|
|
|
|
|
result = titler.cleanup(titles)
|
|
|
|
|
|
|
|
expect(result).to eq(expected_title)
|
|
|
|
end
|
2024-02-09 12:49:28 -05:00
|
|
|
|
|
|
|
it "parses the XML" do
|
|
|
|
titles = "Here is your title <title>The solitary horse</title> my friend"
|
|
|
|
expected_title = "The solitary horse"
|
|
|
|
|
|
|
|
result = titler.cleanup(titles)
|
|
|
|
|
|
|
|
expect(result).to eq(expected_title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "truncates long titles" do
|
|
|
|
titles = "O cavalo trota pelo campo" + " Pocotó" * 100
|
|
|
|
|
|
|
|
result = titler.cleanup(titles)
|
|
|
|
|
|
|
|
expect(result.size).to be <= 100
|
|
|
|
end
|
2024-02-07 13:05:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#thread_content" do
|
|
|
|
it "returns the chat message and user" do
|
|
|
|
expect(titler.thread_content(thread)).to include(chat_message.message)
|
|
|
|
expect(titler.thread_content(thread)).to include(chat_message.user.username)
|
|
|
|
end
|
2023-11-27 11:03:24 -05:00
|
|
|
end
|
|
|
|
end
|