2021-02-20 05:52:06 +01:00
|
|
|
import {strict as assert} from "assert";
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
import type {Page} from "puppeteer";
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2023-02-22 23:04:11 +01:00
|
|
|
import * as common from "./lib/common";
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function copy_messages(
|
|
|
|
page: Page,
|
|
|
|
start_message: string,
|
|
|
|
end_message: string,
|
|
|
|
): Promise<string[]> {
|
2020-08-20 18:15:18 +02:00
|
|
|
return await page.evaluate(
|
2021-02-20 05:52:06 +01:00
|
|
|
(start_message: string, end_message: string) => {
|
|
|
|
function get_message_node(message: string): Element {
|
2024-01-17 07:53:40 +01:00
|
|
|
return [...document.querySelectorAll(".message-list .message_content")].find(
|
2021-03-27 04:40:48 +01:00
|
|
|
(node) => node.textContent?.trim() === message,
|
|
|
|
)!;
|
2020-08-20 18:15:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// select messages from start_message to end_message
|
|
|
|
const selectedRange = document.createRange();
|
|
|
|
selectedRange.setStartAfter(get_message_node(start_message));
|
|
|
|
selectedRange.setEndBefore(get_message_node(end_message));
|
2021-02-20 05:52:06 +01:00
|
|
|
window.getSelection()!.removeAllRanges();
|
|
|
|
window.getSelection()!.addRange(selectedRange);
|
2020-08-20 18:15:18 +02:00
|
|
|
|
|
|
|
// Remove existing copy/paste divs, which may linger from the previous
|
|
|
|
// example. (The code clears these out with a zero-second timeout, which
|
|
|
|
// is probably sufficient for human users, but which causes problems here.)
|
2021-03-27 04:40:48 +01:00
|
|
|
document.querySelector("#copytempdiv")?.remove();
|
2020-08-20 18:15:18 +02:00
|
|
|
|
|
|
|
// emulate copy event
|
2021-03-27 04:40:48 +01:00
|
|
|
document.dispatchEvent(
|
|
|
|
new KeyboardEvent("keydown", {
|
|
|
|
key: "c",
|
|
|
|
code: "KeyC",
|
|
|
|
ctrlKey: true,
|
|
|
|
keyCode: 67,
|
|
|
|
which: 67,
|
2023-10-25 02:19:36 +02:00
|
|
|
}),
|
2021-03-27 04:40:48 +01:00
|
|
|
);
|
2020-08-20 18:15:18 +02:00
|
|
|
|
|
|
|
// find temp div with copied text
|
2021-03-27 04:40:48 +01:00
|
|
|
return [...document.querySelectorAll("#copytempdiv > p")].map((p) => p.textContent!);
|
2020-08-20 18:15:18 +02:00
|
|
|
},
|
|
|
|
start_message,
|
|
|
|
end_message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_first_message_from_topic(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test C");
|
2021-02-20 05:52:06 +01:00
|
|
|
const expected_copied_lines: string[] = [];
|
2020-08-20 18:15:18 +02:00
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_last_message_from_topic(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test E", "copy paste test E");
|
2021-02-20 05:52:06 +01:00
|
|
|
const expected_copied_lines: string[] = [];
|
2020-08-20 18:15:18 +02:00
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_first_two_messages_from_topic(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test D");
|
|
|
|
const expected_copied_lines = ["Desdemona: copy paste test C", "Desdemona: copy paste test D"];
|
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_all_messages_from_topic(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test E");
|
|
|
|
const expected_copied_lines = [
|
|
|
|
"Desdemona: copy paste test C",
|
|
|
|
"Desdemona: copy paste test D",
|
|
|
|
"Desdemona: copy paste test E",
|
|
|
|
];
|
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_last_from_prev_first_from_next(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test C");
|
|
|
|
const expected_copied_lines = [
|
|
|
|
"Verona > copy-paste-topic #1 Today",
|
|
|
|
"Desdemona: copy paste test B",
|
|
|
|
"Verona > copy-paste-topic #2 Today",
|
|
|
|
"Desdemona: copy paste test C",
|
|
|
|
];
|
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_last_from_prev_all_from_next(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test E");
|
|
|
|
const expected_copied_lines = [
|
|
|
|
"Verona > copy-paste-topic #1 Today",
|
|
|
|
"Desdemona: copy paste test B",
|
|
|
|
"Verona > copy-paste-topic #2 Today",
|
|
|
|
"Desdemona: copy paste test C",
|
|
|
|
"Desdemona: copy paste test D",
|
|
|
|
"Desdemona: copy paste test E",
|
|
|
|
];
|
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_all_from_prev_first_from_next(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test A", "copy paste test C");
|
|
|
|
const expected_copied_lines = [
|
|
|
|
"Verona > copy-paste-topic #1 Today",
|
|
|
|
"Desdemona: copy paste test A",
|
|
|
|
"Desdemona: copy paste test B",
|
|
|
|
"Verona > copy-paste-topic #2 Today",
|
|
|
|
"Desdemona: copy paste test C",
|
|
|
|
];
|
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function test_copying_messages_from_several_topics(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test F");
|
|
|
|
const expected_copied_lines = [
|
|
|
|
"Verona > copy-paste-topic #1 Today",
|
|
|
|
"Desdemona: copy paste test B",
|
|
|
|
"Verona > copy-paste-topic #2 Today",
|
|
|
|
"Desdemona: copy paste test C",
|
|
|
|
"Desdemona: copy paste test D",
|
|
|
|
"Desdemona: copy paste test E",
|
|
|
|
"Verona > copy-paste-topic #3 Today",
|
|
|
|
"Desdemona: copy paste test F",
|
|
|
|
];
|
|
|
|
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async function copy_paste_test(page: Page): Promise<void> {
|
2020-08-20 18:15:18 +02:00
|
|
|
await common.log_in(page);
|
|
|
|
await common.send_multiple_messages(page, [
|
2023-08-04 06:46:35 +02:00
|
|
|
{stream_name: "Verona", topic: "copy-paste-topic #1", content: "copy paste test A"},
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2023-08-04 06:46:35 +02:00
|
|
|
{stream_name: "Verona", topic: "copy-paste-topic #1", content: "copy paste test B"},
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2023-08-04 06:46:35 +02:00
|
|
|
{stream_name: "Verona", topic: "copy-paste-topic #2", content: "copy paste test C"},
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2023-08-04 06:46:35 +02:00
|
|
|
{stream_name: "Verona", topic: "copy-paste-topic #2", content: "copy paste test D"},
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2023-08-04 06:46:35 +02:00
|
|
|
{stream_name: "Verona", topic: "copy-paste-topic #2", content: "copy paste test E"},
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2023-08-04 06:46:35 +02:00
|
|
|
{stream_name: "Verona", topic: "copy-paste-topic #3", content: "copy paste test F"},
|
2020-08-20 18:15:18 +02:00
|
|
|
|
2023-08-04 06:46:35 +02:00
|
|
|
{stream_name: "Verona", topic: "copy-paste-topic #3", content: "copy paste test G"},
|
2020-08-20 18:15:18 +02:00
|
|
|
]);
|
|
|
|
|
2024-03-06 13:12:08 +01:00
|
|
|
await page.click("#left-sidebar-navigation-list .top_left_all_messages");
|
2024-10-07 04:27:59 +02:00
|
|
|
const message_list_id = await common.get_current_msg_list_id(page, true);
|
2024-01-17 07:53:40 +01:00
|
|
|
await common.check_messages_sent(page, message_list_id, [
|
2020-08-20 18:15:18 +02:00
|
|
|
["Verona > copy-paste-topic #1", ["copy paste test A", "copy paste test B"]],
|
|
|
|
[
|
|
|
|
"Verona > copy-paste-topic #2",
|
|
|
|
["copy paste test C", "copy paste test D", "copy paste test E"],
|
|
|
|
],
|
|
|
|
["Verona > copy-paste-topic #3", ["copy paste test F", "copy paste test G"]],
|
|
|
|
]);
|
|
|
|
console.log("Messages were sent successfully");
|
|
|
|
|
|
|
|
await test_copying_first_message_from_topic(page);
|
|
|
|
await test_copying_last_message_from_topic(page);
|
|
|
|
await test_copying_first_two_messages_from_topic(page);
|
|
|
|
await test_copying_all_messages_from_topic(page);
|
|
|
|
await test_copying_last_from_prev_first_from_next(page);
|
|
|
|
await test_copying_last_from_prev_all_from_next(page);
|
|
|
|
await test_copying_all_from_prev_first_from_next(page);
|
|
|
|
await test_copying_messages_from_several_topics(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
common.run_test(copy_paste_test);
|