Skip to content

A platform for community discussion. Free, open, simple.

3.3 KB
Raw Blame
# frozen_string_literal: true
RSpec.describe "Share conversation" do
  fab!(:admin) { Fabricate(:admin, username: "ai_sharer") }
  fab!(:gpt_4) { Fabricate(:llm_model, name: "gpt-4") }
  fab!(:agent) { Fabricate(:ai_agent, name: "Share Agent", default_llm: gpt_4).tap(&:ensure_user!) }
  let(:bot_user) { agent.user }

  let(:pm) do
    Fabricate(
      :private_message_topic,
      title: "This is my special PM",
      user: admin,
      topic_allowed_users: [
        Fabricate.build(:topic_allowed_user, user: admin),
        Fabricate.build(:topic_allowed_user, user: bot_user),
      ],
    )
  end

  let(:pm_posts) do
    posts = []
    i = 1
    3.times do
      posts << Fabricate(:post, topic: pm, user: admin, raw: "test test test user reply #{i}")
      posts << Fabricate(:post, topic: pm, user: bot_user, raw: "test test test bot reply #{i}")
      i += 1
    end

    posts
  end

  let(:cdp) { PageObjects::CDP.new }

  before do
    enable_current_plugin
    SiteSetting.ai_bot_enabled = true
    toggle_enabled_bots(bots: [gpt_4])
    sign_in(admin)

    Group.refresh_automatic_groups!

    cdp.allow_clipboard
    page.execute_script("window.navigator.clipboard.writeText('')")
  end

  it "can share a conversation with a agent user" do
    clip_text = nil

    agent = Fabricate(:ai_agent, name: "Tester")
    agent.create_user!

    Fabricate(:post, topic: pm, user: admin, raw: "How do I do stuff?")
    Fabricate(:post, topic: pm, user: agent.user, raw: "No idea")

    visit(pm.url)

    find("#post_2 .post-action-menu__share-ai").click

    clip_text = cdp.read_clipboard
    expect(clip_text).not_to eq("")

    conversation = <<~TEXT.strip
      <details class='ai-quote'>
      <summary>
      <span>This is my special PM</span>
      <span title='Conversation with AI'>AI</span>
      </summary>

      **ai_sharer:**

      How do I do stuff?

      **Tester_bot:**

      No idea
      </details>
    TEXT

    expect(conversation).to eq(clip_text)
  end

  it "can share a conversation" do
    clip_text = nil

    pm
    pm_posts

    visit(pm.url)

    find("#post_2 .post-action-menu__share-ai").click

    clip_text = cdp.read_clipboard
    expect(clip_text).not_to eq("")

    conversation = <<~TEXT.strip
      <details class='ai-quote'>
      <summary>
      <span>This is my special PM</span>
      <span title='Conversation with AI'>AI</span>
      </summary>

      **ai_sharer:**

      test test test user reply 1

      **#{bot_user.username}:**

      test test test bot reply 1
      </details>
    TEXT

    expect(conversation).to eq(clip_text)

    page.execute_script("window.navigator.clipboard.writeText('')")

    find("#post_6 .post-action-menu__share-ai").click
    find(".ai-share-modal__slider input").set("2")
    find(".ai-share-modal button.btn-primary").click

    clip_text = cdp.read_clipboard
    expect(clip_text).not_to eq("")

    conversation = <<~TEXT.strip
      <details class='ai-quote'>
      <summary>
      <span>This is my special PM</span>
      <span title='Conversation with AI'>AI</span>
      </summary>

      **ai_sharer:**

      test test test user reply 2

      **#{bot_user.username}:**

      test test test bot reply 2

      **ai_sharer:**

      test test test user reply 3

      **#{bot_user.username}:**

      test test test bot reply 3
      </details>
    TEXT

    expect(conversation).to eq(clip_text)
  end
end