package server import ( "context" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" "os" "path/filepath" "strings" "testing" ) func TestClientSelectedRepositoryWorkspaceAndAudit(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "workspace.db")) if err != nil { t.Fatal(err) } defer store.Close() root := t.TempDir() a := filepath.Join(root, "repo-a") b := filepath.Join(root, "repo-b") os.Mkdir(a, 0700) os.Mkdir(b, 0700) outside := t.TempDir() if _, err := store.Submit(ctx, "test", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } items, _ := store.List(ctx) r, err := newRunner(ctx, store) if err != nil { t.Fatal(err) } r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { return stage.Result{Outcome: "success", Output: stage.Values{"done": true}}, nil } os.Symlink(outside, filepath.Join(root, "linked-repo")) for i, path := range []string{a, b, outside, filepath.Join(root, "linked-repo")} { detail, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: path, Input: map[string]any{"prompt": "task"}, Workspace: path}) if err != nil { t.Fatal(err) } r.wg.Wait() detail, err = store.GetRun(ctx, detail.ID) if err != nil { t.Fatal(err) } expected, _ := filepath.EvalSymlinks(path) if detail.Workspace != expected || detail.Status != "succeeded" { t.Fatal("incorrect workspace audit", i, detail) } } file := filepath.Join(root, "file") if err := os.WriteFile(file, []byte("not a directory"), 0600); err != nil { t.Fatal(err) } for _, path := range []string{filepath.Join(root, "missing"), file, "relative/repo"} { if _, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: path, Input: map[string]any{"prompt": "task"}, Workspace: path}); err == nil { t.Fatal("invalid run directory accepted", path) } } missing := filepath.Join(root, "missing") if _, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "missing-diagnostic", Input: map[string]any{"prompt": "task"}, Workspace: missing}); err == nil || !strings.Contains(err.Error(), missing) || !strings.Contains(err.Error(), "mount this host directory") { t.Fatal("missing workspace error hid requested path or Docker remedy", err) } }