Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit b05128a

Browse files
committed
DEV: Add spec and apply some fixes
1 parent 8144c8d commit b05128a

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

lib/utils/diff_utils/safety_checker.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ def sanitize(html)
3737
end
3838

3939
def unclosed_markdown_links?
40-
open_bracket = @text.rindex("[")
41-
close_bracket = @text.rindex("]")
42-
open_paren = @text.rindex("(")
43-
close_paren = @text.rindex(")")
44-
open_bracket && open_paren && (close_bracket.nil? || close_paren.nil?)
40+
open_brackets = @text.count("[")
41+
close_brackets = @text.count("]")
42+
open_parens = @text.count("(")
43+
close_parens = @text.count(")")
44+
45+
open_brackets != close_brackets || open_parens != close_parens
4546
end
4647

4748
def unclosed_raw_html_tag?
@@ -80,11 +81,9 @@ def unclosed_triple_backticks?
8081
end
8182

8283
def partial_emoji?
83-
@text
84-
.scan(/:[a-z0-9_+.-]*:?/i)
85-
.any? do |match|
86-
match.count(":") == 1 || (match[-1] != ":" && match =~ /:[a-z0-9_+-]+\.\z/i)
87-
end
84+
text = @text.gsub(/!\[.*?\]\(.*?\)/, "").gsub(%r{https?://[^\s]+}, "")
85+
tokens = text.scan(/:[a-z0-9_+\-\.]+:?/i)
86+
tokens.any? { |token| token.start_with?(":") && !token.end_with?(":") }
8887
end
8988
end
9089
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# spec/lib/discourse_ai/utils/diff_utils/safety_checker_spec.rb
2+
3+
require "rails_helper"
4+
5+
RSpec.describe DiscourseAi::Utils::DiffUtils::SafetyChecker do
6+
describe "#safe?" do
7+
subject { described_class.new(text).safe? }
8+
9+
context "with safe text" do
10+
let(:text) { "This is a simple safe text without issues." }
11+
12+
it { is_expected.to eq(true) }
13+
14+
context "with normal HTML tags" do
15+
let(:text) { "Here is <strong>bold</strong> and <em>italic</em> text." }
16+
it { is_expected.to eq(true) }
17+
end
18+
19+
context "with balanced markdown and no partial emoji" do
20+
let(:text) { "This is **bold**, *italic*, and a smiley :smile:!" }
21+
it { is_expected.to eq(true) }
22+
end
23+
24+
context "with balanced quote blocks" do
25+
let(:text) { "[quote]Quoted text[/quote]" }
26+
it { is_expected.to eq(true) }
27+
end
28+
29+
context "with complete image markdown" do
30+
let(:text) { "![alt text](https://example.com/image.png)" }
31+
it { is_expected.to eq(true) }
32+
end
33+
end
34+
35+
context "with unsafe text" do
36+
context "with unclosed markdown link" do
37+
let(:text) { "This is a [link(https://example.com)" }
38+
it { is_expected.to eq(false) }
39+
end
40+
41+
context "with unclosed raw HTML tag" do
42+
let(:text) { "Text with <div unclosed tag" }
43+
it { is_expected.to eq(false) }
44+
end
45+
46+
context "with trailing incomplete URL" do
47+
let(:text) { "Check this out https://example.com/something" } # no closing punctuation
48+
it { is_expected.to eq(false) }
49+
end
50+
51+
context "with unclosed backticks" do
52+
let(:text) { "Here is some `inline code without closing" }
53+
it { is_expected.to eq(false) }
54+
end
55+
56+
context "with unbalanced bold or italic markdown" do
57+
let(:text) { "This is *italic without closing" }
58+
it { is_expected.to eq(false) }
59+
end
60+
61+
context "with incomplete image markdown" do
62+
let(:text) { "Image ![alt text](https://example.com/image.png" } # missing closing )
63+
it { is_expected.to eq(false) }
64+
end
65+
66+
context "with unbalanced quote blocks" do
67+
let(:text) { "[quote]Unclosed quote block" }
68+
it { is_expected.to eq(false) }
69+
end
70+
71+
context "with unclosed triple backticks" do
72+
let(:text) { "```code block without closing" }
73+
it { is_expected.to eq(false) }
74+
end
75+
76+
# context "with partial emoji" do
77+
# let(:text) { "A partial emoji :smile" }
78+
# it { is_expected.to eq(false) }
79+
# end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)