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 topic info tests #2756

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions src/topic/TopicInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,18 @@ export default class TopicInfo {
* @returns {TopicInfo}
*/
static fromBytes(bytes) {
return TopicInfo._fromProtobuf({
topicInfo: HashgraphProto.proto.ConsensusTopicInfo.decode(bytes),
});
return TopicInfo._fromProtobuf(
HashgraphProto.proto.ConsensusGetTopicInfoResponse.decode(bytes),
);
}

/**
* @returns {Uint8Array}
*/
toBytes() {
return HashgraphProto.proto.ConsensusTopicInfo.encode(
/** @type {HashgraphProto.proto.IConsensusTopicInfo} */ (
this._toProtobuf().topicInfo
return HashgraphProto.proto.ConsensusGetTopicInfoResponse.encode(
/** @type {HashgraphProto.proto.ConsensusGetTopicInfoResponse} */ (
this._toProtobuf()
),
).finish();
}
Expand Down
144 changes: 144 additions & 0 deletions test/unit/TopicInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { expect } from "chai";
import Duration from "../../src/Duration.js";
import {
AccountId,
LedgerId,
Long,
PrivateKey,
Timestamp,
TopicId,
TopicInfo,
} from "../../src/index.js";

describe("TopicInfo", function () {
it("encodes to correct protobuf", function () {
const adminKey = PrivateKey.generateED25519();
const submitKey = PrivateKey.generateED25519();
const autoRenewAccountId = new AccountId(1, 2, 3);
const autoRenewPeriod = new Duration(123);
const topicId = new TopicId(0);
const topicMemo = "topic";
const runningHash = Uint8Array.from([0]);
const expirationTime = new Timestamp(123, 456);
const sequenceNumber = 0;
const ledgerId = LedgerId.MAINNET;

const topicInfo = new TopicInfo({
topicId,
topicMemo,
runningHash,
sequenceNumber,
expirationTime,
adminKey,
submitKey,
autoRenewAccountId,
autoRenewPeriod,
ledgerId,
});

const expectedProtobuf = {
topicID: topicId._toProtobuf(),
topicInfo: {
memo: topicMemo,
runningHash: runningHash,
sequenceNumber: sequenceNumber,
expirationTime: expirationTime._toProtobuf(),
adminKey: adminKey._toProtobufKey(),
submitKey: submitKey._toProtobufKey(),
autoRenewAccount: autoRenewAccountId._toProtobuf(),
autoRenewPeriod: autoRenewPeriod,
},
};

expect(topicInfo._toProtobuf()).to.deep.equal(expectedProtobuf);
});

it("decodes from correct protobuf", function () {
const adminKey = PrivateKey.generateED25519().publicKey;
const submitKey = PrivateKey.generateED25519().publicKey;
const autoRenewAccountId = new AccountId(1, 2, 3);
const autoRenewPeriod = new Duration(123);
const topicId = new TopicId(0);
const topicMemo = "topic";
const runningHash = Uint8Array.from([0]);
const expirationTime = new Timestamp(123, 456);
const sequenceNumber = Long.fromString("0");

const expectedProtobuf = {
topicID: topicId._toProtobuf(),
topicInfo: {
memo: topicMemo,
runningHash: runningHash,
sequenceNumber: sequenceNumber.toBytes(),
expirationTime: expirationTime._toProtobuf(),
adminKey: adminKey._toProtobufKey(),
submitKey: submitKey._toProtobufKey(),
autoRenewAccount: autoRenewAccountId._toProtobuf(),
autoRenewPeriod: autoRenewPeriod,
},
};

const topicInfo = new TopicInfo({
topicId,
topicMemo,
runningHash,
sequenceNumber,
expirationTime,
adminKey,
submitKey,
autoRenewAccountId,
autoRenewPeriod,
ledgerId: null,
});

expect(TopicInfo._fromProtobuf(expectedProtobuf)).to.deep.equal(
topicInfo,
);
});

it("should decode/encode correctly", function () {
0xivanov marked this conversation as resolved.
Show resolved Hide resolved
const adminKey = PrivateKey.generateED25519().publicKey;
const submitKey = PrivateKey.generateED25519().publicKey;
const autoRenewAccountId = new AccountId(1, 2, 3);
const autoRenewPeriod = new Duration(123);
const topicId = new TopicId(0);
const topicMemo = "topic";
const runningHash = Uint8Array.from([0]);
const expirationTime = new Timestamp(123, 456);
const sequenceNumber = Long.fromString("0");

const topicInfo = new TopicInfo({
topicId,
topicMemo,
runningHash,
sequenceNumber,
expirationTime,
adminKey,
submitKey,
autoRenewAccountId,
autoRenewPeriod,
});

const topicInfoBytes = topicInfo.toBytes();
const topicInfo2 = TopicInfo.fromBytes(topicInfoBytes);

expect(topicInfo2.adminKey.toString()).to.eql(adminKey.toString());
expect(topicInfo2.submitKey.toString()).to.eql(submitKey.toString());
expect(topicInfo2.autoRenewAccountId.toString()).to.eql(
autoRenewAccountId.toString(),
);
expect(topicInfo2.autoRenewPeriod.seconds.toInt()).to.eql(
autoRenewPeriod.seconds.toInt(),
);
expect(topicInfo2.topicId.toString()).to.eql(topicId.toString());
expect(topicInfo2.topicMemo).to.eql(topicMemo);
expect(topicInfo2.runningHash).to.eql(runningHash);
expect(topicInfo2.expirationTime.seconds).to.eql(
expirationTime.seconds,
);
expect(topicInfo2.expirationTime.nanos).to.eql(expirationTime.nanos);
expect(topicInfo2.sequenceNumber.toInt()).to.eql(
sequenceNumber.toInt(),
);
});
});