discourse-ai/spec/models/rag_document_fragment_spec.rb

77 lines
2.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe RagDocumentFragment do
fab!(:persona) { Fabricate(:ai_persona) }
fab!(:upload_1) { Fabricate(:upload) }
fab!(:upload_2) { Fabricate(:upload) }
before do
SiteSetting.ai_embeddings_enabled = true
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
end
describe ".link_uploads_and_persona" do
it "does nothing if there is no persona" do
expect { described_class.link_persona_and_uploads(nil, [upload_1.id]) }.not_to change(
Jobs::DigestRagUpload.jobs,
:size,
)
end
it "does nothing if there are no uploads" do
expect { described_class.link_persona_and_uploads(persona, []) }.not_to change(
Jobs::DigestRagUpload.jobs,
:size,
)
end
it "queues a job for each upload to generate fragments" do
expect {
described_class.link_persona_and_uploads(persona, [upload_1.id, upload_2.id])
}.to change(Jobs::DigestRagUpload.jobs, :size).by(2)
end
it "creates references between the persona an each upload" do
described_class.link_persona_and_uploads(persona, [upload_1.id, upload_2.id])
refs = UploadReference.where(target: persona).pluck(:upload_id)
expect(refs).to contain_exactly(upload_1.id, upload_2.id)
end
end
describe ".update_persona_uploads" do
it "does nothing if there is no persona" do
expect { described_class.update_persona_uploads(nil, [upload_1.id]) }.not_to change(
Jobs::DigestRagUpload.jobs,
:size,
)
end
it "deletes the fragment if its not present in the uploads list" do
fragment = Fabricate(:rag_document_fragment, ai_persona: persona)
described_class.update_persona_uploads(persona, [])
expect { fragment.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it "delete references between the upload and the persona" do
described_class.link_persona_and_uploads(persona, [upload_1.id, upload_2.id])
described_class.update_persona_uploads(persona, [upload_2.id])
refs = UploadReference.where(target: persona).pluck(:upload_id)
expect(refs).to contain_exactly(upload_2.id)
end
it "queues jobs to generate new fragments" do
expect { described_class.update_persona_uploads(persona, [upload_1.id]) }.to change(
Jobs::DigestRagUpload.jobs,
:size,
).by(1)
end
end
end