@@ -138,6 +138,66 @@ describe('CantonCommandBuilder', () => {
138138 } ) ;
139139 } ) ;
140140
141+ describe ( 'token()' , ( ) => {
142+ it ( 'should set the token' , function ( ) {
143+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
144+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
145+ builder . initBuilder ( tx ) ;
146+ builder . commandId ( 'cmd-tok-1' ) . actAs ( [ PARTY_A ] ) . command ( sampleExerciseCommand ) . token ( 'tcanton:testtoken' ) ;
147+ assert . equal ( builder . toRequestObject ( ) . token , 'tcanton:testtoken' ) ;
148+ } ) ;
149+
150+ it ( 'should trim whitespace' , function ( ) {
151+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
152+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
153+ builder . initBuilder ( tx ) ;
154+ builder . commandId ( 'cmd-tok-2' ) . actAs ( [ PARTY_A ] ) . command ( sampleExerciseCommand ) . token ( ' tcanton:testtoken ' ) ;
155+ assert . equal ( builder . toRequestObject ( ) . token , 'tcanton:testtoken' ) ;
156+ } ) ;
157+
158+ it ( 'should throw on empty string' , function ( ) {
159+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
160+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
161+ builder . initBuilder ( tx ) ;
162+ assert . throws ( ( ) => builder . token ( '' ) , / t o k e n m u s t b e a n o n - e m p t y s t r i n g / ) ;
163+ } ) ;
164+
165+ it ( 'should throw on whitespace-only string' , function ( ) {
166+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
167+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
168+ builder . initBuilder ( tx ) ;
169+ assert . throws ( ( ) => builder . token ( ' ' ) , / t o k e n m u s t b e a n o n - e m p t y s t r i n g / ) ;
170+ } ) ;
171+
172+ it ( 'should throw on an unregistered coin name' , function ( ) {
173+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
174+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
175+ builder . initBuilder ( tx ) ;
176+ assert . throws ( ( ) => builder . token ( 'tcanton:fakecoin' ) , / t o k e n i s n o t a r e g i s t e r e d c o i n / ) ;
177+ } ) ;
178+
179+ it ( 'should throw when token is not a canton family token' , function ( ) {
180+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
181+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
182+ builder . initBuilder ( tx ) ;
183+ assert . throws ( ( ) => builder . token ( 'teth' ) , / t o k e n m u s t b e a r e g i s t e r e d c a n t o n t o k e n / ) ;
184+ } ) ;
185+
186+ it ( 'should throw when token network does not match builder network' , function ( ) {
187+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
188+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
189+ builder . initBuilder ( tx ) ;
190+ assert . throws ( ( ) => builder . token ( 'canton:usd1' ) , / t o k e n n e t w o r k m u s t m a t c h b u i l d e r n e t w o r k / ) ;
191+ } ) ;
192+
193+ it ( 'should throw when token is the base canton coin (not a token)' , function ( ) {
194+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
195+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
196+ builder . initBuilder ( tx ) ;
197+ assert . throws ( ( ) => builder . token ( 'tcanton' ) , / t o k e n m u s t b e a r e g i s t e r e d c a n t o n t o k e n / ) ;
198+ } ) ;
199+ } ) ;
200+
141201 describe ( 'resolveContracts()' , ( ) => {
142202 it ( 'should set the spec array' , function ( ) {
143203 const spec = [ { templateId : TEMPLATE_ID , actAs : [ PARTY_A ] , injectAs : 'command.ExerciseCommand.contractId' } ] ;
@@ -206,6 +266,24 @@ describe('CantonCommandBuilder', () => {
206266 const req = builder . toRequestObject ( ) ;
207267 assert . deepEqual ( req . resolveContracts , [ ] ) ;
208268 } ) ;
269+
270+ it ( 'should include token when set' , function ( ) {
271+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
272+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
273+ builder . initBuilder ( tx ) ;
274+ builder . commandId ( 'cmd-003' ) . actAs ( [ PARTY_A ] ) . command ( sampleExerciseCommand ) . token ( 'tcanton:testtoken' ) ;
275+ const req = builder . toRequestObject ( ) ;
276+ assert . equal ( req . token , 'tcanton:testtoken' ) ;
277+ } ) ;
278+
279+ it ( 'should not include token key when not set' , function ( ) {
280+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
281+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
282+ builder . initBuilder ( tx ) ;
283+ builder . commandId ( 'cmd-004' ) . actAs ( [ PARTY_A ] ) . command ( sampleExerciseCommand ) ;
284+ const req = builder . toRequestObject ( ) ;
285+ assert . ok ( ! ( 'token' in req ) ) ;
286+ } ) ;
209287 } ) ;
210288
211289 describe ( 'initBuilder()' , ( ) => {
0 commit comments