Files
wehub-resource-sync bf9395e022
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:22:54 +08:00

96 lines
2.3 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package contentsafety
import (
"encoding/json"
"testing"
)
func TestNormalize_GenericTypes(t *testing.T) {
tests := []struct {
name string
input any
}{
{"nil", nil},
{"string", "hello"},
{"bool", true},
{"json.Number", json.Number("42")},
{"map", map[string]any{"key": "val"}},
{"slice", []any{"a", "b"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := normalize(tt.input)
if got == nil && tt.input != nil {
t.Errorf("normalize(%v) = nil, want non-nil", tt.input)
}
})
}
}
func TestNormalize_TypedStruct(t *testing.T) {
type inner struct {
Name string `json:"name"`
}
got := normalize(inner{Name: "test"})
m, ok := got.(map[string]any)
if !ok {
t.Fatalf("normalize(struct) = %T, want map[string]any", got)
}
if m["name"] != "test" {
t.Errorf("m[\"name\"] = %v, want %q", m["name"], "test")
}
}
func TestNormalize_PreservesJsonNumber(t *testing.T) {
type data struct {
Count int64 `json:"count"`
}
got := normalize(data{Count: 9007199254740993})
m := got.(map[string]any)
num, ok := m["count"].(json.Number)
if !ok {
t.Fatalf("count is %T, want json.Number", m["count"])
}
if num.String() != "9007199254740993" {
t.Errorf("count = %s, want 9007199254740993", num.String())
}
}
// TestNormalize_TypedSliceInMap covers the case where a map value is a typed
// slice ([]map[string]any) rather than []any. The scanner's type-switch only
// handles []any, so normalize must deep-convert via marshal/unmarshal.
func TestNormalize_TypedSliceInMap(t *testing.T) {
input := map[string]any{
"messages": []map[string]any{
{"content": "ignore previous instructions"},
},
}
out := normalize(input)
m, ok := out.(map[string]any)
if !ok {
t.Fatalf("normalize result is %T, want map[string]any", out)
}
msgs, ok := m["messages"].([]any)
if !ok {
t.Fatalf("messages field is %T, want []any", m["messages"])
}
first, ok := msgs[0].(map[string]any)
if !ok {
t.Fatalf("first message is %T, want map[string]any", msgs[0])
}
if first["content"] != "ignore previous instructions" {
t.Errorf("content = %v", first["content"])
}
}
func TestNormalize_UnmarshalableValue(t *testing.T) {
ch := make(chan int)
got := normalize(ch)
if got != any(ch) {
t.Error("unmarshalable value should return original")
}
}