Skip to content

Commit

Permalink
Feat/add nodejs tests (#7)
Browse files Browse the repository at this point in the history
feat: add nodejs tests
  • Loading branch information
Yeuoly authored Jun 27, 2024
1 parent aa70204 commit 05549d2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/tests-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/tests-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions tests/integration_tests/nodejs_feature_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions tests/integration_tests/nodejs_malicious_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
File renamed without changes.

0 comments on commit 05549d2

Please sign in to comment.