Files
ARTEX-chixuweihu/server/sandbox_attachments_test.go
T
ji ruo 9c43e694a3 feat(sandbox): isolate worker/chat execution in Docker containers with lifecycle UI
- Add Docker sandbox manager, non-root sandbox image, compose isolation, and host firewall script (#217)
- Route Bash, custom commands/scripts, stdio MCP, chat sessions, MainAgent chat, and task-scoped reporter screenshots into sandbox containers with per-task browser concurrency gate
- Enforce proxy SSRF guard against loopback, metadata, internal services, and admin port
- Add dedicated /system/sandbox management page, /chat session sandbox control, and task detail sandbox popover with progressive stats loading
2026-12-01 21:13:21 +08:00

113 lines
3.6 KiB
Go

package server
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/internal/sandbox"
)
func TestTransferDraftAndSessionAttachmentsToTask(t *testing.T) {
tmp := t.TempDir()
// Simulate Docker sandbox enabled for path rewriting to /workspace/uploads/...
mgr, err := sandbox.New(sandbox.Config{
Mode: sandbox.ModeDocker,
DockerHost: "unix:///nonexistent.sock",
DataDir: tmp,
HostDataDir: tmp,
})
if err != nil {
t.Fatal(err)
}
prev := sandbox.Default()
sandbox.SetDefault(mgr)
defer sandbox.SetDefault(prev)
// 1. Draft staging upload handoff
draftDir := filepath.Join(tmp, "drafts", "draft-abc", "uploads")
if err := os.MkdirAll(draftDir, 0o755); err != nil {
t.Fatal(err)
}
draftFile := filepath.Join(draftDir, "scope.txt")
if err := os.WriteFile(draftFile, []byte("https://example.com\n"), 0o644); err != nil {
t.Fatal(err)
}
desc := "请测试以下附件中的目标:\n- " + draftFile
newDesc, newGoal, copied := transferDraftUploadsToTask(tmp, "101", desc, "扫描全部目标")
if copied != 1 {
t.Fatalf("draft copied = %d, want 1", copied)
}
if !strings.Contains(newDesc, "/workspace/uploads/scope.txt") {
t.Fatalf("rewritten draft desc = %q", newDesc)
}
if newGoal != "扫描全部目标" {
t.Fatalf("unexpected goal change: %q", newGoal)
}
if b, err := os.ReadFile(filepath.Join(tmp, "tasks", "101", "uploads", "scope.txt")); err != nil || string(b) != "https://example.com\n" {
t.Fatalf("copied draft file check failed: %q, %v", string(b), err)
}
// 2. Auto/chat session -> spawn_task handoff (explicit + inline reference)
sessDir := filepath.Join(tmp, "sessions", "conv-7", "uploads")
if err := os.MkdirAll(sessDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(sessDir, "api.yaml"), []byte("openapi: 3.0.0\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(sessDir, "creds.txt"), []byte("user:pass\n"), 0o644); err != nil {
t.Fatal(err)
}
desc2, goal2, copied2 := transferSessionAttachmentsToTask(
tmp, "conv-7", "202",
[]string{"uploads/creds.txt"},
"分析接口文档 /workspace/uploads/api.yaml",
"测试所有鉴权接口",
)
if copied2 != 2 {
t.Fatalf("session copied = %d, want 2", copied2)
}
if !strings.Contains(desc2, "/workspace/uploads/api.yaml") || !strings.Contains(desc2, "/workspace/uploads/creds.txt") {
t.Fatalf("rewritten session desc = %q", desc2)
}
if goal2 != "测试所有鉴权接口" {
t.Fatalf("unexpected goal2: %q", goal2)
}
for _, fn := range []string{"api.yaml", "creds.txt"} {
if _, err := os.Stat(filepath.Join(tmp, "tasks", "202", "uploads", fn)); err != nil {
t.Fatalf("expected %s in task 202 uploads: %v", fn, err)
}
}
}
func TestAcquireSandboxBrowserSlotCapsConcurrencyAndHonorsCancel(t *testing.T) {
key := "task:browser-slot-test"
rel1, err := acquireSandboxBrowserSlot(t.Context(), key)
if err != nil {
t.Fatalf("slot 1: %v", err)
}
defer rel1()
rel2, err := acquireSandboxBrowserSlot(t.Context(), key)
if err != nil {
t.Fatalf("slot 2: %v", err)
}
// Third concurrent browser for the same task must block until timeout/release.
cctx, cancel := context.WithTimeout(t.Context(), 40*time.Millisecond)
defer cancel()
if _, err := acquireSandboxBrowserSlot(cctx, key); err == nil {
t.Fatalf("expected 3rd slot to block and fail on context deadline")
}
// Releasing slot 2 immediately allows a new acquisition (idempotent double-release safe).
rel2()
rel2()
rel3, err := acquireSandboxBrowserSlot(t.Context(), key)
if err != nil {
t.Fatalf("slot after release: %v", err)
}
rel3()
}