create kitchen sink spec for fake data

This commit is contained in:
Joffrey JAFFEUX 2025-01-07 23:26:17 +01:00
parent 80210f11d2
commit 90a03dec31
8 changed files with 107 additions and 9 deletions

View File

@ -42,7 +42,7 @@ module DiscourseRewind
end
def enabled?
SiteSetting.discourse_reaction_enabled || SiteSetting.chat_enabled
SiteSetting.discourse_reactions_enabled || SiteSetting.chat_enabled
end
def sort_and_limit(reactions)

View File

@ -10,7 +10,7 @@ module DiscourseRewind
{
data: {
reading_time: reading_time,
books: best_book_fit(reading_time),
book: best_book_fit(reading_time),
},
identifier: "reading-time",
}
@ -44,11 +44,13 @@ module DiscourseRewind
def best_book_fit(reading_time)
reading_time_rest = reading_time
books = []
while reading_time_rest > 0
books << popular_book_reading_time.min_by { |_, v| (v - reading_time_rest).abs }.first
reading_time_rest -= popular_book_reading_time[books.last]
end
books.group_by(&:itself).transform_values(&:count)
books.group_by(&:itself).transform_values(&:count).max_by { |_, count| count }.first
end
end
end

View File

@ -0,0 +1,46 @@
import Component from "@glimmer/component";
import { concat } from "@ember/helper";
import { htmlSafe } from "@ember/template";
import emoji from "discourse/helpers/emoji";
export default class BestTopics extends Component {
rank(idx) {
return idx + 1;
}
emojiName(rank) {
if (rank + 1 === 1) {
return "1st_place_medal";
} else if (rank + 1 === 2) {
return "2nd_place_medal";
} else if (rank + 1 === 3) {
return "3rd_place_medal";
} else {
return "medal";
}
}
<template>
<div class="rewind-report-page -best-topics">
<h2 class="rewind-report-title">Your 3 best topics</h2>
<div class="rewind-report-container">
{{log @report.data}}
<div class="rewind-card">
{{#each @report.data as |topic idx|}}
<a
href={{concat "/t/-/" topic.topic_id}}
class={{concat "best-topics__topic" " rank-" (this.rank idx)}}
>
<span class="best-topics__rank">{{emoji
(this.emojiName idx)
}}</span><h2>{{topic.title}}</h2>
<span class="best-topics__excerpt">{{htmlSafe
topic.excerpt
}}</span>
</a>
{{/each}}
</div>
</div>
</div>
</template>
}

View File

@ -31,6 +31,7 @@ export default class Reactions extends Component {
}
<template>
{{log @report}}
<div class="rewind-report-page -post-received-reactions">
<h2 class="rewind-report-title">Most received reactions in topics</h2>
<div class="rewind-report-container">

View File

@ -3,12 +3,12 @@ import Component from "@glimmer/component";
// eslint-disable-next-line ember/no-empty-glimmer-component-classes
export default class ReadingTime extends Component {
<template>
<div class="rewind-report-page">
Reading time
</div>
<div class="rewind-report-page">
page 2
<div class="rewind-report-page -reading-time">
<h2 class="rewind-report-title">Reading time</h2>
<div class="rewind-report-container">
<span class="reading-time__time">{{@report.data.reading_time}}</span>
<span class="reading-time__book">{{@report.data.book}}</span>
</div>
</div>
</template>
}

View File

@ -124,6 +124,8 @@ export default class Rewind extends Component {
<ActivityCalendar @report={{report}} />
{{else if (eq report.identifier "favorite-tags")}}
<FavoriteTags @report={{report}} />
{{else if (eq report.identifier "reading-time")}}
<ReadingTime @report={{report}} />
{{else if (eq report.identifier "favorite-categories")}}
<FavoriteCategories @report={{report}} />
{{/if}}

View File

@ -0,0 +1,47 @@
# frozen_string_literal: true
describe "Discourse Rewind - kitchen sink", type: :system do
fab!(:current_user) { Fabricate(:user) }
before do
SiteSetting.discourse_reactions_enabled = true
SiteSetting.chat_enabled = true
SiteSetting.discourse_rewind_enabled = true
sign_in(current_user)
# reactions received
reactions = %w[open_mouth confetti_ball hugs +1 laughing heart]
Fabricate
.times(10, :post, user: current_user)
.each do |post|
DiscourseReactions::Reaction.create!(
created_at: (Date.new(2024, 1, 1) + rand(1..200).days),
post:,
reaction_value: reactions.sample,
)
end
# reading time
100.times do |i|
UserVisit.create!(
user_id: current_user.id,
time_read: rand(1..200),
visited_at: (Date.new(2024, 1, 1) + i.days).strftime("%Y-%m-%d"),
)
end
# calendar
Fabricate.times(10, :post, user: current_user, created_at: Date.new(2024, 1, 25))
Fabricate.times(1, :post, user: current_user, created_at: Date.new(2024, 3, 12))
Fabricate.times(5, :post, user: current_user, created_at: Date.new(2024, 8, 19))
Fabricate.times(1, :post, user: current_user, created_at: Date.new(2024, 10, 29))
Fabricate.times(20, :post, user: current_user, created_at: Date.new(2024, 11, 2))
end
it "can visit the rewind page" do
visit("/my/activity/rewind")
pause_test
end
end