1313import assert from "assert" ;
1414import it from "../assertionHandler.mjs" ;
1515import chef from "../../../src/node/index.mjs" ;
16- import OperationError from "../../../src/core/errors/OperationError .mjs" ;
16+ import { OperationError , ExcludedOperationError } from "../../../src/core/errors/index .mjs" ;
1717import NodeDish from "../../../src/node/NodeDish.mjs" ;
1818
19- import { toBase32 } from "../../../src/node/index.mjs" ;
19+ import { toBase32 , magic } from "../../../src/node/index.mjs" ;
2020import TestRegister from "../../lib/TestRegister.mjs" ;
2121
2222TestRegister . addApiTests ( [
@@ -181,22 +181,17 @@ TestRegister.addApiTests([
181181 } ) ,
182182
183183 it ( "chef.bake: should complain if recipe isnt a valid object" , ( ) => {
184- try {
185- chef . bake ( "some input" , 3264 ) ;
186- } catch ( e ) {
187- assert . strictEqual ( e . name , "TypeError" ) ;
188- assert . strictEqual ( e . message , "Recipe can only contain function names or functions" ) ;
189- }
184+ assert . throws ( ( ) => chef . bake ( "some input" , 3264 ) , {
185+ name : "TypeError" ,
186+ message : "Recipe can only contain function names or functions"
187+ } ) ;
190188 } ) ,
191189
192190 it ( "chef.bake: Should complain if string op is invalid" , ( ) => {
193- try {
194- chef . bake ( "some input" , "not a valid operation" ) ;
195- assert . fail ( "Shouldn't be hit" ) ;
196- } catch ( e ) {
197- assert . strictEqual ( e . name , "TypeError" ) ;
198- assert . strictEqual ( e . message , "Couldn't find an operation with name 'not a valid operation'." ) ;
199- }
191+ assert . throws ( ( ) => chef . bake ( "some input" , "not a valid operation" ) , {
192+ name : "TypeError" ,
193+ message : "Couldn't find an operation with name 'not a valid operation'."
194+ } ) ;
200195 } ) ,
201196
202197 it ( "chef.bake: Should take an input and an operation and perform it" , ( ) => {
@@ -205,13 +200,10 @@ TestRegister.addApiTests([
205200 } ) ,
206201
207202 it ( "chef.bake: Should complain if an invalid operation is inputted" , ( ) => {
208- try {
209- chef . bake ( "https://google.com/search?q=help" , ( ) => { } ) ;
210- assert . fail ( "Shouldn't be hit" ) ;
211- } catch ( e ) {
212- assert . strictEqual ( e . name , "TypeError" ) ;
213- assert . strictEqual ( e . message , "Inputted function not a Chef operation." ) ;
214- }
203+ assert . throws ( ( ) => chef . bake ( "https://google.com/search?q=help" , ( ) => { } ) , {
204+ name : "TypeError" ,
205+ message : "Inputted function not a Chef operation."
206+ } ) ;
215207 } ) ,
216208
217209 it ( "chef.bake: accepts an array of operation names and performs them all in order" , ( ) => {
@@ -241,12 +233,10 @@ TestRegister.addApiTests([
241233 } ) ,
242234
243235 it ( "should complain if an invalid operation is inputted as part of array" , ( ) => {
244- try {
245- chef . bake ( "something" , [ ( ) => { } ] ) ;
246- } catch ( e ) {
247- assert . strictEqual ( e . name , "TypeError" ) ;
248- assert . strictEqual ( e . message , "Inputted function not a Chef operation." ) ;
249- }
236+ assert . throws ( ( ) => chef . bake ( "something" , [ ( ) => { } ] ) , {
237+ name : "TypeError" ,
238+ message : "Inputted function not a Chef operation."
239+ } ) ;
250240 } ) ,
251241
252242 it ( "chef.bake: should take single JSON object describing op and args OBJ" , ( ) => {
@@ -275,15 +265,13 @@ TestRegister.addApiTests([
275265 } ) ,
276266
277267 it ( "chef.bake: should error if op in JSON is not chef op" , ( ) => {
278- try {
279- chef . bake ( "some input" , {
280- op : ( ) => { } ,
281- args : [ "Colon" ] ,
282- } ) ;
283- } catch ( e ) {
284- assert . strictEqual ( e . name , "TypeError" ) ;
285- assert . strictEqual ( e . message , "Inputted function not a Chef operation." ) ;
286- }
268+ assert . throws ( ( ) => chef . bake ( "some input" , {
269+ op : ( ) => { } ,
270+ args : [ "Colon" ] ,
271+ } ) , {
272+ name : "TypeError" ,
273+ message : "Inputted function not a Chef operation."
274+ } ) ;
287275 } ) ,
288276
289277 it ( "chef.bake: should take multiple ops in JSON object form, some ops by string" , ( ) => {
@@ -357,22 +345,38 @@ TestRegister.addApiTests([
357345 assert . strictEqual ( result . toString ( ) , "begin_something_aaaaaaaaaaaaaa_end_something" ) ;
358346 } ) ,
359347
360- it ( "Excluded operations: throw a sensible error when you try and call one" , ( ) => {
361- try {
362- chef . fork ( ) ;
363- } catch ( e ) {
364- assert . strictEqual ( e . type , "ExcludedOperationError" ) ;
365- assert . strictEqual ( e . message , "Sorry, the Fork operation is not available in the Node.js version of CyberChef." ) ;
366- }
348+ it ( "chef.bake: cannot accept flowControl operations in recipe" , ( ) => {
349+ assert . throws ( ( ) => chef . bake ( "some input" , "magic" ) , {
350+ name : "TypeError" ,
351+ message : "flowControl operations like Magic are not currently allowed in recipes for chef.bake"
352+ } ) ;
353+ assert . throws ( ( ) => chef . bake ( "some input" , magic ) , {
354+ name : "TypeError" ,
355+ message : "flowControl operations like Magic are not currently allowed in recipes for chef.bake"
356+ } ) ;
357+ assert . throws ( ( ) => chef . bake ( "some input" , [ "to base 64" , "magic" ] ) , {
358+ name : "TypeError" ,
359+ message : "flowControl operations like Magic are not currently allowed in recipes for chef.bake"
360+ } ) ;
367361 } ) ,
368362
369363 it ( "Excluded operations: throw a sensible error when you try and call one" , ( ) => {
370- try {
371- chef . renderImage ( ) ;
372- } catch ( e ) {
373- assert . strictEqual ( e . type , "ExcludedOperationError" ) ;
374- assert . strictEqual ( e . message , "Sorry, the RenderImage operation is not available in the Node.js version of CyberChef." ) ;
375- }
364+ assert . throws ( chef . fork ,
365+ ( err ) => {
366+ assert ( err instanceof ExcludedOperationError ) ;
367+ assert . deepEqual ( err . message , "Sorry, the Fork operation is not available in the Node.js version of CyberChef." ) ;
368+ return true ;
369+ } ,
370+ "Unexpected error type"
371+ ) ;
372+ assert . throws ( chef . javaScriptBeautify ,
373+ ( err ) => {
374+ assert ( err instanceof ExcludedOperationError ) ;
375+ assert . deepEqual ( err . message , "Sorry, the JavaScriptBeautify operation is not available in the Node.js version of CyberChef." ) ;
376+ return true ;
377+ } ,
378+ "Unexpected error type"
379+ ) ;
376380 } ) ,
377381
378382 it ( "Operation arguments: should be accessible from operation object if op has array arg" , ( ) => {
@@ -405,4 +409,5 @@ TestRegister.addApiTests([
405409 assert . equal ( chef . convertDistance . args . inputUnits . options [ 0 ] , "Nanometres (nm)" ) ;
406410 assert . equal ( chef . defangURL . args . process . options [ 1 ] , "Only full URLs" ) ;
407411 } ) ,
412+
408413] ) ;
0 commit comments