From 10fdcba6a6026a3caf85be8bf6d8e3d6e6607b3c Mon Sep 17 00:00:00 2001 From: DR497 <47689875+dr497@users.noreply.github.com> Date: Fri, 19 Jan 2024 11:15:21 +0800 Subject: [PATCH] add twitter tests --- js/tests/pyth.test.ts | 1 + js/tests/twitter.test.ts | 119 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 js/tests/twitter.test.ts diff --git a/js/tests/pyth.test.ts b/js/tests/pyth.test.ts index 1e35bbe..6cac411 100644 --- a/js/tests/pyth.test.ts +++ b/js/tests/pyth.test.ts @@ -1,3 +1,4 @@ +require("dotenv").config(); import { PythHttpClient, getPythProgramKeyForCluster, diff --git a/js/tests/twitter.test.ts b/js/tests/twitter.test.ts new file mode 100644 index 0000000..5cf2ad9 --- /dev/null +++ b/js/tests/twitter.test.ts @@ -0,0 +1,119 @@ +require("dotenv").config(); +import { test, expect } from "@jest/globals"; +import { + getTwitterHandleandRegistryKeyViaFilters, + getHandleAndRegistryKey, + createVerifiedTwitterRegistry, + deleteTwitterRegistry, + getTwitterRegistryKey, + getTwitterRegistry, + ReverseTwitterRegistryState, +} from "../src/twitter_bindings"; +import { Connection, Keypair, PublicKey, Transaction } from "@solana/web3.js"; +import { randomBytes } from "crypto"; +import { TWITTER_ROOT_PARENT_REGISTRY_KEY } from "../src/constants"; + +jest.setTimeout(50_000); + +const connection = new Connection(process.env.RPC_URL!); + +test("Resolution & derivation", async () => { + // Example randomly taken + const expected = { + handle: "plenthor", + registry: "HrguVp54KnhQcRPaEBULTRhC2PWcyGTQBfwBNVX9SW2i", + reverse: "RyRmCEVW4wq8xaRGh3sPvp2HDDzb3BgGGMavnXZrbWG", + }; + const owner = new PublicKey("FSMFjujJ1Xi2CN424e4wfFsSMNcdYDGktsuxunazsnjd"); + + //////////////////////////////////////////////////////////////////////// + + expect((await getTwitterRegistryKey(expected.handle)).toBase58()).toBe( + expected.registry, + ); + + //////////////////////////////////////////////////////////////////////// + + const twitterRegistry = await getTwitterRegistry(connection, expected.handle); + expect(twitterRegistry.class.toBase58()).toBe(PublicKey.default.toBase58()); + expect(twitterRegistry.parentName.toBase58()).toBe( + TWITTER_ROOT_PARENT_REGISTRY_KEY.toBase58(), + ); + expect(twitterRegistry.owner.toBase58()).toBe(owner.toBase58()); + + //////////////////////////////////////////////////////////////////////// + + const reverse = await ReverseTwitterRegistryState.retrieve( + connection, + new PublicKey(expected.reverse), + ); + expect(reverse.twitterHandle).toBe(expected.handle); + expect(new PublicKey(reverse.twitterRegistryKey).toBase58()).toBe( + expected.registry, + ); + + //////////////////////////////////////////////////////////////////////// + + let [handle, registry] = await getHandleAndRegistryKey(connection, owner); + + expect(handle).toBe(expected.handle); + expect(registry.toBase58()).toBe(expected.registry); + + //////////////////////////////////////////////////////////////////////// + + [handle, registry] = await getTwitterHandleandRegistryKeyViaFilters( + connection, + owner, + ); + expect(handle).toBe(expected.handle); + expect(registry.toBase58()).toBe(expected.registry); +}); + +test("Create instruction", async () => { + const tx = new Transaction(); + + const handle = randomBytes(10).toString("hex"); + const user = Keypair.generate().publicKey; + const payer = new PublicKey("FSMFjujJ1Xi2CN424e4wfFsSMNcdYDGktsuxunazsnjd"); + + const ix = await createVerifiedTwitterRegistry( + connection, + handle, + user, + 10, + payer, + ); + + tx.add(...ix); + + tx.feePayer = payer; + tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash; + + const { value } = await connection.simulateTransaction(tx); + expect(value.err).toBe(null); +}); + +test("Create & delete instruction", async () => { + const tx = new Transaction(); + + const handle = randomBytes(10).toString("hex"); + const user = Keypair.generate().publicKey; + const payer = new PublicKey("FSMFjujJ1Xi2CN424e4wfFsSMNcdYDGktsuxunazsnjd"); + + tx.add( + ...(await createVerifiedTwitterRegistry( + connection, + handle, + user, + 10, + payer, + )), + ); + tx.add(...(await deleteTwitterRegistry(handle, user))); + + tx.feePayer = payer; + tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash; + + const { value } = await connection.simulateTransaction(tx); + expect(value.err).toBe(null); +});