-
Notifications
You must be signed in to change notification settings - Fork 18
/
Mocks.ts
132 lines (118 loc) · 5.06 KB
/
Mocks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {SessionData} from "./SessionData";
import {Config} from "./Config";
export default class Mocks {
private static base_config: Config = {
auto_labeling_parent_label: "",
go_link: "",
hour_of_day_to_run_sanity_checking: 0,
max_threads: 50,
processed_label: "myProcessed",
processing_failed_label: "zFailed",
processing_frequency_in_minutes: 5,
unprocessed_label: "myUnprocessed",
};
public static getMockConfig = (overrides: Partial<Config> = {}) => (
Object.assign({}, Mocks.base_config, overrides)
);
private static base_session_data: SessionData = {
user_email: "abc@gmail.com",
config: Mocks.getMockConfig(),
labels: {},
rules: [],
processing_start_time: new Date(12345),
oldest_to_process: new Date(23456),
getOrCreateLabel: () => ({} as GoogleAppsScript.Gmail.GmailLabel),
};
public static getMockSessionData = (overrides: Partial<SessionData> = {}) => (
Object.assign({}, Mocks.base_session_data, overrides)
);
private static base_label = {
getName: () => '',
} as GoogleAppsScript.Gmail.GmailLabel;
private static base_thread = {
getLabels: () => [Mocks.base_label],
isImportant: () => false,
isInInbox: () => false,
isInPriorityInbox: () => false,
isInSpam: () => false,
isInTrash: () => false,
hasStarredMessages: () => false,
isUnread: () => false,
getFirstMessageSubject: () => '',
getMessages: () => [],
} as unknown as GoogleAppsScript.Gmail.GmailThread;
private static base_message = {
getFrom: () => '',
getTo: () => '',
getCc: () => '',
getBcc: () => '',
getReplyTo: () => '',
getSubject: () => '',
getPlainBody: () => '',
getRawContent: () => '',
getHeader: (_name: string) => '',
getDate: () => Date.now() as unknown as GoogleAppsScript.Base.Date,
getThread: () => Mocks.base_thread
} as GoogleAppsScript.Gmail.GmailMessage;
public static getMockMessage = (
override_message: Partial<GoogleAppsScript.Gmail.GmailMessage> = {},
override_thread: Partial<GoogleAppsScript.Gmail.GmailThread> = {},
labels: string[] = []): GoogleAppsScript.Gmail.GmailMessage => {
const gmail_labels: GoogleAppsScript.Gmail.GmailLabel[] = [];
labels.forEach(label => {
gmail_labels.push(Object.assign({}, Mocks.base_label, {getName: () => label}));
});
let overridden_thread: GoogleAppsScript.Gmail.GmailThread =
Object.assign({}, Mocks.base_thread, override_thread);
overridden_thread = Object.assign({}, overridden_thread, {getLabels: () => gmail_labels});
let overridden_message = Object.assign({}, Mocks.base_message, override_message);
overridden_message = Object.assign({}, overridden_message, {getThread: () => overridden_thread});
return overridden_message;
};
public static getMockThreadOfMessages = (
override_messages: Partial<GoogleAppsScript.Gmail.GmailMessage>[] = [{}],
override_thread: Partial<GoogleAppsScript.Gmail.GmailThread> = {},
labels: string[] = []): GoogleAppsScript.Gmail.GmailThread => {
const gmail_labels: GoogleAppsScript.Gmail.GmailLabel[] = [];
labels.forEach(label => {
gmail_labels.push(Object.assign({}, Mocks.base_label, {getName: () => label}));
});
const messages: GoogleAppsScript.Gmail.GmailMessage[] = [];
let overridden_thread: GoogleAppsScript.Gmail.GmailThread =
Object.assign({}, Mocks.base_thread, override_thread);
overridden_thread = Object.assign(
{}, overridden_thread, {
getMessages: () => messages,
getLabels: () => gmail_labels,
});
override_messages.forEach(message => {
let overridden_message = Object.assign({}, Mocks.base_message, message);
overridden_message = Object.assign({}, overridden_message, {getThread: () => overridden_thread});
messages.push(overridden_message);
});
return overridden_thread;
};
public static getMockTestSheetHeaders(): string[] {
return [
"conditions", "add_labels", "move_to", "mark_important",
"mark_read", "stage", "auto_label", "disabled", "action_after_match"];
}
public static getMockTestSheet = (
rows: { [key: string]: string}[],
headers: string[] = Mocks.getMockTestSheetHeaders()): string[][] => {
const sheet: string[][] = [headers];
rows.forEach(row_details => {
const row: string[] = [];
for (const header in headers) {
const header_name = headers[header];
if (header_name in row_details) {
row.push(row_details[header_name]);
} else {
row.push("");
}
}
sheet.push(row);
});
return sheet;
}
}