Files
archie/internal/app/archiemessaging/enablement_test.go
T
sam c5f24ec914 chore: delete code no binary reaches
Removes the Kit task-tool result shim (and with it the MCP go-sdk
dependency), the messaging enable/disable/restart helpers and channel
rebuild closures nothing calls since channels became extensions, the
unused messaging capability, ephemeral and text-fallback types, and
single stragglers in telemetry, releaseupdate, skill, storepkg and
webui. The egress sandbox relay stays for #1867.
2026-11-13 03:33:32 +11:00

175 lines
4.6 KiB
Go

package archiemessaging
import (
"context"
"encoding/json"
"log/slog"
"sync"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/channels"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// servingChannel reports itself running once it starts, which is what an
// enable is reported active on: a channel that only reaches "starting" is not
// serving yet.
type servingChannel struct {
mu sync.Mutex
starts int
show bool
}
func (c *servingChannel) Name() string { return "test" }
func (c *servingChannel) Start(ctx context.Context, _ messaging.ChatContract, lifecycle channels.Lifecycle) error {
c.mu.Lock()
c.starts++
c.mu.Unlock()
if lifecycle.Starting != nil {
lifecycle.Starting()
}
if lifecycle.Running != nil {
lifecycle.Running()
}
<-ctx.Done()
return nil
}
func (c *servingChannel) Stop(context.Context) error { return nil }
func (c *servingChannel) ConfigSchema() json.RawMessage { return nil }
func (c *servingChannel) ValidateConfig(map[string]any) error { return nil }
func (c *servingChannel) SetShowToolCalls(show bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.show = show
}
func (c *servingChannel) count() int {
c.mu.Lock()
defer c.mu.Unlock()
return c.starts
}
func (c *servingChannel) showToolCalls() bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.show
}
// startTestService composes a service and adds one scripted channel instance
// under supervision, so enablement can be driven without a real channel.
func startTestService(t *testing.T, ch *servingChannel) *Service {
t.Helper()
srv, err := compose(context.Background(), deps{Log: slog.Default()})
if err != nil {
t.Fatalf("compose: %v", err)
}
inst := &channelInstance{
name: "test", kind: "test",
notify: make(chan struct{}, 1),
}
srv.retry = channelRetryPolicy{attempts: 3, backoff: time.Millisecond, maxBackoff: time.Millisecond}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
defer close(done)
_ = srv.Start(ctx)
}()
t.Cleanup(func() {
cancel()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Error("service did not stop after cancel")
}
})
srv.addChannel(ctx, inst)
waitSupervised(t, srv, "test")
return srv
}
// waitSupervised blocks until the channel's supervisor exists, so an enable
// issued right after Start is not refused for racing it.
func waitSupervised(t *testing.T, srv *Service, id string) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
inst := instanceByID(srv, id)
inst.mu.Lock()
supervised := inst.supervised
inst.mu.Unlock()
if supervised {
return
}
time.Sleep(time.Millisecond)
}
t.Fatalf("channel %q was never supervised", id)
}
// waitCount blocks until count reaches want, so an async start is observed
// rather than slept for.
func waitCount(t *testing.T, count func() int, want int) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
if count() >= want {
return
}
time.Sleep(time.Millisecond)
}
t.Fatalf("channel started %d times, want at least %d", count(), want)
}
// fakeSettings serves a stored chat config at a version, the way the
// control-plane client does.
type fakeSettings struct {
chat config.ChatConfig
version int64
}
func (f fakeSettings) RuntimeChatConfig(context.Context, config.ChatConfig) (config.ChatConfig, int64, error) {
return f.chat, f.version, nil
}
// TestReconcileAppliesShowToolCalls pins the live settings path that remains:
// a stored channel-settings version newer than the applied one reaches the
// running channels that accept show_tool_calls, and is recorded as applied.
func TestReconcileAppliesShowToolCalls(t *testing.T) {
ch := &servingChannel{}
srv := startTestService(t, ch)
if err := restart(srv, "test", ch); err != nil {
t.Fatalf("start: %v", err)
}
waitCount(t, ch.count, 1)
srv.settingsSource = fakeSettings{
chat: config.ChatConfig{ShowToolCalls: true},
version: 1,
}
if err := srv.reconcileOnce(context.Background()); err != nil {
t.Fatalf("reconcile: %v", err)
}
if !ch.showToolCalls() {
t.Fatal("show_tool_calls was not applied to the running channel")
}
if srv.appliedVersion.Load() != 1 {
t.Fatalf("appliedVersion = %d, want 1", srv.appliedVersion.Load())
}
if srv.currentConfig().ShowToolCalls != true {
t.Fatal("running config does not carry the applied setting")
}
// An older version is a no-op.
srv.settingsSource = fakeSettings{version: 1}
if err := srv.reconcileOnce(context.Background()); err != nil {
t.Fatalf("reconcile stale: %v", err)
}
}