discourse/plugins/chat/spec/lib/chat_sdk/thread_spec.rb
Joffrey JAFFEUX d8d756cd2f
DEV: chat streaming (#25736)
This commit introduces the possibility to stream messages. To allow plugins to use streaming this commit also ships a `ChatSDK` library to allow to interact with few parts of discourse chat.

```ruby
ChatSDK::Message.create_with_stream(raw: "test") do |helper|
  5.times do |i|
    is_streaming = helper.stream(raw: "more #{i}")
    next if !is_streaming
    sleep 2
  end
end
```

This commit also introduces all the frontend parts:
- messages can now be marked as streaming
- when streaming their content will be updated when a new content is appended
- a special UI will be showing (a blinking indicator)
- a cancel button allows the user to stop the streaming, when cancelled `helper.stream(...)` will return `false`, and the plugin can decide exit early
2024-02-20 09:49:19 +01:00

82 lines
2.1 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
require "rails_helper"
describe ChatSDK::Thread do
describe ".update_title" do
fab!(:thread_1) { Fabricate(:chat_thread) }
let(:params) do
{
title: "New Title",
channel_id: thread_1.channel_id,
thread_id: thread_1.id,
guardian: Discourse.system_user.guardian,
}
end
it "changes the title" do
expect { described_class.update_title(**params) }.to change { thread_1.reload.title }.from(
thread_1.title,
).to(params[:title])
end
context "when missing param" do
it "fails" do
params.delete(:thread_id)
expect { described_class.update_title(**params) }.to raise_error("Thread can't be blank")
end
end
context "when guardian can't see the channel" do
fab!(:thread_1) { Fabricate(:chat_thread, channel: Fabricate(:private_category_channel)) }
it "fails" do
params[:guardian] = Fabricate(:user).guardian
expect { described_class.update_title(**params) }.to raise_error(
"Guardian can't view channel",
)
end
end
context "when guardian can't edit the thread" do
it "fails" do
params[:guardian] = Fabricate(:user).guardian
expect { described_class.update_title(**params) }.to raise_error(
"Guardian can't edit thread",
)
end
end
context "when the threadind is not enabled" do
before { thread_1.channel.update!(threading_enabled: false) }
it "fails" do
expect { described_class.update_title(**params) }.to raise_error(
"Threading is not enabled for this channel",
)
end
end
context "when the thread doesn't exist" do
it "fails" do
params[:thread_id] = -999
expect { described_class.update_title(**params) }.to raise_error(
"Couldnt find thread with id: `-999`",
)
end
end
context "when target_message doesnt exist" do
it "fails" do
expect { described_class.messages(**params, target_message_id: -999) }.to raise_error(
"Target message doesn't exist",
)
end
end
end
end