nodejs_feature_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 TestNodejsBase64(t *testing.T) {
  9. // Test case for base64
  10. runMultipleTestings(t, 30, func(t *testing.T) {
  11. resp := service.RunNodeJsCode(`
  12. const base64 = Buffer.from("hello world").toString("base64");
  13. console.log(Buffer.from(base64, "base64").toString());
  14. `, "", &types.RunnerOptions{
  15. EnableNetwork: true,
  16. })
  17. if resp.Code != 0 {
  18. t.Fatal(resp)
  19. }
  20. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
  21. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  22. }
  23. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  24. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  25. }
  26. })
  27. }
  28. func TestNodejsJSON(t *testing.T) {
  29. // Test case for json
  30. runMultipleTestings(t, 30, func(t *testing.T) {
  31. resp := service.RunNodeJsCode(`
  32. console.log(JSON.stringify({"hello": "world"}));
  33. `, "", &types.RunnerOptions{
  34. EnableNetwork: true,
  35. })
  36. if resp.Code != 0 {
  37. t.Error(resp)
  38. }
  39. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello":"world"}`) {
  40. t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
  41. }
  42. if resp.Data.(*service.RunCodeResponse).Stderr != "" {
  43. t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
  44. }
  45. })
  46. }