瀏覽代碼

Feat/add nodejs tests (#7)

feat: add nodejs tests
Yeuoly 1 年之前
父節點
當前提交
05549d2c84

+ 8 - 0
.github/workflows/tests-amd64.yml

@@ -35,6 +35,14 @@ jobs:
       - name: Compile library
         run: bash ./build/build_amd64.sh
 
+      - name: Setup Nodejs20.11.1
+        uses: actions/setup-node@v3
+        with:
+          node-version: '20.11.1'
+
+      - name: Link Nodejs
+        run: sudo ln -sf "$(which node)" /usr/local/bin/node
+
       - name: Setup Python3.10
         uses: actions/setup-python@v2
         with:

+ 5 - 0
.github/workflows/tests-arm64.yml

@@ -35,6 +35,11 @@ jobs:
       - name: Compile library
         run: bash ./build/build_arm64.sh
 
+      - name: Setup Nodejs20.11.1
+        uses: actions/setup-node@v3
+        with:
+          node-version: '20.11.1'
+
       - name: Setup Python3.10
         run: sudo apt-get install -y python3.10 python3-pip
 

+ 50 - 0
tests/integration_tests/nodejs_feature_test.go

@@ -0,0 +1,50 @@
+package integrationtests_test
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/langgenius/dify-sandbox/internal/core/runner/types"
+	"github.com/langgenius/dify-sandbox/internal/service"
+)
+
+func TestNodejsBase64(t *testing.T) {
+	// Test case for base64
+	resp := service.RunNodeJsCode(`
+const base64 = Buffer.from("hello world").toString("base64");
+console.log(Buffer.from(base64, "base64").toString());
+	`, "", &types.RunnerOptions{
+		EnableNetwork: true,
+	})
+	if resp.Code != 0 {
+		t.Error(resp)
+	}
+
+	if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
+		t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
+	}
+
+	if resp.Data.(*service.RunCodeResponse).Stderr != "" {
+		t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
+	}
+}
+
+func TestNodejsJSON(t *testing.T) {
+	// Test case for json
+	resp := service.RunNodeJsCode(`
+console.log(JSON.stringify({"hello": "world"}));
+	`, "", &types.RunnerOptions{
+		EnableNetwork: true,
+	})
+	if resp.Code != 0 {
+		t.Error(resp)
+	}
+
+	if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello":"world"}`) {
+		t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
+	}
+
+	if resp.Data.(*service.RunCodeResponse).Stderr != "" {
+		t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
+	}
+}

+ 36 - 0
tests/integration_tests/nodejs_malicious_test.go

@@ -0,0 +1,36 @@
+package integrationtests_test
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/langgenius/dify-sandbox/internal/core/runner/types"
+	"github.com/langgenius/dify-sandbox/internal/service"
+)
+
+func TestNodejsRunCommand(t *testing.T) {
+	// Test case for run_command
+	resp := service.RunNodeJsCode(`
+const { spawn } = require( 'child_process' );
+const ls = spawn( 'ls', [ '-lh', '/usr' ] );
+
+ls.stdout.on( 'data', ( data ) => {
+    console.log(data);
+} );
+
+ls.stderr.on( 'data', ( data ) => {
+    console.log(data);
+} );
+
+ls.on( 'close', ( code ) => {
+    console.log(code);
+} );
+	`, "", &types.RunnerOptions{})
+	if resp.Code != 0 {
+		t.Error(resp)
+	}
+
+	if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
+		t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
+	}
+}

+ 3 - 3
tests/integration_tests/ordinary_feature_test.go

@@ -8,7 +8,7 @@ import (
 	"github.com/langgenius/dify-sandbox/internal/service"
 )
 
-func TestBase64(t *testing.T) {
+func TestPythonBase64(t *testing.T) {
 	// Test case for base64
 	resp := service.RunPython3Code(`
 import base64
@@ -29,7 +29,7 @@ print(base64.b64decode(base64.b64encode(b"hello world")).decode())
 	}
 }
 
-func TestJSON(t *testing.T) {
+func TestPythonJSON(t *testing.T) {
 	// Test case for json
 	resp := service.RunPython3Code(`
 import json
@@ -50,7 +50,7 @@ print(json.dumps({"hello": "world"}))
 	}
 }
 
-func TestHttp(t *testing.T) {
+func TestPythonHttp(t *testing.T) {
 	// Test case for http
 	resp := service.RunPython3Code(`
 import requests

tests/integration_tests/malicious_test.go → tests/integration_tests/python_malicious_test.go