mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-01 20:12:15 +00:00
This update adds a safety checker which scans the streamed updates. It ensures that incomplete segments of text are not sent yet over message bus as this will cause breakage with the diff streamer. It also updates the diff streamer to handle a thinking state for when we are waiting for message bus updates.
81 lines
2.4 KiB
Ruby
81 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe DiscourseAi::Utils::DiffUtils::SafetyChecker do
|
|
describe "#safe?" do
|
|
subject { described_class.new(text).safe? }
|
|
|
|
context "with safe text" do
|
|
let(:text) { "This is a simple safe text without issues." }
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
context "with normal HTML tags" do
|
|
let(:text) { "Here is <strong>bold</strong> and <em>italic</em> text." }
|
|
it { is_expected.to eq(true) }
|
|
end
|
|
|
|
context "with balanced markdown and no partial emoji" do
|
|
let(:text) { "This is **bold**, *italic*, and a smiley :smile:!" }
|
|
it { is_expected.to eq(true) }
|
|
end
|
|
|
|
context "with balanced quote blocks" do
|
|
let(:text) { "[quote]Quoted text[/quote]" }
|
|
it { is_expected.to eq(true) }
|
|
end
|
|
|
|
context "with complete image markdown" do
|
|
let(:text) { "" }
|
|
it { is_expected.to eq(true) }
|
|
end
|
|
end
|
|
|
|
context "with unsafe text" do
|
|
context "with unclosed markdown link" do
|
|
let(:text) { "This is a [link(https://example.com)" }
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with unclosed raw HTML tag" do
|
|
let(:text) { "Text with <div unclosed tag" }
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with trailing incomplete URL" do
|
|
let(:text) { "Check this out https://example.com/something" } # no closing punctuation
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with unclosed backticks" do
|
|
let(:text) { "Here is some `inline code without closing" }
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with unbalanced bold or italic markdown" do
|
|
let(:text) { "This is *italic without closing" }
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with incomplete image markdown" do
|
|
let(:text) { "Image 
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with unbalanced quote blocks" do
|
|
let(:text) { "[quote]Unclosed quote block" }
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with unclosed triple backticks" do
|
|
let(:text) { "```code block without closing" }
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
|
|
context "with partial emoji" do
|
|
let(:text) { "A partial emoji :smile" }
|
|
it { is_expected.to eq(false) }
|
|
end
|
|
end
|
|
end
|
|
end
|