From 6e5bbef78da317dcde97bec412542fcb5a37891e Mon Sep 17 00:00:00 2001 From: "wanghuaiyu@qiniu.com" Date: Fri, 20 Feb 2026 17:03:10 +0800 Subject: [PATCH] fix: use typed context keys in tests to satisfy staticcheck - Define custom key types (testStringKey, testBoolKey, testIntKey) - Replace string literals with typed constants - Fixes SA1029: should not use built-in type string as key for value --- internal/agent/tools/context_test.go | 32 ++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/internal/agent/tools/context_test.go b/internal/agent/tools/context_test.go index 67a106bf23f9721fb8cb025dd2a6a7d9f349b188..ee57313f36ae38df1412656118fbd1bbbf707d0b 100644 --- a/internal/agent/tools/context_test.go +++ b/internal/agent/tools/context_test.go @@ -5,6 +5,20 @@ import ( "testing" ) +// Test-specific context key types to avoid collisions +type ( + testStringKey string + testBoolKey string + testIntKey string +) + +const ( + testKey testStringKey = "testKey" + missingKey testStringKey = "missingKey" + boolTestKey testBoolKey = "boolKey" + intTestKey testIntKey = "intKey" +) + func TestGetContextValue(t *testing.T) { tests := []struct { name string @@ -16,9 +30,9 @@ func TestGetContextValue(t *testing.T) { { name: "returns string value", setup: func(ctx context.Context) context.Context { - return context.WithValue(ctx, "testKey", "testValue") + return context.WithValue(ctx, testKey, "testValue") }, - key: "testKey", + key: testKey, defaultValue: "", want: "testValue", }, @@ -27,34 +41,34 @@ func TestGetContextValue(t *testing.T) { setup: func(ctx context.Context) context.Context { return ctx }, - key: "missingKey", + key: missingKey, defaultValue: "default", want: "default", }, { name: "returns default when type mismatch", setup: func(ctx context.Context) context.Context { - return context.WithValue(ctx, "testKey", 123) // int, not string + return context.WithValue(ctx, testKey, 123) // int, not string }, - key: "testKey", + key: testKey, defaultValue: "default", want: "default", }, { name: "returns bool value", setup: func(ctx context.Context) context.Context { - return context.WithValue(ctx, "boolKey", true) + return context.WithValue(ctx, boolTestKey, true) }, - key: "boolKey", + key: boolTestKey, defaultValue: false, want: true, }, { name: "returns int value", setup: func(ctx context.Context) context.Context { - return context.WithValue(ctx, "intKey", 42) + return context.WithValue(ctx, intTestKey, 42) }, - key: "intKey", + key: intTestKey, defaultValue: 0, want: 42, },