2020-08-01 03:43:15 +02:00
|
|
|
|
"use strict";
|
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
|
const {mock_esm, set_global, zrequire, with_overrides} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
|
const $ = require("../zjsunit/zjquery");
|
2021-07-28 16:00:58 +02:00
|
|
|
|
const {user_settings} = require("../zjsunit/zpage_params");
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2022-11-02 01:10:11 +01:00
|
|
|
|
const blueslip = zrequire("blueslip");
|
2022-08-28 03:48:27 +02:00
|
|
|
|
const compose_pm_pill = zrequire("compose_pm_pill");
|
|
|
|
|
const user_pill = zrequire("user_pill");
|
2022-05-17 03:13:57 +02:00
|
|
|
|
const people = zrequire("people");
|
2022-08-28 03:48:27 +02:00
|
|
|
|
const compose_state = zrequire("compose_state");
|
2022-10-14 02:18:56 +02:00
|
|
|
|
const sub_store = zrequire("sub_store");
|
|
|
|
|
const stream_data = zrequire("stream_data");
|
2022-11-02 01:10:11 +01:00
|
|
|
|
|
2022-05-17 03:13:57 +02:00
|
|
|
|
const aaron = {
|
|
|
|
|
email: "aaron@zulip.com",
|
|
|
|
|
user_id: 6,
|
|
|
|
|
full_name: "Aaron",
|
|
|
|
|
};
|
|
|
|
|
people.add_active_user(aaron);
|
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
|
const noop = () => {};
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2021-11-30 19:36:27 +01:00
|
|
|
|
const setTimeout_delay = 3000;
|
|
|
|
|
set_global("setTimeout", (f, delay) => {
|
|
|
|
|
assert.equal(delay, setTimeout_delay);
|
|
|
|
|
f();
|
|
|
|
|
});
|
2021-03-10 06:10:32 +01:00
|
|
|
|
mock_esm("../../static/js/markdown", {
|
2021-03-06 17:37:51 +01:00
|
|
|
|
apply_markdown: noop,
|
|
|
|
|
});
|
2022-07-10 01:06:33 +02:00
|
|
|
|
mock_esm("../../static/js/overlays", {
|
|
|
|
|
open_overlay: noop,
|
|
|
|
|
});
|
2022-10-14 02:18:56 +02:00
|
|
|
|
|
2021-11-30 19:36:27 +01:00
|
|
|
|
const tippy_sel = ".top_left_drafts .unread_count";
|
|
|
|
|
let tippy_args;
|
|
|
|
|
let tippy_show_called;
|
|
|
|
|
let tippy_destroy_called;
|
|
|
|
|
mock_esm("tippy.js", {
|
|
|
|
|
default: (sel, opts) => {
|
|
|
|
|
assert.equal(sel, tippy_sel);
|
|
|
|
|
assert.deepEqual(opts, tippy_args);
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
show: () => {
|
|
|
|
|
tippy_show_called = true;
|
|
|
|
|
},
|
|
|
|
|
destroy: () => {
|
|
|
|
|
tippy_destroy_called = true;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-07-28 16:00:58 +02:00
|
|
|
|
user_settings.twenty_four_hour_time = false;
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
|
const {localstorage} = zrequire("localstorage");
|
|
|
|
|
const drafts = zrequire("drafts");
|
|
|
|
|
const timerender = zrequire("timerender");
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_1 = {
|
2017-02-22 02:34:05 +01:00
|
|
|
|
stream: "stream",
|
2020-06-06 23:57:20 +02:00
|
|
|
|
stream_id: 30,
|
2018-12-16 17:36:41 +01:00
|
|
|
|
topic: "topic",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_2 = {
|
2017-02-22 02:34:05 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
2017-03-29 08:54:26 +02:00
|
|
|
|
reply_to: "aaron@zulip.com",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const short_msg = {
|
2018-08-21 23:04:30 +02:00
|
|
|
|
stream: "stream",
|
2022-09-28 21:35:43 +02:00
|
|
|
|
topic: "topic",
|
2018-08-21 23:04:30 +02:00
|
|
|
|
type: "stream",
|
|
|
|
|
content: "a",
|
|
|
|
|
};
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-03-14 10:30:49 +01:00
|
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
|
run_test(label, (helpers) => {
|
2021-06-04 19:27:45 +02:00
|
|
|
|
$("#draft_overlay").css = () => {};
|
2022-04-10 02:16:59 +02:00
|
|
|
|
window.localStorage.clear();
|
2022-07-10 01:06:33 +02:00
|
|
|
|
f(helpers);
|
2021-03-14 10:30:49 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
test("draft_model add", ({override}) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
2021-02-14 12:55:03 +01:00
|
|
|
|
assert.equal(ls.get("draft"), undefined);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
|
const $unread_count = $("<unread-count-stub>");
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$(".top_left_drafts").set_find_results(".unread_count", $unread_count);
|
2021-07-04 23:17:03 +02:00
|
|
|
|
|
2021-02-14 12:55:03 +01:00
|
|
|
|
override(Date, "now", () => 1);
|
|
|
|
|
const expected = {...draft_1};
|
|
|
|
|
expected.updatedAt = 1;
|
|
|
|
|
const id = draft_model.addDraft({...draft_1});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
|
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-03-14 10:30:49 +01:00
|
|
|
|
test("draft_model edit", () => {
|
2021-02-14 12:55:03 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
assert.equal(ls.get("draft"), undefined);
|
|
|
|
|
let id;
|
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
|
const $unread_count = $("<unread-count-stub>");
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$(".top_left_drafts").set_find_results(".unread_count", $unread_count);
|
2021-07-04 23:17:03 +02:00
|
|
|
|
|
2022-01-08 09:54:05 +01:00
|
|
|
|
with_overrides(({override}) => {
|
2021-02-14 12:55:03 +01:00
|
|
|
|
override(Date, "now", () => 1);
|
|
|
|
|
const expected = {...draft_1};
|
|
|
|
|
expected.updatedAt = 1;
|
|
|
|
|
id = draft_model.addDraft({...draft_1});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-08 09:54:05 +01:00
|
|
|
|
with_overrides(({override}) => {
|
2021-02-14 12:55:03 +01:00
|
|
|
|
override(Date, "now", () => 2);
|
|
|
|
|
const expected = {...draft_2};
|
|
|
|
|
expected.updatedAt = 2;
|
|
|
|
|
draft_model.editDraft(id, {...draft_2});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
test("draft_model delete", ({override}) => {
|
2021-02-14 12:55:03 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
assert.equal(ls.get("draft"), undefined);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
|
const $unread_count = $("<unread-count-stub>");
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$(".top_left_drafts").set_find_results(".unread_count", $unread_count);
|
2021-07-04 23:17:03 +02:00
|
|
|
|
|
2021-02-14 12:55:03 +01:00
|
|
|
|
override(Date, "now", () => 1);
|
|
|
|
|
const expected = {...draft_1};
|
|
|
|
|
expected.updatedAt = 1;
|
|
|
|
|
const id = draft_model.addDraft({...draft_1});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-02-14 12:55:03 +01:00
|
|
|
|
draft_model.deleteDraft(id);
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), false);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-03-29 08:54:26 +02:00
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
test("snapshot_message", ({override_rewire}) => {
|
2022-10-14 02:18:56 +02:00
|
|
|
|
stream_data.get_sub = (stream_name) => {
|
|
|
|
|
assert.equal(stream_name, "stream");
|
|
|
|
|
return {stream_id: 30};
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
override_rewire(user_pill, "get_user_ids", () => [aaron.user_id]);
|
|
|
|
|
override_rewire(compose_pm_pill, "set_from_emails", noop);
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
let curr_draft;
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
function set_compose_state() {
|
|
|
|
|
compose_state.set_message_type(curr_draft.type);
|
|
|
|
|
compose_state.message_content(curr_draft.content);
|
|
|
|
|
compose_state.stream_name(curr_draft.stream);
|
|
|
|
|
compose_state.topic(curr_draft.topic);
|
|
|
|
|
compose_state.private_message_recipient(curr_draft.private_message_recipient);
|
2017-03-29 08:54:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
curr_draft = draft_1;
|
2022-08-28 03:48:27 +02:00
|
|
|
|
set_compose_state();
|
2017-03-29 08:54:26 +02:00
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_1);
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
curr_draft = draft_2;
|
2022-08-28 03:48:27 +02:00
|
|
|
|
set_compose_state();
|
2017-03-29 08:54:26 +02:00
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_2);
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
curr_draft = short_msg;
|
2022-08-28 03:48:27 +02:00
|
|
|
|
set_compose_state();
|
2018-08-21 23:04:30 +02:00
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), undefined);
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
curr_draft = {};
|
2022-08-28 03:48:27 +02:00
|
|
|
|
set_compose_state();
|
2017-03-29 08:54:26 +02:00
|
|
|
|
assert.equal(drafts.snapshot_message(), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-06-02 23:12:15 +02:00
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
|
test("initialize", ({override_rewire}) => {
|
2021-02-23 14:37:26 +01:00
|
|
|
|
window.addEventListener = (event_name, f) => {
|
2017-06-02 23:12:15 +02:00
|
|
|
|
assert.equal(event_name, "beforeunload");
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let called = false;
|
2022-01-08 10:27:06 +01:00
|
|
|
|
override_rewire(drafts, "update_draft", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
|
called = true;
|
2021-02-28 00:37:57 +01:00
|
|
|
|
});
|
2017-06-02 23:12:15 +02:00
|
|
|
|
f();
|
2021-06-10 08:32:54 +02:00
|
|
|
|
assert.ok(called);
|
2017-06-02 23:12:15 +02:00
|
|
|
|
};
|
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
|
const $unread_count = $("<unread-count-stub>");
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$(".top_left_drafts").set_find_results(".unread_count", $unread_count);
|
2021-07-04 23:17:03 +02:00
|
|
|
|
|
2017-06-02 23:12:15 +02:00
|
|
|
|
drafts.initialize();
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2018-02-15 17:38:31 +01:00
|
|
|
|
|
2021-03-14 10:30:49 +01:00
|
|
|
|
test("remove_old_drafts", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_3 = {
|
2018-02-15 17:38:31 +01:00
|
|
|
|
stream: "stream",
|
2022-09-28 21:35:43 +02:00
|
|
|
|
topic: "topic",
|
2018-02-15 17:38:31 +01:00
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message",
|
2018-02-15 17:38:31 +01:00
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_4 = {
|
2018-02-15 17:38:31 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "aaron@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message",
|
2018-02-15 17:38:31 +01:00
|
|
|
|
updatedAt: new Date().setDate(-30),
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
const data = {id3: draft_3, id4: draft_4};
|
2018-02-15 17:38:31 +01:00
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
assert.deepEqual(draft_model.get(), data);
|
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
|
const $unread_count = $("<unread-count-stub>");
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$(".top_left_drafts").set_find_results(".unread_count", $unread_count);
|
2021-07-04 23:17:03 +02:00
|
|
|
|
|
2018-02-15 17:38:31 +01:00
|
|
|
|
drafts.remove_old_drafts();
|
|
|
|
|
assert.deepEqual(draft_model.get(), {id3: draft_3});
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
test("update_draft", ({override, override_rewire}) => {
|
|
|
|
|
compose_state.set_message_type(null);
|
2021-11-30 19:36:27 +01:00
|
|
|
|
let draft_id = drafts.update_draft();
|
|
|
|
|
assert.equal(draft_id, undefined);
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
override_rewire(compose_pm_pill, "set_from_emails", noop);
|
|
|
|
|
override_rewire(user_pill, "get_user_ids", () => [aaron.user_id]);
|
|
|
|
|
compose_state.set_message_type("private");
|
|
|
|
|
compose_state.message_content("dummy content");
|
|
|
|
|
compose_state.private_message_recipient(aaron.email);
|
2021-11-30 19:36:27 +01:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
const $container = $(".top_left_drafts");
|
|
|
|
|
const $child = $(".unread_count");
|
|
|
|
|
$container.set_find_results(".unread_count", $child);
|
2021-11-30 19:36:27 +01:00
|
|
|
|
|
|
|
|
|
tippy_args = {
|
|
|
|
|
content: "translated: Saved as draft",
|
|
|
|
|
arrow: true,
|
|
|
|
|
placement: "right",
|
|
|
|
|
};
|
|
|
|
|
tippy_show_called = false;
|
|
|
|
|
tippy_destroy_called = false;
|
|
|
|
|
|
|
|
|
|
override(Date, "now", () => 5);
|
|
|
|
|
override(Math, "random", () => 2);
|
|
|
|
|
draft_id = drafts.update_draft();
|
2021-11-30 17:52:16 +01:00
|
|
|
|
assert.equal(draft_id, "5-2");
|
2021-11-30 19:36:27 +01:00
|
|
|
|
assert.ok(tippy_show_called);
|
|
|
|
|
assert.ok(tippy_destroy_called);
|
|
|
|
|
|
|
|
|
|
override(Date, "now", () => 6);
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
compose_state.message_content("dummy content edited once");
|
2021-11-30 19:36:27 +01:00
|
|
|
|
tippy_show_called = false;
|
|
|
|
|
tippy_destroy_called = false;
|
|
|
|
|
draft_id = drafts.update_draft();
|
2021-11-30 17:52:16 +01:00
|
|
|
|
assert.equal(draft_id, "5-2");
|
2021-11-30 19:36:27 +01:00
|
|
|
|
assert.ok(tippy_show_called);
|
|
|
|
|
assert.ok(tippy_destroy_called);
|
|
|
|
|
|
|
|
|
|
override(Date, "now", () => 7);
|
|
|
|
|
|
2021-11-30 17:52:16 +01:00
|
|
|
|
// message contents not edited
|
2021-11-30 19:36:27 +01:00
|
|
|
|
tippy_show_called = false;
|
|
|
|
|
tippy_destroy_called = false;
|
2021-11-30 17:52:16 +01:00
|
|
|
|
draft_id = drafts.update_draft();
|
|
|
|
|
assert.equal(draft_id, "5-2");
|
2021-11-30 19:36:27 +01:00
|
|
|
|
assert.ok(!tippy_show_called);
|
|
|
|
|
assert.ok(!tippy_destroy_called);
|
2021-11-30 17:52:16 +01:00
|
|
|
|
|
|
|
|
|
override(Date, "now", () => 8);
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
compose_state.message_content("dummy content edited a second time");
|
2021-11-30 17:52:16 +01:00
|
|
|
|
tippy_show_called = false;
|
|
|
|
|
tippy_destroy_called = false;
|
|
|
|
|
draft_id = drafts.update_draft({no_notify: true});
|
2021-11-30 19:36:27 +01:00
|
|
|
|
assert.equal(draft_id, "5-2");
|
2021-11-30 17:52:16 +01:00
|
|
|
|
assert.ok(!tippy_show_called);
|
|
|
|
|
assert.ok(!tippy_destroy_called);
|
2021-11-30 19:36:27 +01:00
|
|
|
|
});
|
|
|
|
|
|
2022-10-14 02:18:56 +02:00
|
|
|
|
test("rename_stream_recipient", ({override_rewire}) => {
|
|
|
|
|
override_rewire(drafts, "set_count", noop);
|
|
|
|
|
|
|
|
|
|
const stream_A = {
|
|
|
|
|
subscribed: false,
|
|
|
|
|
name: "A",
|
|
|
|
|
stream_id: 1,
|
|
|
|
|
};
|
|
|
|
|
stream_data.add_sub(stream_A);
|
|
|
|
|
const stream_B = {
|
|
|
|
|
subscribed: false,
|
|
|
|
|
name: "B",
|
|
|
|
|
stream_id: 2,
|
|
|
|
|
};
|
|
|
|
|
stream_data.add_sub(stream_B);
|
|
|
|
|
|
|
|
|
|
const draft_1 = {
|
|
|
|
|
stream: stream_A.name,
|
|
|
|
|
stream_id: stream_A.stream_id,
|
|
|
|
|
topic: "a",
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message",
|
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
const draft_2 = {
|
|
|
|
|
stream: stream_A.name,
|
|
|
|
|
stream_id: stream_A.stream_id,
|
|
|
|
|
topic: "b",
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message",
|
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
const draft_3 = {
|
|
|
|
|
stream: stream_B.name,
|
|
|
|
|
stream_id: stream_B.stream_id,
|
|
|
|
|
topic: "a",
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message",
|
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
const draft_4 = {
|
|
|
|
|
stream: stream_B.name,
|
|
|
|
|
stream_id: stream_B.stream_id,
|
|
|
|
|
topic: "c",
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message",
|
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
const data = {id1: draft_1, id2: draft_2, id3: draft_3, id4: draft_4};
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
|
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
function assert_draft(draft_id, stream_name, topic_name) {
|
|
|
|
|
const draft = draft_model.getDraft(draft_id);
|
|
|
|
|
assert.equal(draft.topic, topic_name);
|
|
|
|
|
assert.equal(draft.stream, stream_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// There are no drafts in B>b, so moving messages from there doesn't change drafts
|
|
|
|
|
drafts.rename_stream_recipient(stream_B.stream_id, "b", undefined, "c");
|
|
|
|
|
assert_draft("id1", "A", "a");
|
|
|
|
|
assert_draft("id2", "A", "b");
|
|
|
|
|
assert_draft("id3", "B", "a");
|
|
|
|
|
assert_draft("id4", "B", "c");
|
|
|
|
|
|
|
|
|
|
// Update with both stream and topic changes Bc -> Aa
|
|
|
|
|
drafts.rename_stream_recipient(stream_B.stream_id, "c", stream_A.stream_id, "a");
|
|
|
|
|
assert_draft("id1", "A", "a");
|
|
|
|
|
assert_draft("id2", "A", "b");
|
|
|
|
|
assert_draft("id3", "B", "a");
|
|
|
|
|
assert_draft("id4", "A", "a");
|
|
|
|
|
|
|
|
|
|
// Update with only stream change Aa -> Ba
|
|
|
|
|
drafts.rename_stream_recipient(stream_A.stream_id, "a", stream_B.stream_id, undefined);
|
|
|
|
|
assert_draft("id1", "B", "a");
|
|
|
|
|
assert_draft("id2", "A", "b");
|
|
|
|
|
assert_draft("id3", "B", "a");
|
|
|
|
|
assert_draft("id4", "B", "a");
|
|
|
|
|
|
|
|
|
|
// Update with only topic change, affecting three messages
|
|
|
|
|
drafts.rename_stream_recipient(stream_B.stream_id, "a", undefined, "e");
|
|
|
|
|
assert_draft("id1", "B", "e");
|
|
|
|
|
assert_draft("id2", "A", "b");
|
|
|
|
|
assert_draft("id3", "B", "e");
|
|
|
|
|
assert_draft("id4", "B", "e");
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-02 01:10:11 +01:00
|
|
|
|
// There were some buggy drafts that had their topics
|
|
|
|
|
// renamed to `undefined` in #23238.
|
|
|
|
|
// TODO/compatibility: The next two tests can be deleted
|
2022-11-02 01:13:42 +01:00
|
|
|
|
// when we get to delete drafts.fix_drafts_with_undefined_topics.
|
2022-11-02 01:10:11 +01:00
|
|
|
|
test("catch_buggy_draft_error", () => {
|
|
|
|
|
const stream_A = {
|
|
|
|
|
subscribed: false,
|
|
|
|
|
name: "A",
|
|
|
|
|
stream_id: 1,
|
|
|
|
|
};
|
|
|
|
|
stream_data.add_sub(stream_A);
|
|
|
|
|
const stream_B = {
|
|
|
|
|
subscribed: false,
|
|
|
|
|
name: "B",
|
|
|
|
|
stream_id: 2,
|
|
|
|
|
};
|
|
|
|
|
stream_data.add_sub(stream_B);
|
|
|
|
|
|
|
|
|
|
const buggy_draft = {
|
|
|
|
|
stream: stream_B.name,
|
|
|
|
|
stream_id: stream_B.stream_id,
|
|
|
|
|
topic: undefined,
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message",
|
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
const data = {id1: buggy_draft};
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
|
|
|
|
|
// An error is logged but the draft isn't fixed in this codepath.
|
|
|
|
|
blueslip.expect(
|
|
|
|
|
"error",
|
|
|
|
|
"Cannot compare strings; at least one value is undefined: undefined, old_topic",
|
|
|
|
|
);
|
|
|
|
|
drafts.rename_stream_recipient(
|
|
|
|
|
stream_B.stream_id,
|
|
|
|
|
"old_topic",
|
|
|
|
|
stream_A.stream_id,
|
|
|
|
|
"new_topic",
|
|
|
|
|
);
|
|
|
|
|
const draft = draft_model.getDraft("id1");
|
|
|
|
|
assert.equal(draft.stream, stream_B.name);
|
|
|
|
|
assert.equal(draft.topic, undefined);
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-02 01:13:42 +01:00
|
|
|
|
test("fix_buggy_draft", ({override_rewire}) => {
|
|
|
|
|
override_rewire(drafts, "set_count", noop);
|
|
|
|
|
|
|
|
|
|
const buggy_draft = {
|
|
|
|
|
stream: "stream name",
|
|
|
|
|
stream_id: 1,
|
|
|
|
|
// This is the bug: topic never be undefined for a stream
|
|
|
|
|
// message draft.
|
|
|
|
|
topic: undefined,
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message",
|
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
const data = {id1: buggy_draft};
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
|
|
|
|
|
drafts.fix_drafts_with_undefined_topics();
|
|
|
|
|
const draft = draft_model.getDraft("id1");
|
|
|
|
|
assert.equal(draft.stream, "stream name");
|
|
|
|
|
assert.equal(draft.topic, "");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-12 11:09:12 +01:00
|
|
|
|
test("delete_all_drafts", () => {
|
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
const data = {draft_1, draft_2, short_msg};
|
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
assert.deepEqual(draft_model.get(), data);
|
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
|
const $unread_count = $("<unread-count-stub>");
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$(".top_left_drafts").set_find_results(".unread_count", $unread_count);
|
2021-11-12 11:09:12 +01:00
|
|
|
|
|
|
|
|
|
drafts.delete_all_drafts();
|
|
|
|
|
assert.deepEqual(draft_model.get(), {});
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
test("format_drafts", ({override_rewire, mock_template}) => {
|
2022-10-14 02:18:56 +02:00
|
|
|
|
stream_data.get_color = () => "#FFFFFF";
|
2021-03-14 10:30:49 +01:00
|
|
|
|
function feb12() {
|
|
|
|
|
return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function date(offset) {
|
|
|
|
|
return feb12().setDate(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const draft_1 = {
|
|
|
|
|
stream: "stream",
|
|
|
|
|
topic: "topic",
|
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message",
|
2020-06-06 23:57:20 +02:00
|
|
|
|
stream_id: 30,
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: feb12().getTime(),
|
|
|
|
|
};
|
|
|
|
|
const draft_2 = {
|
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "aaron@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-1),
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_3 = {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
stream: "stream 2",
|
2022-09-28 21:35:43 +02:00
|
|
|
|
topic: "topic",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message 2",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-10),
|
2018-03-11 22:03:42 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_4 = {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "iago@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message 2",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-5),
|
2018-03-11 22:03:42 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_5 = {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "zoe@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message 3",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-2),
|
2018-03-11 22:03:42 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const expected = [
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id1",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: true,
|
2020-10-24 21:24:21 +02:00
|
|
|
|
stream_name: "stream",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
stream_color: "#FFFFFF",
|
|
|
|
|
dark_background: "",
|
|
|
|
|
topic: "topic",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test stream message",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "7:55 AM",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id2",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: false,
|
2022-05-17 03:13:57 +02:00
|
|
|
|
recipients: "Aaron",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test private message",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 30",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id5",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: false,
|
2022-05-17 03:13:57 +02:00
|
|
|
|
recipients: "Aaron",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test private message 3",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 29",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id4",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: false,
|
2022-05-17 03:13:57 +02:00
|
|
|
|
recipients: "Aaron",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test private message 2",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 26",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id3",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: true,
|
2020-10-24 21:24:21 +02:00
|
|
|
|
stream_name: "stream 2",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
stream_color: "#FFFFFF",
|
|
|
|
|
dark_background: "",
|
|
|
|
|
topic: "topic",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test stream message 2",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 21",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
];
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
$("#drafts_table").append = noop;
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
2020-07-16 22:40:18 +02:00
|
|
|
|
const data = {id1: draft_1, id2: draft_2, id3: draft_3, id4: draft_4, id5: draft_5};
|
2018-03-11 22:03:42 +01:00
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
assert.deepEqual(draft_model.get(), data);
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const stub_render_now = timerender.render_now;
|
2022-01-08 10:27:06 +01:00
|
|
|
|
override_rewire(timerender, "render_now", (time) =>
|
|
|
|
|
stub_render_now(time, new Date(1549958107000)),
|
|
|
|
|
);
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2020-06-06 23:57:20 +02:00
|
|
|
|
sub_store.get = function (stream_id) {
|
|
|
|
|
assert.equal(stream_id, 30);
|
|
|
|
|
return {name: "stream"};
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
override_rewire(user_pill, "get_user_ids", () => []);
|
|
|
|
|
compose_state.set_message_type("private");
|
|
|
|
|
compose_state.private_message_recipient(null);
|
2022-05-17 03:13:57 +02:00
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
|
mock_template("draft_table_body.hbs", false, (data) => {
|
2022-05-17 03:13:57 +02:00
|
|
|
|
// Tests formatting and time-sorting of drafts
|
|
|
|
|
assert.deepEqual(data.narrow_drafts, []);
|
|
|
|
|
assert.deepEqual(data.other_drafts, expected);
|
2020-07-15 01:29:15 +02:00
|
|
|
|
return "<draft table stub>";
|
2019-07-11 05:06:20 +02:00
|
|
|
|
});
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
|
override_rewire(drafts, "set_initial_element", noop);
|
2019-04-18 21:11:30 +02:00
|
|
|
|
|
2021-02-22 17:01:29 +01:00
|
|
|
|
$.create("#drafts_table .draft-row", {children: []});
|
2020-06-06 23:57:20 +02:00
|
|
|
|
drafts.launch();
|
|
|
|
|
|
|
|
|
|
$.clear_all_elements();
|
|
|
|
|
$.create("#drafts_table .draft-row", {children: []});
|
|
|
|
|
$("#draft_overlay").css = () => {};
|
|
|
|
|
|
|
|
|
|
sub_store.get = function (stream_id) {
|
|
|
|
|
assert.equal(stream_id, 30);
|
|
|
|
|
return {name: "stream-rename"};
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-24 21:24:21 +02:00
|
|
|
|
expected[0].stream_name = "stream-rename";
|
2020-06-06 23:57:20 +02:00
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
|
const $unread_count = $("<unread-count-stub>");
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$(".top_left_drafts").set_find_results(".unread_count", $unread_count);
|
2021-07-04 23:17:03 +02:00
|
|
|
|
|
2018-12-16 21:10:20 +01:00
|
|
|
|
drafts.launch();
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2022-05-17 03:13:57 +02:00
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
test("filter_drafts", ({override_rewire, mock_template}) => {
|
2022-05-17 03:13:57 +02:00
|
|
|
|
function feb12() {
|
|
|
|
|
return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function date(offset) {
|
|
|
|
|
return feb12().setDate(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stream_draft_1 = {
|
|
|
|
|
stream: "stream",
|
|
|
|
|
topic: "topic",
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message",
|
|
|
|
|
stream_id: 30,
|
|
|
|
|
updatedAt: feb12().getTime(),
|
|
|
|
|
};
|
|
|
|
|
const pm_draft_1 = {
|
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "aaron@zulip.com",
|
|
|
|
|
type: "private",
|
|
|
|
|
content: "Test private message",
|
|
|
|
|
updatedAt: date(-1),
|
|
|
|
|
};
|
|
|
|
|
const stream_draft_2 = {
|
|
|
|
|
stream: "stream 2",
|
2022-09-28 21:35:43 +02:00
|
|
|
|
topic: "topic",
|
2022-05-17 03:13:57 +02:00
|
|
|
|
type: "stream",
|
|
|
|
|
content: "Test stream message 2",
|
|
|
|
|
updatedAt: date(-10),
|
|
|
|
|
};
|
|
|
|
|
const pm_draft_2 = {
|
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "iago@zulip.com",
|
|
|
|
|
type: "private",
|
|
|
|
|
content: "Test private message 2",
|
|
|
|
|
updatedAt: date(-5),
|
|
|
|
|
};
|
|
|
|
|
const pm_draft_3 = {
|
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "zoe@zulip.com",
|
|
|
|
|
type: "private",
|
|
|
|
|
content: "Test private message 3",
|
|
|
|
|
updatedAt: date(-2),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const expected_pm_drafts = [
|
|
|
|
|
{
|
|
|
|
|
draft_id: "id2",
|
|
|
|
|
is_stream: false,
|
|
|
|
|
recipients: "Aaron",
|
|
|
|
|
raw_content: "Test private message",
|
|
|
|
|
time_stamp: "Jan 30",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
draft_id: "id5",
|
|
|
|
|
is_stream: false,
|
|
|
|
|
recipients: "Aaron",
|
|
|
|
|
raw_content: "Test private message 3",
|
|
|
|
|
time_stamp: "Jan 29",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
draft_id: "id4",
|
|
|
|
|
is_stream: false,
|
|
|
|
|
recipients: "Aaron",
|
|
|
|
|
raw_content: "Test private message 2",
|
|
|
|
|
time_stamp: "Jan 26",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const expected_other_drafts = [
|
|
|
|
|
{
|
|
|
|
|
draft_id: "id1",
|
|
|
|
|
is_stream: true,
|
|
|
|
|
stream_name: "stream",
|
|
|
|
|
stream_color: "#FFFFFF",
|
|
|
|
|
dark_background: "",
|
|
|
|
|
topic: "topic",
|
|
|
|
|
raw_content: "Test stream message",
|
|
|
|
|
time_stamp: "7:55 AM",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
draft_id: "id3",
|
|
|
|
|
is_stream: true,
|
|
|
|
|
stream_name: "stream 2",
|
|
|
|
|
stream_color: "#FFFFFF",
|
|
|
|
|
dark_background: "",
|
|
|
|
|
topic: "topic",
|
|
|
|
|
raw_content: "Test stream message 2",
|
|
|
|
|
time_stamp: "Jan 21",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$("#drafts_table").append = noop;
|
|
|
|
|
|
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
const data = {
|
|
|
|
|
id1: stream_draft_1,
|
|
|
|
|
id2: pm_draft_1,
|
|
|
|
|
id3: stream_draft_2,
|
|
|
|
|
id4: pm_draft_2,
|
|
|
|
|
id5: pm_draft_3,
|
|
|
|
|
};
|
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
assert.deepEqual(draft_model.get(), data);
|
|
|
|
|
|
|
|
|
|
const stub_render_now = timerender.render_now;
|
|
|
|
|
override_rewire(timerender, "render_now", (time) =>
|
|
|
|
|
stub_render_now(time, new Date(1549958107000)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
sub_store.get = function (stream_id) {
|
|
|
|
|
assert.equal(stream_id, 30);
|
|
|
|
|
return {name: "stream"};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mock_template("draft_table_body.hbs", false, (data) => {
|
|
|
|
|
// Tests splitting up drafts by current narrow.
|
|
|
|
|
assert.deepEqual(data.narrow_drafts, expected_pm_drafts);
|
|
|
|
|
assert.deepEqual(data.other_drafts, expected_other_drafts);
|
|
|
|
|
return "<draft table stub>";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
override_rewire(drafts, "set_initial_element", noop);
|
|
|
|
|
|
2022-08-28 03:48:27 +02:00
|
|
|
|
override_rewire(user_pill, "get_user_ids", () => [aaron.user_id]);
|
|
|
|
|
override_rewire(compose_pm_pill, "set_from_emails", noop);
|
|
|
|
|
compose_state.set_message_type("private");
|
|
|
|
|
compose_state.private_message_recipient(aaron.email);
|
2022-05-17 03:13:57 +02:00
|
|
|
|
|
|
|
|
|
$.create("#drafts_table .draft-row", {children: []});
|
|
|
|
|
drafts.launch();
|
|
|
|
|
});
|