1+ # frozen_string_literal: true
2+
3+ RSpec . configure { |c | c . filter_run_when_matching :focus }
4+
5+ RSpec . describe DiscourseModifications ::TopicSlug do
6+ let ( :topic ) { Fabricate ( :topic , id : 42 ) }
7+
8+ describe ".slug_for_topic" do
9+ before do
10+ # Default to ascii slug generation
11+ allow ( SiteSetting ) . to receive ( :slug_generation_method ) . and_return ( "ascii" )
12+ end
13+
14+ it "removes emoji codes from the title" do
15+ title = "Hello :smile: World"
16+ slug = "hello-world"
17+ expect ( described_class . slug_for_topic ( topic , slug , title ) ) . to eq ( "hello-world" )
18+ end
19+
20+ it "unicode normalizes the title" do
21+ title = "Café"
22+ slug = "cafe"
23+ expect ( described_class . slug_for_topic ( topic , slug , title ) ) . to eq ( "cafe" )
24+ end
25+
26+ it "returns the original slug if slug_generation_method is not ascii" do
27+ allow ( SiteSetting ) . to receive ( :slug_generation_method ) . and_return ( "encoded" )
28+ expect ( described_class . slug_for_topic ( topic , "original-slug" , "Some Title" ) ) . to eq ( "original-slug" )
29+ end
30+
31+ it "returns a fallback slug if the result is blank" do
32+ title = ":smile:"
33+ slug = ""
34+ expect ( described_class . slug_for_topic ( topic , slug , title ) ) . to eq ( "topic-42" )
35+ end
36+
37+ it "returns a fallback slug if the result is only numbers" do
38+ title = "123456"
39+ slug = "123456"
40+ expect ( described_class . slug_for_topic ( topic , slug , title ) ) . to eq ( "topic-42" )
41+ end
42+
43+ it "truncates the slug to the max length" do
44+ long_title = "a" * ( Slug ::MAX_LENGTH + 10 )
45+ slug = "a" * ( Slug ::MAX_LENGTH + 10 )
46+ result = described_class . slug_for_topic ( topic , slug , long_title )
47+ expect ( result . length ) . to eq ( Slug ::MAX_LENGTH )
48+ end
49+
50+ it "handles titles with multiple emoji codes" do
51+ title = "Hello :smile: World :rocket:"
52+ slug = "hello-world"
53+ expect ( described_class . slug_for_topic ( topic , slug , title ) ) . to eq ( "hello-world" )
54+ end
55+
56+ it "handles titles with emoji codes with t modifier" do
57+ title = "Hello :wave:t2: World"
58+ slug = "hello-world"
59+ expect ( described_class . slug_for_topic ( topic , slug , title ) ) . to eq ( "hello-world" )
60+ end
61+ end
62+ end
0 commit comments