nodejs_feature_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package integrationtests_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  6. "github.com/langgenius/dify-sandbox/internal/service"
  7. )
  8. func TestNodejsBasicTemplate(t *testing.T) {
  9. const code = `// declare main function
  10. function main({a}) {
  11. return {b: a}
  12. }
  13. // decode and prepare input object
  14. var inputs_obj = JSON.parse(Buffer.from('eyJhIjoiYSJ9', 'base64').toString('utf-8'))
  15. // execute main function
  16. var output_obj = main(inputs_obj)
  17. // convert output to json and print
  18. var output_json = JSON.stringify(output_obj)
  19. var result = ` + "`<<RESULT>>${output_json}<<RESULT>>`" + `
  20. console.log(result)`
  21. runMultipleTestings(t, 30, func(t *testing.T) {
  22. resp := service.RunNodeJsCode(code, "", &types.RunnerOptions{
  23. EnableNetwork: true,
  24. })
  25. if resp.Code != 0 {
  26. t.Fatal(resp)
  27. }
  28. })
  29. }
  30. func TestNodejsBase64(t *testing.T) {
  31. // Test case for base64
  32. runMultipleTestings(t, 30, func(t *testing.T) {
  33. resp := service.RunNodeJsCode(`
  34. const base64 = Buffer.from("hello world").toString("base64");
  35. console.log(Buffer.from(base64, "base64").toString());
  36. `, "", &types.RunnerOptions{
  37. EnableNetwork: true,
  38. })
  39. if resp.Code != 0 {
  40. t.Fatal(resp)
  41. }
  42. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  43. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  44. }
  45. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
  46. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  47. }
  48. })
  49. }
  50. func TestNodejsJSON(t *testing.T) {
  51. // Test case for json
  52. runMultipleTestings(t, 30, func(t *testing.T) {
  53. resp := service.RunNodeJsCode(`
  54. console.log(JSON.stringify({"hello": "world"}));
  55. `, "", &types.RunnerOptions{
  56. EnableNetwork: true,
  57. })
  58. if resp.Code != 0 {
  59. t.Error(resp)
  60. }
  61. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  62. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  63. }
  64. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello":"world"}`) {
  65. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  66. }
  67. })
  68. }