2021-03-15 13:03:00 +01:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
2024-08-02 15:34:06 +02:00
|
|
|
const {make_stream} = require("./lib/example_stream");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// In the Zulip app you can narrow your message stream by topic, by
|
2023-06-16 17:37:19 +02:00
|
|
|
// sender, by direct message recipient, by search keywords, etc.
|
|
|
|
// We will discuss narrows more broadly, but first let's test out a
|
|
|
|
// core piece of code that makes things work.
|
2021-03-15 13:03:00 +01:00
|
|
|
|
2023-02-22 23:03:47 +01:00
|
|
|
const {Filter} = zrequire("../src/filter");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_realm} = zrequire("state_data");
|
2021-03-15 13:03:00 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
|
2024-10-09 22:44:13 +02:00
|
|
|
set_realm({});
|
|
|
|
|
2024-07-11 13:54:09 +02:00
|
|
|
const denmark_stream = make_stream({
|
2021-03-15 13:03:00 +01:00
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 101,
|
|
|
|
subscribed: false,
|
2024-07-11 13:54:09 +02:00
|
|
|
});
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
run_test("filter", () => {
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub(denmark_stream);
|
|
|
|
|
|
|
|
const filter_terms = [
|
2024-08-03 03:05:34 +02:00
|
|
|
{operator: "stream", operand: denmark_stream.stream_id.toString()},
|
2021-03-15 13:03:00 +01:00
|
|
|
{operator: "topic", operand: "copenhagen"},
|
|
|
|
];
|
|
|
|
|
|
|
|
const filter = new Filter(filter_terms);
|
|
|
|
|
|
|
|
const predicate = filter.predicate();
|
|
|
|
|
|
|
|
// We don't need full-fledged messages to test the gist of
|
|
|
|
// our filter. If there are details that are distracting from
|
|
|
|
// your test, you should not feel guilty about removing them.
|
|
|
|
assert.equal(predicate({type: "personal"}), false);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
predicate({
|
|
|
|
type: "stream",
|
|
|
|
stream_id: denmark_stream.stream_id,
|
|
|
|
topic: "does not match filter",
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
predicate({
|
|
|
|
type: "stream",
|
|
|
|
stream_id: denmark_stream.stream_id,
|
|
|
|
topic: "copenhagen",
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// We have a "narrow" abstraction that sits roughly on top of the
|
|
|
|
// "filter" abstraction. If you are in a narrow, we track the
|
|
|
|
// state with the narrow_state module.
|
|
|
|
|
|
|
|
const narrow_state = zrequire("narrow_state");
|
2024-02-13 03:44:04 +01:00
|
|
|
const message_lists = zrequire("message_lists");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
run_test("narrow_state", () => {
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub(denmark_stream);
|
2024-02-13 03:44:04 +01:00
|
|
|
message_lists.set_current(undefined);
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// As we often do, first make assertions about the starting
|
|
|
|
// state:
|
|
|
|
|
2023-06-26 18:52:26 +02:00
|
|
|
assert.equal(narrow_state.stream_name(), undefined);
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Now set up a Filter object.
|
|
|
|
const filter_terms = [
|
2024-08-03 03:05:34 +02:00
|
|
|
{operator: "stream", operand: denmark_stream.stream_id.toString()},
|
2021-03-15 13:03:00 +01:00
|
|
|
{operator: "topic", operand: "copenhagen"},
|
|
|
|
];
|
|
|
|
|
|
|
|
const filter = new Filter(filter_terms);
|
|
|
|
|
|
|
|
// And here is where we actually change state.
|
2024-02-13 03:44:04 +01:00
|
|
|
message_lists.set_current({
|
|
|
|
data: {
|
|
|
|
filter,
|
|
|
|
},
|
|
|
|
});
|
2023-06-26 18:52:26 +02:00
|
|
|
assert.equal(narrow_state.stream_name(), "Denmark");
|
2021-03-15 13:03:00 +01:00
|
|
|
assert.equal(narrow_state.topic(), "copenhagen");
|
|
|
|
});
|