on main help sign in
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

commit6e5bbef78da317dcde97bec412542fcb5a37891e authorwanghuaiyu@qiniu.com <wanghuaiyu@qiniu.com> dateFri 20 Feb 2026, 17:03 (8 months ago) parente0d5454
1 file changed+23−9
internal/agent/tools/context_test.go+23−9
internal/agent/tools/context_test.go+23−9
@@ -5,6 +5,20 @@ import (
55 "testing"
66)
77
8+// Test-specific context key types to avoid collisions
9+type (
10+ testStringKey string
11+ testBoolKey string
12+ testIntKey string
13+)
14+
15+const (
16+ testKey testStringKey = "testKey"
17+ missingKey testStringKey = "missingKey"
18+ boolTestKey testBoolKey = "boolKey"
19+ intTestKey testIntKey = "intKey"
20+)
21+
822func TestGetContextValue(t *testing.T) {
923 tests := []struct {
1024 name string
@@ -16,9 +30,9 @@ func TestGetContextValue(t *testing.T) {
1630 {
1731 name: "returns string value",
1832 setup: func(ctx context.Context) context.Context {
19− return context.WithValue(ctx, "testKey", "testValue")
33+ return context.WithValue(ctx, testKey, "testValue")
2034 },
21− key: "testKey",
35+ key: testKey,
2236 defaultValue: "",
2337 want: "testValue",
2438 },
@@ -27,34 +41,34 @@ func TestGetContextValue(t *testing.T) {
2741 setup: func(ctx context.Context) context.Context {
2842 return ctx
2943 },
30− key: "missingKey",
44+ key: missingKey,
3145 defaultValue: "default",
3246 want: "default",
3347 },
3448 {
3549 name: "returns default when type mismatch",
3650 setup: func(ctx context.Context) context.Context {
37− return context.WithValue(ctx, "testKey", 123) // int, not string
51+ return context.WithValue(ctx, testKey, 123) // int, not string
3852 },
39− key: "testKey",
53+ key: testKey,
4054 defaultValue: "default",
4155 want: "default",
4256 },
4357 {
4458 name: "returns bool value",
4559 setup: func(ctx context.Context) context.Context {
46− return context.WithValue(ctx, "boolKey", true)
60+ return context.WithValue(ctx, boolTestKey, true)
4761 },
48− key: "boolKey",
62+ key: boolTestKey,
4963 defaultValue: false,
5064 want: true,
5165 },
5266 {
5367 name: "returns int value",
5468 setup: func(ctx context.Context) context.Context {
55− return context.WithValue(ctx, "intKey", 42)
69+ return context.WithValue(ctx, intTestKey, 42)
5670 },
57− key: "intKey",
71+ key: intTestKey,
5872 defaultValue: 0,
5973 want: 42,
6074 },