package server import ( "context" "fmt" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "os" "path/filepath" "strings" "testing" "time" ) // A stage hook's `link=` file decides the ntfy and app tap target when its // first line is an http(s) URL, keeping the run one action away; anything else // falls back to the run link and says why on the receipt. func TestStageHookLinkClick(t *testing.T) { t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com") captured := newNtfyCapture(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() store := newOperatorStore(t) if _, err := store.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyNtfyEnabled: true}); err != nil { t.Fatal(err) } if _, err := store.Submit(ctx, "standup", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } items, _ := store.List(ctx) d, _ := store.Get(ctx, items[0].ID, 0) artifacts := t.TempDir() r := &runner{ctx: ctx, store: store, notes: newAppNotifier(store), artifactsRoot: artifacts} const doc = "https://claude.ai/public/artifacts/0f3c-standup" for _, test := range []struct { name, link, open string files map[string]string click bool // the doc URL is the tap target artifact string // the attached artifact path, by today's open= rules diagnostic string }{ {name: "valid", link: "standup-link.txt", files: map[string]string{"standup-link.txt": "\n " + doc + " \r\nnotes\n", "notes.txt": "n"}, click: true}, {name: "valid-only-file", link: "standup-link.txt", files: map[string]string{"standup-link.txt": doc}, click: true, artifact: "standup-link.txt"}, {name: "missing", link: "standup-link.txt", diagnostic: "notify link: no public file matches standup-link.txt"}, {name: "missing-other-file", link: "standup-link.txt", files: map[string]string{"report.md": "# Report"}, artifact: "report.md", diagnostic: "notify link: no public file matches standup-link.txt"}, {name: "relative", link: "standup-link.txt", files: map[string]string{"standup-link.txt": "standup.html\n" + doc}, artifact: "standup-link.txt", diagnostic: "notify link: public/standup-link.txt does not start with an absolute http(s) URL"}, {name: "scheme", link: "standup-link.txt", files: map[string]string{"standup-link.txt": "javascript:alert(1)"}, artifact: "standup-link.txt", diagnostic: "does not start with an absolute http(s) URL"}, {name: "fragment", link: "standup-link.txt", files: map[string]string{"standup-link.txt": doc + "#top"}, artifact: "standup-link.txt", diagnostic: "does not start with an absolute http(s) URL"}, {name: "empty", link: "standup-link.txt", files: map[string]string{"standup-link.txt": " \n\n"}, artifact: "standup-link.txt", diagnostic: "notify link: public/standup-link.txt is empty"}, {name: "large", link: "standup-link.txt", files: map[string]string{"standup-link.txt": doc + "\n" + strings.Repeat("x", 4<<10)}, artifact: "standup-link.txt", diagnostic: "at most 4 KiB"}, {name: "absent", files: map[string]string{"standup-link.txt": doc, "notes.txt": "n"}}, {name: "absent-only-file", files: map[string]string{"standup-link.txt": doc}, artifact: "standup-link.txt"}, {name: "with-open", link: "*-link.txt", open: "*.md", files: map[string]string{"standup-link.txt": doc, "standup.md": "# Standup", "notes.txt": "n"}, click: true, artifact: "standup.md"}, } { t.Run(test.name, func(t *testing.T) { id, _, err := store.CreateRun(ctx, d, test.name, t.TempDir(), false, nil, map[string]any{"prompt": "task"}) if err != nil { t.Fatal(err) } public := filepath.Join(artifacts, fmt.Sprint(id), "public") if err := os.MkdirAll(public, 0700); err != nil { t.Fatal(err) } for name, content := range test.files { if err := os.WriteFile(filepath.Join(public, name), []byte(content), 0600); 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: "completed", Outcome: "success", Target: "two", Output: map[string]any{}} if err := store.FinishStep(ctx, id, step); err != nil { t.Fatal(err) } before := len(captured()) policy := &protocol.NotificationPolicy{On: []string{"success"}, Destination: "system", Link: test.link, Open: test.open} r.notifyStep(id, d.Name, t.TempDir(), policy, step) // Without a usable link everything is as before: ntfy taps open the // run, the app opens the attached artifact (else the run). runClick := fmt.Sprintf("pipelines://run/%d", id) wantNtfy, wantClick, wantRunClick := runClick, runClick, "" if test.artifact != "" { wantClick, wantRunClick = fmt.Sprintf("pipelines://artifact/%d/1/%s", id, test.artifact), runClick } if test.click { wantNtfy, wantClick, wantRunClick = doc, doc, runClick } got := captured() if len(got) != before+1 { t.Fatalf("ntfy publications = %+v", got[before:]) } if got[before].Click != wantNtfy || got[before].Actions != fmt.Sprintf("view, Open in browser, https://pipelines.example.com/runs/%d", id) { t.Fatalf("ntfy click/actions = %q %q, want %q", got[before].Click, got[before].Actions, wantNtfy) } active, _, err := store.ActiveAppNotifications(ctx, "", time.Now()) if err != nil { t.Fatal(err) } var n *protocol.AppNotification for i := range active { if active[i].ID == fmt.Sprintf("run:%d:1:success", id) { n = &active[i] } } if n == nil || n.Click != wantClick || n.RunClick != wantRunClick { t.Fatalf("app notification = %+v, want click %q run %q", n, wantClick, wantRunClick) } if (n.Artifact == nil) != (test.artifact == "") || (n.Artifact != nil && n.Artifact.Path != test.artifact) { t.Fatalf("app artifact = %+v, want %q", n.Artifact, test.artifact) } detail, err := store.GetRun(ctx, id) if err != nil { t.Fatal(err) } receipts := detail.Steps[0].Notifications if len(receipts) != 1 || receipts[0].Status != "sent" || !strings.Contains(receipts[0].Diagnostic, test.diagnostic) || (test.diagnostic == "") != (receipts[0].Diagnostic == "") { t.Fatalf("receipts = %+v, want diagnostic %q", receipts, test.diagnostic) } }) } }