2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const submessage = zrequire("submessage");
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const channel = set_global("channel", {});
|
|
|
|
const widgetize = set_global("widgetize", {});
|
|
|
|
const message_store = set_global("message_store", {});
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_message_events", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let msg = {};
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
submessages: [],
|
|
|
|
};
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const submessages = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{id: 222, sender_id: 99, content: "84"},
|
|
|
|
{id: 9, sender_id: 33, content: "42"},
|
2018-02-12 15:12:40 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
locally_echoed: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
submessages,
|
2018-02-12 15:12:40 +01:00
|
|
|
};
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
|
|
|
msg = {
|
2020-07-20 22:18:43 +02:00
|
|
|
submessages,
|
2018-02-12 15:12:40 +01:00
|
|
|
};
|
|
|
|
assert.deepEqual(submessage.get_message_events(msg), [
|
|
|
|
{sender_id: 33, data: 42},
|
|
|
|
{sender_id: 99, data: 84},
|
|
|
|
]);
|
2018-05-30 20:29:49 +02:00
|
|
|
});
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("make_server_callback", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = 444;
|
|
|
|
const callback = submessage.make_server_callback(message_id);
|
|
|
|
let was_posted;
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
channel.post = function (opts) {
|
|
|
|
was_posted = true;
|
|
|
|
assert.deepEqual(opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/submessage",
|
2018-02-12 15:12:40 +01:00
|
|
|
data: {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "whatever",
|
2018-02-12 15:12:40 +01:00
|
|
|
content: '{"foo":32}',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
callback({
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "whatever",
|
2018-02-12 15:12:40 +01:00
|
|
|
data: {foo: 32},
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(was_posted);
|
2018-05-30 20:29:49 +02:00
|
|
|
});
|
2018-05-30 20:36:09 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("handle_event", () => {
|
2018-05-30 20:36:09 +02:00
|
|
|
const message = {
|
|
|
|
id: 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
const event = {
|
2018-05-30 22:52:11 +02:00
|
|
|
id: 11,
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "widget",
|
2018-05-30 20:36:09 +02:00
|
|
|
sender_id: 99,
|
|
|
|
message_id: message.id,
|
2018-05-30 21:12:16 +02:00
|
|
|
content: '"some_data"',
|
2018-05-30 20:36:09 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let args;
|
2018-05-30 20:36:09 +02:00
|
|
|
widgetize.handle_event = (opts) => {
|
|
|
|
args = opts;
|
|
|
|
};
|
|
|
|
|
2018-05-30 22:52:11 +02:00
|
|
|
message_store.get = (msg_id) => {
|
|
|
|
assert.equal(msg_id, message.id);
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2018-05-30 20:36:09 +02:00
|
|
|
submessage.handle_event(event);
|
|
|
|
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
sender_id: 99,
|
|
|
|
message_id: 42,
|
2020-07-15 01:29:15 +02:00
|
|
|
data: "some_data",
|
2018-05-30 20:36:09 +02:00
|
|
|
});
|
2018-05-30 22:52:11 +02:00
|
|
|
|
|
|
|
assert.deepEqual(message.submessages[0], event);
|
2018-05-30 20:36:09 +02:00
|
|
|
});
|