From 7147b1e2d2b891e34e8e17aa7509a3c071f8106b Mon Sep 17 00:00:00 2001 From: Ivaylo Nikolov Date: Mon, 30 Sep 2024 14:37:12 +0300 Subject: [PATCH] test: add more integration tests Signed-off-by: Ivaylo Nikolov --- test/integration/FileAppendIntegrationTest.js | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/test/integration/FileAppendIntegrationTest.js b/test/integration/FileAppendIntegrationTest.js index 45dc4174e..0ff6191eb 100644 --- a/test/integration/FileAppendIntegrationTest.js +++ b/test/integration/FileAppendIntegrationTest.js @@ -1,5 +1,6 @@ import { FileAppendTransaction, + FileContentsQuery, FileCreateTransaction, FileDeleteTransaction, FileInfoQuery, @@ -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(); });