package server import ( "context" "encoding/json" "fmt" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" "io" "mime" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "sync/atomic" "syscall" "testing" ) func TestStageNotificationEventsAndArtifactAudit(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "notify.db")) if err != nil { t.Fatal(err) } defer store.Close() workspace := t.TempDir() if err := os.WriteFile(filepath.Join(workspace, "report.txt"), []byte("created file"), 0600); err != nil { t.Fatal(err) } var calls atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { count := calls.Add(1) filename, _ := new(mime.WordDecoder).DecodeHeader(r.Header.Get("Filename")) body, _ := io.ReadAll(r.Body) if filename == "report.txt" && string(body) != "created file" { t.Error("wrong file content") } if filename == "digest.md" && string(body) != "# Digest" { t.Error("wrong markdown") } ack := map[string]any{"id": fmt.Sprintf("msg%d", count), "event": "message"} if filename != "" { ack["attachment"] = map[string]string{"name": filename, "url": "https://ntfy.example.com/file/" + filename} } _ = json.NewEncoder(w).Encode(ack) })) defer srv.Close() t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/pipelines") if _, err := store.Submit(ctx, "notifications", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } items, _ := store.List(ctx) d, _ := store.Get(ctx, items[0].ID, 0) r, _ := newRunner(ctx, store) for i, test := range []struct { status, outcome, target, on string attachments bool expected int32 }{ {"completed", "success", protocol.SuccessEnd, "success", true, 2}, {"completed", "blocked", protocol.FailureEnd, "failure", false, 1}, {"failed", "error", protocol.FailureEnd, "failure", false, 1}, {"completed", "error", protocol.FailureEnd, "success", false, 0}, } { id, _, err := store.CreateRun(ctx, d, fmt.Sprintf("case%d", i), workspace, false, nil, map[string]any{"prompt": "task"}) if err != nil { t.Fatal(err) } if err := store.BeginStep(ctx, id, 1, "one", nil); err != nil { t.Fatal(err) } step := protocol.RunStep{Sequence: 1, Stage: "one", Status: test.status, Outcome: test.outcome, Target: test.target, Output: map[string]any{}} policy := &protocol.NotificationPolicy{On: []string{test.on}, Destination: "anime"} if test.attachments { policy.Artifacts = []string{"markdown", "files"} step.Output = map[string]any{"markdown": "# Digest", "artifacts": []any{"report.txt"}} } if err := store.FinishStep(ctx, id, step); err != nil { t.Fatal(err) } before := calls.Load() r.notifyStep(id, d, workspace, policy, step) detail, err := store.GetRun(ctx, id) if err != nil { t.Fatal(err) } if calls.Load()-before != test.expected || len(detail.Steps[0].Notifications) != int(test.expected) { t.Fatal("event/attachments mismatch", detail, calls.Load()-before) } for _, receipt := range detail.Steps[0].Notifications { if receipt.Status != "sent" || receipt.MessageID == "" { t.Fatal("notification receipt not persisted", receipt) } } } } func TestNotificationFailureDoesNotChangeRunRouting(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "routing.db")) if err != nil { t.Fatal(err) } defer store.Close() source := strings.Replace(pipeline.Template([]string{"one"}, true), "# stage one", "# stage one\n```notify\non=success\ndestination=anime\nartifacts=markdown\n```", 1) if _, err := store.Submit(ctx, "test", source); err != nil { t.Fatal(err) } items, _ := store.List(ctx) r, _ := newRunner(ctx, store) r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Result"}}, nil } t.Setenv("PIPELINES_NTFY_URL", "") run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "once", Input: map[string]any{"prompt": "task"}}) if err != nil { t.Fatal(err) } r.wg.Wait() detail, _ := store.GetRun(ctx, run.ID) if detail.Status != "succeeded" || len(detail.Steps[0].Notifications) != 1 || detail.Steps[0].Notifications[0].Status != "failed" { t.Fatal("notification altered routing or lost failure audit", detail) } } func TestNotificationArtifactPathSafety(t *testing.T) { root := t.TempDir() os.WriteFile(filepath.Join(root, "safe.txt"), []byte("safe"), 0600) os.WriteFile(filepath.Join(root, ".env"), []byte("secret"), 0600) os.Symlink(filepath.Join(root, ".env"), filepath.Join(root, "alias.txt")) outside := filepath.Join(t.TempDir(), "outside.txt") os.WriteFile(outside, []byte("secret"), 0600) os.Symlink(outside, filepath.Join(root, "external.txt")) os.Mkdir(filepath.Join(root, "directory"), 0700) os.WriteFile(filepath.Join(root, "large.bin"), []byte(strings.Repeat("x", (1<<20)+1)), 0600) if err := syscall.Mkfifo(filepath.Join(root, "pipe"), 0600); err != nil { t.Fatal(err) } for _, name := range []string{"../outside.txt", outside, ".env", "alias.txt", "external.txt", "directory", "large.bin", "pipe", "pipelines.db", "secret.key", "secrets/file.txt", "sub/../safe.txt"} { if _, err := notificationArtifact(root, name); err == nil { t.Fatal("unsafe artifact accepted", name) } } if data, err := notificationArtifact(root, "safe.txt"); err != nil || string(data) != "safe" { t.Fatal("safe artifact rejected", err) } }