|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe "AI Artifact with Data Attributes", type: :system do |
| 4 | + fab!(:admin) |
| 5 | + fab!(:user) |
| 6 | + fab!(:author) { Fabricate(:user) } |
| 7 | + fab!(:category) { Fabricate(:category, user: admin, read_restricted: false) } |
| 8 | + fab!(:topic) { Fabricate(:topic, category: category, user: author) } |
| 9 | + fab!(:post) { Fabricate(:post, topic: topic, user: author) } |
| 10 | + |
| 11 | + before { SiteSetting.discourse_ai_enabled = true } |
| 12 | + |
| 13 | + it "correctly passes data attributes and user info to a public AI artifact embedded in a post" do |
| 14 | + artifact_js = <<~JS |
| 15 | + window.discourseArtifactReady.then(data => { |
| 16 | + const displayElement = document.getElementById('data-display'); |
| 17 | + if (displayElement) { |
| 18 | + displayElement.innerText = JSON.stringify(data); |
| 19 | + } |
| 20 | + }).catch(err => { |
| 21 | + const displayElement = document.getElementById('data-display'); |
| 22 | + if (displayElement) { |
| 23 | + displayElement.innerText = 'Error: ' + err.message; |
| 24 | + } |
| 25 | + console.error("Artifact JS Error:", err); |
| 26 | + }); |
| 27 | + JS |
| 28 | + |
| 29 | + ai_artifact = |
| 30 | + Fabricate( |
| 31 | + :ai_artifact, |
| 32 | + user: author, |
| 33 | + name: "Data Passing Test Artifact", |
| 34 | + html: "<div id='data-display'>Waiting for data...</div>", |
| 35 | + js: artifact_js.strip, |
| 36 | + metadata: { |
| 37 | + public: true, |
| 38 | + }, |
| 39 | + ) |
| 40 | + |
| 41 | + raw_post_content = |
| 42 | + "<div class='ai-artifact' data-ai-artifact-id='#{ai_artifact.id}' data-custom-message='hello-from-post' data-post-author-id='#{author.id}'></div>" |
| 43 | + _post = Fabricate(:post, topic: topic, user: author, raw: raw_post_content) |
| 44 | + |
| 45 | + sign_in(user) |
| 46 | + visit "/t/#{topic.slug}/#{topic.id}" |
| 47 | + |
| 48 | + find(".ai-artifact__click-to-run button").click |
| 49 | + |
| 50 | + artifact_element_selector = ".ai-artifact[data-ai-artifact-id='#{ai_artifact.id}']" |
| 51 | + iframe_selector = "#{artifact_element_selector} iframe" |
| 52 | + |
| 53 | + expect(page).to have_css(iframe_selector, wait: 10) # Wait for iframe to appear |
| 54 | + |
| 55 | + iframe_element = find(iframe_selector) |
| 56 | + expect(iframe_element["data-custom-message"]).to eq("hello-from-post") |
| 57 | + expect(iframe_element["data-post-author-id"]).to eq(author.id.to_s) |
| 58 | + |
| 59 | + page.within_frame(iframe_element) do |
| 60 | + inner_iframe = find("iframe") |
| 61 | + page.within_frame(inner_iframe) do |
| 62 | + data_display_element = find("#data-display") |
| 63 | + |
| 64 | + try_until_success(timeout: 10) do |
| 65 | + expect(data_display_element.text).not_to be_empty |
| 66 | + expect(data_display_element.text).not_to eq("Waiting for data...") |
| 67 | + expect(data_display_element.text).not_to include("Error:") |
| 68 | + end |
| 69 | + |
| 70 | + artifact_data_json = data_display_element.text |
| 71 | + artifact_data = JSON.parse(artifact_data_json) |
| 72 | + |
| 73 | + expect(artifact_data["customMessage"]).to eq("hello-from-post") |
| 74 | + expect(artifact_data["postAuthorId"]).to eq(author.id.to_s) |
| 75 | + |
| 76 | + expect(artifact_data["username"]).to eq(user.username) |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments