fix(config): de-duplicate disabled-forge predicate, export ForgeDisabled

cmd/archied carried a copy of the none/off/disabled predicate because the
configuration package's version was unexported. Two copies of a validation
predicate drift: a fourth disabled alias added in one place would silently
leave the other accepting it.

Export ForgeDisabled in internal/infrastructure/configuration as the single
definition, referenced by both validateIdentities and resolveForge, and
delete the duplicated copy. TestForgeDisabled pins the exact alias set
(including case-variant and empty-string guards); the resolveForge
degradation test now asserts on the warn log so the silent disabled path is
distinguishable from the token-unavailable fallback (verified to fail under
a re-inlined 'none'-only copy).
This commit is contained in:
sam committed 2026-08-15 01:43:31 +10:00
1 parent 968872e55a
commit 1915ef8ab4
4 files changed
+102 -18

No files matched your search

+1 -13
View File
@@ -255,7 +255,7 @@ func configuredMCPProvider(server config.MCPServer) (toolprovider.Engine, error)
// configuration is a different matter and still fails fast in validation, well
// before this point.
func resolveForge(cfg config.Forge, secrets *secret.Registry, log *slog.Logger) (forge.Forge, string) {
if isForgeDisabled(cfg.Type) {
if configuration.ForgeDisabled(cfg.Type) {
return forge.NewNoop(log), ""
}
token, err := cfg.Token.Resolve(secrets)
@@ -276,18 +276,6 @@ func resolveForge(cfg config.Forge, secrets *secret.Registry, log *slog.Logger)
return client, token
}
// isForgeDisabled reports whether a forge type explicitly opts out of forge
// integration. It mirrors the identically named check in the configuration
// package, which is unexported there.
func isForgeDisabled(forgeType string) bool {
switch forgeType {
case "none", "off", "disabled":
return true
default:
return false
}
}
func openProductionTaskStore(ctx context.Context, path string) (store.TaskStore, error) {
return store.Open(ctx, path)
}
+63 -3
View File
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
@@ -31,6 +32,36 @@ import (
"github.com/samcharles93/archie-core/internal/worktreerpc"
)
// recordingHandler captures warning messages so tests can distinguish the
// silent disabled-forge path from the token-unavailable and
// client-construction-failed fallbacks, which both log before returning the
// same noop forge.
type recordingHandler struct {
mu sync.Mutex
warns []string
}
func (h *recordingHandler) Enabled(context.Context, slog.Level) bool { return true }
func (h *recordingHandler) Handle(_ context.Context, r slog.Record) error {
h.mu.Lock()
defer h.mu.Unlock()
if r.Level == slog.LevelWarn {
h.warns = append(h.warns, r.Message)
}
return nil
}
func (h *recordingHandler) WithAttrs([]slog.Attr) slog.Handler { return h }
func (h *recordingHandler) WithGroup(string) slog.Handler { return h }
// Warnings returns a copy of the captured warning messages.
func (h *recordingHandler) Warnings() []string {
h.mu.Lock()
defer h.mu.Unlock()
return append([]string(nil), h.warns...)
}
// TestResolveForgeDegradesInsteadOfFailing guards a startup behaviour change:
// a missing forge credential used to abort archied with exit 1. The forge is
// one feature among many, so that denied the operator chat, the gateway and
@@ -40,16 +71,29 @@ import (
func TestResolveForgeDegradesInsteadOfFailing(t *testing.T) {
t.Parallel()
log := slog.New(slog.DiscardHandler)
tests := []struct {
name string
cfg config.Forge
// wantWarnSubstring discriminates the disabled path from the
// fallback paths: a type ForgeDisabled accepts returns the noop forge
// silently, while a token that cannot resolve or a forge that cannot
// be constructed logs a warning first. Without this assertion the
// alias cases are indistinguishable from the fallbacks -- a future
// copy of the predicate that only recognized "none" would pass.
wantWarnSubstring string
}{
{
name: "explicitly disabled",
cfg: config.Forge{Type: "none"},
},
{
name: "off alias",
cfg: config.Forge{Type: "off"},
},
{
name: "disabled alias",
cfg: config.Forge{Type: "disabled"},
},
{
name: "token refers to an unset environment variable",
cfg: config.Forge{
@@ -57,10 +101,15 @@ func TestResolveForgeDegradesInsteadOfFailing(t *testing.T) {
Host: "https://github.com",
Token: secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_TOKEN_DEFINITELY_UNSET"},
},
wantWarnSubstring: "token unavailable",
},
{
name: "token reference is empty",
cfg: config.Forge{Type: "github", Host: "https://github.com"},
// The zero-value SecretRef resolves to an empty token, which hits
// the token-unavailable branch -- but only after logging. This
// stays discriminating: the disabled aliases return silently.
wantWarnSubstring: "token unavailable",
},
}
@@ -68,7 +117,8 @@ func TestResolveForgeDegradesInsteadOfFailing(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client, token := resolveForge(tc.cfg, secret.NewRegistry(), log)
rec := &recordingHandler{}
client, token := resolveForge(tc.cfg, secret.NewRegistry(), slog.New(rec))
if client == nil {
t.Fatal("resolveForge returned no forge client; startup would nil-panic")
}
@@ -84,6 +134,16 @@ func TestResolveForgeDegradesInsteadOfFailing(t *testing.T) {
if len(issues) != 0 {
t.Errorf("disabled forge returned %d issues, want 0", len(issues))
}
if tc.wantWarnSubstring == "" {
if warns := rec.Warnings(); len(warns) != 0 {
t.Errorf("disabled alias logged %d warning(s), want none: %v", len(warns), warns)
}
return
}
warns := rec.Warnings()
if len(warns) != 1 || !strings.Contains(warns[0], tc.wantWarnSubstring) {
t.Errorf("warnings = %v, want exactly one containing %q", warns, tc.wantWarnSubstring)
}
})
}
}
@@ -146,7 +146,7 @@ func validateIdentities(identities []config.IdentityConfig) error {
if !oneOf(id.Forge.Type, forgeTypes) {
return fmt.Errorf("%w: identities[%d].forge.type %q (want %s)", ErrInvalidInput, i, id.Forge.Type, list(forgeTypes))
}
if !isForgeDisabled(id.Forge.Type) && id.Forge.Token == (secret.SecretRef{}) {
if !ForgeDisabled(id.Forge.Type) && id.Forge.Token == (secret.SecretRef{}) {
return fmt.Errorf("%w: identities[%d].forge.token is required (each identity needs its own secret reference; unlike the top-level [forge], there is no default)", ErrInvalidInput, i)
}
if err := validateRepos(id.Repos); err != nil {
@@ -156,7 +156,13 @@ func validateIdentities(identities []config.IdentityConfig) error {
return nil
}
func isForgeDisabled(t string) bool {
// ForgeDisabled reports whether a forge type explicitly opts out of forge
// integration. It is the single definition shared by config validation and
// cmd/archied's resolveForge: adding or removing an alias here applies to
// both paths together. It is not consulted by forge.New, which has its own
// construction-time dispatch for disabled types (and the empty string);
// that dispatch is a different concern from this validation predicate.
func ForgeDisabled(t string) bool {
return t == forgeTypeNone || t == forgeTypeOff || t == forgeTypeDisabled
}
@@ -25,6 +25,36 @@ func minimalValidConfig() config.Config {
}
}
// TestForgeDisabled pins the single definition of the disabled-forge
// predicate: cmd/archied's resolveForge calls ForgeDisabled instead of
// maintaining its own copy, so any alias added or removed here must hold
// for both config validation and daemon startup. The table deliberately
// includes a case-variant alias and the empty string to catch a future
// implementation that lowercases or matches prefixes instead of comparing
// the exact aliases.
func TestForgeDisabled(t *testing.T) {
tests := []struct {
name string
typ string
want bool
}{
{name: "none", typ: "none", want: true},
{name: "off", typ: "off", want: true},
{name: "disabled", typ: "disabled", want: true},
{name: "github", typ: "github", want: false},
{name: "gitea", typ: "gitea", want: false},
{name: "empty string", typ: "", want: false},
{name: "case variant is not an alias", typ: "None", want: false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := ForgeDisabled(tc.typ); got != tc.want {
t.Errorf("ForgeDisabled(%q) = %v, want %v", tc.typ, got, tc.want)
}
})
}
}
func TestValidate_IsCallableOutsideThePackage(t *testing.T) {
cfg := minimalValidConfig()
if err := Validate(&cfg); err != nil {