diff --git a/cmd/archie-ui/main_test.go b/cmd/archie-ui/main_test.go index 317eb10a0..d0a49743e 100644 --- a/cmd/archie-ui/main_test.go +++ b/cmd/archie-ui/main_test.go @@ -33,6 +33,7 @@ import ( "google.golang.org/grpc" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/domain/messaging" "github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc" @@ -82,15 +83,32 @@ func (f *fakeGateway) Route(context.Context, messaging.Inbound) (messaging.ChatR } // Stream emits the contract's documented order: started, a delta, then done. +// +// A turn that used a tool emits its tool event between the delta and the done, +// which is where the Gateway's own stream reports completed tool activity. +// Whether the dashboard renders it is the operator's setting, carried to this +// process only by the published configuration projection (GitHub #821). func (f *fakeGateway) Stream(ctx context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) { - events := make(chan messaging.ChatEvent, 3) + events := make(chan messaging.ChatEvent, 4) go func() { defer close(events) - for _, event := range []messaging.ChatEvent{ + turn := []messaging.ChatEvent{ {Kind: "started", SessionID: "s1"}, {Kind: "delta", Text: "hello " + in.Message.Text, SessionID: "s1"}, {Kind: "done", SessionID: "s1"}, - } { + } + if in.Message.Text == toolCallPrompt { + turn = []messaging.ChatEvent{ + {Kind: "started", SessionID: "s1"}, + {Kind: "tool", SessionID: "s1", Tool: messaging.ToolCallEvent{ + ID: "call-1", Name: "read_file", Parameters: `{"path":"main.go"}`, + Output: "package main", + }}, + {Kind: "delta", Text: "read it", SessionID: "s1"}, + {Kind: "done", SessionID: "s1"}, + } + } + for _, event := range turn { select { case events <- event: case <-ctx.Done(): @@ -101,6 +119,10 @@ func (f *fakeGateway) Stream(ctx context.Context, in messaging.Inbound) (<-chan return events, nil } +// toolCallPrompt is the message this suite sends when it wants the fake +// Gateway to report a completed tool call in the turn. +const toolCallPrompt = "read main.go" + func (f *fakeGateway) Cancel(context.Context, string) (messaging.ChatCancellation, error) { return messaging.ChatCancellation{Cancelled: true}, nil } @@ -403,9 +425,63 @@ func (d dashboard) get(path string) reply { return d.do(d.request(http.MethodGet, path, "", true)) } +// chatFrame is one `data: {...}` frame of the chat stream, decoded into the +// fields this suite reads. The UI process only ever receives these over HTTP, +// so asserting on them is asserting on what the browser is handed. +type chatFrame struct { + Type string `json:"type"` + Text string `json:"text"` + Tool string `json:"tool"` +} + +// streamChat runs one chat turn over the real HTTP stream and returns every +// frame up to and including the terminating done. +func streamChat(t *testing.T, d dashboard, prompt string) []chatFrame { + t.Helper() + body, err := json.Marshal(map[string]string{"text": prompt, "channel_id": "dash"}) + if err != nil { + t.Fatalf("encode the chat turn: %v", err) + } + resp, err := http.DefaultClient.Do(d.request(http.MethodPost, "/api/chat/stream", string(body), true)) + if err != nil { + t.Fatalf("POST /api/chat/stream: %v", err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Fatalf("POST /api/chat/stream = %d, want 200", resp.StatusCode) + } + var frames []chatFrame + scanner := bufio.NewScanner(resp.Body) + for scanner.Scan() { + payload, ok := strings.CutPrefix(scanner.Text(), "data: ") + if !ok { + continue + } + var frame chatFrame + if err := json.Unmarshal([]byte(payload), &frame); err != nil { + t.Fatalf("decode stream frame %q: %v", payload, err) + } + frames = append(frames, frame) + if frame.Type == "done" { + break + } + } + if err := scanner.Err(); err != nil { + t.Fatalf("read the chat stream: %v", err) + } + return frames +} + // seedStore fills a real task database and publishes the configuration // projection, so everything the dashboard renders arrives over the wire from // a store this process never opens. +// +// The document is rendered by webui.BuildConfigView -- the same function the +// daemon calls -- from a config.Config rather than hand-written as a +// ConfigView. A suite that hand-writes the document cannot catch a producer +// that forgets to publish a field, which is the failure mode this covers: +// chat.show_tool_calls, the operator name and the channel flag all reach the +// extracted process through this projection and nowhere else (GitHub #821). func seedStore(t *testing.T, dir string) *store.Store { t.Helper() st, err := store.Open(t.Context(), filepath.Join(dir, "tasks.sqlite")) @@ -427,28 +503,40 @@ func seedStore(t *testing.T, dir string) *store.Store { t.Fatalf("record PR number: %v", err) } - published, err := json.Marshal(webui.ConfigView{ - Identity: webui.IdentityView{ - BotUser: "archie-bot", - ForgeType: "github", - ForgeHost: "https://github.example.com", + publishConfig(t, st, daemonConfig(true)) + return st +} + +// daemonConfig is the configuration the daemon would hold for this smoke run. +// showToolCalls is the setting the chat page's expansion is gated on. +func daemonConfig(showToolCalls bool) config.Config { + return config.Config{ + BotUser: "archie-bot", + Forge: config.Forge{Type: "github", Host: "https://github.example.com"}, + Repos: []config.Repo{{Owner: "acme", Name: "widget", Base: "main"}}, + Chat: config.ChatConfig{ + ShowToolCalls: showToolCalls, + Operator: "Sam", + WebhookAddr: "127.0.0.1:9099", }, - Repositories: []webui.RepoView{{Owner: "acme", Name: "widget", Base: "main"}}, - Chat: webui.ChatView{ - ShowToolCalls: true, - Operator: "Sam", - ChannelConfigured: true, - }, - }) + } +} + +// publishConfig renders cfg the way the daemon's configuration owner does and +// writes the result to the store the UI process reads it back from. The UI +// process holds no configuration, so this is the only path by which a setting +// reaches its pages. +func publishConfig(t *testing.T, st *store.Store, cfg config.Config) { + t.Helper() + document, err := json.Marshal(webui.BuildConfigView(webui.ConfigViewInput{Config: cfg})) if err != nil { - t.Fatal(err) + t.Fatalf("render the configuration projection: %v", err) } if err := st.PutConfigSnapshot(t.Context(), store.ConfigSnapshot{ - Schema: webui.ConfigViewSchema, Document: published, + Schema: webui.ConfigViewSchema, Document: document, }); err != nil { t.Fatalf("publish config snapshot: %v", err) } - return st } // TestUIProcessLinksMultiIdentityTaskRowsToTheirOwningForge drives the real @@ -706,6 +794,48 @@ func TestUIProcessServesTheDashboardAgainstLiveDependencies(t *testing.T) { } }) + t.Run("the daemon's show_tool_calls setting decides whether tool calls reach the chat page", func(t *testing.T) { + // The chat page expands a completed tool call only when the frame + // arrives, and this process has no [chat] section of its own: the + // setting reaches it through the published projection alone. A turn + // streamed with the setting off must carry no tool frame, and one + // streamed with it on must carry the tool the Gateway reported + // (GitHub #821). + // + // Each case republishes before streaming, the way a config reload + // does; the default is restored afterwards so a later subtest does + // not inherit whichever case ran last. + tests := []struct { + name string + showTools bool + wantFrame bool + }{ + {name: "off", showTools: false, wantFrame: false}, + {name: "on", showTools: true, wantFrame: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + publishConfig(t, st, daemonConfig(tc.showTools)) + + frames := streamChat(t, d, toolCallPrompt) + var sawTool bool + for _, frame := range frames { + if frame.Tool == "read_file" { + sawTool = true + } + if frame.Type == "done" { + break + } + } + if sawTool != tc.wantFrame { + t.Errorf("show_tool_calls=%v produced tool frame = %v, want %v; frames = %+v", + tc.showTools, sawTool, tc.wantFrame, frames) + } + }) + } + publishConfig(t, st, daemonConfig(true)) + }) + t.Run("an operator task action crosses to the Gateway", func(t *testing.T) { listed := d.get("/api/tasks") var rows []struct { diff --git a/docs/prds/ui-service-boundary.md b/docs/prds/ui-service-boundary.md index 069b417ea..3cda49dcb 100644 --- a/docs/prds/ui-service-boundary.md +++ b/docs/prds/ui-service-boundary.md @@ -294,8 +294,27 @@ and the `/api/setup` checklist (the panel was omitted rather than rendered). Both are closed by `archie-core-ml30`: `ConfigView` gained a `chat` section carrying `show_tool_calls`, the operator name, and whether any chat channel is configured, and both surfaces now read the projection rather than a live -holder. The end-to-end suite asserts the checklist renders from the published -snapshot. +holder. The end-to-end suite drives the real binary and streams a turn, so the +`show_tool_calls` assertion is made on the frames a browser receives. + +The checklist stays. The question raised when it was republished -- is a +read-only setup report wanted in a process that cannot apply the setup it +reports? -- resolves in favour of keeping it, because the panel never promised +to apply anything. It renders each step as a title, a detail and a done mark +with no control behind it, so it answers "what is this deployment missing?" +rather than "fix it here", and the page that CAN act on it (the Configuration +page) is where an operator goes next, in the daemon's own words. Removing it +would delete the one surface that distinguishes a fresh deployment from a +finished one for no gain, since the values it reads already cross the boundary +in `ConfigView` and the read path costs the daemon nothing. The write path +remains descoped (see `/api/config` (write) in the route table): the checklist +reports configuration, it does not change it. + +`archie-core-ml30` published the channel flag from two of the three chat +front-ends, so an email-only deployment read as configured on `/api/channels` +and unconfigured on the checklist at the same time (GitHub #821). Both now +derive from `config.ChatConfig.FrontEnds`, the single definition of a configured +front-end, and a test pins the two surfaces to the same answer. A third value degraded for the same reason and is closed by `archie-core-pv6t`: in a deployment configuring `[[identities]]` the projection diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index b21a379fa..1dc56ff38 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -384,6 +384,35 @@ func (b *boot) loadCatalog(ctx context.Context, cfgPath string) { b.log.Info("model catalog loaded", "providers", len(catalog.Providers), "models", len(b.catalogModels)) } +// channelDescriptors projects the configured chat front-ends into the +// dashboard's channel-status descriptors. +// +// Configured comes from config.ChatConfig.FrontEnds so this list and the +// configuration projection published for the extracted UI process cannot +// disagree about whether a front-end is set up (GitHub #821). ReloadSupported +// and Detail stay here because they are channel-lifecycle facts about the bot +// the daemon runs, not configuration: only Telegram can be reloaded in place +// today, and its allowlist caveat is about who the running bot answers. +func channelDescriptors(chat config.ChatConfig) []status.Descriptor { + frontEnds := chat.FrontEnds() + descriptors := make([]status.Descriptor, 0, len(frontEnds)) + for _, frontEnd := range frontEnds { + descriptor := status.Descriptor{ + ID: frontEnd.ID, + Name: frontEnd.Name, + Configured: frontEnd.Configured, + } + if frontEnd.ID == "telegram" { + descriptor.ReloadSupported = frontEnd.Configured + if frontEnd.Configured && len(chat.Telegram.AllowedUserIDs) == 0 { + descriptor.Detail = "Token set, but the allowlist is empty -- the bot answers nobody." + } + } + descriptors = append(descriptors, descriptor) + } + return descriptors +} + // setupObservability builds the event bus, channel manager and dashboard // server. Every event is logged to SQLite (stamped with its row id) and // then fanned out to live dashboard connections. @@ -392,15 +421,7 @@ func (b *boot) setupObservability(ctx context.Context) { bus := events.NewBus() b.bus = bus b.addCleanup(func() { bus.Close() }) - telegramDetail := "" - if (cfg.Chat.Telegram.Token != (secret.SecretRef{}) || cfg.Chat.Telegram.TokenEnv != "") && len(cfg.Chat.Telegram.AllowedUserIDs) == 0 { - telegramDetail = "Token set, but the allowlist is empty -- the bot answers nobody." - } - b.channelManager = status.NewManager([]status.Descriptor{ - {ID: "telegram", Name: "Telegram", Configured: cfg.Chat.Telegram.Token != (secret.SecretRef{}) || cfg.Chat.Telegram.TokenEnv != "", ReloadSupported: cfg.Chat.Telegram.Token != (secret.SecretRef{}) || cfg.Chat.Telegram.TokenEnv != "", Detail: telegramDetail}, - {ID: "email", Name: "Email", Configured: cfg.Chat.Email.ListenAddr != ""}, - {ID: "webhook", Name: "Webhook gateway", Configured: cfg.Chat.WebhookAddr != ""}, - }) + b.channelManager = status.NewManager(channelDescriptors(cfg.Chat)) b.cfgHolder = config.NewHolder(cfg) b.web = &webui.Server{Log: log.With("component", "webui"), Channels: b.channelManager, Skills: skillCatalogAdapter{b.cfgHolder}} // The watchdog leaves its verdict in a file on this host, so the daemon diff --git a/internal/app/archied/config_snapshot_channels_test.go b/internal/app/archied/config_snapshot_channels_test.go new file mode 100644 index 000000000..ee998a898 --- /dev/null +++ b/internal/app/archied/config_snapshot_channels_test.go @@ -0,0 +1,92 @@ +package archied + +import ( + "encoding/json" + "log/slog" + "path/filepath" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/store" + "github.com/samcharles93/archie-core/internal/webui" +) + +// TestPublishedProjectionAgreesWithTheChannelManagerOnConfiguredChannels: the +// daemon answers "is a chat channel configured" twice -- once through the +// channel status manager it exposes on /api/channels, and once through the +// ChatView.ChannelConfigured boolean it publishes for the dashboard's setup +// checklist. Those two answers are read by different pages of the same +// extracted process, so a deployment that the manager calls configured must +// not be told by the checklist to connect a channel it already has. Both sides +// now derive from one definition (config.ChatConfig.FrontEnds); this test is +// what keeps them from being re-decided separately. +func TestPublishedProjectionAgreesWithTheChannelManagerOnConfiguredChannels(t *testing.T) { + tests := []struct { + name string + cfg config.Config + }{ + {name: "nothing configured", cfg: config.Config{}}, + { + name: "telegram token", + cfg: config.Config{Chat: config.ChatConfig{ + Telegram: config.TelegramConfig{TokenEnv: "TELEGRAM_TOKEN"}, + }}, + }, + { + name: "telegram token through the secret engine", + cfg: config.Config{Chat: config.ChatConfig{ + Telegram: config.TelegramConfig{Token: config.SecretRef{Engine: "builtin", Key: "telegram"}}, + }}, + }, + { + name: "webhook gateway", + cfg: config.Config{Chat: config.ChatConfig{WebhookAddr: "127.0.0.1:9099"}}, + }, + { + name: "inbound mail gateway", + cfg: config.Config{Chat: config.ChatConfig{Email: config.EmailConfig{ListenAddr: "127.0.0.1:2525"}}}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "state.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = st.Close() }) + + b := &boot{ + cfg: tc.cfg, + log: slog.New(slog.DiscardHandler), + stateStore: st, + cfgHolder: config.NewHolder(tc.cfg), + } + b.setupObservability(t.Context()) + + // What the dashboard's /api/channels page reads. + managerSaysConfigured := false + for _, descriptor := range b.channelManager.Snapshot() { + if descriptor.Configured { + managerSaysConfigured = true + } + } + + // What the dashboard's setup checklist reads, through the + // same round trip a reading process performs. + document, err := json.Marshal(webui.BuildConfigView(b.configViewInput(t.Context()))) + if err != nil { + t.Fatalf("render the published projection: %v", err) + } + var published webui.ConfigView + if err := json.Unmarshal(document, &published); err != nil { + t.Fatalf("decode the published projection: %v", err) + } + + if published.Chat.ChannelConfigured != managerSaysConfigured { + t.Errorf("published Chat.ChannelConfigured = %v, but the channel manager reports a configured front-end = %v (%+v)", + published.Chat.ChannelConfigured, managerSaysConfigured, published.Chat) + } + }) + } +} diff --git a/internal/config/chat_frontends.go b/internal/config/chat_frontends.go new file mode 100644 index 000000000..a3d4a7f1b --- /dev/null +++ b/internal/config/chat_frontends.go @@ -0,0 +1,62 @@ +package config + +import "strings" + +// ChatFrontEnd is one conversational front-end [chat] can enable. +// +// It carries the answer to "is this front-end configured", and nothing else: +// which process starts it, whether it can be reloaded, and what an operator +// reads about it are channel-lifecycle facts that belong to the daemon, not to +// configuration. ID is the stable identifier every surface keys on (the +// dashboard's channel list, the channel reload route). +type ChatFrontEnd struct { + ID string + Name string + Configured bool +} + +// FrontEnds reports every conversational front-end this configuration enables, +// in the order the dashboard lists them. +// +// This is the single definition of "a chat channel is configured". It exists +// because that question used to be answered twice from the same config and the +// two answers drifted: the daemon's channel status manager counted the inbound +// mail gateway, and the dashboard projection published for the extracted UI +// process did not, so an email-only deployment read as configured on +// /api/channels and unconfigured on the setup checklist at the same time +// (GitHub #821). Front-ends are enumerated once here so a front-end added to +// [chat] cannot be counted by one surface and missed by the other. +// +// A whitespace-only value is not configured on any front-end: an env var name +// of " " resolves to nothing, and an address of " " cannot be listened on. +func (c ChatConfig) FrontEnds() []ChatFrontEnd { + return []ChatFrontEnd{ + { + ID: "telegram", + Name: "Telegram", + Configured: c.Telegram.Token != (SecretRef{}) || strings.TrimSpace(c.Telegram.TokenEnv) != "", + }, + { + ID: "email", + Name: "Email", + Configured: strings.TrimSpace(c.Email.ListenAddr) != "", + }, + { + ID: "webhook", + Name: "Webhook gateway", + Configured: strings.TrimSpace(c.WebhookAddr) != "", + }, + } +} + +// AnyFrontEndConfigured reports whether at least one conversational front-end +// has credentials. The setup checklist asks this as a yes/no question; every +// other consumer wants the per-front-end detail FrontEnds returns. +func (c ChatConfig) AnyFrontEndConfigured() bool { + for _, frontEnd := range c.FrontEnds() { + if frontEnd.Configured { + return true + } + } + return false +} diff --git a/internal/config/chat_frontends_test.go b/internal/config/chat_frontends_test.go new file mode 100644 index 000000000..382e9f426 --- /dev/null +++ b/internal/config/chat_frontends_test.go @@ -0,0 +1,93 @@ +package config + +import "testing" + +// TestChatConfigFrontEnds is the single definition of "a chat front-end is +// configured". Two surfaces read it -- the daemon's channel status manager and +// the configuration projection published for the extracted UI process -- so a +// front-end counted by one and missed by the other is a dashboard that +// contradicts itself (GitHub #821). +func TestChatConfigFrontEnds(t *testing.T) { + tests := []struct { + name string + chat ChatConfig + want map[string]bool + }{ + { + name: "nothing configured", + chat: ChatConfig{}, + want: map[string]bool{"telegram": false, "email": false, "webhook": false}, + }, + { + name: "telegram bot token from the environment", + chat: ChatConfig{Telegram: TelegramConfig{TokenEnv: "TELEGRAM_TOKEN"}}, + want: map[string]bool{"telegram": true, "email": false, "webhook": false}, + }, + { + name: "telegram bot token through the secret engine", + chat: ChatConfig{Telegram: TelegramConfig{Token: SecretRef{Engine: "builtin", Key: "telegram"}}}, + want: map[string]bool{"telegram": true, "email": false, "webhook": false}, + }, + { + name: "inbound mail gateway", + chat: ChatConfig{Email: EmailConfig{ListenAddr: "127.0.0.1:2525"}}, + want: map[string]bool{"telegram": false, "email": true, "webhook": false}, + }, + { + name: "webhook gateway", + chat: ChatConfig{WebhookAddr: "127.0.0.1:9099"}, + want: map[string]bool{"telegram": false, "email": false, "webhook": true}, + }, + { + name: "every front-end at once", + chat: ChatConfig{ + Telegram: TelegramConfig{TokenEnv: "TELEGRAM_TOKEN"}, + Email: EmailConfig{ListenAddr: "127.0.0.1:2525"}, + WebhookAddr: "127.0.0.1:9099", + }, + want: map[string]bool{"telegram": true, "email": true, "webhook": true}, + }, + { + // Whitespace is not a value: an env var name of " " resolves + // to nothing and " " cannot be listened on. + name: "whitespace-only values are not configured", + chat: ChatConfig{ + Telegram: TelegramConfig{TokenEnv: " "}, + Email: EmailConfig{ListenAddr: " "}, + WebhookAddr: "\t", + }, + want: map[string]bool{"telegram": false, "email": false, "webhook": false}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got := map[string]bool{} + for _, frontEnd := range tc.chat.FrontEnds() { + if frontEnd.ID == "" || frontEnd.Name == "" { + t.Errorf("front-end %+v is missing an identifier or a name", frontEnd) + } + if _, seen := got[frontEnd.ID]; seen { + t.Errorf("front-end %q listed twice", frontEnd.ID) + } + got[frontEnd.ID] = frontEnd.Configured + } + if len(got) != len(tc.want) { + t.Fatalf("FrontEnds() = %+v, want one entry per front-end %+v", got, tc.want) + } + for id, want := range tc.want { + if got[id] != want { + t.Errorf("front-end %q configured = %v, want %v", id, got[id], want) + } + } + + var anyConfigured bool + for _, configured := range got { + anyConfigured = anyConfigured || configured + } + if got, want := tc.chat.AnyFrontEndConfigured(), anyConfigured; got != want { + t.Errorf("AnyFrontEndConfigured() = %v, want %v", got, want) + } + }) + } +} diff --git a/internal/webui/api_config.go b/internal/webui/api_config.go index f9156e960..c99cff504 100644 --- a/internal/webui/api_config.go +++ b/internal/webui/api_config.go @@ -419,13 +419,15 @@ func BuildConfigView(in ConfigViewInput) ConfigView { return view } -// chatChannelConfigured reports whether any conversational front-end has -// credentials. The setup checklist needs the answer, not the tokens, so the +// chatChannelConfigured reports whether any conversational front-end is +// configured. The setup checklist needs the answer, not the tokens, so the // projection carries the boolean. +// +// The definition lives in config.ChatConfig.FrontEnds, not here: the daemon's +// channel status manager answers the same question on /api/channels, and +// deciding it twice is how the two answers drifted apart (GitHub #821). func chatChannelConfigured(chat config.ChatConfig) bool { - return chat.Telegram.Token != (config.SecretRef{}) || - strings.TrimSpace(chat.Telegram.TokenEnv) != "" || - strings.TrimSpace(chat.WebhookAddr) != "" + return chat.AnyFrontEndConfigured() } // RemoteConfigView reads the projection the configuration owner published. diff --git a/internal/webui/api_setup_test.go b/internal/webui/api_setup_test.go index 5af74d3cb..fe6ed83fb 100644 --- a/internal/webui/api_setup_test.go +++ b/internal/webui/api_setup_test.go @@ -95,6 +95,101 @@ func TestSetupChecklistRendersFromTheProjection(t *testing.T) { } } +// TestSetupChecklistChatChannelStepCountsEveryFrontEnd: the checklist's +// "Connect a chat channel" step asks whether ANY conversational front-end is +// configured, so the projection it reads has to answer that question for every +// front-end the daemon serves. The daemon's own channel inventory lists three +// (telegram, the webhook gateway and the inbound mail gateway -- see +// bootstrap.go's status.NewManager), and an email-only deployment is a +// configured deployment: the mail gateway feeds the same gateway.Router as the +// other two. Deriving the answer from two of the three told an email operator +// to connect a channel they already had. +func TestSetupChecklistChatChannelStepCountsEveryFrontEnd(t *testing.T) { + const stepTitle = "Connect a chat channel" + + tests := []struct { + name string + cfg config.Config + want bool + }{ + { + name: "nothing configured", + cfg: config.Config{}, + want: false, + }, + { + name: "telegram token env", + cfg: config.Config{Chat: config.ChatConfig{ + Telegram: config.TelegramConfig{TokenEnv: "TELEGRAM_TOKEN"}, + }}, + want: true, + }, + { + name: "telegram token through the secret engine", + cfg: config.Config{Chat: config.ChatConfig{ + Telegram: config.TelegramConfig{Token: config.SecretRef{Engine: "builtin", Key: "telegram"}}, + }}, + want: true, + }, + { + name: "webhook gateway", + cfg: config.Config{Chat: config.ChatConfig{ + WebhookAddr: "127.0.0.1:9099", + }}, + want: true, + }, + { + name: "inbound mail gateway", + cfg: config.Config{Chat: config.ChatConfig{ + Email: config.EmailConfig{ListenAddr: "127.0.0.1:2525"}, + }}, + want: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + srv := newTestServer(t) + srv.ConfigSource = publishedSource(t, tc.cfg) + + got := getSetup(t, srv) + found := false + for _, step := range got.Steps { + if step.Title != stepTitle { + continue + } + found = true + if step.Done != tc.want { + t.Errorf("step %q done = %v, want %v (config: %+v)", stepTitle, step.Done, tc.want, tc.cfg.Chat) + } + } + if !found { + t.Fatalf("no %q step in %+v", stepTitle, got.Steps) + } + }) + } +} + +// publishedSource renders cfg the way the daemon does -- through +// webui.BuildConfigView -- and hands the server the marshalled document, which +// is how the reading process actually receives it. Rendering here rather than +// hand-writing a ConfigView keeps these tests honest about the producer: a +// field BuildConfigView forgets is one the dashboard cannot see. +func publishedSource(t *testing.T, cfg config.Config) ConfigViewSource { + t.Helper() + document, err := json.Marshal(BuildConfigView(ConfigViewInput{Config: cfg})) + if err != nil { + t.Fatalf("render the published projection: %v", err) + } + return func(context.Context) (ConfigView, bool, error) { + var view ConfigView + if err := json.Unmarshal(document, &view); err != nil { + return ConfigView{}, false, err + } + return view, true, nil + } +} + // TestSetupChecklistOmittedWithoutAProjection: no configuration to read means // the panel is omitted, not guessed at from zeroes. func TestSetupChecklistOmittedWithoutAProjection(t *testing.T) {