Skip to content

Commit

Permalink
Added unit test for the GitHubClient
Browse files Browse the repository at this point in the history
  • Loading branch information
mackdk committed Jun 29, 2023
1 parent cc79cc3 commit ead2745
Show file tree
Hide file tree
Showing 12 changed files with 1,169 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/typescript/client/GitHubApiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Thread {
repository: Repository;
subject: Subject;
unread: boolean;
updated_at: Date;
updated_at: string;
url: string;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/typescript/client/GitHubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface GitHubClient {

listThreads(showParticipatingOnly?: boolean): Promise<GitHub.Thread[]>;
markThreadAsRead(githubNotification: GitHub.Thread): Promise<void>;
markAllThreadsAsRead(): Promise<void>;
markAllThreadsAsRead(updateDate?: Date): Promise<void>;

getWebUrlForSubject(subject: GitHub.Subject): Promise<string>;
}
28 changes: 17 additions & 11 deletions src/main/typescript/client/GitHubClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export class GitHubClientImpl implements GitHubClient {
}
}

public get httpEngine(): HttpEngine {
return this.engine;
}

public async listThreads(showParticipatingOnly: boolean = false): Promise<GitHub.Thread[]> {
const response = await this.doRequest(
HttpMethod.GET,
Expand All @@ -79,12 +83,12 @@ export class GitHubClientImpl implements GitHubClient {
await this.doRequest(HttpMethod.PATCH, notification.url, [HttpStatus.ResetContent, HttpStatus.NotModified]);
}

public async markAllThreadsAsRead(): Promise<void> {
public async markAllThreadsAsRead(updateDate: Date = new Date()): Promise<void> {
await this.doRequest(
HttpMethod.PUT,
`https://${this.baseUrl}/notifications`,
[HttpStatus.Accepted, HttpStatus.ResetContent, HttpStatus.NotModified],
{ last_read_at: new Date().toISOString(), read: true } as GitHub.MarkNotificationsArReadRequest
{ last_read_at: updateDate.toISOString(), read: true } as GitHub.MarkNotificationsArReadRequest
);
}

Expand Down Expand Up @@ -131,34 +135,36 @@ export class GitHubClientImpl implements GitHubClient {

const enforcedPollInterval = Number(header);
if (enforcedPollInterval !== this._pollInterval) {
GitHubClientImpl.LOGGER.info('New polling interval enforced by GitHub {}s', enforcedPollInterval);
GitHubClientImpl.LOGGER.info('New polling interval enforced by GitHub {0}s', enforcedPollInterval);
this._pollInterval = enforcedPollInterval;
}
}

private static handleRequestError(error: unknown): never {
GitHubClientImpl.LOGGER.info('{0} - {1} - {2}', error, typeof error, error instanceof Object);
if (error instanceof Error) {
throw new ApiError(-1, 'Unable to perform API call', error);
} else if (typeof error === 'string') {
throw new ApiError(-1, `Unable to perform API call - ${error}`);
}

// Throw a generic error
throw new ApiError(-1, 'Unable to perform API call');
throw new ApiError(-1, `Unable to perform API call - ${error?.toString() ?? 'error undefined'}`);
}

private static validateResponseCode(response: HttpResponse, ...validStates: number[]): void {
if (!validStates.includes(response.statusCode)) {
const errorResponse = JSON.parse(response.body) as GitHub.BasicError;
const message =
errorResponse.message ??
HttpStatus[response.statusCode]
// insert a space between lower & upper
.replace(/([a-z])([A-Z])/g, '$1 $2')
// space before last upper in a sequence followed by lower
.replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3')
// uppercase the first character
.replace(/^./, (str: string) => str.toUpperCase());
'Invalid status code: ' +
HttpStatus[response.statusCode]
// insert a space between lower & upper
.replace(/([a-z])([A-Z])/g, '$1 $2')
// space before last upper in a sequence followed by lower
.replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3')
// uppercase the first character
.replace(/^./, (str: string) => str.toUpperCase());

throw new ApiError(response.statusCode, message);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/typescript/client/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export class HttpRequest {
private _body: RequestBody | undefined;
private readonly _headers: Map<string, string>;

public constructor(method: HttpMethod, url: string) {
public constructor(method: HttpMethod, url: string, body?: RequestBody, headers?: Map<string, string>) {
this._method = method;
this._url = url;
this._headers = new Map<string, string>();
this._body = body;
this._headers = headers ?? new Map<string, string>();
}

public get method(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/main/typescript/notifications/NotificationAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class NotificationAdapter {

case NotificationMode.SINGLE:
const updatedAtMap = new Map<string, Date>();
oldData.forEach((notification) => updatedAtMap.set(notification.id, notification.updated_at));
oldData.forEach((notification) => updatedAtMap.set(notification.id, new Date(notification.updated_at)));

return newData
.filter((notification) => this.isNotificationNeeded(updatedAtMap, notification))
Expand All @@ -133,7 +133,7 @@ export class NotificationAdapter {

private isNotificationNeeded(updatedAtMap: Map<string, Date>, notification: GitHub.Thread): boolean {
const lastUpdatedAt = updatedAtMap.get(notification.id);
return lastUpdatedAt === undefined || notification.updated_at > lastUpdatedAt;
return lastUpdatedAt === undefined || new Date(notification.updated_at) > lastUpdatedAt;
}

private buildProjectNotification(data: GitHub.Thread): UINotification {
Expand Down
Loading

0 comments on commit ead2745

Please sign in to comment.