This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/integration/widgets/hamburger-menu-test.js
Martin Brennan e15c86e8c5
DEV: Topic tracking state improvements (#13218)
I merged this PR in yesterday, finally thinking this was done https://github.com/discourse/discourse/pull/12958 but then a wild performance regression occurred. These are the problem methods:

1aa20bd681/app/serializers/topic_tracking_state_serializer.rb (L13-L21)

Turns out date comparison is super expensive on the backend _as well as_ the frontend.

The fix was to just move the `treat_as_new_topic_start_date` into the SQL query rather than using the slower `UserOption#treat_as_new_topic_start_date` method in ruby. After this change, 1% of the total time is spent with the `created_in_new_period` comparison instead of ~20%.

----

History:

Original PR which had to be reverted **https://github.com/discourse/discourse/pull/12555**. See the description there for what this PR is achieving, plus below.

The issue with the original PR is addressed in 92ef54f402

If you went to the `x unread` link for a tag Chrome would freeze up and possibly crash, or eventually unfreeze after nearly 10 mins. Other routes for unread/new were similarly slow. From profiling the issue was the `sync` function of `topic-tracking-state.js`, which calls down to `isNew` which in turn calls `moment`, a change I had made in the PR above. The time it takes locally with ~1400 topics in the tracking state is 2.3 seconds.

To solve this issue, I have moved these calculations for "created in new period" and "unread not too old" into the tracking state serializer.

When I was looking at the profiler I also noticed this issue which was just compounding the problem. Every time we modify topic tracking state we recalculate the sidebar tracking/everything/tag counts. However this calls `forEachTracked` and `countTags` which can be quite expensive as they go through the whole tracking state (and were also calling the removed moment functions).

I added some logs and this was being called 30 times when navigating to a new /unread route because  `sync` is being called from `build-topic-route` (one for each topic loaded due to pagination). So I just added a debounce here and it makes things even faster.

Finally, I changed topic tracking state to use a Map so our counts of the state keys is faster (Maps have .size whereas objects you have to do Object.keys(obj) which is O(n).)

<!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2021-06-02 09:06:29 +10:00

280 lines
7.9 KiB
JavaScript

import componentTest, {
setupRenderingTest,
} from "discourse/tests/helpers/component-test";
import {
discourseModule,
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
import { NotificationLevels } from "discourse/lib/notification-levels";
import hbs from "htmlbars-inline-precompile";
const topCategoryIds = [2, 3, 1];
let mutedCategoryIds = [];
let unreadCategoryIds = [];
let categoriesByCount = [];
discourseModule(
"Integration | Component | Widget | hamburger-menu",
function (hooks) {
setupRenderingTest(hooks);
componentTest("prioritize faq", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.siteSettings.faq_url = "http://example.com/faq";
this.currentUser.set("read_faq", false);
},
test(assert) {
assert.ok(queryAll(".faq-priority").length);
assert.ok(!queryAll(".faq-link").length);
},
});
componentTest("prioritize faq - user has read", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.siteSettings.faq_url = "http://example.com/faq";
this.currentUser.set("read_faq", true);
},
test(assert) {
assert.ok(!queryAll(".faq-priority").length);
assert.ok(queryAll(".faq-link").length);
},
});
componentTest("staff menu - not staff", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.currentUser.set("staff", false);
},
test(assert) {
assert.ok(!queryAll(".admin-link").length);
},
});
componentTest("staff menu - moderator", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.currentUser.set("moderator", true);
this.currentUser.set("can_review", true);
},
test(assert) {
assert.ok(queryAll(".admin-link").length);
assert.ok(queryAll(".review").length);
assert.ok(!queryAll(".settings-link").length);
},
});
componentTest("staff menu - admin", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.currentUser.setProperties({ admin: true });
},
test(assert) {
assert.ok(queryAll(".settings-link").length);
},
});
componentTest("logged in links", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
test(assert) {
assert.ok(queryAll(".new-topics-link").length);
assert.ok(queryAll(".unread-topics-link").length);
},
});
componentTest("general links", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
anonymous: true,
test(assert) {
assert.ok(queryAll("li[class='']").length === 0);
assert.ok(queryAll(".latest-topics-link").length);
assert.ok(!queryAll(".new-topics-link").length);
assert.ok(!queryAll(".unread-topics-link").length);
assert.ok(queryAll(".top-topics-link").length);
assert.ok(queryAll(".badge-link").length);
assert.ok(queryAll(".category-link").length > 0);
},
});
let maxCategoriesToDisplay;
componentTest("top categories - anonymous", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
anonymous: true,
beforeEach() {
this.siteSettings.header_dropdown_category_count = 8;
},
test(assert) {
assert.equal(queryAll(".category-link").length, 8);
assert.equal(
queryAll(".category-link .category-name").text(),
this.site
.get("categoriesByCount")
.slice(0, 8)
.map((c) => c.name)
.join("")
);
},
});
componentTest("top categories - allow_uncategorized_topics", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
anonymous: true,
beforeEach() {
this.siteSettings.allow_uncategorized_topics = false;
this.siteSettings.header_dropdown_category_count = 8;
},
test(assert) {
assert.equal(queryAll(".category-link").length, 8);
assert.equal(
queryAll(".category-link .category-name").text(),
this.site
.get("categoriesByCount")
.filter((c) => c.name !== "uncategorized")
.slice(0, 8)
.map((c) => c.name)
.join("")
);
},
});
componentTest("top categories", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.siteSettings.header_dropdown_category_count = 8;
maxCategoriesToDisplay = this.siteSettings
.header_dropdown_category_count;
categoriesByCount = this.site.get("categoriesByCount").slice();
categoriesByCount.every((c) => {
if (!topCategoryIds.includes(c.id)) {
if (mutedCategoryIds.length === 0) {
mutedCategoryIds.push(c.id);
c.set("notification_level", NotificationLevels.MUTED);
} else if (unreadCategoryIds.length === 0) {
unreadCategoryIds.push(c.id);
for (let i = 0; i < 5; i++) {
c.topicTrackingState.modifyState(123 + i, {
category_id: c.id,
last_read_post_number: 1,
highest_post_number: 2,
notification_level: NotificationLevels.TRACKING,
unread_not_too_old: true,
});
}
} else {
unreadCategoryIds.splice(0, 0, c.id);
for (let i = 0; i < 10; i++) {
c.topicTrackingState.modifyState(321 + i, {
category_id: c.id,
last_read_post_number: null,
created_in_new_period: true,
});
}
return false;
}
}
return true;
});
this.currentUser.set("top_category_ids", topCategoryIds);
},
test(assert) {
assert.equal(
queryAll(".category-link").length,
maxCategoriesToDisplay,
"categories displayed limited by header_dropdown_category_count"
);
categoriesByCount = categoriesByCount.filter(
(c) => !mutedCategoryIds.includes(c.id)
);
let ids = [
...unreadCategoryIds,
...topCategoryIds,
...categoriesByCount.map((c) => c.id),
]
.uniq()
.slice(0, maxCategoriesToDisplay);
assert.equal(
queryAll(".category-link .category-name").text(),
ids
.map(
(id) =>
categoriesByCount.find((category) => category.id === id).name
)
.join(""),
"top categories are in the correct order"
);
},
});
componentTest("badges link - disabled", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.siteSettings.enable_badges = false;
},
test(assert) {
assert.ok(!queryAll(".badge-link").length);
},
});
componentTest("badges link", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
test(assert) {
assert.ok(queryAll(".badge-link").length);
},
});
componentTest("user directory link", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
test(assert) {
assert.ok(queryAll(".user-directory-link").length);
},
});
componentTest("user directory link - disabled", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
beforeEach() {
this.siteSettings.enable_user_directory = false;
},
test(assert) {
assert.ok(!queryAll(".user-directory-link").length);
},
});
componentTest("general links", {
template: hbs`{{mount-widget widget="hamburger-menu"}}`,
test(assert) {
assert.ok(queryAll(".about-link").length);
assert.ok(queryAll(".keyboard-shortcuts-link").length);
},
});
}
);