package tools import ( "context" "os" "path/filepath" "strings" "testing" "time" "spark-mcp-go/internal/cluster" ) func TestSparkSubmit_PathPassthrough(t *testing.T) { deps, repo := testDepsWithDataDir(t) deps.SparkSubmitTimeout = 5 * time.Second store := deps.UploadStore fileID, _, _, _, absPath, err := store.Save([]byte("print('hi')\n"), "hello.py") if err != nil { t.Fatalf("save upload: %v", err) } echoed := filepath.Join(t.TempDir(), "echoed") echoScript := filepath.Join(t.TempDir(), "echo-args.sh") if err := os.WriteFile(echoScript, []byte("#!/bin/sh\nfor arg do\n echo \"$arg\" >> \"$ECHO_FILE\"\ndone\n"), 0o755); err != nil { t.Fatalf("write echo script: %v", err) } t.Setenv("ECHO_FILE", echoed) createCluster(t, repo, &cluster.Cluster{ ID: "cluster-echo", Name: "Echo", IsActive: true, AuthType: cluster.AuthNone, SparkSubmitExecuteBin: echoScript, }) req := newToolRequest(SparkSubmitName, map[string]any{ "cluster_id": "cluster-echo", "args": []any{absPath, "--class", "Main"}, }) res, err := deps.SparkSubmitHandler(context.Background(), req) if err != nil { t.Fatalf("handler error: %v", err) } if res.IsError { t.Fatalf("unexpected error result: %v", res.Content) } got, err := os.ReadFile(echoed) if err != nil { t.Fatalf("read echoed args: %v", err) } lines := strings.Split(strings.TrimSpace(string(got)), "\n") want := []string{absPath, "--class", "Main"} if len(lines) != len(want) { t.Fatalf("echoed lines=%v, want %v", lines, want) } for i, l := range lines { if l != want[i] { t.Errorf("line[%d]=%q, want %q", i, l, want[i]) } } // Sanity: the canonical path still contains the minted file ID. if !strings.HasSuffix(absPath, "/"+fileID) { t.Errorf("absPath=%q does not end with fileID %q", absPath, fileID) } } func TestSparkSubmit_ClusterLocalPathPassthrough(t *testing.T) { deps, repo := testDepsWithDataDir(t) deps.SparkSubmitTimeout = 5 * time.Second echoed := filepath.Join(t.TempDir(), "echoed") echoScript := filepath.Join(t.TempDir(), "echo-args.sh") if err := os.WriteFile(echoScript, []byte("#!/bin/sh\nfor arg do\n echo \"$arg\" >> \"$ECHO_FILE\"\ndone\n"), 0o755); err != nil { t.Fatalf("write echo script: %v", err) } t.Setenv("ECHO_FILE", echoed) createCluster(t, repo, &cluster.Cluster{ ID: "cluster-echo", Name: "Echo", IsActive: true, AuthType: cluster.AuthNone, SparkSubmitExecuteBin: echoScript, }) clusterLocalPath := "/usr/bin/env" req := newToolRequest(SparkSubmitName, map[string]any{ "cluster_id": "cluster-echo", "args": []any{clusterLocalPath, "--class", "Main"}, }) res, err := deps.SparkSubmitHandler(context.Background(), req) if err != nil { t.Fatalf("handler error: %v", err) } if res.IsError { t.Fatalf("unexpected error result: %v", res.Content) } got, err := os.ReadFile(echoed) if err != nil { t.Fatalf("read echoed args: %v", err) } lines := strings.Split(strings.TrimSpace(string(got)), "\n") want := []string{clusterLocalPath, "--class", "Main"} if len(lines) != len(want) { t.Fatalf("echoed lines=%v, want %v", lines, want) } for i, l := range lines { if l != want[i] { t.Errorf("line[%d]=%q, want %q", i, l, want[i]) } } }