11import { Buffer } from 'node:buffer' ;
22import { describe , test } from 'node:test' ;
33import { Keypair , PublicKey , SystemProgram , Transaction , TransactionInstruction } from '@solana/web3.js' ;
4+ import { assert } from 'chai' ;
45import { start } from 'solana-bankrun' ;
5- import { AddressInfo , AddressInfoExtender , ExtendedAddressInfo , ReallocInstruction } from './schema' ;
6+ import { AddressInfo , AddressInfoExtender , ExtendedAddressInfo , ReallocInstruction , WorkInfo } from './schema' ;
67
78describe ( 'Realloc!' , async ( ) => {
89 const PROGRAM_ID = new PublicKey ( 'z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35' ) ;
@@ -18,15 +19,14 @@ describe('Realloc!', async () => {
1819 console . log ( `Payer Address : ${ payer . publicKey } ` ) ;
1920 console . log ( `Address Info Acct : ${ addressInfoAccount . publicKey } ` ) ;
2021
21- const ixData = Buffer . concat ( [
22- Buffer . from ( [ ReallocInstruction . Create ] ) ,
23- AddressInfo . fromData ( {
24- name : 'Joe C' ,
25- house_number : 136 ,
26- street : 'Mile High Dr.' ,
27- city : 'Solana Beach' ,
28- } ) . toBuffer ( ) ,
29- ] ) ;
22+ const addressInfo = {
23+ name : 'Joe C' ,
24+ house_number : 136 ,
25+ street : 'Mile High Dr.' ,
26+ city : 'Solana Beach' ,
27+ } ;
28+
29+ const ixData = Buffer . concat ( [ Buffer . from ( [ ReallocInstruction . Create ] ) , AddressInfo . fromData ( addressInfo ) . toBuffer ( ) ] ) ;
3030
3131 const ix = new TransactionInstruction ( {
3232 keys : [
@@ -48,9 +48,7 @@ describe('Realloc!', async () => {
4848 tx . recentBlockhash = blockhash ;
4949 tx . add ( ix ) . sign ( payer , addressInfoAccount ) ;
5050 await client . processTransaction ( tx ) ;
51- } ) ;
5251
53- test ( "Read the new account's data" , async ( ) => {
5452 const accountInfo = await client . getAccount ( addressInfoAccount . publicKey ) ;
5553
5654 const readAddressInfo = AddressInfo . fromAccountData ( Buffer . from ( accountInfo . data ) ) . toData ( ) ;
@@ -59,16 +57,20 @@ describe('Realloc!', async () => {
5957 console . log ( `House Num: ${ readAddressInfo . house_number } ` ) ;
6058 console . log ( `Street : ${ readAddressInfo . street } ` ) ;
6159 console . log ( `City : ${ readAddressInfo . city } ` ) ;
60+
61+ assert ( readAddressInfo . name . slice ( 0 , addressInfo . name . length ) === addressInfo . name , 'name does not match' ) ;
62+ assert ( readAddressInfo . house_number === addressInfo . house_number , 'house number does not match' ) ;
63+ assert ( readAddressInfo . street . slice ( 0 , addressInfo . street . length ) === addressInfo . street , 'street does not match' ) ;
64+ assert ( readAddressInfo . city . slice ( 0 , addressInfo . city . length ) === addressInfo . city , 'city does not match' ) ;
6265 } ) ;
6366
6467 test ( 'Extend the address info account' , async ( ) => {
65- const ixData = Buffer . concat ( [
66- Buffer . from ( [ ReallocInstruction . Extend ] ) ,
67- AddressInfoExtender . fromData ( {
68- state : 'Illinois' ,
69- zip : 12345 ,
70- } ) . toBuffer ( ) ,
71- ] ) ;
68+ const addressInfoExtender = {
69+ state : 'Illinois' ,
70+ zip : 12345 ,
71+ } ;
72+
73+ const ixData = Buffer . concat ( [ Buffer . from ( [ ReallocInstruction . Extend ] ) , AddressInfoExtender . fromData ( addressInfoExtender ) . toBuffer ( ) ] ) ;
7274
7375 const ix = new TransactionInstruction ( {
7476 keys : [
@@ -90,9 +92,7 @@ describe('Realloc!', async () => {
9092 tx . recentBlockhash = blockhash ;
9193 tx . add ( ix ) . sign ( payer , addressInfoAccount ) ;
9294 await client . processTransaction ( tx ) ;
93- } ) ;
9495
95- test ( "Read the new account's data" , async ( ) => {
9696 const accountInfo = await client . getAccount ( addressInfoAccount . publicKey ) ;
9797
9898 const readAddressInfo = ExtendedAddressInfo . fromAccountData ( Buffer . from ( accountInfo . data ) ) . toData ( ) ;
@@ -103,5 +103,54 @@ describe('Realloc!', async () => {
103103 console . log ( `City : ${ readAddressInfo . city } ` ) ;
104104 console . log ( `State : ${ readAddressInfo . state } ` ) ;
105105 console . log ( `Zip : ${ readAddressInfo . zip } ` ) ;
106+
107+ assert ( readAddressInfo . state . slice ( 0 , addressInfoExtender . state . length ) === addressInfoExtender . state , 'state does not match' ) ;
108+ assert ( readAddressInfo . zip === addressInfoExtender . zip , 'zip does not match' ) ;
109+ } ) ;
110+
111+ test ( 'zero init work info account' , async ( ) => {
112+ const workInfo = {
113+ name : 'Pete' ,
114+ company : 'Solana Labs' ,
115+ position : 'Engineer' ,
116+ years_employed : 2 ,
117+ } ;
118+
119+ const ixData = Buffer . concat ( [ Buffer . from ( [ ReallocInstruction . ZeroInit ] ) , WorkInfo . fromData ( workInfo ) . toBuffer ( ) ] ) ;
120+
121+ const ix = new TransactionInstruction ( {
122+ keys : [
123+ { pubkey : payer . publicKey , isSigner : true , isWritable : true } ,
124+ {
125+ pubkey : addressInfoAccount . publicKey ,
126+ isSigner : true ,
127+ isWritable : true ,
128+ } ,
129+ { pubkey : SystemProgram . programId , isSigner : false , isWritable : false } ,
130+ ] ,
131+ programId : PROGRAM_ID ,
132+ data : ixData ,
133+ } ) ;
134+
135+ const blockhash = context . lastBlockhash ;
136+
137+ const tx = new Transaction ( ) ;
138+ tx . recentBlockhash = blockhash ;
139+ tx . add ( ix ) . sign ( payer , addressInfoAccount ) ;
140+ await client . processTransaction ( tx ) ;
141+
142+ const accountInfo = await client . getAccount ( addressInfoAccount . publicKey ) ;
143+
144+ const readWorkInfo = WorkInfo . fromAccountData ( Buffer . from ( accountInfo . data ) ) . toData ( ) ;
145+
146+ console . log ( `Name : ${ readWorkInfo . name } ` ) ;
147+ console . log ( `Position : ${ readWorkInfo . position } ` ) ;
148+ console . log ( `Company : ${ readWorkInfo . company } ` ) ;
149+ console . log ( `Years Employed: ${ readWorkInfo . years_employed } ` ) ;
150+
151+ assert ( readWorkInfo . name . slice ( 0 , workInfo . name . length ) === workInfo . name , 'name does not match' ) ;
152+ assert ( readWorkInfo . position . slice ( 0 , workInfo . position . length ) === workInfo . position , 'position does not match' ) ;
153+ assert ( readWorkInfo . company . slice ( 0 , workInfo . company . length ) === workInfo . company , 'company does not match' ) ;
154+ assert ( readWorkInfo . years_employed === workInfo . years_employed , 'years employed does not match' ) ;
106155 } ) ;
107156} ) ;
0 commit comments