Files
archie/internal/app/archied/config_snapshot_test.go
T
sam 87d8d39321 fix(webui): publish per-identity forges so multi-identity task rows keep their links
Task rows in a deployment configuring [[identities]] rendered no forge links.
ConfigView carried the default identity's forge alone, so resolveForge
(api_tasks.go) withheld the links entirely rather than pointing every row at
the wrong forge -- production behaviour since the UI process cutover
(archie-core-8cda.5.4), where the dashboard runs in a process with no
config.Holder. archie-core-ml30 then removed webui.forgeConfigForTask and its
test, which resolved a task's forge from config.Config and had no reachable
caller once the holder was gone.

Fix at the producer, not the renderer. ConfigView gains `identities`: each
configured identity's forge type and host, plus the repositories it owns. That
is the whole answer to "which forge owns this task", and it is a projection the
daemon already rebuilds and republishes on boot and on every reload, so nothing
new is needed on the publish side. Only coordinates and repository names cross
-- an identity's token is a secret reference and stays out, which the new test
asserts alongside the existing leak guard.

resolveForge ports the matching rules recovered from 5b91a3a's
forgeConfigForTask onto that projection: an exact identity name first, then
repository ownership, and an ambiguous repository -- two identities claiming the
same owner/name -- falling back to the default forge rather than inventing a
link. Two deliberate departures from the recovered shape: it indexes the
identity list once per request instead of rescanning config per task, since the
projection supplies repo slices it can key on directly; and a document that
reports multi-identity but carries no per-identity forges still withholds,
because that is what an older daemon published and guessing there is the
original bug. The recovered code's first-wins scan for a duplicated identity
name is preserved.

Tests: the projection's per-identity forges and their secret-freedom; the three
matching rules and both fallbacks through resolveForge; every row of a
multi-identity GET /api/tasks carrying the right forge's link; and the real
archie-ui binary driven over HTTP against a multi-identity projection, which
fails on the pre-fix resolver with three unlinked rows.
2026-09-14 10:36:29 +00:00

210 lines
7.5 KiB
Go

package archied
import (
"context"
"encoding/json"
"log/slog"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webui"
)
// TestPublishConfigSnapshotRendersTheDaemonsConfiguration: the daemon is the
// configuration owner, so it is the process that renders the dashboard's
// projection and publishes it for the UI process to read. It builds that view
// from its own state rather than from a webui.Server it never serves
// (archie-core-ml30).
func TestPublishConfigSnapshotRendersTheDaemonsConfiguration(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() })
cfg := config.Config{
BotUser: "archie-bot",
Repos: []config.Repo{{Owner: "acme", Name: "widget", Base: "main"}},
Chat: config.ChatConfig{
Operator: "Sam",
ShowToolCalls: true,
WebhookAddr: "127.0.0.1:9099",
},
}
b := &boot{
log: slog.New(slog.DiscardHandler),
stateStore: st,
cfgHolder: config.NewHolder(cfg),
lastReload: func() config.ReloadStatus {
return config.ReloadStatus{LastError: "poll_interval must be positive"}
},
}
b.currentProvenance.Store(&configuration.Provenance{Origins: []configuration.Origin{
{Path: "/etc/archie/config.toml", Role: configuration.RoleMain, Layer: configuration.LayerBase},
}})
b.publishConfigSnapshot(t.Context())
snapshot, found, err := st.ConfigSnapshot(context.Background())
if err != nil || !found {
t.Fatalf("ConfigSnapshot = (found %v, %v), want the published document", found, err)
}
if snapshot.Schema != webui.ConfigViewSchema {
t.Fatalf("Schema = %q, want %q", snapshot.Schema, webui.ConfigViewSchema)
}
var view webui.ConfigView
if err := json.Unmarshal(snapshot.Document, &view); err != nil {
t.Fatalf("decode published document: %v", err)
}
if view.Identity.BotUser != "archie-bot" || len(view.Repositories) != 1 {
t.Errorf("view = %+v, want the daemon's identity and repositories", view)
}
// The chat page and the setup checklist are rendered by a process that
// holds no configuration, so the projection has to carry what they read.
if !view.Chat.ShowToolCalls {
t.Error("Chat.ShowToolCalls = false; the chat page would never expand tool calls")
}
if view.Chat.Operator != "Sam" {
t.Errorf("Chat.Operator = %q, want Sam", view.Chat.Operator)
}
if !view.Chat.ChannelConfigured {
t.Error("Chat.ChannelConfigured = false despite a configured webhook address")
}
if len(view.Provenance) != 1 || view.Provenance[0].Path != "/etc/archie/config.toml" {
t.Errorf("Provenance = %+v, want the daemon's file chain", view.Provenance)
}
if view.Reload == nil || view.Reload.LastError != "poll_interval must be positive" {
t.Errorf("Reload = %+v, want the last reload outcome", view.Reload)
}
// Editable describes the rendering process's write path, never the
// published document.
if view.Editable {
t.Error("the published document claims to be editable")
}
}
// TestPublishedSnapshotCarriesOverlayOverrides: the dashboard marks rows the
// runtime overlay shadows, so the projection has to name those keys. The
// daemon reads them from the overlay store it owns.
func TestPublishedSnapshotCarriesOverlayOverrides(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() })
overlayStore, err := overlay.Open(t.Context(), filepath.Join(t.TempDir(), "config.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = overlayStore.Close() })
if err := overlayStore.Set(t.Context(), "label", `"archie"`, "test"); err != nil {
t.Fatal(err)
}
if err := overlayStore.Set(t.Context(), "budgets.max_steps", "40", "test"); err != nil {
t.Fatal(err)
}
b := &boot{
log: slog.New(slog.DiscardHandler),
stateStore: st,
cfgHolder: config.NewHolder(config.Config{}),
overlayStore: overlayStore,
}
b.publishConfigSnapshot(t.Context())
snapshot, found, err := st.ConfigSnapshot(context.Background())
if err != nil || !found {
t.Fatalf("ConfigSnapshot = (found %v, %v), want the published document", found, err)
}
var view webui.ConfigView
if err := json.Unmarshal(snapshot.Document, &view); err != nil {
t.Fatal(err)
}
want := []string{"budgets.max_steps", "label"}
if len(view.Overridden) != len(want) {
t.Fatalf("Overridden = %+v, want %+v", view.Overridden, want)
}
for i, key := range want {
if view.Overridden[i] != key {
t.Errorf("Overridden[%d] = %q, want %q (sorted)", i, view.Overridden[i], key)
}
}
}
// TestPublishedSnapshotCarriesPerIdentityForges: in a multi-identity
// deployment the task board attributes each task to the forge that owns it,
// and the only process that holds that configuration is the daemon. So the
// published projection has to name every configured identity's forge and the
// repositories it owns (archie-core-pv6t); with the default identity alone,
// every row in such a deployment renders unlinked.
func TestPublishedSnapshotCarriesPerIdentityForges(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{
log: slog.New(slog.DiscardHandler),
stateStore: st,
cfgHolder: config.NewHolder(config.Config{
BotUser: "archie-bot",
Forge: config.Forge{Type: "github", Host: "https://github.example"},
Repos: []config.Repo{{Owner: "acme", Name: "widget", Base: "main"}},
Identities: []config.IdentityConfig{
{
Name: "gitea-bot", BotUser: "archie-gitea",
Forge: config.Forge{Type: "gitea", Host: "https://gitea.example"},
Repos: []config.Repo{{Owner: "beta", Name: "svc", Base: "main"}},
},
},
}),
}
b.publishConfigSnapshot(t.Context())
snapshot, found, err := st.ConfigSnapshot(context.Background())
if err != nil || !found {
t.Fatalf("ConfigSnapshot = (found %v, %v), want the published document", found, err)
}
var view webui.ConfigView
if err := json.Unmarshal(snapshot.Document, &view); err != nil {
t.Fatalf("decode published document: %v", err)
}
if !view.MultiIdentity {
t.Error("MultiIdentity = false with a configured identity")
}
if len(view.Identities) != 1 {
t.Fatalf("Identities = %+v, want the configured identity", view.Identities)
}
got := view.Identities[0]
if got.Name != "gitea-bot" || got.ForgeType != "gitea" || got.ForgeHost != "https://gitea.example" {
t.Errorf("Identities[0] = %+v, want the gitea-bot forge coordinates", got)
}
if len(got.Repos) != 1 || got.Repos[0].Owner != "beta" || got.Repos[0].Name != "svc" {
t.Errorf("Identities[0].Repos = %+v, want the repositories it owns", got.Repos)
}
}
// TestPublishConfigSnapshotWithoutConfiguration: called before the daemon has
// a configuration Holder, publishing is a no-op rather than a panic.
func TestPublishConfigSnapshotWithoutConfiguration(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{log: slog.New(slog.DiscardHandler), stateStore: st}
b.publishConfigSnapshot(t.Context())
if _, found, err := st.ConfigSnapshot(context.Background()); found || err != nil {
t.Fatalf("ConfigSnapshot = (found %v, %v), want nothing published", found, err)
}
}