Skip to content

Commit

Permalink
test: add more test and run format upon commit (#99)
Browse files Browse the repository at this point in the history
* chore: only support node 20 onwards in test.yaml

* test: remove unused interface

* test: add test for logging service

* test: remove unused code

* chore: perform lint on test pipeline

* run npm format
  • Loading branch information
wildan3105 authored May 29, 2024
1 parent 4600cbe commit af06291
Show file tree
Hide file tree
Showing 26 changed files with 733 additions and 635 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
- name: Set up environment variables
run: echo "REDIS_URL=redis://localhost:6379" >> $GITHUB_ENV

- run: npm run lint
- run: npm run build --if-present
- run: npm test
env:
Expand Down
8 changes: 4 additions & 4 deletions constants/elastic-email.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const ElasticEmailDefaultContent = {
recipientAddress: "[email protected]",
senderAddress: "Serp API Healthcheck Notifications <[email protected]>",
subject: "Serp API Healthcheck Result"
}
recipientAddress: "[email protected]",
senderAddress: "Serp API Healthcheck Notifications <[email protected]>",
subject: "Serp API Healthcheck Result"
};
2 changes: 1 addition & 1 deletion enums/months.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export enum PartialMonthToIndex {
Oct = 9,
Nov = 10,
Dec = 11
}
}
328 changes: 175 additions & 153 deletions events/subscriber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,156 +9,178 @@ jest.mock("../modules/http");
jest.mock("../modules/log");

describe("Subscriber integration test", () => {
let redisClient: RedisStorage;
let redisPublishClient: RedisStorage;
let subscriber: Subscriber;
let mockHttpPost: jest.Mock;

beforeAll(async () => {
redisClient = new RedisStorage({
redisURL: process.env.REDIS_URL
})
redisPublishClient = new RedisStorage({
redisURL: process.env.REDIS_URL
})

await redisClient.init();
await redisPublishClient.init();

subscriber = new Subscriber(redisClient);
})

afterAll(async () => {
await redisClient.close();
await redisPublishClient.close();
})

beforeEach(async () => {
jest.clearAllMocks();
mockHttpPost = HTTP.prototype.post as jest.Mock;
})

afterEach(async () => {
jest.clearAllMocks();
})

describe("Subscriber normal flow", () => {
it("should subscribe to a channel and print the parsed message to the terminal", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`));

const hoursToMatch = 48;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
}

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

await new Promise(resolve => setTimeout(resolve, 1000));

expect(loggerService.info).toHaveBeenCalledWith(`New message received: ${JSON.stringify(messageToPublish)}`);
});

it("should subscribe to a channel and print the parsed message to the terminal and then print the message that's about to be tweeted if hours to match is 24", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`));

const hoursToMatch = 24;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
}

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

await new Promise(resolve => setTimeout(resolve, 1000));

expect(loggerService.info).toHaveBeenCalledWith(`New message received: ${JSON.stringify(messageToPublish)}`);

await new Promise(resolve => setTimeout(resolve, 1000));
expect(loggerService.info).toHaveBeenCalledWith(`Attempting to tweet a match that's about to begin in ${hoursToMatch} hours`);
expect(mockHttpPost).toHaveBeenCalled();
});

it("should subscribe to a channel and print the parsed message to the terminal and then print the message that's about to be tweeted if hours to match is 1", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`));

const hoursToMatch = 1;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
}

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

await new Promise(resolve => setTimeout(resolve, 1000));

expect(loggerService.info).toHaveBeenCalledWith(`New message received: ${JSON.stringify(messageToPublish)}`);

await new Promise(resolve => setTimeout(resolve, 1000));
expect(loggerService.info).toHaveBeenCalledWith(`Attempting to tweet a match that's about to begin in ${hoursToMatch} hours`);
expect(mockHttpPost).toHaveBeenCalled();
});

it("should subscribe to a channel and log the error if any process encounters an error", async () => {
const error = new Error("Subscribe error");

jest.spyOn(redisClient, 'subscribe').mockImplementation(() => {
throw error;
});

await subscriber.subscribeToChannel(RedisTerms.channelName);

expect(loggerService.error).toHaveBeenCalledWith(expect.stringContaining("An error occurred when subscribing to upcoming-fixtures: {}"));
});

it("should subscribe to a channel and log the error if handling message is returning error", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`));

const hoursToMatch = 1;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
}

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

const error = new Error("Handle message error");

jest.spyOn(JSON, 'parse').mockImplementation(() => {
throw error;
});

await subscriber.subscribeToChannel(RedisTerms.channelName);

expect(loggerService.error).toHaveBeenCalledWith(expect.stringContaining("An error occurred when subscribing to upcoming-fixtures: {}"));
});
})
});
let redisClient: RedisStorage;
let redisPublishClient: RedisStorage;
let subscriber: Subscriber;
let mockHttpPost: jest.Mock;

beforeAll(async () => {
redisClient = new RedisStorage({
redisURL: process.env.REDIS_URL
});
redisPublishClient = new RedisStorage({
redisURL: process.env.REDIS_URL
});

await redisClient.init();
await redisPublishClient.init();

subscriber = new Subscriber(redisClient);
});

afterAll(async () => {
await redisClient.close();
await redisPublishClient.close();
});

beforeEach(async () => {
jest.clearAllMocks();
mockHttpPost = HTTP.prototype.post as jest.Mock;
});

afterEach(async () => {
jest.clearAllMocks();
});

describe("Subscriber normal flow", () => {
it("should subscribe to a channel and print the parsed message to the terminal", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(
expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`)
);

const hoursToMatch = 48;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
};

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

await new Promise(resolve => setTimeout(resolve, 1000));

expect(loggerService.info).toHaveBeenCalledWith(
`New message received: ${JSON.stringify(messageToPublish)}`
);
});

it("should subscribe to a channel and print the parsed message to the terminal and then print the message that's about to be tweeted if hours to match is 24", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(
expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`)
);

const hoursToMatch = 24;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
};

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

await new Promise(resolve => setTimeout(resolve, 1000));

expect(loggerService.info).toHaveBeenCalledWith(
`New message received: ${JSON.stringify(messageToPublish)}`
);

await new Promise(resolve => setTimeout(resolve, 1000));
expect(loggerService.info).toHaveBeenCalledWith(
`Attempting to tweet a match that's about to begin in ${hoursToMatch} hours`
);
expect(mockHttpPost).toHaveBeenCalled();
});

it("should subscribe to a channel and print the parsed message to the terminal and then print the message that's about to be tweeted if hours to match is 1", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(
expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`)
);

const hoursToMatch = 1;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
};

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

await new Promise(resolve => setTimeout(resolve, 1000));

expect(loggerService.info).toHaveBeenCalledWith(
`New message received: ${JSON.stringify(messageToPublish)}`
);

await new Promise(resolve => setTimeout(resolve, 1000));
expect(loggerService.info).toHaveBeenCalledWith(
`Attempting to tweet a match that's about to begin in ${hoursToMatch} hours`
);
expect(mockHttpPost).toHaveBeenCalled();
});

it("should subscribe to a channel and log the error if any process encounters an error", async () => {
const error = new Error("Subscribe error");

jest.spyOn(redisClient, "subscribe").mockImplementation(() => {
throw error;
});

await subscriber.subscribeToChannel(RedisTerms.channelName);

expect(loggerService.error).toHaveBeenCalledWith(
expect.stringContaining("An error occurred when subscribing to upcoming-fixtures: {}")
);
});

it("should subscribe to a channel and log the error if handling message is returning error", async () => {
await subscriber.subscribeToChannel(RedisTerms.channelName);
expect(loggerService.info).toHaveBeenCalledWith(
expect.stringContaining(`Subscribing to ${RedisTerms.channelName} ...`)
);

const hoursToMatch = 1;
const futureDate: Date = new Date(Date.now() + hoursToMatch * 60 * 60 * 1000);
const messageToPublish: IPublishedMessage = {
message: {
participants: "Inter vs @ChelseaFC",
tournament: "Champions League",
date_time: futureDate,
stadium: "San Siro"
},
hours_to_match: hoursToMatch
};

await redisPublishClient.publish(RedisTerms.channelName, JSON.stringify(messageToPublish));

const error = new Error("Handle message error");

jest.spyOn(JSON, "parse").mockImplementation(() => {
throw error;
});

await subscriber.subscribeToChannel(RedisTerms.channelName);

expect(loggerService.error).toHaveBeenCalledWith(
expect.stringContaining("An error occurred when subscribing to upcoming-fixtures: {}")
);
});
});
});
Loading

0 comments on commit af06291

Please sign in to comment.