Skip to content

Commit

Permalink
test: add more integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ivaylo Nikolov <[email protected]>
  • Loading branch information
ivaylonikolov7 committed Sep 30, 2024
1 parent 685c6f0 commit 7147b1e
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions test/integration/FileAppendIntegrationTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
FileAppendTransaction,
FileContentsQuery,
FileCreateTransaction,
FileDeleteTransaction,
FileInfoQuery,
Expand Down Expand Up @@ -271,6 +272,97 @@ describe("FileAppend", function () {
expect(txFromBytes.isFrozen()).to.be.true;
});

it("should be able to work with one chunk", async function () {
const CHUNK_SIZE = 4096;
const operatorKey = env.operatorKey.publicKey;

let response = await new FileCreateTransaction()
.setKeys([operatorKey])
.setContents(Buffer.from(""))
.execute(env.client);

let { fileId } = await response.getReceipt(env.client);

expect(fileId).to.not.be.null;
expect(fileId != null ? fileId.num > 0 : false).to.be.true;

const tx = await new FileAppendTransaction()
.setFileId(fileId)
.setContents(generateUInt8Array(CHUNK_SIZE))
.setChunkSize(CHUNK_SIZE)
.execute(env.client);

const receipt = await tx.getReceipt(env.client);
expect(receipt.status).to.be.equal(Status.Success);

const fileInfo = await new FileContentsQuery()
.setFileId(fileId)
.execute(env.client);

expect(fileInfo.length).to.be.equal(CHUNK_SIZE);
});

it("should not be able to sign transaction with 2 chunks", async function () {
const operatorKey = env.operatorKey.publicKey;
const CHUNKS_LENGTH = 2;
const CHUNK_SIZE = 1;

let response = await new FileCreateTransaction()
.setKeys([operatorKey])
.setContents(Buffer.from(""))
.execute(env.client);

let { fileId } = await response.getReceipt(env.client);

const tx = new FileAppendTransaction()
.setFileId(fileId)
.setContents(generateUInt8Array(CHUNKS_LENGTH))
.setChunkSize(CHUNK_SIZE);

let error = false;
try {
env.operatorKey.signTransaction(tx);
} catch (err) {
error = err.message.includes(
"Add signature is not supported for chunked transactions",
);
}

expect(error).to.be.true;
});

it("should be able to sign transaction with 1 chunk", async function () {
const operatorKey = env.operatorKey.publicKey;

let response = await new FileCreateTransaction()
.setKeys([operatorKey])
.setContents(Buffer.from(""))
.execute(env.client);

let { fileId } = await response.getReceipt(env.client);

const txBytes = new FileAppendTransaction()
.setFileId(fileId)
.setContents(Buffer.from("test"))
.toBytes();

const fromBytesTx = FileAppendTransaction.fromBytes(txBytes).freezeWith(
env.client,
);
// should start empty before adding signature
expect(fromBytesTx._signerPublicKeys).to.be.empty;

// should have 1 signature after adding signature
const signature = env.operatorKey.signTransaction(fromBytesTx);
fromBytesTx.addSignature(env.operatorKey.publicKey, signature);
expect(fromBytesTx._signerPublicKeys).to.have.length(1);

const receipt = await (
await fromBytesTx.execute(env.client)
).getReceipt(env.client);
expect(receipt.status).to.be.equal(Status.Success);
});

after(async function () {
await env.close();
});
Expand Down

0 comments on commit 7147b1e

Please sign in to comment.