2023-12-18 20:04:15 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Automation
|
|
|
|
describe ReportRunner do
|
|
|
|
fab!(:user)
|
2024-01-19 06:51:26 -05:00
|
|
|
fab!(:receiver) { Fabricate(:user) }
|
2023-12-18 20:04:15 -05:00
|
|
|
fab!(:post) { Fabricate(:post, user: user) }
|
|
|
|
fab!(:group)
|
|
|
|
fab!(:secure_category) { Fabricate(:private_category, group: group) }
|
|
|
|
fab!(:secure_topic) { Fabricate(:topic, category: secure_category) }
|
|
|
|
fab!(:secure_post) { Fabricate(:post, raw: "Top secret date !!!!", topic: secure_topic) }
|
|
|
|
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:category)
|
2024-01-29 23:55:05 -05:00
|
|
|
fab!(:topic_in_category) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:post_in_category) do
|
|
|
|
Fabricate(:post, raw: "I am in a category", topic: topic_in_category)
|
|
|
|
end
|
|
|
|
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:tag)
|
2024-07-02 02:38:33 -04:00
|
|
|
fab!(:hidden_tag) { Fabricate(:tag, name: "hidden-tag") }
|
|
|
|
fab!(:tag_group) do
|
|
|
|
tag_group = TagGroup.new(name: "test tag group")
|
|
|
|
tag_group.tag_group_permissions.build(group_id: Group::AUTO_GROUPS[:trust_level_1])
|
|
|
|
|
|
|
|
tag_group.save!
|
|
|
|
TagGroupMembership.create!(tag_group_id: tag_group.id, tag_id: hidden_tag.id)
|
|
|
|
tag_group
|
|
|
|
end
|
|
|
|
fab!(:topic_with_tag) { Fabricate(:topic, tags: [tag, hidden_tag]) }
|
2024-01-29 23:55:05 -05:00
|
|
|
fab!(:post_with_tag) { Fabricate(:post, raw: "I am in a tag", topic: topic_with_tag) }
|
|
|
|
|
2023-12-18 20:04:15 -05:00
|
|
|
describe "#run!" do
|
2023-12-19 01:51:49 -05:00
|
|
|
it "is able to generate email reports" do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(["magical report"]) do
|
|
|
|
ReportRunner.run!(
|
|
|
|
sender_username: user.username,
|
|
|
|
receivers: ["fake@discourse.com"],
|
|
|
|
title: "test report %DATE%",
|
2024-01-29 16:07:29 -05:00
|
|
|
model: "gpt-4",
|
2023-12-19 01:51:49 -05:00
|
|
|
category_ids: nil,
|
|
|
|
tags: nil,
|
|
|
|
allow_secure_categories: false,
|
|
|
|
sample_size: 100,
|
|
|
|
instructions: "make a magic report",
|
|
|
|
days: 7,
|
|
|
|
offset: 0,
|
|
|
|
priority_group_id: nil,
|
|
|
|
tokens_per_post: 150,
|
|
|
|
debug_mode: nil,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.length).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.first.subject).to eq(
|
|
|
|
"test report #{7.days.ago.strftime("%Y-%m-%d")} - #{Time.zone.now.strftime("%Y-%m-%d")}",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-04-22 02:05:24 -04:00
|
|
|
it "can exclude categories (including sub categories)" do
|
|
|
|
subcategory = Fabricate(:category, parent_category_id: category.id)
|
|
|
|
topic_in_subcategory = Fabricate(:topic, category: subcategory)
|
|
|
|
post_in_subcategory =
|
|
|
|
Fabricate(:post, raw: "I am in a subcategory abcd", topic: topic_in_subcategory)
|
|
|
|
|
2024-04-22 18:53:51 -04:00
|
|
|
other_category = Fabricate(:category)
|
|
|
|
topic2 = Fabricate(:topic, category: other_category)
|
|
|
|
post2 = Fabricate(:post, raw: "I am in another category 123", topic: topic2)
|
|
|
|
|
2024-01-29 23:55:05 -05:00
|
|
|
freeze_time
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(["magical report"]) do
|
|
|
|
ReportRunner.run!(
|
|
|
|
sender_username: user.username,
|
|
|
|
receivers: [receiver.username],
|
|
|
|
title: "test report",
|
|
|
|
model: "gpt-4",
|
|
|
|
category_ids: nil,
|
|
|
|
tags: nil,
|
|
|
|
allow_secure_categories: false,
|
|
|
|
debug_mode: true,
|
|
|
|
sample_size: 100,
|
|
|
|
instructions: "make a magic report",
|
|
|
|
days: 7,
|
|
|
|
offset: 0,
|
|
|
|
priority_group_id: nil,
|
|
|
|
tokens_per_post: 150,
|
|
|
|
exclude_category_ids: [category.id],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
report = Topic.where(title: "test report").first
|
|
|
|
debugging = report.ordered_posts.last.raw
|
|
|
|
|
|
|
|
expect(debugging).not_to include(post_in_category.raw)
|
2024-04-22 02:05:24 -04:00
|
|
|
expect(debugging).not_to include(post_in_subcategory.raw)
|
2024-04-22 18:53:51 -04:00
|
|
|
expect(debugging).to include(post2.raw)
|
2024-07-02 02:38:33 -04:00
|
|
|
expect(debugging).to include(post_with_tag.raw)
|
|
|
|
expect(debugging).to include(tag.name)
|
|
|
|
expect(debugging).not_to include(hidden_tag.name)
|
2024-01-29 23:55:05 -05:00
|
|
|
end
|
|
|
|
|
2024-03-15 17:05:03 -04:00
|
|
|
it "can suppress notifications by remapping content" do
|
2024-03-19 22:00:39 -04:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2024-03-15 17:05:03 -04:00
|
|
|
markdown = <<~MD
|
2024-03-19 22:00:39 -04:00
|
|
|
@#{user.username} is a person
|
2024-03-15 17:05:03 -04:00
|
|
|
[test1](/test) is an internal link
|
|
|
|
[test2](/test?1=2) is an internal link
|
|
|
|
[test3](https://example.com) is an external link
|
|
|
|
[test4](#{Discourse.base_url}) is an internal link
|
|
|
|
<a href='/test'>test5</a> is an internal link
|
|
|
|
[test6](/test?test=test#anchor) is an internal link with fragment
|
|
|
|
[test7](//[[test) is a link with an invalid URL
|
|
|
|
MD
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([markdown]) do
|
|
|
|
ReportRunner.run!(
|
|
|
|
sender_username: user.username,
|
|
|
|
receivers: [receiver.username],
|
|
|
|
title: "test report",
|
|
|
|
model: "gpt-4",
|
|
|
|
category_ids: nil,
|
|
|
|
tags: nil,
|
|
|
|
allow_secure_categories: false,
|
|
|
|
debug_mode: false,
|
|
|
|
sample_size: 100,
|
|
|
|
instructions: "make a magic report",
|
|
|
|
days: 7,
|
|
|
|
offset: 0,
|
|
|
|
priority_group_id: nil,
|
|
|
|
tokens_per_post: 150,
|
|
|
|
suppress_notifications: true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
report = Topic.where(title: "test report").first
|
|
|
|
|
|
|
|
# note, magic surprise & is correct HTML 5 representation
|
|
|
|
expected = <<~HTML
|
2024-03-19 22:00:39 -04:00
|
|
|
<p><a class="mention" href="/u/#{user.username}">#{user.username}</a> is a person<br>
|
2024-03-15 17:05:03 -04:00
|
|
|
<a href="/test?silent=true">test1</a> is an internal link<br>
|
|
|
|
<a href="/test?1=2&silent=true">test2</a> is an internal link<br>
|
|
|
|
<a href="https://example.com" rel="noopener nofollow ugc">test3</a> is an external link<br>
|
|
|
|
<a href="http://test.localhost?silent=true">test4</a> is an internal link<br>
|
|
|
|
<a href="/test?silent=true">test5</a> is an internal link<br>
|
|
|
|
<a href="/test?test=test&silent=true#anchor">test6</a> is an internal link with fragment<br>
|
|
|
|
<a href="//%5B%5Btest?silent=true" rel="noopener nofollow ugc">test7</a> is a link with an invalid URL</p>
|
|
|
|
HTML
|
|
|
|
|
2024-03-19 22:00:39 -04:00
|
|
|
post = report.ordered_posts.first
|
|
|
|
|
|
|
|
expect(post.mentions.to_a).to eq([])
|
|
|
|
expect(post.raw.strip).to eq(expected.strip)
|
2024-03-15 17:05:03 -04:00
|
|
|
end
|
|
|
|
|
2024-01-29 23:55:05 -05:00
|
|
|
it "can exclude tags" do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(["magical report"]) do
|
|
|
|
ReportRunner.run!(
|
|
|
|
sender_username: user.username,
|
|
|
|
receivers: [receiver.username],
|
|
|
|
title: "test report",
|
|
|
|
model: "gpt-4",
|
|
|
|
category_ids: nil,
|
|
|
|
tags: nil,
|
|
|
|
allow_secure_categories: false,
|
|
|
|
debug_mode: true,
|
|
|
|
sample_size: 100,
|
|
|
|
instructions: "make a magic report",
|
|
|
|
days: 7,
|
|
|
|
offset: 0,
|
|
|
|
priority_group_id: nil,
|
|
|
|
tokens_per_post: 150,
|
|
|
|
exclude_tags: [tag.name],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
report = Topic.where(title: "test report").first
|
|
|
|
debugging = report.ordered_posts.last.raw
|
|
|
|
|
|
|
|
expect(debugging).to include(post_in_category.raw)
|
|
|
|
expect(debugging).not_to include(post_with_tag.raw)
|
|
|
|
end
|
|
|
|
|
2023-12-18 20:04:15 -05:00
|
|
|
it "generates correctly respects the params" do
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(["magical report"]) do
|
|
|
|
ReportRunner.run!(
|
|
|
|
sender_username: user.username,
|
2024-01-19 06:51:26 -05:00
|
|
|
receivers: [receiver.username],
|
2023-12-18 20:04:15 -05:00
|
|
|
title: "test report",
|
2024-01-29 16:07:29 -05:00
|
|
|
model: "gpt-4",
|
2023-12-18 20:04:15 -05:00
|
|
|
category_ids: nil,
|
|
|
|
tags: nil,
|
|
|
|
allow_secure_categories: false,
|
|
|
|
debug_mode: true,
|
|
|
|
sample_size: 100,
|
|
|
|
instructions: "make a magic report",
|
|
|
|
days: 7,
|
|
|
|
offset: 0,
|
|
|
|
priority_group_id: nil,
|
|
|
|
tokens_per_post: 150,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
report = Topic.where(title: "test report").first
|
|
|
|
expect(report.ordered_posts.first.raw).to eq("magical report")
|
|
|
|
debugging = report.ordered_posts.last.raw
|
|
|
|
|
|
|
|
expect(debugging).to include(post.raw)
|
2024-01-29 23:55:05 -05:00
|
|
|
expect(debugging).to include(post_in_category.raw)
|
|
|
|
expect(debugging).to include(post_with_tag.raw)
|
2023-12-18 20:04:15 -05:00
|
|
|
expect(debugging).not_to include(secure_post.raw)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|