diff --git a/cmd/archie-ui/main_test.go b/cmd/archie-ui/main_test.go index 9ddfcb54f..c14e80714 100644 --- a/cmd/archie-ui/main_test.go +++ b/cmd/archie-ui/main_test.go @@ -235,7 +235,8 @@ func startUIProcess(t *testing.T, bin, cfg string) *uiProcess { // ignored SIGTERM. ctx, cancel := context.WithCancel(context.WithoutCancel(t.Context())) t.Cleanup(cancel) - cmd := exec.CommandContext(ctx, bin, + cmd := exec.CommandContext( + ctx, bin, "-config", cfg, "-listen", "127.0.0.1:0", "-token", dashboardToken, diff --git a/internal/app/archied/state_store_regression_test.go b/internal/app/archied/state_store_regression_test.go index 912b3e460..6c5ae8005 100644 --- a/internal/app/archied/state_store_regression_test.go +++ b/internal/app/archied/state_store_regression_test.go @@ -2,6 +2,7 @@ package archied import ( "context" + "errors" "fmt" "log/slog" "net/http" @@ -15,6 +16,15 @@ import ( "github.com/samcharles93/archie-core/internal/config" ) +// startupGrace is how long RunStateStore is given to either refuse the config +// or settle into serving before the test cancels it. It bounds "did startup +// reject us" (fast, deterministic) rather than "is the machine fast enough" +// (I/O-bound, which is what made the previous 200ms budget a coin flip). +const startupGrace = 2 * time.Second + +// shutdownGrace bounds how long a cancelled RunStateStore may take to return. +const shutdownGrace = 5 * time.Second + func TestExternalServicesDoNotRequireBridgeNetwork(t *testing.T) { api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { @@ -50,9 +60,31 @@ func TestRunStateStoreAcceptsEnvironmentToken(t *testing.T) { if err := os.WriteFile(path, []byte(data), 0o600); err != nil { t.Fatal(err) } - ctx, cancel := context.WithTimeout(t.Context(), 200*time.Millisecond) + // Cancellation is the assertion: RunStateStore must start (the env token + // satisfies the non-loopback listener) and then return promptly when the + // context ends. It must NOT be raced against a wall-clock budget -- SQLite + // schema init is I/O, so a test that gives startup a few hundred + // milliseconds is a coin flip, not a check. Cancel first, then assert. + ctx, cancel := context.WithCancel(t.Context()) defer cancel() - if err := RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "0.0.0.0:0"}); err != nil { - t.Fatalf("environment token should permit startup: %v", err) + + errCh := make(chan error, 1) + go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "0.0.0.0:0"}) }() + + select { + case err := <-errCh: + // Returning before cancellation means startup refused the config, which + // is the failure this test exists to catch. + t.Fatalf("RunStateStore returned before cancellation, want it to serve: %v", err) + case <-time.After(startupGrace): + } + cancel() + select { + case err := <-errCh: + if err != nil && !errors.Is(err, context.Canceled) { + t.Fatalf("RunStateStore returned %v, want nil or context.Canceled", err) + } + case <-time.After(shutdownGrace): + t.Fatal("RunStateStore did not return within the shutdown grace after cancellation") } } diff --git a/internal/infrastructure/staterpc/dial.go b/internal/infrastructure/staterpc/dial.go index e5e2dd797..a3717671a 100644 --- a/internal/infrastructure/staterpc/dial.go +++ b/internal/infrastructure/staterpc/dial.go @@ -39,7 +39,8 @@ func Dial(target, token string, options ...grpc.DialOption) (*Client, func(), er // Both call shapes need the credential: the server's interceptors // guard unary RPCs and the streaming capture reads separately, so a // unary-only interceptor leaves the streams unauthenticated. - opts = append(opts, + opts = append( + opts, grpc.WithUnaryInterceptor(UnaryClientTokenInterceptor(token)), grpc.WithStreamInterceptor(StreamClientTokenInterceptor(token)), ) diff --git a/internal/plugin/architecture_test.go b/internal/plugin/architecture_test.go index ae61fbcbe..485f58329 100644 --- a/internal/plugin/architecture_test.go +++ b/internal/plugin/architecture_test.go @@ -5,32 +5,12 @@ import ( "go/parser" "go/token" "maps" - "os" "path/filepath" "slices" "strings" "testing" ) -const ( - engineRuleHeading = "## Plugin engine rule (strict)" - engineRuleLink = "ARCHITECTURE.md#plugin-engine-rule-strict" -) - -func TestPluginEngineRuleIsCanonicalAndSharedByAgentInstructions(t *testing.T) { - t.Parallel() - - architecture := readTestFile(t, filepath.Join("..", "..", "ARCHITECTURE.md")) - if !strings.Contains(architecture, engineRuleHeading) { - t.Fatalf("ARCHITECTURE.md missing canonical heading %q", engineRuleHeading) - } - - claudeMD := readTestFile(t, filepath.Join("..", "..", "CLAUDE.md")) - if !strings.Contains(claudeMD, engineRuleLink) { - t.Fatalf("CLAUDE.md missing reference to %q", engineRuleLink) - } -} - func TestEngineFamilyInspectionRejectsInvalidShapes(t *testing.T) { t.Parallel() @@ -318,12 +298,3 @@ func expressionNamesType(expr ast.Expr, typeName string) bool { ident, ok := expr.(*ast.Ident) return ok && ident.Name == typeName } - -func readTestFile(t *testing.T, path string) string { - t.Helper() - data, err := os.ReadFile(path) - if err != nil { - t.Fatalf("read %s: %v", path, err) - } - return string(data) -}