Skip to content

Commit 17fe9b4

Browse files
committed
Collab Service tests
1 parent b34ea76 commit 17fe9b4

File tree

15 files changed

+1805
-55
lines changed

15 files changed

+1805
-55
lines changed

services/collaboration/package-lock.json

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

services/collaboration/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
"start": "npm run build && node ./dist/server.js",
99
"dev": "nodemon --files ./src/server.ts",
1010
"lint": "npx eslint .",
11-
"lint:fix": "npx eslint . --fix"
11+
"lint:fix": "npx eslint . --fix",
12+
"test": "mocha --require ts-node/register tests/**/*.spec.ts"
1213
},
1314
"dependencies": {
1415
"amqplib": "^0.10.4",
1516
"body-parser": "^1.19.0",
1617
"cors": "^2.8.5",
18+
"dotenv": "^16.4.5",
1719
"express": "^4.21.0",
1820
"jsonwebtoken": "^9.0.2",
1921
"mongodb": "^5.9.2",
@@ -28,6 +30,7 @@
2830
"devDependencies": {
2931
"@types/amqplib": "^0.10.5",
3032
"@types/cors": "^2.8.17",
33+
"@types/eslint__js": "^8.42.3",
3134
"@types/express": "^4.17.13",
3235
"@types/jsonwebtoken": "^9.0.7",
3336
"@types/mocha": "^10.0.8",

services/collaboration/src/events/broker.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import config from '../config';
66
* https://hassanfouad.medium.com/using-rabbitmq-with-nodejs-and-typescript-8b33d56a62cc
77
*/
88
class MessageBroker {
9-
connection!: Connection;
10-
channel!: Channel;
9+
connection: Connection | undefined;
10+
channel: Channel | undefined;
1111
private connected = false;
1212

1313
async connect(): Promise<void> {
@@ -31,7 +31,11 @@ class MessageBroker {
3131
if (!this.connected) {
3232
await this.connect();
3333
}
34-
this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)));
34+
if (this.channel) {
35+
this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)));
36+
} else {
37+
throw new Error('Channel is not initialized');
38+
}
3539
} catch (error) {
3640
console.error('Failed to produce message:', error);
3741
throw error;
@@ -44,7 +48,11 @@ class MessageBroker {
4448
await this.connect();
4549
}
4650

47-
await this.channel.assertQueue(queue, { durable: true });
51+
if (this.channel) {
52+
await this.channel.assertQueue(queue, { durable: true });
53+
} else {
54+
throw new Error('Channel is not initialized');
55+
}
4856
await this.channel.consume(
4957
queue,
5058
msg => {
@@ -53,7 +61,11 @@ class MessageBroker {
5361
}
5462
const parsedMessage = JSON.parse(msg.content.toString()) as T;
5563
onMessage(parsedMessage);
56-
this.channel.ack(msg);
64+
if (this.channel) {
65+
this.channel.ack(msg);
66+
} else {
67+
console.error('Channel is not initialized');
68+
}
5769
},
5870
{ noAck: false },
5971
);

services/collaboration/src/events/consumer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { produceCollabCreated, produceCollabCreateFailedEvent, produceCreateHist
88
* Consume the question found event and create a room
99
* @param message
1010
*/
11-
async function consumeQuestionFound(message: QuestionFoundEvent) {
11+
export async function consumeQuestionFound(message: QuestionFoundEvent) {
1212
console.log('Attempting to create room:', message);
1313
const { user1, user2, question } = message;
1414

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Visit https://aka.ms/tsconfig to read more about this file */
2+
{
3+
"compilerOptions": {
4+
"target": "ES6",
5+
"module": "CommonJS",
6+
"outDir": "../dist",
7+
"composite": true,
8+
"strict": true,
9+
"esModuleInterop": true,
10+
"skipLibCheck": true,
11+
"forceConsistentCasingInFileNames": true,
12+
},
13+
"include": ["**/*.ts"]
14+
}

0 commit comments

Comments
 (0)