@@ -138,6 +138,59 @@ 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 ( 'eth' ) , / 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 is the base canton coin (not a token)' , 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 ( '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 / ) ;
191+ } ) ;
192+ } ) ;
193+
141194 describe ( 'resolveContracts()' , ( ) => {
142195 it ( 'should set the spec array' , function ( ) {
143196 const spec = [ { templateId : TEMPLATE_ID , actAs : [ PARTY_A ] , injectAs : 'command.ExerciseCommand.contractId' } ] ;
@@ -206,6 +259,24 @@ describe('CantonCommandBuilder', () => {
206259 const req = builder . toRequestObject ( ) ;
207260 assert . deepEqual ( req . resolveContracts , [ ] ) ;
208261 } ) ;
262+
263+ it ( 'should include token when set' , function ( ) {
264+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
265+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
266+ builder . initBuilder ( tx ) ;
267+ builder . commandId ( 'cmd-003' ) . actAs ( [ PARTY_A ] ) . command ( sampleExerciseCommand ) . token ( 'tcanton:testtoken' ) ;
268+ const req = builder . toRequestObject ( ) ;
269+ assert . equal ( req . token , 'tcanton:testtoken' ) ;
270+ } ) ;
271+
272+ it ( 'should not include token key when not set' , function ( ) {
273+ const builder = new CantonCommandBuilder ( coins . get ( 'tcanton' ) ) ;
274+ const tx = new Transaction ( coins . get ( 'tcanton' ) ) ;
275+ builder . initBuilder ( tx ) ;
276+ builder . commandId ( 'cmd-004' ) . actAs ( [ PARTY_A ] ) . command ( sampleExerciseCommand ) ;
277+ const req = builder . toRequestObject ( ) ;
278+ assert . ok ( ! ( 'token' in req ) ) ;
279+ } ) ;
209280 } ) ;
210281
211282 describe ( 'initBuilder()' , ( ) => {
0 commit comments