2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const stream_sort = zrequire("stream_sort");
|
2017-06-01 06:21:01 +02:00
|
|
|
|
2018-04-24 19:26:35 +02:00
|
|
|
const scalene = {
|
2017-06-01 06:21:01 +02:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "scalene",
|
2017-06-01 06:21:01 +02:00
|
|
|
stream_id: 1,
|
|
|
|
pin_to_top: true,
|
2018-04-24 19:26:35 +02:00
|
|
|
};
|
|
|
|
const fast_tortoise = {
|
2017-06-01 06:21:01 +02:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "fast tortoise",
|
2017-06-01 06:21:01 +02:00
|
|
|
stream_id: 2,
|
|
|
|
pin_to_top: false,
|
2018-04-24 19:26:35 +02:00
|
|
|
};
|
|
|
|
const pneumonia = {
|
2017-06-01 06:21:01 +02:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "pneumonia",
|
2017-06-01 06:21:01 +02:00
|
|
|
stream_id: 3,
|
|
|
|
pin_to_top: false,
|
2018-04-24 19:26:35 +02:00
|
|
|
};
|
|
|
|
const clarinet = {
|
2017-06-01 06:21:01 +02:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "clarinet",
|
2017-06-01 06:21:01 +02:00
|
|
|
stream_id: 4,
|
|
|
|
pin_to_top: false,
|
2018-04-24 19:26:35 +02:00
|
|
|
};
|
|
|
|
const weaving = {
|
2017-06-01 06:21:01 +02:00
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "weaving",
|
2017-06-01 06:21:01 +02:00
|
|
|
stream_id: 5,
|
|
|
|
pin_to_top: false,
|
2018-04-24 19:26:35 +02:00
|
|
|
};
|
2022-06-16 11:39:05 +02:00
|
|
|
const stream_hyphen_underscore_slash = {
|
|
|
|
subscribed: true,
|
|
|
|
name: "stream-hyphen_underscore/slash",
|
|
|
|
stream_id: 6,
|
|
|
|
pin_to_top: false,
|
|
|
|
};
|
2022-02-20 21:32:14 +01:00
|
|
|
const muted_active = {
|
|
|
|
subscribed: true,
|
|
|
|
name: "muted active",
|
|
|
|
stream_id: 7,
|
|
|
|
pin_to_top: false,
|
|
|
|
is_muted: true,
|
|
|
|
};
|
|
|
|
const muted_pinned = {
|
|
|
|
subscribed: true,
|
|
|
|
name: "muted pinned",
|
|
|
|
stream_id: 8,
|
|
|
|
pin_to_top: true,
|
|
|
|
is_muted: true,
|
|
|
|
};
|
2018-04-24 19:26:35 +02:00
|
|
|
|
2019-12-27 21:45:28 +01:00
|
|
|
function sort_groups(query) {
|
2020-07-09 15:08:01 +02:00
|
|
|
const streams = stream_data.subscribed_stream_ids();
|
2019-12-27 21:45:28 +01:00
|
|
|
return stream_sort.sort_groups(streams, query);
|
|
|
|
}
|
|
|
|
|
2021-03-13 14:51:32 +01:00
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2021-03-13 14:51:32 +01:00
|
|
|
stream_data.clear_subscriptions();
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2021-03-13 14:51:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("no_subscribed_streams", () => {
|
|
|
|
const sorted = sort_groups("");
|
|
|
|
assert.deepEqual(sorted, {
|
|
|
|
dormant_streams: [],
|
2022-02-20 21:32:14 +01:00
|
|
|
muted_active_streams: [],
|
|
|
|
muted_pinned_streams: [],
|
2021-03-13 14:51:32 +01:00
|
|
|
normal_streams: [],
|
|
|
|
pinned_streams: [],
|
|
|
|
same_as_before: sorted.same_as_before,
|
|
|
|
});
|
|
|
|
assert.equal(stream_sort.first_stream_id(), undefined);
|
|
|
|
});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
test("basics", ({override_rewire}) => {
|
2021-03-13 14:51:32 +01:00
|
|
|
stream_data.add_sub(scalene);
|
|
|
|
stream_data.add_sub(fast_tortoise);
|
|
|
|
stream_data.add_sub(pneumonia);
|
|
|
|
stream_data.add_sub(clarinet);
|
|
|
|
stream_data.add_sub(weaving);
|
2022-06-16 11:39:05 +02:00
|
|
|
stream_data.add_sub(stream_hyphen_underscore_slash);
|
2022-02-20 21:32:14 +01:00
|
|
|
stream_data.add_sub(muted_active);
|
|
|
|
stream_data.add_sub(muted_pinned);
|
2021-03-13 14:51:32 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(stream_data, "is_active", (sub) => sub.name !== "pneumonia");
|
2017-06-01 06:21:01 +02:00
|
|
|
|
|
|
|
// Test sorting into categories/alphabetized
|
2019-12-27 21:45:28 +01:00
|
|
|
let sorted = sort_groups("");
|
2020-07-09 15:08:01 +02:00
|
|
|
assert.deepEqual(sorted.pinned_streams, [scalene.stream_id]);
|
2022-06-16 11:39:05 +02:00
|
|
|
assert.deepEqual(sorted.normal_streams, [
|
|
|
|
clarinet.stream_id,
|
|
|
|
fast_tortoise.stream_id,
|
|
|
|
stream_hyphen_underscore_slash.stream_id,
|
|
|
|
]);
|
2022-02-20 21:32:14 +01:00
|
|
|
assert.deepEqual(sorted.muted_pinned_streams, [muted_pinned.stream_id]);
|
|
|
|
assert.deepEqual(sorted.muted_active_streams, [muted_active.stream_id]);
|
2020-07-09 15:08:01 +02:00
|
|
|
assert.deepEqual(sorted.dormant_streams, [pneumonia.stream_id]);
|
2017-06-01 06:21:01 +02:00
|
|
|
|
2018-04-24 16:59:01 +02:00
|
|
|
// Test cursor helpers.
|
|
|
|
assert.equal(stream_sort.first_stream_id(), scalene.stream_id);
|
|
|
|
|
|
|
|
assert.equal(stream_sort.prev_stream_id(scalene.stream_id), undefined);
|
2022-02-20 21:32:14 +01:00
|
|
|
assert.equal(stream_sort.prev_stream_id(muted_pinned.stream_id), scalene.stream_id);
|
|
|
|
assert.equal(stream_sort.prev_stream_id(clarinet.stream_id), muted_pinned.stream_id);
|
2018-04-24 16:59:01 +02:00
|
|
|
|
2022-06-16 11:39:05 +02:00
|
|
|
assert.equal(
|
|
|
|
stream_sort.next_stream_id(fast_tortoise.stream_id),
|
|
|
|
stream_hyphen_underscore_slash.stream_id,
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
stream_sort.next_stream_id(stream_hyphen_underscore_slash.stream_id),
|
2022-02-20 21:32:14 +01:00
|
|
|
muted_active.stream_id,
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
stream_sort.next_stream_id(fast_tortoise.stream_id),
|
|
|
|
stream_hyphen_underscore_slash.stream_id,
|
2022-06-16 11:39:05 +02:00
|
|
|
);
|
2022-02-20 21:32:14 +01:00
|
|
|
assert.equal(stream_sort.next_stream_id(muted_active.stream_id), pneumonia.stream_id);
|
2018-04-24 16:59:01 +02:00
|
|
|
assert.equal(stream_sort.next_stream_id(pneumonia.stream_id), undefined);
|
|
|
|
|
2017-06-01 06:21:01 +02:00
|
|
|
// Test filtering
|
2019-12-27 21:45:28 +01:00
|
|
|
sorted = sort_groups("s");
|
2020-07-09 15:08:01 +02:00
|
|
|
assert.deepEqual(sorted.pinned_streams, [scalene.stream_id]);
|
2022-06-16 11:39:05 +02:00
|
|
|
assert.deepEqual(sorted.normal_streams, [stream_hyphen_underscore_slash.stream_id]);
|
2017-06-01 06:21:01 +02:00
|
|
|
assert.deepEqual(sorted.dormant_streams, []);
|
|
|
|
|
2018-04-24 16:59:01 +02:00
|
|
|
assert.equal(stream_sort.prev_stream_id(clarinet.stream_id), undefined);
|
|
|
|
|
|
|
|
assert.equal(stream_sort.next_stream_id(clarinet.stream_id), undefined);
|
|
|
|
|
2017-06-01 06:21:01 +02:00
|
|
|
// Test searching entire word, case-insensitive
|
2019-12-27 21:45:28 +01:00
|
|
|
sorted = sort_groups("PnEuMoNiA");
|
2017-06-01 06:21:01 +02:00
|
|
|
assert.deepEqual(sorted.pinned_streams, []);
|
|
|
|
assert.deepEqual(sorted.normal_streams, []);
|
2020-07-09 15:08:01 +02:00
|
|
|
assert.deepEqual(sorted.dormant_streams, [pneumonia.stream_id]);
|
2017-09-15 06:53:25 +02:00
|
|
|
|
|
|
|
// Test searching part of word
|
2019-12-27 21:45:28 +01:00
|
|
|
sorted = sort_groups("tortoise");
|
2017-09-15 06:53:25 +02:00
|
|
|
assert.deepEqual(sorted.pinned_streams, []);
|
2020-07-09 15:08:01 +02:00
|
|
|
assert.deepEqual(sorted.normal_streams, [fast_tortoise.stream_id]);
|
2017-09-15 06:53:25 +02:00
|
|
|
assert.deepEqual(sorted.dormant_streams, []);
|
|
|
|
|
|
|
|
// Test searching stream with spaces
|
2019-12-27 21:45:28 +01:00
|
|
|
sorted = sort_groups("fast t");
|
2017-09-15 06:53:25 +02:00
|
|
|
assert.deepEqual(sorted.pinned_streams, []);
|
2020-07-09 15:08:01 +02:00
|
|
|
assert.deepEqual(sorted.normal_streams, [fast_tortoise.stream_id]);
|
2017-09-15 06:53:25 +02:00
|
|
|
assert.deepEqual(sorted.dormant_streams, []);
|
2022-06-16 11:39:05 +02:00
|
|
|
|
|
|
|
// Test searching part of stream name with non space word separators
|
|
|
|
sorted = sort_groups("hyphen");
|
|
|
|
assert.deepEqual(sorted.pinned_streams, []);
|
|
|
|
assert.deepEqual(sorted.normal_streams, [stream_hyphen_underscore_slash.stream_id]);
|
|
|
|
assert.deepEqual(sorted.dormant_streams, []);
|
|
|
|
|
|
|
|
sorted = sort_groups("hyphen_underscore");
|
|
|
|
assert.deepEqual(sorted.pinned_streams, []);
|
|
|
|
assert.deepEqual(sorted.normal_streams, [stream_hyphen_underscore_slash.stream_id]);
|
|
|
|
assert.deepEqual(sorted.dormant_streams, []);
|
|
|
|
|
|
|
|
// Test searching part of stream name with non space word separators
|
|
|
|
sorted = sort_groups("underscore");
|
|
|
|
assert.deepEqual(sorted.pinned_streams, []);
|
|
|
|
assert.deepEqual(sorted.normal_streams, [stream_hyphen_underscore_slash.stream_id]);
|
|
|
|
assert.deepEqual(sorted.dormant_streams, []);
|
2017-06-01 06:21:01 +02:00
|
|
|
});
|