Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dabit3
Last active October 29, 2021 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabit3/26881c8a0e121b8b0433e6fb46709cb4 to your computer and use it in GitHub Desktop.
Save dabit3/26881c8a0e121b8b0433e6fb46709cb4 to your computer and use it in GitHub Desktop.
Test for Solana Blog
const anchor = require("@project-serum/anchor");
const assert = require("assert");
const { PublicKey } = anchor.web3;
describe("blog", () => {
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.Blog;
const post = anchor.web3.Keypair.generate();
it("Creates a post", async () => {
await program.rpc.createPost("12345", {
accounts: {
post: post.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
instructions: [
await program.account.post.createInstruction(post),
],
signers: [post],
});
const postdata = await program.account.post.fetch(post.publicKey);
const id = new TextDecoder("utf-8").decode(new Uint8Array(postdata.id));
assert.ok(id.startsWith("12345")); // [u8; 280] => trailing zeros.
console.log('postdata:' , postdata)
assert.ok(postdata.items.length === 33607);
});
it("Creates a blog", async () => {
const authority = program.provider.wallet.publicKey;
const [blog, bump] = await PublicKey.findProgramAddress(
[authority.toBuffer()],
program.programId
);
await program.rpc.createBlog("My Blog", bump, {
accounts: {
blog,
authority,
systemProgram: anchor.web3.SystemProgram.programId,
},
});
const account = await program.account.blog.fetch(blog);
assert.ok(account.name === "My Blog");
assert.ok(account.authority.equals(authority));
});
it("Creates posts", async () => {
const authority = program.provider.wallet.publicKey;
const blog = (
await PublicKey.findProgramAddress(
[authority.toBuffer()],
program.programId
)
)[0];
// create a few posts
const numPosts = 2;
// Generate random id strings.
const posts = new Array(numPosts).fill("").map((msg) => {
return (
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
);
});
// create each post.
for (let k = 0; k < numPosts; k += 1) {
console.log("creating post " + k);
await program.rpc.addPost(posts[k], {
accounts: {
blog,
authority,
post: post.publicKey,
},
});
}
const postData = await program.account.post.fetch(post.publicKey);
const data = new TextDecoder("utf-8").decode(new Uint8Array(postData.id));
assert.ok(data.startsWith("12345")); // [u8; 280] => trailing zeros.
assert.ok(postData.items.length === 33607);
assert.ok(postData.head.toNumber() === numPosts);
assert.ok(postData.tail.toNumber() === 0);
postData.items.forEach((post, idx) => {
if (idx < 2) {
const data = new TextDecoder("utf-8").decode(new Uint8Array(post.data));
assert.ok(post.from.equals(blog));
assert.ok(data.startsWith(posts[idx]));
} else {
assert.ok(anchor.web3.PublicKey.default);
assert.ok(
JSON.stringify(post.data) === JSON.stringify(new Array(280).fill(0))
);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment