From 0e54e59eabbcc1b31d3c8580125090ece16ce848 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:16:20 +0530 Subject: [PATCH 01/14] add tests for assignedMessage Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .prettierignore | 1 + .../common/core/messages/assignedMessage.js | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 .prettierignore create mode 100644 packages/caliper-core/test/common/core/messages/assignedMessage.js diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 000000000..f59ec20aa --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +* \ No newline at end of file diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js new file mode 100644 index 000000000..bfb71c5a9 --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -0,0 +1,66 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const AssignedMessage = require('../../../../lib/common/messages/assignedMessage'); +const MessageTypes = require('../../../../lib/common/utils/constants').Messages.Types; + +const chai = require('chai'); +chai.should(); + +describe('AssignedMessage', () => { + describe('Constructor', () => { + it('should create an AssignedMessage instance with sender, recipients, and type', () => { + const sender = 'John'; + const recipients = ['Alice', 'Bob']; + const message = new AssignedMessage(sender, recipients); + + message.should.be.an.instanceOf(AssignedMessage); + message.sender.should.equal(sender); + message.recipients.should.deep.equal(recipients); + message.type.should.equal(MessageTypes.Assigned); + }); + + it('should parse the date argument as a Date object for the AssignedMessage', () => { + const date = '2024-04-25'; + const message = new AssignedMessage('John', ['Alice', 'Bob'], date); + + message.date.should.be.an.instanceOf(Date); + message.date.toISOString().should.equal(new Date(date).toISOString()); + }); + + it('should set the date as an Invalid Date object if the date string is not a valid date', () => { + const date = 'Invalid date'; + const message = new AssignedMessage('John', ['Alice', 'Bob'], date); + + chai.expect(message.date).to.be.an.instanceOf(Date); + chai.expect(message.date.toString()).to.equal('Invalid Date'); + }); + + it('should set the error correctly if passed', () => { + const error = 'Some error'; + const message = new AssignedMessage('John', ['Alice', 'Bob'], undefined, error); + + message.error.should.equal(error); + }); + + it('should create an AssignedMessage with undefined date and error if not provided', () => { + const message = new AssignedMessage('John', ['Alice', 'Bob']); + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; + }); + }); +}); \ No newline at end of file From 2e233db28e97d085a5e530d4001051d667be352b Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:16:38 +0530 Subject: [PATCH 02/14] remove prettierignore Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .prettierignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index f59ec20aa..000000000 --- a/.prettierignore +++ /dev/null @@ -1 +0,0 @@ -* \ No newline at end of file From 2d3ed62de79c94f7ec65ba11ac88c21bca7ed80a Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:30:28 +0530 Subject: [PATCH 03/14] add test Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../common/core/messages/assignedMessage.js | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index bfb71c5a9..0518725be 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -1,12 +1,12 @@ /* -* Licensed under the Apache License, Version 2.0 (the "License"); +* Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, +* distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -33,12 +33,20 @@ describe('AssignedMessage', () => { message.type.should.equal(MessageTypes.Assigned); }); + it('should set the content of the message as an empty object', () => { + const message = new AssignedMessage('John', ['Alice', 'Bob']); + + message.content.should.deep.equal({}); + }); + it('should parse the date argument as a Date object for the AssignedMessage', () => { const date = '2024-04-25'; const message = new AssignedMessage('John', ['Alice', 'Bob'], date); message.date.should.be.an.instanceOf(Date); - message.date.toISOString().should.equal(new Date(date).toISOString()); + message.date + .toISOString() + .should.equal(new Date(date).toISOString()); }); it('should set the date as an Invalid Date object if the date string is not a valid date', () => { @@ -51,7 +59,12 @@ describe('AssignedMessage', () => { it('should set the error correctly if passed', () => { const error = 'Some error'; - const message = new AssignedMessage('John', ['Alice', 'Bob'], undefined, error); + const message = new AssignedMessage( + 'John', + ['Alice', 'Bob'], + undefined, + error + ); message.error.should.equal(error); }); @@ -63,4 +76,4 @@ describe('AssignedMessage', () => { chai.expect(message.error).to.be.undefined; }); }); -}); \ No newline at end of file +}); From e7f3927519b61fec0721a6f4f33fa78650a91705 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:34:29 +0530 Subject: [PATCH 04/14] fix license Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../caliper-core/test/common/core/messages/assignedMessage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index 0518725be..eb382bb1a 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -1,12 +1,12 @@ /* -* Licensed under the Apache License, Version 2.0 (the 'License'); +* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an 'AS IS' BASIS, +* distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. From 27fddfc7e711aedb366f56d3aa17b160871fdff3 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:51:16 +0530 Subject: [PATCH 05/14] move to constant assertion style Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../test/common/core/messages/assignedMessage.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index eb382bb1a..fecd9946e 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -53,8 +53,10 @@ describe('AssignedMessage', () => { const date = 'Invalid date'; const message = new AssignedMessage('John', ['Alice', 'Bob'], date); - chai.expect(message.date).to.be.an.instanceOf(Date); - chai.expect(message.date.toString()).to.equal('Invalid Date'); + message.date.should.be.an.instanceOf(Date); + message.date + .toString() + .should.equal('Invalid Date'); }); it('should set the error correctly if passed', () => { @@ -72,8 +74,8 @@ describe('AssignedMessage', () => { it('should create an AssignedMessage with undefined date and error if not provided', () => { const message = new AssignedMessage('John', ['Alice', 'Bob']); - chai.expect(message.date).to.be.undefined; - chai.expect(message.error).to.be.undefined; + message.date.should.be.undefined; + message.error.should.be.undefined; }); }); }); From 9b2628d54adaa93ad4cfe649091a876f45170790 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Wed, 1 May 2024 14:34:01 +0530 Subject: [PATCH 06/14] add tests for other contructor types --- .../common/core/messages/assignedMessage.js | 16 +---- .../common/core/messages/preparedMessage.js | 71 +++++++++++++++++++ .../test/common/core/messages/readyMessage.js | 71 +++++++++++++++++++ 3 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 packages/caliper-core/test/common/core/messages/preparedMessage.js create mode 100644 packages/caliper-core/test/common/core/messages/readyMessage.js diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index fecd9946e..229fe2321 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -49,16 +49,6 @@ describe('AssignedMessage', () => { .should.equal(new Date(date).toISOString()); }); - it('should set the date as an Invalid Date object if the date string is not a valid date', () => { - const date = 'Invalid date'; - const message = new AssignedMessage('John', ['Alice', 'Bob'], date); - - message.date.should.be.an.instanceOf(Date); - message.date - .toString() - .should.equal('Invalid Date'); - }); - it('should set the error correctly if passed', () => { const error = 'Some error'; const message = new AssignedMessage( @@ -73,9 +63,9 @@ describe('AssignedMessage', () => { it('should create an AssignedMessage with undefined date and error if not provided', () => { const message = new AssignedMessage('John', ['Alice', 'Bob']); - - message.date.should.be.undefined; - message.error.should.be.undefined; + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; }); }); }); diff --git a/packages/caliper-core/test/common/core/messages/preparedMessage.js b/packages/caliper-core/test/common/core/messages/preparedMessage.js new file mode 100644 index 000000000..c199ed86f --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/preparedMessage.js @@ -0,0 +1,71 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const PreparedMessage = require('../../../../lib/common/messages/preparedMessage'); +const MessageTypes = require('../../../../lib/common/utils/constants').Messages.Types; + +const chai = require('chai'); +chai.should(); + +describe('PreparedMessage', () => { + describe('Constructor', () => { + it('should create an PreparedMessage instance with sender, recipients, and type', () => { + const sender = 'John'; + const recipients = ['Alice', 'Bob']; + const message = new PreparedMessage(sender, recipients); + + message.should.be.an.instanceOf(PreparedMessage); + message.sender.should.equal(sender); + message.recipients.should.deep.equal(recipients); + message.type.should.equal(MessageTypes.Prepared); + }); + + it('should set the content of the message as an empty object', () => { + const message = new PreparedMessage('John', ['Alice', 'Bob']); + + message.content.should.deep.equal({}); + }); + + it('should parse the date argument as a Date object for the PreparedMessage', () => { + const date = '2024-04-25'; + const message = new PreparedMessage('John', ['Alice', 'Bob'], date); + + message.date.should.be.an.instanceOf(Date); + message.date + .toISOString() + .should.equal(new Date(date).toISOString()); + }); + + it('should set the error correctly if passed', () => { + const error = 'Some error'; + const message = new PreparedMessage( + 'John', + ['Alice', 'Bob'], + undefined, + error + ); + + message.error.should.equal(error); + }); + + it('should create an PreparedMessage with undefined date and error if not provided', () => { + const message = new PreparedMessage('John', ['Alice', 'Bob']); + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; + }); + }); +}); diff --git a/packages/caliper-core/test/common/core/messages/readyMessage.js b/packages/caliper-core/test/common/core/messages/readyMessage.js new file mode 100644 index 000000000..f4077bca6 --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/readyMessage.js @@ -0,0 +1,71 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const ReadyMessage = require('../../../../lib/common/messages/readyMessage'); +const MessageTypes = require('../../../../lib/common/utils/constants').Messages.Types; + +const chai = require('chai'); +chai.should(); + +describe('ReadyMessage', () => { + describe('Constructor', () => { + it('should create an ReadyMessage instance with sender, recipients, and type', () => { + const sender = 'John'; + const recipients = ['Alice', 'Bob']; + const message = new ReadyMessage(sender, recipients); + + message.should.be.an.instanceOf(ReadyMessage); + message.sender.should.equal(sender); + message.recipients.should.deep.equal(recipients); + message.type.should.equal(MessageTypes.Ready); + }); + + it('should set the content of the message as an empty object', () => { + const message = new ReadyMessage('John', ['Alice', 'Bob']); + + message.content.should.deep.equal({}); + }); + + it('should parse the date argument as a Date object for the ReadyMessage', () => { + const date = '2024-04-25'; + const message = new ReadyMessage('John', ['Alice', 'Bob'], date); + + message.date.should.be.an.instanceOf(Date); + message.date + .toISOString() + .should.equal(new Date(date).toISOString()); + }); + + it('should set the error correctly if passed', () => { + const error = 'Some error'; + const message = new ReadyMessage( + 'John', + ['Alice', 'Bob'], + undefined, + error + ); + + message.error.should.equal(error); + }); + + it('should create an ReadyMessage with undefined date and error if not provided', () => { + const message = new ReadyMessage('John', ['Alice', 'Bob']); + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; + }); + }); +}); From f9a97ce72eddb455f45f902c2a554c4b3f8f2376 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Wed, 1 May 2024 15:01:42 +0530 Subject: [PATCH 07/14] add tests for message class Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../test/common/core/messages/message.js | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 packages/caliper-core/test/common/core/messages/message.js diff --git a/packages/caliper-core/test/common/core/messages/message.js b/packages/caliper-core/test/common/core/messages/message.js new file mode 100644 index 000000000..0fbcb3f57 --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/message.js @@ -0,0 +1,140 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const Message = require('../../../../lib/common/messages/message'); +const AllMessageTarget = require('../../../../lib/common/utils/constants').Messages.Targets.All + +const chai = require('chai'); +chai.should(); + +describe('Message', () => { + const mockSender = 'Test User'; + const mockRecipients = ["recepient-id-1", "recepient-id-2", "recepient-id-3"]; + const mockType = 'Assigned'; + const mockContent = { message: 'Hello' }; + const mockDate = '2024-04-25'; + const mockError = "Test Error"; + + describe("Constructor", () => { + it("should create a Message instance with sender, recipients, type, content, date, and error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + + message.should.be.an.instanceOf(Message); + message.sender.should.equal(mockSender); + message.recipients.should.deep.equal(mockRecipients); + message.type.should.equal(mockType); + message.content.should.deep.equal(mockContent); + message.date.should.be.an.instanceOf(Date); + message.date.toISOString().should.equal(new Date(mockDate).toISOString()); + message.error.should.equal(mockError); + }); + + it("should set the date of the message as undefined not passed", () => { + const message = new Message(mockSender, mockRecipients, mockType); + + chai.expect(message.date).to.be.undefined; + }); + + it("should set the date of the message as an invalid Date object if the date is invalid", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, 'Invalid Date'); + + message.date.should.be.an.instanceOf(Date); + chai.expect(message.date.toString()).to.equal('Invalid Date'); + }) + }) + + describe("Getters", () => { + beforeEach(() => { + this.message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + }) + + it("should get the sender of the message", () => { + this.message.getSender().should.equal(mockSender); + }); + + it("should get the recipients of the message", () => { + this.message.getRecipients().should.deep.equal(mockRecipients); + }); + + it("should get the type of the message", () => { + this.message.getType().should.equal(mockType); + }); + + it("should get the content of the message", () => { + this.message.getContent().should.deep.equal(mockContent); + }); + }) + + describe("hasError", () => { + it("should return true if the message has an error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + message.hasError().should.be.true; + }) + + it("should return false if the message has no error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate); + message.hasError().should.be.false; + }) + }) + + describe("forRecipient", () => { + it("should return true if the message is for the recipient", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate); + + mockRecipients.forEach(recipient => { + message.forRecipient(recipient).should.be.true; + }) + }) + + it("should return true for all people if the message if for all", () => { + const message = new Message(mockSender, [AllMessageTarget], mockType, mockContent, mockDate); + + message.forRecipient(AllMessageTarget).should.be.true; + message.forRecipient('random-id').should.be.true; + }) + }) + + describe("stringify", () => { + it("should contain the date and the error if present", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + const stringifiedMessage = message.stringify(); + const decodedMessage = JSON.parse(stringifiedMessage); + + decodedMessage.sender.should.equal(mockSender); + decodedMessage.recipients.should.deep.equal(mockRecipients); + decodedMessage.type.should.equal(mockType); + decodedMessage.content.should.deep.equal(mockContent); + decodedMessage.date.should.equal(new Date(mockDate).toISOString()); + decodedMessage.error.should.equal(mockError); + }) + + it("should not include the error attribute if the message had no error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate); + const stringifiedMessage = message.stringify(); + const decodedMessage = JSON.parse(stringifiedMessage); + + chai.expect(decodedMessage.error).to.be.undefined; + }) + + it("should set the current date if the date is not provided", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent); + const stringifiedMessage = message.stringify(); + const decodedMessage = JSON.parse(stringifiedMessage); + + decodedMessage.date.should.equal(new Date().toISOString()); + }) + }) +}) \ No newline at end of file From 38cfd996cd251f35ef8833682167ea8acd478cff Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:16:20 +0530 Subject: [PATCH 08/14] add tests for assignedMessage Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .prettierignore | 1 + .../common/core/messages/assignedMessage.js | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 .prettierignore create mode 100644 packages/caliper-core/test/common/core/messages/assignedMessage.js diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 000000000..f59ec20aa --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +* \ No newline at end of file diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js new file mode 100644 index 000000000..bfb71c5a9 --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -0,0 +1,66 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const AssignedMessage = require('../../../../lib/common/messages/assignedMessage'); +const MessageTypes = require('../../../../lib/common/utils/constants').Messages.Types; + +const chai = require('chai'); +chai.should(); + +describe('AssignedMessage', () => { + describe('Constructor', () => { + it('should create an AssignedMessage instance with sender, recipients, and type', () => { + const sender = 'John'; + const recipients = ['Alice', 'Bob']; + const message = new AssignedMessage(sender, recipients); + + message.should.be.an.instanceOf(AssignedMessage); + message.sender.should.equal(sender); + message.recipients.should.deep.equal(recipients); + message.type.should.equal(MessageTypes.Assigned); + }); + + it('should parse the date argument as a Date object for the AssignedMessage', () => { + const date = '2024-04-25'; + const message = new AssignedMessage('John', ['Alice', 'Bob'], date); + + message.date.should.be.an.instanceOf(Date); + message.date.toISOString().should.equal(new Date(date).toISOString()); + }); + + it('should set the date as an Invalid Date object if the date string is not a valid date', () => { + const date = 'Invalid date'; + const message = new AssignedMessage('John', ['Alice', 'Bob'], date); + + chai.expect(message.date).to.be.an.instanceOf(Date); + chai.expect(message.date.toString()).to.equal('Invalid Date'); + }); + + it('should set the error correctly if passed', () => { + const error = 'Some error'; + const message = new AssignedMessage('John', ['Alice', 'Bob'], undefined, error); + + message.error.should.equal(error); + }); + + it('should create an AssignedMessage with undefined date and error if not provided', () => { + const message = new AssignedMessage('John', ['Alice', 'Bob']); + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; + }); + }); +}); \ No newline at end of file From 7b89e3513863505d3143d4a2b9b22f1857b121d0 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:16:38 +0530 Subject: [PATCH 09/14] remove prettierignore Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .prettierignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index f59ec20aa..000000000 --- a/.prettierignore +++ /dev/null @@ -1 +0,0 @@ -* \ No newline at end of file From 600b0f244ca4956b72ddf08a9b79bb5d7f557cf6 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:30:28 +0530 Subject: [PATCH 10/14] add test Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../common/core/messages/assignedMessage.js | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index bfb71c5a9..0518725be 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -1,12 +1,12 @@ /* -* Licensed under the Apache License, Version 2.0 (the "License"); +* Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, +* distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -33,12 +33,20 @@ describe('AssignedMessage', () => { message.type.should.equal(MessageTypes.Assigned); }); + it('should set the content of the message as an empty object', () => { + const message = new AssignedMessage('John', ['Alice', 'Bob']); + + message.content.should.deep.equal({}); + }); + it('should parse the date argument as a Date object for the AssignedMessage', () => { const date = '2024-04-25'; const message = new AssignedMessage('John', ['Alice', 'Bob'], date); message.date.should.be.an.instanceOf(Date); - message.date.toISOString().should.equal(new Date(date).toISOString()); + message.date + .toISOString() + .should.equal(new Date(date).toISOString()); }); it('should set the date as an Invalid Date object if the date string is not a valid date', () => { @@ -51,7 +59,12 @@ describe('AssignedMessage', () => { it('should set the error correctly if passed', () => { const error = 'Some error'; - const message = new AssignedMessage('John', ['Alice', 'Bob'], undefined, error); + const message = new AssignedMessage( + 'John', + ['Alice', 'Bob'], + undefined, + error + ); message.error.should.equal(error); }); @@ -63,4 +76,4 @@ describe('AssignedMessage', () => { chai.expect(message.error).to.be.undefined; }); }); -}); \ No newline at end of file +}); From 436230667cff53c580f76a931894183d94fe6053 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:34:29 +0530 Subject: [PATCH 11/14] fix license Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../caliper-core/test/common/core/messages/assignedMessage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index 0518725be..eb382bb1a 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -1,12 +1,12 @@ /* -* Licensed under the Apache License, Version 2.0 (the 'License'); +* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an 'AS IS' BASIS, +* distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. From 99950f1a0021bf0dde686228016d9dcf20d32298 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:51:16 +0530 Subject: [PATCH 12/14] move to constant assertion style Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../test/common/core/messages/assignedMessage.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index eb382bb1a..fecd9946e 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -53,8 +53,10 @@ describe('AssignedMessage', () => { const date = 'Invalid date'; const message = new AssignedMessage('John', ['Alice', 'Bob'], date); - chai.expect(message.date).to.be.an.instanceOf(Date); - chai.expect(message.date.toString()).to.equal('Invalid Date'); + message.date.should.be.an.instanceOf(Date); + message.date + .toString() + .should.equal('Invalid Date'); }); it('should set the error correctly if passed', () => { @@ -72,8 +74,8 @@ describe('AssignedMessage', () => { it('should create an AssignedMessage with undefined date and error if not provided', () => { const message = new AssignedMessage('John', ['Alice', 'Bob']); - chai.expect(message.date).to.be.undefined; - chai.expect(message.error).to.be.undefined; + message.date.should.be.undefined; + message.error.should.be.undefined; }); }); }); From 1cf696f6ef2695d8b4da1feaa1a150c8a80a2fc9 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Wed, 1 May 2024 14:34:01 +0530 Subject: [PATCH 13/14] add tests for other contructor types Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../common/core/messages/assignedMessage.js | 16 +---- .../common/core/messages/preparedMessage.js | 71 +++++++++++++++++++ .../test/common/core/messages/readyMessage.js | 71 +++++++++++++++++++ 3 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 packages/caliper-core/test/common/core/messages/preparedMessage.js create mode 100644 packages/caliper-core/test/common/core/messages/readyMessage.js diff --git a/packages/caliper-core/test/common/core/messages/assignedMessage.js b/packages/caliper-core/test/common/core/messages/assignedMessage.js index fecd9946e..229fe2321 100644 --- a/packages/caliper-core/test/common/core/messages/assignedMessage.js +++ b/packages/caliper-core/test/common/core/messages/assignedMessage.js @@ -49,16 +49,6 @@ describe('AssignedMessage', () => { .should.equal(new Date(date).toISOString()); }); - it('should set the date as an Invalid Date object if the date string is not a valid date', () => { - const date = 'Invalid date'; - const message = new AssignedMessage('John', ['Alice', 'Bob'], date); - - message.date.should.be.an.instanceOf(Date); - message.date - .toString() - .should.equal('Invalid Date'); - }); - it('should set the error correctly if passed', () => { const error = 'Some error'; const message = new AssignedMessage( @@ -73,9 +63,9 @@ describe('AssignedMessage', () => { it('should create an AssignedMessage with undefined date and error if not provided', () => { const message = new AssignedMessage('John', ['Alice', 'Bob']); - - message.date.should.be.undefined; - message.error.should.be.undefined; + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; }); }); }); diff --git a/packages/caliper-core/test/common/core/messages/preparedMessage.js b/packages/caliper-core/test/common/core/messages/preparedMessage.js new file mode 100644 index 000000000..c199ed86f --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/preparedMessage.js @@ -0,0 +1,71 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const PreparedMessage = require('../../../../lib/common/messages/preparedMessage'); +const MessageTypes = require('../../../../lib/common/utils/constants').Messages.Types; + +const chai = require('chai'); +chai.should(); + +describe('PreparedMessage', () => { + describe('Constructor', () => { + it('should create an PreparedMessage instance with sender, recipients, and type', () => { + const sender = 'John'; + const recipients = ['Alice', 'Bob']; + const message = new PreparedMessage(sender, recipients); + + message.should.be.an.instanceOf(PreparedMessage); + message.sender.should.equal(sender); + message.recipients.should.deep.equal(recipients); + message.type.should.equal(MessageTypes.Prepared); + }); + + it('should set the content of the message as an empty object', () => { + const message = new PreparedMessage('John', ['Alice', 'Bob']); + + message.content.should.deep.equal({}); + }); + + it('should parse the date argument as a Date object for the PreparedMessage', () => { + const date = '2024-04-25'; + const message = new PreparedMessage('John', ['Alice', 'Bob'], date); + + message.date.should.be.an.instanceOf(Date); + message.date + .toISOString() + .should.equal(new Date(date).toISOString()); + }); + + it('should set the error correctly if passed', () => { + const error = 'Some error'; + const message = new PreparedMessage( + 'John', + ['Alice', 'Bob'], + undefined, + error + ); + + message.error.should.equal(error); + }); + + it('should create an PreparedMessage with undefined date and error if not provided', () => { + const message = new PreparedMessage('John', ['Alice', 'Bob']); + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; + }); + }); +}); diff --git a/packages/caliper-core/test/common/core/messages/readyMessage.js b/packages/caliper-core/test/common/core/messages/readyMessage.js new file mode 100644 index 000000000..f4077bca6 --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/readyMessage.js @@ -0,0 +1,71 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const ReadyMessage = require('../../../../lib/common/messages/readyMessage'); +const MessageTypes = require('../../../../lib/common/utils/constants').Messages.Types; + +const chai = require('chai'); +chai.should(); + +describe('ReadyMessage', () => { + describe('Constructor', () => { + it('should create an ReadyMessage instance with sender, recipients, and type', () => { + const sender = 'John'; + const recipients = ['Alice', 'Bob']; + const message = new ReadyMessage(sender, recipients); + + message.should.be.an.instanceOf(ReadyMessage); + message.sender.should.equal(sender); + message.recipients.should.deep.equal(recipients); + message.type.should.equal(MessageTypes.Ready); + }); + + it('should set the content of the message as an empty object', () => { + const message = new ReadyMessage('John', ['Alice', 'Bob']); + + message.content.should.deep.equal({}); + }); + + it('should parse the date argument as a Date object for the ReadyMessage', () => { + const date = '2024-04-25'; + const message = new ReadyMessage('John', ['Alice', 'Bob'], date); + + message.date.should.be.an.instanceOf(Date); + message.date + .toISOString() + .should.equal(new Date(date).toISOString()); + }); + + it('should set the error correctly if passed', () => { + const error = 'Some error'; + const message = new ReadyMessage( + 'John', + ['Alice', 'Bob'], + undefined, + error + ); + + message.error.should.equal(error); + }); + + it('should create an ReadyMessage with undefined date and error if not provided', () => { + const message = new ReadyMessage('John', ['Alice', 'Bob']); + + chai.expect(message.date).to.be.undefined; + chai.expect(message.error).to.be.undefined; + }); + }); +}); From 4555e15edf1402255dc94e48a1cf26c329005c46 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Wed, 1 May 2024 15:01:42 +0530 Subject: [PATCH 14/14] add tests for message class Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- .../test/common/core/messages/message.js | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 packages/caliper-core/test/common/core/messages/message.js diff --git a/packages/caliper-core/test/common/core/messages/message.js b/packages/caliper-core/test/common/core/messages/message.js new file mode 100644 index 000000000..0fbcb3f57 --- /dev/null +++ b/packages/caliper-core/test/common/core/messages/message.js @@ -0,0 +1,140 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const Message = require('../../../../lib/common/messages/message'); +const AllMessageTarget = require('../../../../lib/common/utils/constants').Messages.Targets.All + +const chai = require('chai'); +chai.should(); + +describe('Message', () => { + const mockSender = 'Test User'; + const mockRecipients = ["recepient-id-1", "recepient-id-2", "recepient-id-3"]; + const mockType = 'Assigned'; + const mockContent = { message: 'Hello' }; + const mockDate = '2024-04-25'; + const mockError = "Test Error"; + + describe("Constructor", () => { + it("should create a Message instance with sender, recipients, type, content, date, and error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + + message.should.be.an.instanceOf(Message); + message.sender.should.equal(mockSender); + message.recipients.should.deep.equal(mockRecipients); + message.type.should.equal(mockType); + message.content.should.deep.equal(mockContent); + message.date.should.be.an.instanceOf(Date); + message.date.toISOString().should.equal(new Date(mockDate).toISOString()); + message.error.should.equal(mockError); + }); + + it("should set the date of the message as undefined not passed", () => { + const message = new Message(mockSender, mockRecipients, mockType); + + chai.expect(message.date).to.be.undefined; + }); + + it("should set the date of the message as an invalid Date object if the date is invalid", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, 'Invalid Date'); + + message.date.should.be.an.instanceOf(Date); + chai.expect(message.date.toString()).to.equal('Invalid Date'); + }) + }) + + describe("Getters", () => { + beforeEach(() => { + this.message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + }) + + it("should get the sender of the message", () => { + this.message.getSender().should.equal(mockSender); + }); + + it("should get the recipients of the message", () => { + this.message.getRecipients().should.deep.equal(mockRecipients); + }); + + it("should get the type of the message", () => { + this.message.getType().should.equal(mockType); + }); + + it("should get the content of the message", () => { + this.message.getContent().should.deep.equal(mockContent); + }); + }) + + describe("hasError", () => { + it("should return true if the message has an error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + message.hasError().should.be.true; + }) + + it("should return false if the message has no error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate); + message.hasError().should.be.false; + }) + }) + + describe("forRecipient", () => { + it("should return true if the message is for the recipient", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate); + + mockRecipients.forEach(recipient => { + message.forRecipient(recipient).should.be.true; + }) + }) + + it("should return true for all people if the message if for all", () => { + const message = new Message(mockSender, [AllMessageTarget], mockType, mockContent, mockDate); + + message.forRecipient(AllMessageTarget).should.be.true; + message.forRecipient('random-id').should.be.true; + }) + }) + + describe("stringify", () => { + it("should contain the date and the error if present", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate, mockError); + const stringifiedMessage = message.stringify(); + const decodedMessage = JSON.parse(stringifiedMessage); + + decodedMessage.sender.should.equal(mockSender); + decodedMessage.recipients.should.deep.equal(mockRecipients); + decodedMessage.type.should.equal(mockType); + decodedMessage.content.should.deep.equal(mockContent); + decodedMessage.date.should.equal(new Date(mockDate).toISOString()); + decodedMessage.error.should.equal(mockError); + }) + + it("should not include the error attribute if the message had no error", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent, mockDate); + const stringifiedMessage = message.stringify(); + const decodedMessage = JSON.parse(stringifiedMessage); + + chai.expect(decodedMessage.error).to.be.undefined; + }) + + it("should set the current date if the date is not provided", () => { + const message = new Message(mockSender, mockRecipients, mockType, mockContent); + const stringifiedMessage = message.stringify(); + const decodedMessage = JSON.parse(stringifiedMessage); + + decodedMessage.date.should.equal(new Date().toISOString()); + }) + }) +}) \ No newline at end of file