package server import ( "context" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "io" "net/http" "net/http/httptest" "path/filepath" "testing" ) func TestRunnerNtfyDeliveryIsAuditedAndIdempotent(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "ntfy.db")) if err != nil { t.Fatal(err) } defer store.Close() calls := 0 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls++ body, _ := io.ReadAll(r.Body) if string(body) != "# Anime\nA report." { t.Error("wrong markdown", string(body)) } _, _ = w.Write([]byte(`{"id":"delivery123","event":"message","attachment":{"name":"digest.md","url":"https://ntfy.example.com/file/digest.md"}}`)) })) defer srv.Close() t.Setenv("PIPELINES_NTFY_ANIME_URL", srv.URL+"/anime") source := "# stage notify\n```configs\ntype=ntfy\ndestination=anime\n```\n```edges\nsuccess -> #success\nerror -> #failure\n```\n" if _, err := store.Submit(ctx, "notify", source); err != nil { t.Fatal(err) } items, _ := store.List(ctx) r, _ := newRunner(ctx, store) for i, approved := range []bool{false, true} { key := []string{"notify", "approved-notify"}[i] request := protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: key, AllowTools: approved, Input: map[string]any{"prompt": "notify", "title": "Anime", "summary": "Your digest is ready", "markdown": "# Anime\nA report."}} run, err := r.start(request) if err != nil { t.Fatal(err) } r.wg.Wait() detail, _ := store.GetRun(ctx, run.ID) if detail.Status != "succeeded" || len(detail.Steps) != 1 || detail.Steps[0].Output["delivery"] == nil || detail.Steps[0].SessionID != "" { t.Fatal("native delivery not audited", detail) } if duplicate, err := r.start(request); err != nil || duplicate.ID != run.ID { t.Fatal("repeat not idempotent", err) } } if calls != 2 { t.Fatal("duplicate publication", calls) } t.Setenv("PIPELINES_NTFY_ANIME_URL", "") failed, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "failure", Input: map[string]any{"prompt": "notify", "title": "Anime", "summary": "Ready", "markdown": "# report"}}) if err != nil { t.Fatal(err) } r.wg.Wait() detail, _ := store.GetRun(ctx, failed.ID) if detail.Status != "failed" || detail.Steps[0].Outcome != "error" { t.Fatal("delivery failure not routed", detail) } }