package daemon import ( "context" "os" "path/filepath" "testing" ) func TestSharedIsolation_Setup(t *testing.T) { iso := SharedIsolation{} dir, err := iso.Setup(context.Background(), "s1", "/some/base") if err != nil { t.Fatal(err) } if dir != "/some/base" { t.Errorf("shared isolation should return baseDir unchanged, got %q", dir) } } func TestSharedIsolation_Teardown(t *testing.T) { iso := SharedIsolation{} if err := iso.Teardown(context.Background(), "s1"); err != nil { t.Errorf("shared teardown should be noop, got %v", err) } } func TestSharedIsolation_Name(t *testing.T) { iso := SharedIsolation{} if iso.Name() != "shared" { t.Error("expected name 'shared'") } } func TestIsolatedIsolation_Setup_CreatesDir(t *testing.T) { baseDir := t.TempDir() iso := &IsolatedIsolation{} workDir, err := iso.Setup(context.Background(), "session-abc", baseDir) if err != nil { t.Fatal(err) } // 工作目录应该存在 if _, statErr := os.Stat(workDir); os.IsNotExist(statErr) { t.Errorf("workdir %q should exist", workDir) } // 路径应该包含 session ID expected := filepath.Join(baseDir, "sessions", "session-abc", "workdir") if workDir != expected { t.Errorf("expected workdir %q, got %q", expected, workDir) } } func TestIsolatedIsolation_Setup_Idempotent(t *testing.T) { baseDir := t.TempDir() iso := &IsolatedIsolation{} // 两次 Setup 不应报错(MkdirAll 是幂等的) if _, err := iso.Setup(context.Background(), "s1", baseDir); err != nil { t.Fatal(err) } if _, err := iso.Setup(context.Background(), "s1", baseDir); err != nil { t.Fatal(err) } } func TestIsolatedIsolation_DifferentSessions_DifferentDirs(t *testing.T) { baseDir := t.TempDir() iso := &IsolatedIsolation{} dir1, _ := iso.Setup(context.Background(), "session-1", baseDir) dir2, _ := iso.Setup(context.Background(), "session-2", baseDir) if dir1 == dir2 { t.Error("different sessions should have different work directories") } } func TestIsolatedIsolation_Name(t *testing.T) { if (&IsolatedIsolation{}).Name() != "isolated" { t.Error("expected name 'isolated'") } } func TestNewIsolation_Factory(t *testing.T) { tests := []struct { mode IsolationMode want string }{ {IsolationShared, "shared"}, {IsolationIsolated, "isolated"}, {"unknown", "shared"}, // 未知模式默认 shared } for _, tt := range tests { iso := NewIsolation(tt.mode) if iso.Name() != tt.want { t.Errorf("NewIsolation(%q).Name() = %q, want %q", tt.mode, iso.Name(), tt.want) } } }