DEV: Convert presence service tests to actual unit tests (#20076)

This commit is contained in:
Jarek Radosz 2023-01-30 23:31:39 +01:00 committed by GitHub
parent 2c81b70b01
commit 7c1e1ef72b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 67 additions and 61 deletions

View File

@ -1,11 +1,14 @@
import { import {
acceptance, currentUser,
publishToMessageBus, publishToMessageBus,
} from "discourse/tests/helpers/qunit-helpers"; } from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit"; import { module, test } from "qunit";
import { PresenceChannelNotFound } from "discourse/services/presence"; import { PresenceChannelNotFound } from "discourse/services/presence";
import { setTestPresence } from "discourse/lib/user-presence"; import { setTestPresence } from "discourse/lib/user-presence";
import sinon from "sinon"; import sinon from "sinon";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
function usersFixture() { function usersFixture() {
return [ return [
@ -30,35 +33,37 @@ function usersFixture() {
]; ];
} }
acceptance("Presence - Subscribing", function (needs) { module("Unit | Service | presence | subscribing", function (hooks) {
needs.pretender((server, helper) => { setupTest(hooks);
server.get("/presence/get", async (request) => {
hooks.beforeEach(function () {
pretender.get("/presence/get", async (request) => {
const channels = request.queryParams.channels; const channels = request.queryParams.channels;
const response = {}; const result = {};
channels.forEach((c) => { channels.forEach((c) => {
if (c.startsWith("/test/")) { if (c.startsWith("/test/")) {
response[c] = { result[c] = {
count: 3, count: 3,
last_message_id: 1, last_message_id: 1,
users: usersFixture(), users: usersFixture(),
}; };
} else if (c.startsWith("/count-only/")) { } else if (c.startsWith("/count-only/")) {
response[c] = { result[c] = {
count: 3, count: 3,
last_message_id: 1, last_message_id: 1,
}; };
} else { } else {
response[c] = null; result[c] = null;
} }
}); });
return helper.response(200, response); return response(result);
}); });
}); });
test("subscribing and receiving updates", async function (assert) { test("subscribing and receiving updates", async function (assert) {
let presenceService = this.container.lookup("service:presence"); let presenceService = getOwner(this).lookup("service:presence");
let channel = presenceService.getChannel("/test/ch1"); let channel = presenceService.getChannel("/test/ch1");
let changes = 0; let changes = 0;
const countChanges = () => changes++; const countChanges = () => changes++;
@ -101,7 +106,7 @@ acceptance("Presence - Subscribing", function (needs) {
}); });
test("fetches data when no initial state", async function (assert) { test("fetches data when no initial state", async function (assert) {
let presenceService = this.container.lookup("service:presence"); let presenceService = getOwner(this).lookup("service:presence");
let channel = presenceService.getChannel("/test/ch1"); let channel = presenceService.getChannel("/test/ch1");
await channel.subscribe(); await channel.subscribe();
@ -147,7 +152,7 @@ acceptance("Presence - Subscribing", function (needs) {
}); });
test("raises error when subscribing to nonexistent channel", async function (assert) { test("raises error when subscribing to nonexistent channel", async function (assert) {
let presenceService = this.container.lookup("service:presence"); let presenceService = getOwner(this).lookup("service:presence");
let channel = presenceService.getChannel("/nonexistent/ch1"); let channel = presenceService.getChannel("/nonexistent/ch1");
assert.rejects( assert.rejects(
@ -158,7 +163,7 @@ acceptance("Presence - Subscribing", function (needs) {
}); });
test("can subscribe to count_only channel", async function (assert) { test("can subscribe to count_only channel", async function (assert) {
let presenceService = this.container.lookup("service:presence"); let presenceService = getOwner(this).lookup("service:presence");
let channel = presenceService.getChannel("/count-only/ch1"); let channel = presenceService.getChannel("/count-only/ch1");
await channel.subscribe(); await channel.subscribe();
@ -195,25 +200,21 @@ acceptance("Presence - Subscribing", function (needs) {
}); });
test("can share data between multiple PresenceChannel objects", async function (assert) { test("can share data between multiple PresenceChannel objects", async function (assert) {
let presenceService = this.container.lookup("service:presence"); let presenceService = getOwner(this).lookup("service:presence");
let channel = presenceService.getChannel("/test/ch1"); let channel = presenceService.getChannel("/test/ch1");
let channelDup = presenceService.getChannel("/test/ch1"); let channelDup = presenceService.getChannel("/test/ch1");
await channel.subscribe(); await channel.subscribe();
assert.strictEqual(channel.subscribed, true, "channel is subscribed"); assert.true(channel.subscribed, "channel is subscribed");
assert.strictEqual(channel.count, 3, "channel has the correct count"); assert.strictEqual(channel.count, 3, "channel has the correct count");
assert.strictEqual(channel.users.length, 3, "channel has users"); assert.strictEqual(channel.users.length, 3, "channel has users");
assert.strictEqual( assert.false(channelDup.subscribed, "channelDup is not subscribed");
channelDup.subscribed,
false,
"channelDup is not subscribed"
);
assert.strictEqual(channelDup.count, undefined, "channelDup has no count"); assert.strictEqual(channelDup.count, undefined, "channelDup has no count");
assert.strictEqual(channelDup.users, undefined, "channelDup has users"); assert.strictEqual(channelDup.users, undefined, "channelDup has users");
await channelDup.subscribe(); await channelDup.subscribe();
assert.strictEqual(channelDup.subscribed, true, "channelDup can subscribe"); assert.true(channelDup.subscribed, "channelDup can subscribe");
assert.ok( assert.ok(
channelDup._presenceState, channelDup._presenceState,
"channelDup has a valid internal state" "channelDup has a valid internal state"
@ -225,7 +226,7 @@ acceptance("Presence - Subscribing", function (needs) {
); );
await channel.unsubscribe(); await channel.unsubscribe();
assert.strictEqual(channel.subscribed, false, "channel can unsubscribe"); assert.false(channel.subscribed, "channel can unsubscribe");
assert.strictEqual( assert.strictEqual(
channelDup._presenceState, channelDup._presenceState,
presenceService._presenceChannelStates.get(channel.name), presenceService._presenceChannelStates.get(channel.name),
@ -233,7 +234,7 @@ acceptance("Presence - Subscribing", function (needs) {
); );
await channelDup.unsubscribe(); await channelDup.unsubscribe();
assert.strictEqual(channel.subscribed, false, "channelDup can unsubscribe"); assert.false(channel.subscribed, "channelDup can unsubscribe");
assert.strictEqual( assert.strictEqual(
presenceService._presenceChannelStates.get(channel.name), presenceService._presenceChannelStates.get(channel.name),
undefined, undefined,
@ -242,8 +243,8 @@ acceptance("Presence - Subscribing", function (needs) {
}); });
}); });
acceptance("Presence - Entering and Leaving", function (needs) { module("Unit | Service | presence | entering and leaving", function (hooks) {
needs.user(); setupTest(hooks);
let responseStartPromise; let responseStartPromise;
let resolveResponseStartPromise; let resolveResponseStartPromise;
@ -251,42 +252,41 @@ acceptance("Presence - Entering and Leaving", function (needs) {
const requests = []; const requests = [];
needs.hooks.beforeEach(() => { hooks.beforeEach(function () {
responseStartPromise = new Promise( responseStartPromise = new Promise(
(resolve) => (resolveResponseStartPromise = resolve) (resolve) => (resolveResponseStartPromise = resolve)
); );
});
needs.hooks.afterEach(() => { pretender.post("/presence/update", async (request) => {
requests.clear();
responseWaitPromise = null;
responseStartPromise = null;
});
needs.pretender((server, helper) => {
server.post("/presence/update", async (request) => {
resolveResponseStartPromise(); resolveResponseStartPromise();
await responseWaitPromise; await responseWaitPromise;
const body = new URLSearchParams(request.requestBody); const body = new URLSearchParams(request.requestBody);
requests.push(body); requests.push(body);
const response = {}; const result = {};
const channelsRequested = body.getAll("present_channels[]"); const channelsRequested = body.getAll("present_channels[]");
channelsRequested.forEach((c) => { channelsRequested.forEach((c) => {
if (c.startsWith("/test/")) { if (c.startsWith("/test/")) {
response[c] = true; result[c] = true;
} else { } else {
response[c] = false; result[c] = false;
} }
}); });
return helper.response(response); return response(result);
}); });
}); });
hooks.afterEach(function () {
requests.clear();
responseWaitPromise = null;
responseStartPromise = null;
});
test("can join and leave channels", async function (assert) { test("can join and leave channels", async function (assert) {
const presenceService = this.container.lookup("service:presence"); const presenceService = getOwner(this).lookup("service:presence");
presenceService.currentUser = currentUser();
const channel = presenceService.getChannel("/test/ch1"); const channel = presenceService.getChannel("/test/ch1");
await channel.enter(); await channel.enter();
@ -312,7 +312,8 @@ acceptance("Presence - Entering and Leaving", function (needs) {
}); });
test("join should be a no-op if already present", async function (assert) { test("join should be a no-op if already present", async function (assert) {
const presenceService = this.container.lookup("service:presence"); const presenceService = getOwner(this).lookup("service:presence");
presenceService.currentUser = currentUser();
const channel = presenceService.getChannel("/test/ch1"); const channel = presenceService.getChannel("/test/ch1");
await channel.enter(); await channel.enter();
@ -327,7 +328,8 @@ acceptance("Presence - Entering and Leaving", function (needs) {
}); });
test("leave should be a no-op if not present", async function (assert) { test("leave should be a no-op if not present", async function (assert) {
const presenceService = this.container.lookup("service:presence"); const presenceService = getOwner(this).lookup("service:presence");
presenceService.currentUser = currentUser();
const channel = presenceService.getChannel("/test/ch1"); const channel = presenceService.getChannel("/test/ch1");
await channel.enter(); await channel.enter();
@ -350,7 +352,8 @@ acceptance("Presence - Entering and Leaving", function (needs) {
(resolve) => (resolveServerResponse = resolve) (resolve) => (resolveServerResponse = resolve)
); );
const presenceService = this.container.lookup("service:presence"); const presenceService = getOwner(this).lookup("service:presence");
presenceService.currentUser = currentUser();
const channel = presenceService.getChannel("/test/ch1"); const channel = presenceService.getChannel("/test/ch1");
const enterPromise = channel.enter(); const enterPromise = channel.enter();
@ -374,7 +377,8 @@ acceptance("Presence - Entering and Leaving", function (needs) {
}); });
test("raises an error when entering a non-existent channel", async function (assert) { test("raises an error when entering a non-existent channel", async function (assert) {
const presenceService = this.container.lookup("service:presence"); const presenceService = getOwner(this).lookup("service:presence");
presenceService.currentUser = currentUser();
const channel = presenceService.getChannel("/blah/does-not-exist"); const channel = presenceService.getChannel("/blah/does-not-exist");
await assert.rejects( await assert.rejects(
channel.enter(), channel.enter(),
@ -384,45 +388,47 @@ acceptance("Presence - Entering and Leaving", function (needs) {
}); });
test("deduplicates calls from multiple PresenceChannel instances", async function (assert) { test("deduplicates calls from multiple PresenceChannel instances", async function (assert) {
const presenceService = this.container.lookup("service:presence"); const presenceService = getOwner(this).lookup("service:presence");
presenceService.currentUser = currentUser();
const channel = presenceService.getChannel("/test/ch1"); const channel = presenceService.getChannel("/test/ch1");
const channelDup = presenceService.getChannel("/test/ch1"); const channelDup = presenceService.getChannel("/test/ch1");
await channel.enter(); await channel.enter();
assert.strictEqual(channel.present, true, "channel is present"); assert.true(channel.present, "channel is present");
assert.strictEqual(channelDup.present, false, "channelDup is absent"); assert.false(channelDup.present, "channelDup is absent");
assert.ok( assert.true(
presenceService._presentChannels.has("/test/ch1"), presenceService._presentChannels.has("/test/ch1"),
"service shows present" "service shows present"
); );
await channelDup.enter(); await channelDup.enter();
assert.strictEqual(channel.present, true, "channel is present"); assert.true(channel.present, "channel is present");
assert.strictEqual(channelDup.present, true, "channelDup is present"); assert.true(channelDup.present, "channelDup is present");
assert.ok( assert.true(
presenceService._presentChannels.has("/test/ch1"), presenceService._presentChannels.has("/test/ch1"),
"service shows present" "service shows present"
); );
await channel.leave(); await channel.leave();
assert.strictEqual(channel.present, false, "channel is absent"); assert.false(channel.present, "channel is absent");
assert.strictEqual(channelDup.present, true, "channelDup is present"); assert.true(channelDup.present, "channelDup is present");
assert.ok( assert.true(
presenceService._presentChannels.has("/test/ch1"), presenceService._presentChannels.has("/test/ch1"),
"service shows present" "service shows present"
); );
await channelDup.leave(); await channelDup.leave();
assert.strictEqual(channel.present, false, "channel is absent"); assert.false(channel.present, "channel is absent");
assert.strictEqual(channel.present, false, "channelDup is absent"); assert.false(channel.present, "channelDup is absent");
assert.notOk( assert.false(
presenceService._presentChannels.has("/test/ch1"), presenceService._presentChannels.has("/test/ch1"),
"service shows absent" "service shows absent"
); );
}); });
test("handles the onlyWhileActive flag", async function (assert) { test("handles the onlyWhileActive flag", async function (assert) {
const presenceService = this.container.lookup("service:presence"); const presenceService = getOwner(this).lookup("service:presence");
presenceService.currentUser = currentUser();
const channel = presenceService.getChannel("/test/ch1"); const channel = presenceService.getChannel("/test/ch1");
await channel.enter(); await channel.enter();
requests.pop(); // Throw away this request requests.pop(); // Throw away this request
@ -451,7 +457,7 @@ acceptance("Presence - Entering and Leaving", function (needs) {
["/test/ch2"], ["/test/ch2"],
"ch2 remained present" "ch2 remained present"
); );
assert.ok( assert.true(
request.getAll("leave_channels[]").includes("/test/ch1"), request.getAll("leave_channels[]").includes("/test/ch1"),
"left ch1" "left ch1"
); );
@ -459,7 +465,7 @@ acceptance("Presence - Entering and Leaving", function (needs) {
await channel2.leave(); await channel2.leave();
assert.strictEqual(requests.length, 1, "updated the server"); assert.strictEqual(requests.length, 1, "updated the server");
request = requests.pop(); request = requests.pop();
assert.ok( assert.true(
request.getAll("leave_channels[]").includes("/test/ch2"), request.getAll("leave_channels[]").includes("/test/ch2"),
"left ch2" "left ch2"
); );