Skip to content

Commit

Permalink
test: add FileAppend unit tests for better code coverage (#2570)
Browse files Browse the repository at this point in the history
* test: add more file append unit tests

Signed-off-by: Ivaylo Nikolov <[email protected]>

* chore(test): remove redundant comment

Signed-off-by: Ivaylo Nikolov <[email protected]>

---------

Signed-off-by: Ivaylo Nikolov <[email protected]>
  • Loading branch information
ivaylonikolov7 authored Nov 15, 2024
1 parent e44cdf8 commit 7034135
Showing 1 changed file with 83 additions and 8 deletions.
91 changes: 83 additions & 8 deletions test/unit/FileAppendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ describe("FileAppendTransaction", function () {
const timestamp1 = new Timestamp(14, 15);
const fee = new Hbar(5);
const chunkSize = 1000;
const smallContent = "abcdef";

it("setChunkSize()", function () {
const contents = "1".repeat(1000) + "2".repeat(1000) + "3".repeat(1000);
const bigContent =
"1".repeat(1000) + "2".repeat(1000) + "3".repeat(1000);

let transaction = new FileAppendTransaction()
.setTransactionId(
Expand All @@ -28,19 +30,16 @@ describe("FileAppendTransaction", function () {
.setNodeAccountIds([nodeAccountId])
.setFileId(fileId)
.setChunkSize(chunkSize)
.setContents(contents)
.setContents(bigContent)
.freeze();

const transactionId = transaction.transactionId;
// TOOD: fix `FileAppendTransaction.fromBytes()` it seems the transaction IDs
// aren't be saved?
//
// transaction = /** @type {FileAppendTransaction} */ (
// Transaction.fromBytes(transaction.toBytes())
// ).setChunkSize(1000);

expect(transaction._transactionIds.list.length).to.be.equal(3);
expect(transaction._nodeAccountIds.list.length).to.be.equal(1);
expect(transaction.chunkSize).to.be.equal(chunkSize);
expect(transaction.contents.toString()).to.be.equal(bigContent);
expect(transaction.fileId).to.be.deep.equal(fileId);

let body = transaction._makeTransactionBody(nodeAccountId);

Expand Down Expand Up @@ -138,4 +137,80 @@ describe("FileAppendTransaction", function () {
),
).to.deep.equal(Long.fromNumber(chunkInterval * (requiredChunks - 1)));
});

it("should not be able to build transaction with more chunks than maxRequiredChunks", function () {
let transaction = new FileAppendTransaction()
.setContents(smallContent)
.setChunkSize(1)
.setMaxChunks(smallContent.length - 1);

expect(() => {
transaction.toBytes();
}).to.throw(
`cannot build \`FileAppendTransaction\` with more than ${
smallContent.length - 1
} chunks`,
);
});

it("should not be able to build all signed transaction with more than allowed chunks", async function () {
const transaction = new FileAppendTransaction()
.setNodeAccountIds([new AccountId(3)])
.setTransactionId(TransactionId.generate(new AccountId(1)))
.setContents(smallContent)
.setChunkSize(1)
.setMaxChunks(smallContent.length - 1)
.freeze();

expect(() => {
transaction.toBytes();
}).to.throw(
`cannot build \`FileAppendTransaction\` with more than ${
smallContent.length - 1
} chunks`,
);
});

it("should be able to build all signed transaction", async function () {
const transaction = new FileAppendTransaction()
.setNodeAccountIds([new AccountId(3)])
.setTransactionId(TransactionId.generate(new AccountId(1)))
.setContents(smallContent)
.setChunkSize(1)
.freeze();

expect(transaction.isFrozen()).to.be.true;

// calling toBytes will sign all transactions
transaction.toBytes();

expect(transaction._transactions.length).to.equal(smallContent.length);
expect(transaction._signedTransactions.length).to.equal(
smallContent.length,
);
});

it("should set maxChunk if set in constructor", function () {
const maxChunks = 10;
const tx = new FileAppendTransaction({ maxChunks });
expect(tx.maxChunks).to.equal(maxChunks);
});

it("should set chunkSize if set in constructor", function () {
const chunkSize = 10;
const tx = new FileAppendTransaction({ chunkSize });
expect(tx.chunkSize).to.equal(chunkSize);
});

it("should not be able to set scheduled message with bigger content than chunkSize", async function () {
const tx = new FileAppendTransaction()
.setContents(smallContent)
.setChunkSize(1);

expect(() => {
tx.schedule();
}).to.throw(
`cannot schedule \`FileAppendTransaction\` with message over ${tx.chunkSize} bytes`,
);
});
});

0 comments on commit 7034135

Please sign in to comment.