Skip to content

Commit cca42f8

Browse files
committed
more flexible google mock
1 parent 0b268ae commit cca42f8

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

test/source/mock/google/google-data.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type GmailMsg$payload$part = { partId?: string, body?: GmailMsg$payload$body, fi
1111
type GmailMsg$payload = { partId?: string, filename?: string, parts?: GmailMsg$payload$part[], headers?: GmailMsg$header[], mimeType?: string, body?: GmailMsg$payload$body };
1212
type GmailMsg$labelId = 'INBOX' | 'UNREAD' | 'CATEGORY_PERSONAL' | 'IMPORTANT' | 'SENT' | 'CATEGORY_UPDATES' | 'DRAFT';
1313
type GmailThread = { historyId: string; id: string; snippet: string; };
14-
type Label = { id: string, name: "CATEGORY_SOCIAL", messageListVisibility: "hide", labelListVisibility: "labelHide", type: 'system' };
14+
type Label = { id: string, name: string, messageListVisibility: 'show' | 'hide', labelListVisibility: 'labelShow' | 'labelHide', type: 'system' };
1515
type AcctDataFile = { messages: GmailMsg[]; drafts: GmailMsg[], attachments: { [id: string]: { data: string, size: number, filename?: string } }, labels: Label[] };
1616
type ExportedMsg = { acctEmail: string, full: GmailMsg, raw: GmailMsg, attachments: { [id: string]: { data: string, size: number } } };
1717

@@ -111,7 +111,13 @@ export class GoogleData {
111111

112112
public static withInitializedData = async (acct: string): Promise<GoogleData> => {
113113
if (typeof DATA[acct] === 'undefined') {
114-
const acctData: AcctDataFile = { drafts: [], messages: [], attachments: {}, labels: [] };
114+
const acctData: AcctDataFile = {
115+
drafts: [], messages: [], attachments: {}, labels:
116+
[
117+
{ id: 'INBOX', name: 'Inbox', messageListVisibility: 'show', labelListVisibility: 'labelShow', type: 'system' },
118+
{ id: 'DRAFT', name: 'Drafts', messageListVisibility: 'show', labelListVisibility: 'labelShow', type: 'system' }
119+
]
120+
};
115121
const dir = GoogleData.exportedMsgsPath;
116122
const filenames: string[] = await new Promise((res, rej) => readdir(dir, (e, f) => e ? rej(e) : res(f)));
117123
const filePromises = filenames.map(f => new Promise((res, rej) => readFile(dir + f, (e, d) => e ? rej(e) : res(d))));
@@ -204,11 +210,11 @@ export class GoogleData {
204210
}
205211

206212
public getMessage = (id: string): GmailMsg | undefined => {
207-
return DATA[this.acct].messages.find(m => m.id === id);
213+
return this.getMessages().find(m => m.id === id);
208214
}
209215

210216
public getMessageBySubject = (subject: string): GmailMsg | undefined => {
211-
return DATA[this.acct].messages.find(m => {
217+
return this.getMessages().find(m => {
212218
if (m.payload?.headers) {
213219
const subjectHeader = m.payload.headers.find(x => x.name === 'Subject');
214220
if (subjectHeader) {
@@ -220,7 +226,7 @@ export class GoogleData {
220226
}
221227

222228
public getMessagesByThread = (threadId: string) => {
223-
return DATA[this.acct].messages.filter(m => m.threadId === threadId);
229+
return this.getMessages().filter(m => m.threadId === threadId);
224230
}
225231

226232
public searchMessages = (q: string) => {
@@ -266,26 +272,33 @@ export class GoogleData {
266272
return DATA[this.acct].labels;
267273
}
268274

269-
public getThreads = () => {
275+
public getThreads = (labelIds: string[]) => {
270276
const threads: GmailThread[] = [];
271-
for (const thread of DATA[this.acct].messages.map(m => ({ historyId: m.historyId, id: m.threadId!, snippet: `MOCK SNIPPET: ${GoogleData.msgSubject(m)}` }))) {
277+
for (const thread of this.getMessages().
278+
filter(m => labelIds ? (m.labelIds || []).some(l => labelIds.includes(l)) : true).
279+
map(m => ({ historyId: m.historyId, id: m.threadId!, snippet: `MOCK SNIPPET: ${GoogleData.msgSubject(m)}` }))) {
272280
if (thread.id && !threads.map(t => t.id).includes(thread.id)) {
273281
threads.push(thread);
274282
}
275283
}
276284
return threads;
277285
}
278286

287+
// returns ordinary messages and drafts
288+
private getMessages = () => {
289+
return DATA[this.acct].messages.concat(DATA[this.acct].drafts);
290+
}
291+
279292
private searchMessagesBySubject = (subject: string) => {
280293
subject = subject.trim().toLowerCase();
281-
const messages = DATA[this.acct].messages.filter(m => GoogleData.msgSubject(m).toLowerCase().includes(subject));
294+
const messages = this.getMessages().filter(m => GoogleData.msgSubject(m).toLowerCase().includes(subject));
282295
return messages;
283296
}
284297

285298
private searchMessagesByPeople = (includePeople: string[], excludePeople: string[]) => {
286299
includePeople = includePeople.map(person => person.trim().toLowerCase());
287300
excludePeople = excludePeople.map(person => person.trim().toLowerCase());
288-
return DATA[this.acct].messages.filter(m => {
301+
return this.getMessages().filter(m => {
289302
const msgPeople = GoogleData.msgPeople(m).toLowerCase();
290303
let shouldInclude = false;
291304
let shouldExclude = false;

test/source/mock/google/google-endpoints.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ export const mockGoogleEndpoints: HandlersDefinition = {
173173
}
174174
throw new HttpClientErr(`Method not implemented for ${req.url}: ${req.method}`);
175175
},
176-
'/gmail/v1/users/me/threads': async ({ }, req) => {
176+
'/gmail/v1/users/me/threads': async (parsedReq, req) => {
177177
const acct = oauth.checkAuthorizationHeaderWithAccessToken(req.headers.authorization);
178178
if (isGet(req)) {
179-
const threads = (await GoogleData.withInitializedData(acct)).getThreads();
179+
const threads = (await GoogleData.withInitializedData(acct)).getThreads([parsedReq.query.labelIds]); // todo: support arrays?
180180
return { threads, resultSizeEstimate: threads.length };
181181
}
182182
throw new HttpClientErr(`Method not implemented for ${req.url}: ${req.method}`);
@@ -218,7 +218,7 @@ export const mockGoogleEndpoints: HandlersDefinition = {
218218
const acct = oauth.checkAuthorizationHeaderWithAccessToken(req.headers.authorization);
219219
const body = parsedReq.body as DraftSaveModel;
220220
if (body && body.message && body.message.raw && typeof body.message.raw === 'string') {
221-
if (body.message.threadId && !(await GoogleData.withInitializedData(acct)).getThreads().find(t => t.id === body.message.threadId)) {
221+
if (body.message.threadId && !(await GoogleData.withInitializedData(acct)).getThreads(['DRAFT']).find(t => t.id === body.message.threadId)) {
222222
throw new HttpClientErr('The thread you are replying to not found', 404);
223223
}
224224
const decoded = await Parse.convertBase64ToMimeMsg(body.message.raw);

0 commit comments

Comments
 (0)