Files
archie/internal/app/archiemessaging/channel_status_publish_test.go
T
sam d6f526ce4f feat(state): carry channel lifecycle from the Messaging Service to the dashboard
archie-core-8cda.6.8's carrier. The service now records channel lifecycle and has
nowhere to put it: /api/channels answered with nothing because
webui.Server.Channels had no production source, and reload answered 501 for the
same reason. This adds the surface the register decided on, following the split
archie-core-ymut established for config snapshots -- a pair of administrative RPCs
whose writer is one process and whose reader is the UI -- and wires both ends.

Wire and store: PutChannelStatus / ListChannelStatus, with ChannelStatus carrying
id, name, state, detail, configured, reload_supported and observed_at. The store
holds one row per channel and replaces the reported set, so a channel the reporter
no longer mentions is deleted rather than left standing as a stale "running"; an
empty report clears the table, because hosting no channels is a fact worth
reporting. A row without an id is refused: it could never be replaced or removed
by a later report.

Writer: the service publishes on every lifecycle transition through a coalescing
signal -- a burst of transitions is one write, since a reader only wants the latest
state of every channel -- plus a start-up report and a final report after shutdown,
so a stopped service leaves the store honest. A failed write is logged and retried
on the next signal; the dashboard losing channel state is worth a warning and never
worth failing a channel start over.

Reader: webui.Server.Channels becomes a source rather than a concrete manager, and
webui.RemoteChannelStatus reads the store the way RemoteConfigView reads the config
snapshot. A store it cannot reach reports nothing rather than a stale "running",
and a state the reader does not recognise is carried through rather than dropped.

The contract's own index gains the group in the same change that adds the RPCs --
a surface the grouped table does not list is one nobody finds -- and the count moves
to 51. The register records why the surface is the State Store rather than a
resource kind, and why reload gets its own surface rather than a field here: reading
status must not be able to retrigger an action, a transient request is not
distinguishable from state once anything polls it, and it would invert the
writer/reader relationship the precedent establishes.

Verified: task check exit 0. New coverage: the store's replace-the-set semantics
and its refusal of an id-less row, the reader's mapping including an unknown state,
and the publisher's start-up, transition and final reports.
2026-09-22 08:04:44 +10:00

99 lines
3.2 KiB
Go

package archiemessaging
import (
"context"
"fmt"
"log/slog"
"sync"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// recordingChannelStatusStore records every report the service publishes.
type recordingChannelStatusStore struct {
mu sync.Mutex
writes [][]storecontract.ChannelStatus
}
func (s *recordingChannelStatusStore) PutChannelStatus(_ context.Context, channels []storecontract.ChannelStatus) error {
s.mu.Lock()
defer s.mu.Unlock()
s.writes = append(s.writes, append([]storecontract.ChannelStatus(nil), channels...))
return nil
}
func (s *recordingChannelStatusStore) ChannelStatus(context.Context) ([]storecontract.ChannelStatus, error) {
return nil, nil
}
// waitForPublishedState polls the recorded reports until one carries the channel
// in want, so the assertion races the publisher rather than the clock.
func waitForPublishedState(t *testing.T, store *recordingChannelStatusStore, id, want string) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
store.mu.Lock()
for _, write := range store.writes {
for _, channel := range write {
if channel.ID == id && channel.State == want {
store.mu.Unlock()
return
}
}
}
store.mu.Unlock()
time.Sleep(10 * time.Millisecond)
}
store.mu.Lock()
defer store.mu.Unlock()
t.Fatalf("no report carried %s as %q; reports were %+v", id, want, store.writes)
}
// TestServicePublishesChannelStatus is the point of archie-core-8cda.6.8: the
// report has to reach the store the dashboard reads, not stay in this process.
// Without this the surface exists and no operator ever sees it.
func TestServicePublishesChannelStatus(t *testing.T) {
writer := &recordingChannelStatusStore{}
srv, err := compose(t.Context(), deps{
ChannelStatus: writer,
Config: ResolvedConfig{
Options: Options{ShutdownTimeout: 2 * time.Second},
WebhookAddr: fmt.Sprintf("127.0.0.1:%d", freePort(t)),
Webhook: config.WebhookRoute{Path: "/smoke"},
},
Log: slog.Default(),
Chat: &recordingChatContract{routes: make(chan messaging.Inbound, 1), reply: "ok"},
})
if err != nil {
t.Fatalf("compose: %v", err)
}
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
started := make(chan error, 1)
go func() { started <- srv.Start(ctx) }()
// The start-up report and the channel's own "starting" coalesce by design --
// signals coalesce, because a reader only ever wants the latest state of every
// channel -- so the first published state is either "configured" or "starting"
// depending on which side wins. What must hold is that the transitions reach
// the store, and that the last word is the truth.
waitForPublishedState(t, writer, "webhook", "running")
srv.Stop()
select {
case err := <-started:
if err != nil {
t.Fatalf("srv.Start returned %v, want a clean stop", err)
}
case <-time.After(3 * time.Second):
t.Fatal("srv.Start did not return after Stop")
}
// The final report is what leaves the store honest about a stopped service.
waitForPublishedState(t, writer, "webhook", "stopped")
}