From da731338f139e04b11e6ebd61396657e07bbda0b Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 30 Sep 2026 07:24:09 +1000 Subject: [PATCH] fix(test): a table case holds the org, not a context containedctx rejects a struct field of type context.Context, and both new table tests stored one. Each case now carries what the caller put on the context -- a builder in the org test, the org id in the client test -- and builds the context inside the subtest. --- internal/app/controlplane/client_org_test.go | 23 ++++++++---- internal/domain/org/context_test.go | 39 +++++++++++--------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/internal/app/controlplane/client_org_test.go b/internal/app/controlplane/client_org_test.go index 1c964b92f..660d5544b 100644 --- a/internal/app/controlplane/client_org_test.go +++ b/internal/app/controlplane/client_org_test.go @@ -53,26 +53,33 @@ type eofWatchStream struct{ grpc.ClientStream } func (eofWatchStream) Recv() (*pb.WatchResponse, error) { return nil, io.EOF } func TestClientStampsCallerOrgOnEveryRequestShape(t *testing.T) { + // The table holds the org, not a context: a stored context.Context field is + // what containedctx rejects, and each case only needs to say what the caller + // put on its context. tests := []struct { - name string - ctx context.Context - want string + name string + orgID string + want string }{ - {name: "no org on the context is the default org", ctx: context.Background(), want: string(org.DefaultOrgID)}, - {name: "org on the context travels on the wire", ctx: org.WithOrg(context.Background(), "acme"), want: "acme"}, + {name: "no org on the context is the default org", want: string(org.DefaultOrgID)}, + {name: "org on the context travels on the wire", orgID: "acme", want: "acme"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + if tt.orgID != "" { + ctx = org.WithOrg(ctx, org.OrgID(tt.orgID)) + } rpc := &orgRecordingClient{} client := NewRPCClient(rpc) - if _, _, err := client.Query(tt.ctx, ChannelSettingsKind, func([]byte) error { return nil }); err != nil { + if _, _, err := client.Query(ctx, ChannelSettingsKind, func([]byte) error { return nil }); err != nil { t.Fatalf("Query: %v", err) } - if _, err := client.ReplaceSchedules(tt.ctx, nil, 0, "actor", "source", "request"); err != nil { + if _, err := client.ReplaceSchedules(ctx, nil, 0, "actor", "source", "request"); err != nil { t.Fatalf("ReplaceSchedules: %v", err) } - if _, err := client.WatchResource(tt.ctx, WorkflowExecutionSettingsKind, 0); err != nil { + if _, err := client.WatchResource(ctx, WorkflowExecutionSettingsKind, 0); err != nil { t.Fatalf("WatchResource: %v", err) } diff --git a/internal/domain/org/context_test.go b/internal/domain/org/context_test.go index 955b210a9..32f4f3fc8 100644 --- a/internal/domain/org/context_test.go +++ b/internal/domain/org/context_test.go @@ -10,38 +10,43 @@ import ( type foreignContextKey struct{} func TestOrgFromContext(t *testing.T) { + // build derives each case's context from a fresh background one. The table + // holds no context.Context field on purpose: a stored context is what + // containedctx rejects, and a builder keeps every case readable without one. tests := []struct { - name string - ctx context.Context - want OrgID + name string + build func(context.Context) context.Context + want OrgID }{ - {name: "unset is the default org", ctx: context.Background(), want: DefaultOrgID}, - {name: "set org", ctx: WithOrg(context.Background(), "acme"), want: "acme"}, + {name: "unset is the default org", build: func(ctx context.Context) context.Context { return ctx }, want: DefaultOrgID}, + {name: "set org", build: func(ctx context.Context) context.Context { return WithOrg(ctx, "acme") }, want: "acme"}, { - name: "inner org overrides an outer one", - ctx: WithOrg(WithOrg(context.Background(), "outer"), "inner"), - want: "inner", + name: "inner org overrides an outer one", + build: func(ctx context.Context) context.Context { return WithOrg(WithOrg(ctx, "outer"), "inner") }, + want: "inner", }, { - name: "nil stored value is the default org", - ctx: context.WithValue(context.Background(), orgContextKey{}, nil), - want: DefaultOrgID, + name: "nil stored value is the default org", + build: func(ctx context.Context) context.Context { return context.WithValue(ctx, orgContextKey{}, nil) }, + want: DefaultOrgID, }, { - name: "a value of a foreign type is the default org", - ctx: context.WithValue(context.Background(), orgContextKey{}, "acme"), - want: DefaultOrgID, + name: "a value of a foreign type is the default org", + build: func(ctx context.Context) context.Context { return context.WithValue(ctx, orgContextKey{}, "acme") }, + want: DefaultOrgID, }, { name: "a value under a foreign key is the default org", - ctx: context.WithValue(context.Background(), foreignContextKey{}, OrgID("acme")), + build: func(ctx context.Context) context.Context { + return context.WithValue(ctx, foreignContextKey{}, OrgID("acme")) + }, want: DefaultOrgID, }, - {name: "empty org is the default org", ctx: WithOrg(context.Background(), ""), want: DefaultOrgID}, + {name: "empty org is the default org", build: func(ctx context.Context) context.Context { return WithOrg(ctx, "") }, want: DefaultOrgID}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := OrgFromContext(tt.ctx); got != tt.want { + if got := OrgFromContext(tt.build(context.Background())); got != tt.want { t.Fatalf("OrgFromContext() = %q, want %q", got, tt.want) } })