2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
2024-09-06 08:40:06 +02:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2023-02-22 23:04:10 +01:00
|
|
|
const $ = require("./lib/zjquery");
|
|
|
|
|
|
|
|
const message_edit = mock_esm("../src/message_edit");
|
|
|
|
const message_lists = mock_esm("../src/message_lists");
|
2023-10-06 22:29:25 +02:00
|
|
|
const message_notifications = mock_esm("../src/message_notifications");
|
2023-02-22 23:04:10 +01:00
|
|
|
const pm_list = mock_esm("../src/pm_list");
|
|
|
|
const stream_list = mock_esm("../src/stream_list");
|
|
|
|
const unread_ui = mock_esm("../src/unread_ui");
|
2024-09-06 08:40:06 +02:00
|
|
|
mock_esm("../src/message_parser", {
|
|
|
|
message_has_attachment: noop,
|
|
|
|
message_has_image: noop,
|
|
|
|
message_has_link: noop,
|
|
|
|
});
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current = {};
|
2024-04-21 05:31:35 +02:00
|
|
|
message_lists.all_rendered_message_lists = () => [message_lists.current];
|
2021-02-28 00:40:18 +01:00
|
|
|
|
2021-03-07 13:57:14 +01:00
|
|
|
const people = zrequire("people");
|
2020-12-01 23:21:38 +01:00
|
|
|
const message_events = zrequire("message_events");
|
2024-09-06 08:40:06 +02:00
|
|
|
message_events.__Rewire__("update_views_filtered_on_message_property", () => {});
|
2021-03-28 17:57:53 +02:00
|
|
|
const message_helper = zrequire("message_helper");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_realm} = zrequire("state_data");
|
2020-12-01 23:21:38 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const stream_topic_history = zrequire("stream_topic_history");
|
|
|
|
const unread = zrequire("unread");
|
2024-10-09 08:44:21 +02:00
|
|
|
const {initialize_user_settings} = zrequire("user_settings");
|
|
|
|
|
2024-10-09 22:44:13 +02:00
|
|
|
const realm = {};
|
|
|
|
set_realm(realm);
|
|
|
|
|
2024-10-09 08:44:21 +02:00
|
|
|
initialize_user_settings({user_settings: {}});
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2018-09-11 17:16:52 +02:00
|
|
|
user_id: 32,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Patel",
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2020-04-11 02:15:41 +02:00
|
|
|
const denmark = {
|
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Denmark",
|
2020-04-11 02:15:41 +02:00
|
|
|
stream_id: 101,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
function test_helper(side_effects) {
|
|
|
|
const events = [];
|
|
|
|
|
2021-02-11 01:42:59 +01:00
|
|
|
for (const [module, field] of side_effects) {
|
|
|
|
module[field] = () => {
|
|
|
|
events.push([module, field]);
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
const self = {};
|
|
|
|
|
|
|
|
self.verify = () => {
|
|
|
|
assert.deepEqual(side_effects, events);
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
run_test("update_messages", ({override}) => {
|
2024-05-28 22:06:15 +02:00
|
|
|
const raw_message = {
|
2018-09-11 17:16:52 +02:00
|
|
|
id: 111,
|
2020-04-11 02:15:41 +02:00
|
|
|
display_recipient: denmark.name,
|
2020-07-15 01:29:15 +02:00
|
|
|
flags: ["mentioned"],
|
2018-09-11 17:16:52 +02:00
|
|
|
sender_id: alice.user_id,
|
2020-04-11 02:15:41 +02:00
|
|
|
stream_id: denmark.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
|
|
|
type: "stream",
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
|
2024-05-28 22:06:15 +02:00
|
|
|
const original_message = message_helper.process_new_message(raw_message);
|
2020-04-11 02:15:41 +02:00
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
assert.equal(original_message.mentioned, true);
|
2020-04-11 02:15:41 +02:00
|
|
|
assert.equal(original_message.unread, true);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(stream_topic_history.get_recent_topic_names(denmark.stream_id), ["lunch"]);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
unread.update_message_for_mention(original_message);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(unread.unread_mentions_counter.has(original_message.id));
|
2020-04-11 02:51:45 +02:00
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
const events = [
|
|
|
|
{
|
2020-04-11 02:15:41 +02:00
|
|
|
message_id: original_message.id,
|
2018-09-11 17:16:52 +02:00
|
|
|
flags: [],
|
2020-07-15 01:29:15 +02:00
|
|
|
orig_content: "old stuff",
|
|
|
|
content: "**new content**",
|
|
|
|
rendered_content: "<b>new content</b>",
|
2018-09-11 17:16:52 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.view = {};
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let rendered_mgs;
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.view.rerender_messages = (msgs_to_rerender, message_content_edited) => {
|
2018-09-11 17:16:52 +02:00
|
|
|
rendered_mgs = msgs_to_rerender;
|
|
|
|
assert.equal(message_content_edited, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const side_effects = [
|
2022-08-09 23:23:32 +02:00
|
|
|
[message_edit, "end_message_edit"],
|
2023-10-06 22:29:25 +02:00
|
|
|
[message_notifications, "received_messages"],
|
2021-02-11 01:42:59 +01:00
|
|
|
[unread_ui, "update_unread_counts"],
|
|
|
|
[stream_list, "update_streams_sidebar"],
|
|
|
|
[pm_list, "update_private_messages"],
|
2018-09-11 17:16:52 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const helper = test_helper(side_effects);
|
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_allow_edit_history", false);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $message_edit_history_modal = $.create("#message-edit-history");
|
|
|
|
const $modal = $.create("micromodal").addClass("modal--open");
|
|
|
|
$message_edit_history_modal.set_parents_result(".micromodal", $modal);
|
2021-07-07 11:47:18 +02:00
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
// TEST THIS:
|
|
|
|
message_events.update_messages(events);
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!unread.unread_mentions_counter.has(original_message.id));
|
2020-04-11 02:51:45 +02:00
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
helper.verify();
|
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
assert.deepEqual(rendered_mgs, [
|
2018-09-11 17:16:52 +02:00
|
|
|
{
|
2024-05-28 08:11:17 +02:00
|
|
|
display_reply_to: undefined,
|
2018-09-11 17:16:52 +02:00
|
|
|
alerted: false,
|
2024-05-28 22:55:41 +02:00
|
|
|
clean_reactions: new Map(),
|
2020-04-11 02:15:41 +02:00
|
|
|
collapsed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "<b>new content</b>",
|
2020-04-11 02:15:41 +02:00
|
|
|
display_recipient: denmark.name,
|
|
|
|
historical: false,
|
2018-09-11 17:16:52 +02:00
|
|
|
id: 111,
|
2020-04-11 02:15:41 +02:00
|
|
|
is_stream: true,
|
2024-05-28 08:11:17 +02:00
|
|
|
is_private: false,
|
2018-09-11 17:16:52 +02:00
|
|
|
last_edit_timestamp: undefined,
|
|
|
|
mentioned: false,
|
2023-11-03 15:20:44 +01:00
|
|
|
stream_wildcard_mentioned: false,
|
|
|
|
topic_wildcard_mentioned: false,
|
2018-09-11 17:16:52 +02:00
|
|
|
mentioned_me_directly: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
raw_content: "**new content**",
|
2020-04-11 02:15:41 +02:00
|
|
|
reply_to: alice.email,
|
|
|
|
sender_email: alice.email,
|
|
|
|
sender_full_name: alice.full_name,
|
2018-09-11 17:16:52 +02:00
|
|
|
sender_id: 32,
|
|
|
|
sent_by_me: false,
|
2020-04-11 02:15:41 +02:00
|
|
|
starred: false,
|
2022-02-05 19:13:05 +01:00
|
|
|
status_emoji_info: undefined,
|
2020-04-11 02:15:41 +02:00
|
|
|
stream_id: denmark.stream_id,
|
2024-05-28 08:11:17 +02:00
|
|
|
stream: "Denmark",
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
|
|
|
type: "stream",
|
2020-04-11 02:15:41 +02:00
|
|
|
unread: true,
|
2018-09-11 17:16:52 +02:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|