Skip to content

Commit b5ea3b8

Browse files
hankypanky666josteink
authored andcommitted
Add support for mor New Gmail events
Improve tests to do end-to-end testing and event-parsing on real XHR-data.
1 parent a3eeead commit b5ea3b8

File tree

3 files changed

+93
-28
lines changed

3 files changed

+93
-28
lines changed

src/gmail.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,26 @@ var Gmail = function(localJQuery) {
993993
};
994994

995995
api.check.data.is_action = function(obj) {
996+
return api.check.data.is_first_type_action(obj)
997+
|| api.check.data.is_second_type_action(obj);
998+
};
999+
1000+
api.check.data.is_first_type_action = function(obj) {
9961001
return obj
9971002
&& obj["1"]
9981003
&& Array.isArray(obj["1"])
9991004
&& obj["1"].length === 1
10001005
&& typeof obj["1"]["0"] === 'string';
10011006
};
10021007

1008+
api.check.data.is_second_type_action = function(obj) {
1009+
return obj
1010+
&& obj["2"]
1011+
&& Array.isArray(obj["2"])
1012+
&& obj["2"].length
1013+
&& typeof obj["2"]["0"] === 'string';
1014+
};
1015+
10031016
api.check.data.is_smartlabels_array = function(obj) {
10041017
const isNotArray = !obj || !Array.isArray(obj) ||obj.length === 0;
10051018
if (isNotArray) {
@@ -1046,8 +1059,21 @@ var Gmail = function(localJQuery) {
10461059
&& obj["2"]["7"];
10471060
};
10481061

1049-
api.tools.get_action_type = function(obj) {
1050-
return obj[1][0];
1062+
api.tools.get_action = function(obj) {
1063+
return api.tools.get_first_type_action(obj)
1064+
|| api.tools.get_second_type_action(obj);
1065+
};
1066+
1067+
api.tools.get_first_type_action = function(obj) {
1068+
return obj
1069+
&& obj[1]
1070+
&& obj[1].join('');
1071+
};
1072+
1073+
api.tools.get_second_type_action = function(obj) {
1074+
return obj
1075+
&& obj[2]
1076+
&& obj[2].join('');
10511077
};
10521078

10531079
api.tools.get_message_ids = function(obj) {
@@ -1103,42 +1129,42 @@ var Gmail = function(localJQuery) {
11031129
api.tools.check_event_type = function(threadObj) {
11041130
const action_map = {
11051131
// "" : "add_to_tasks",
1106-
"^a" : "archive",
1107-
"^k" : "delete",
1132+
"^a": "archive",
1133+
"^k": "delete",
11081134
// "" : "delete_message_in_thread",
11091135
// "" : "delete_forever",
11101136
// "" : "delete_label",
11111137
// "" : "discard_draft",
11121138
// "" : "expand_categories",
11131139
// "" : "filter_messages_like_these",
11141140
// "" : "label",
1115-
// "" : "mark_as_important",
1116-
// "" : "mark_as_not_important",
1141+
// "^io_im^imi": "mark_as_important",
1142+
// "^imn": "mark_as_not_important",
11171143
// "" : "mark_as_not_spam",
11181144
// "" : "mark_as_spam",
11191145
// "" : "move_label",
11201146
// "" : "move_to_inbox",
11211147
// "" : "mute",
1122-
// "" : "read",
1148+
"^u^us": "read",
11231149
// "" : "save_draft",
11241150
// "" : "send_message",
11251151
// "" : "show_newly_arrived_message",
1126-
// "" : "star",
1152+
// "^t^ss_sy": "star",
11271153
// "" : "undo_send",
11281154
// "" : "unmute",
11291155
"^u" : "unread",
1130-
// "" : "unstar",
1156+
// "^t^ss_sy^ss_so^ss_sr^ss_sp^ss_sb^ss_sg^ss_cr^ss_co^ss_cy^ss_cg^ss_cb^ss_cp": "unstar",
11311157
// "" : "new_email",
11321158
// "" : "poll",
11331159
// "" : "refresh",
11341160
// "" : "restore_message_in_thread",
1135-
"^o" : "open_email",
1161+
"^o": "open_email",
11361162
// "" : "toggle_threads"
11371163
};
11381164
const threadData = api.tools.get_thread_data(threadObj);
11391165

11401166
if (threadData && api.check.data.is_action(threadData)) {
1141-
const action = api.tools.get_action_type(threadData);
1167+
const action = api.tools.get_action(threadData);
11421168

11431169
return action_map[action];
11441170
} else {

test/test.tools.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ let gmail = new Gmail();
55

66
const testData = require("./testdata-parser.js");
77

8+
function testXhrEventParsing(jsonXhrData, eventName) {
9+
const api = new Gmail();
10+
const xhrData = JSON.parse(jsonXhrData);
11+
12+
const threads = api.tools.extract_from_graph(xhrData, api.check.data.is_thread);
13+
const actionType = api.tools.check_event_type(threads[0]);
14+
15+
assert.equal(eventName, actionType);
16+
};
17+
818
describe("Monkeypatching", () => {
919
it("patching functions works", () => {
1020
var ns = {};
@@ -51,36 +61,53 @@ describe("Monkeypatching", () => {
5161
describe("Test tools for parsing new gmail body_params", () => {
5262
const gmail = new Gmail();
5363
const data = JSON.parse(testData.new_gmail_archive_action_body_params);
64+
const threads = gmail.tools.extract_from_graph(data, gmail.check.data.is_thread);
65+
const threadData = threads.map(thread => gmail.tools.get_thread_data(thread))[0];
5466

5567
it("get thread id", () => {
56-
const thread = gmail.tools.get_thread_id(data);
68+
const thread = gmail.tools.get_thread_id(threads[0]);
5769

58-
assert.equal(thread, 'thread-f:1600724307680265309');
70+
assert.equal(thread, 'thread-f:1603171109786600032');
5971
});
72+
6073
it("get thread data", () => {
61-
const mockThreadData = data[2][7];
62-
const threadData = gmail.tools.get_thread_data(data);
74+
const mockThreadData = threads[0][2][7];
75+
const threadData = gmail.tools.get_thread_data(threads[0]);
76+
console.log(threadData);
6377

6478
assert.deepEqual(threadData, mockThreadData);
6579
});
80+
6681
it("get messages ids", () => {
67-
const mockMessageIds = ['msg-f:1600724307680265309', 'msg-f:1600724938213937205', 'msg-f:1600725174437456906', 'msg-f:1600725319255992336', 'msg-f:1600725448529658711'];
68-
const threadData = gmail.tools.get_thread_data(data);
82+
const mockMessageIds = ['msg-f:1603171109786600032', 'msg-f:1603245801543734539', 'msg-f:1603245862071354412', 'msg-f:1603246018478443087', 'msg-f:1603256094012730022', 'msg-f:1603256564311088576', 'msg-f:1603256665279246114', 'msg-f:1603256682384715664', 'msg-f:1603376719891477511', 'msg-f:1603376909485932601', 'msg-f:1603376994923202634', 'msg-f:1603380395240179639'];
6983
const messagesIds = gmail.tools.get_message_ids(threadData);
7084

71-
assert.equal(messagesIds.length, 5);
85+
assert.equal(messagesIds.length, 12);
7286
assert.deepEqual(messagesIds, mockMessageIds);
7387
});
74-
it("get action type", () => {
75-
const threadData = gmail.tools.get_thread_data(data);
76-
const action = gmail.tools.get_action_type(threadData);
7788

78-
assert.equal(action, "^a");
89+
it("parses archived messages", () => {
90+
const xhrData = testData.new_gmail_archive_action_body_params;
91+
testXhrEventParsing(xhrData, "archive");
92+
});
93+
94+
it("parses deleted messages", () => {
95+
const xhrData = testData.new_gmail_delete_action_body_params;
96+
testXhrEventParsing(xhrData, "delete");
97+
});
98+
99+
it("parses read messages", () => {
100+
const xhrData = testData.new_gmail_read_action_body_params;
101+
testXhrEventParsing(xhrData, "read");
102+
});
103+
104+
it("parses unread messages", () => {
105+
const xhrData = testData.new_gmail_unread_action_body_params;
106+
testXhrEventParsing(xhrData, "unread");
79107
});
80-
it("is action type isset in thread object", () => {
81-
const threadData = gmail.tools.get_thread_data(data);
82-
const issetAction = gmail.check.data.is_action(threadData);
83108

84-
assert.equal(issetAction, true);
109+
it("parses open_email messages", () => {
110+
const xhrData = testData.new_gmail_open_email_action_body_params;
111+
testXhrEventParsing(xhrData, "open_email");
85112
});
86113
});

test/testdata-parser.js

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)