Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add FileAppend unit tests for better code coverage #2570

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 98 additions & 2 deletions test/unit/FileAppendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@ import {
import Long from "long";

describe("FileAppendTransaction", function () {
let smallContent;

beforeEach(function () {
smallContent = "abcdef";
});

it("setChunkSize()", function () {
const spenderAccountId1 = new AccountId(7);
const fileId = new FileId(8);
const nodeAccountId = new AccountId(10, 11, 12);
const timestamp1 = new Timestamp(14, 15);
const fee = new Hbar(5);
const chunkSize = 1000;
const bigContent =
"1".repeat(1000) + "2".repeat(1000) + "3".repeat(1000);

let transaction = new FileAppendTransaction()
.setTransactionId(
TransactionId.withValidStart(spenderAccountId1, timestamp1),
)
.setNodeAccountIds([nodeAccountId])
.setFileId(fileId)
.setChunkSize(1000)
.setContents("1".repeat(1000) + "2".repeat(1000) + "3".repeat(1000))
.setChunkSize(chunkSize)
.setContents(bigContent)
.freeze();

const transactionId = transaction.transactionId;
Expand All @@ -38,6 +47,9 @@ describe("FileAppendTransaction", function () {

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 @@ -88,4 +100,88 @@ describe("FileAppendTransaction", function () {
expect(body.fileAppend.contents.length).to.be.equal(1000);
expect(body.fileAppend.contents[0]).to.be.equal(51);
});

it("should not be able to build transaction with no content set", function () {
let transaction = new FileAppendTransaction().setFileId(new FileId(1));

expect(() => {
transaction.toBytes();
}).to.throw("contents is not set");
});

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("cant 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`,
);
});
});