|
| 1 | +package hooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestHookContextCloneDeepCopyMetadata(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + original := HookContext{ |
| 13 | + RunID: "run-1", |
| 14 | + SessionID: "session-1", |
| 15 | + Metadata: map[string]any{ |
| 16 | + "slice": []any{"a", map[string]any{"k": "v"}}, |
| 17 | + "map": map[string]any{"nested": []string{"x", "y"}}, |
| 18 | + }, |
| 19 | + } |
| 20 | + |
| 21 | + cloned := original.Clone() |
| 22 | + metadataSlice, ok := cloned.Metadata["slice"].([]any) |
| 23 | + if !ok { |
| 24 | + t.Fatalf("slice metadata type = %T, want []any", cloned.Metadata["slice"]) |
| 25 | + } |
| 26 | + nestedMap, ok := metadataSlice[1].(map[string]any) |
| 27 | + if !ok { |
| 28 | + t.Fatalf("nested map type = %T, want map[string]any", metadataSlice[1]) |
| 29 | + } |
| 30 | + nestedMap["k"] = "changed" |
| 31 | + |
| 32 | + clonedMap, ok := cloned.Metadata["map"].(map[string]any) |
| 33 | + if !ok { |
| 34 | + t.Fatalf("map metadata type = %T, want map[string]any", cloned.Metadata["map"]) |
| 35 | + } |
| 36 | + nestedSlice, ok := clonedMap["nested"].([]string) |
| 37 | + if !ok { |
| 38 | + t.Fatalf("nested slice type = %T, want []string", clonedMap["nested"]) |
| 39 | + } |
| 40 | + nestedSlice[0] = "changed" |
| 41 | + |
| 42 | + originalSlice := original.Metadata["slice"].([]any) |
| 43 | + originalNestedMap := originalSlice[1].(map[string]any) |
| 44 | + if got := originalNestedMap["k"]; got != "v" { |
| 45 | + t.Fatalf("original nested map value = %v, want v", got) |
| 46 | + } |
| 47 | + originalMap := original.Metadata["map"].(map[string]any) |
| 48 | + originalNestedSlice := originalMap["nested"].([]string) |
| 49 | + if got := originalNestedSlice[0]; got != "x" { |
| 50 | + t.Fatalf("original nested slice value = %q, want x", got) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestHookContextCloneDeepCopyStructFields(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + type nested struct { |
| 58 | + Flag bool |
| 59 | + } |
| 60 | + type metaPayload struct { |
| 61 | + Attrs map[string]string |
| 62 | + Items []int |
| 63 | + Ref *nested |
| 64 | + } |
| 65 | + |
| 66 | + original := HookContext{ |
| 67 | + Metadata: map[string]any{ |
| 68 | + "struct": metaPayload{ |
| 69 | + Attrs: map[string]string{"k": "v"}, |
| 70 | + Items: []int{1, 2, 3}, |
| 71 | + Ref: &nested{Flag: true}, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + cloned := original.Clone() |
| 77 | + payload, ok := cloned.Metadata["struct"].(metaPayload) |
| 78 | + if !ok { |
| 79 | + t.Fatalf("struct metadata type = %T, want metaPayload", cloned.Metadata["struct"]) |
| 80 | + } |
| 81 | + |
| 82 | + payload.Attrs["k"] = "changed" |
| 83 | + payload.Items[0] = 99 |
| 84 | + payload.Ref.Flag = false |
| 85 | + cloned.Metadata["struct"] = payload |
| 86 | + |
| 87 | + originPayload := original.Metadata["struct"].(metaPayload) |
| 88 | + if got := originPayload.Attrs["k"]; got != "v" { |
| 89 | + t.Fatalf("original Attrs[k] = %q, want v", got) |
| 90 | + } |
| 91 | + if got := originPayload.Items[0]; got != 1 { |
| 92 | + t.Fatalf("original Items[0] = %d, want 1", got) |
| 93 | + } |
| 94 | + if got := originPayload.Ref.Flag; got != true { |
| 95 | + t.Fatalf("original Ref.Flag = %v, want true", got) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestHookContextCloneNoMetadata(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + |
| 102 | + original := HookContext{RunID: "run-1", SessionID: "session-1"} |
| 103 | + cloned := original.Clone() |
| 104 | + if cloned.RunID != original.RunID || cloned.SessionID != original.SessionID { |
| 105 | + t.Fatalf("Clone() basic fields mismatch: got %+v, want %+v", cloned, original) |
| 106 | + } |
| 107 | + if cloned.Metadata != nil { |
| 108 | + t.Fatalf("Clone().Metadata = %#v, want nil", cloned.Metadata) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestCloneMetadataValueNil(t *testing.T) { |
| 113 | + t.Parallel() |
| 114 | + |
| 115 | + if got := cloneMetadataValue(nil); got != nil { |
| 116 | + t.Fatalf("cloneMetadataValue(nil) = %#v, want nil", got) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestDeepCloneValueEdgeCases(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + |
| 123 | + invalid := deepCloneValue(reflect.Value{}) |
| 124 | + if invalid.IsValid() { |
| 125 | + t.Fatalf("deepCloneValue(invalid).IsValid() = true, want false") |
| 126 | + } |
| 127 | + |
| 128 | + var nilPtr *int |
| 129 | + clonedNilPtr := deepCloneValue(reflect.ValueOf(nilPtr)) |
| 130 | + if !clonedNilPtr.IsNil() { |
| 131 | + t.Fatalf("cloned nil pointer should be nil") |
| 132 | + } |
| 133 | + |
| 134 | + var nilMap map[string]int |
| 135 | + clonedNilMap := deepCloneValue(reflect.ValueOf(nilMap)) |
| 136 | + if !clonedNilMap.IsNil() { |
| 137 | + t.Fatalf("cloned nil map should be nil") |
| 138 | + } |
| 139 | + |
| 140 | + var nilSlice []int |
| 141 | + clonedNilSlice := deepCloneValue(reflect.ValueOf(nilSlice)) |
| 142 | + if !clonedNilSlice.IsNil() { |
| 143 | + t.Fatalf("cloned nil slice should be nil") |
| 144 | + } |
| 145 | + |
| 146 | + // 走到 interface nil 分支。 |
| 147 | + nilIfaceValue := reflect.New(reflect.TypeOf((*any)(nil)).Elem()).Elem() |
| 148 | + clonedNilIface := deepCloneValue(nilIfaceValue) |
| 149 | + if !clonedNilIface.IsNil() { |
| 150 | + t.Fatalf("cloned nil interface should be nil") |
| 151 | + } |
| 152 | + |
| 153 | + arr := [2]map[string]int{ |
| 154 | + {"a": 1}, |
| 155 | + {"b": 2}, |
| 156 | + } |
| 157 | + clonedArr := deepCloneValue(reflect.ValueOf(arr)).Interface().([2]map[string]int) |
| 158 | + clonedArr[0]["a"] = 99 |
| 159 | + if arr[0]["a"] != 1 { |
| 160 | + t.Fatalf("array nested map shared, got %d, want 1", arr[0]["a"]) |
| 161 | + } |
| 162 | + |
| 163 | + // time.Time 的内部字段不可 set,可覆盖 struct 分支中的 CanSet=false 路径。 |
| 164 | + valueWithTime := struct { |
| 165 | + When time.Time |
| 166 | + }{ |
| 167 | + When: time.Now(), |
| 168 | + } |
| 169 | + clonedWithTime := deepCloneValue(reflect.ValueOf(valueWithTime)).Interface().(struct { |
| 170 | + When time.Time |
| 171 | + }) |
| 172 | + if !clonedWithTime.When.Equal(valueWithTime.When) { |
| 173 | + t.Fatalf("cloned struct with time mismatch") |
| 174 | + } |
| 175 | +} |
0 commit comments