package server import ( "context" "encoding/json" "os" "path/filepath" "slices" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" "git.a-wan.net/alan/pipelines/internal/workenv" "git.a-wan.net/alan/pipelines/internal/workenv/workenvtest" ) func envValue(env []string, key string) (string, bool) { for i := len(env) - 1; i >= 0; i-- { if k, v, ok := strings.Cut(env[i], "="); ok && k == key { return v, true } } return "", false } // runInWorkspace runs the one-stage template in workspace and returns what the // stage saw in its context and the stored run. func runInWorkspace(t *testing.T, workspace string) (workenv.Environment, *protocol.RunDetail) { t.Helper() ctx, r, id := concurrentStore(t) r.dirEnv = &workenv.Resolver{Timeout: 10 * time.Second} var seen workenv.Environment r.execute = func(ctx context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) { seen, _ = workenv.FromContext(ctx) return stage.Result{Outcome: "success"}, nil } run, err := r.start(protocol.Message{PipelineID: id, Version: 1, RequestKey: "env-" + filepath.Base(workspace), Workspace: workspace, Input: map[string]any{"prompt": "task"}}) if err != nil { t.Fatal(err) } r.wg.Wait() detail, err := r.store.GetRun(ctx, run.ID) if err != nil { t.Fatal(err) } return seen, detail } func TestRunAppliesAndRecordsWorkspaceEnvironment(t *testing.T) { workenvtest.InstallDirenv(t) t.Setenv("ANTHROPIC_API_KEY", "server-key") project, _ := filepath.EvalSymlinks(t.TempDir()) workspace := filepath.Join(project, "repos", "worktrees", "item") if err := os.MkdirAll(workspace, 0o755); err != nil { t.Fatal(err) } secret := "value-that-must-not-be-recorded" workenvtest.WriteEnvrc(t, project, true, map[string]*string{"CLAUDE_CONFIG_DIR": workenvtest.Str(filepath.Join(project, ".claude-profile")), "SCRATCH_VAR": workenvtest.Str(secret)}) seen, detail := runInWorkspace(t, workspace) if v, _ := envValue(seen.Env, "SCRATCH_VAR"); v != secret { t.Fatal("stage context did not carry the workspace environment") } if _, ok := envValue(seen.Env, "ANTHROPIC_API_KEY"); ok { t.Fatal("inherited credential kept although the directory chooses the account") } recorded := detail.Environment if recorded == nil || recorded.Directory != workspace || recorded.ServiceKey == "" || !slices.Equal(recorded.Set, []string{"CLAUDE_CONFIG_DIR", "SCRATCH_VAR"}) || !slices.Contains(recorded.Dropped, "ANTHROPIC_API_KEY") || !slices.Equal(recorded.Files, []string{filepath.Join(project, ".envrc")}) || !slices.Equal(recorded.Sources, []string{"direnv"}) { t.Fatalf("recorded environment = %+v", recorded) } data, _ := json.Marshal(detail) if strings.Contains(string(data), secret) || strings.Contains(string(data), "server-key") { t.Fatal("run detail carries an environment value") } } func TestRunWarnsAboutBlockedWorkspaceEnvrc(t *testing.T) { workenvtest.InstallDirenv(t) project, _ := filepath.EvalSymlinks(t.TempDir()) workspace := filepath.Join(project, "item") os.Mkdir(workspace, 0o755) workenvtest.WriteEnvrc(t, project, false, map[string]*string{"SCRATCH_VAR": workenvtest.Str("x")}) seen, detail := runInWorkspace(t, workspace) if seen.Applies() { t.Fatal("blocked .envrc applied") } if detail.Status != "succeeded" { t.Fatalf("status %s", detail.Status) } found := false for _, warning := range detail.Warnings { found = found || (strings.Contains(warning, "Workspace environment") && strings.Contains(warning, filepath.Join(project, ".envrc")) && strings.Contains(warning, "direnv allow")) } if !found || detail.Environment == nil || len(detail.Environment.Warnings) != 1 { t.Fatalf("no blocked .envrc warning: %v", detail.Warnings) } } func TestDeliveryCheckSeesRunEnvironment(t *testing.T) { workenvtest.InstallDirenv(t) project := t.TempDir() workenvtest.WriteEnvrc(t, project, true, map[string]*string{"SCRATCH_VAR": workenvtest.Str("from-envrc")}) env := (&workenv.Resolver{Timeout: 10 * time.Second}).Environment(context.Background(), project, os.Environ()) ctx := workenv.WithEnvironment(context.Background(), env) candidate := t.TempDir() if err := runDeliveryCheck(ctx, candidate, `test "$SCRATCH_VAR" = from-envrc && test "$PWD" = "`+candidate+`"`); err != nil { t.Fatal(err) } if err := runDeliveryCheck(context.Background(), candidate, `test -z "$SCRATCH_VAR"`); err != nil { t.Fatal("a check without a run environment saw it:", err) } }