package ctxmgr_test import ( "context" "math" "testing" "chat-agent-system/internal/ctxmgr" ) func TestDeterministicEmbedder_EmbeddingProperties(t *testing.T) { emb := ctxmgr.NewDeterministicEmbedder(1024) ctx := context.Background() texts := []string{ "超声软组织切割止血手术设备", "微创外科硬管内窥镜摄像系统", "超声软组织切割止血手术设备", // 重复文本验证确定性 } vecs, err := emb.Embed(ctx, texts) if err != nil { t.Fatalf("embed failed: %v", err) } if len(vecs) != 3 { t.Fatalf("expected 3 vectors, got %d", len(vecs)) } // 1. 验证维度 for i, v := range vecs { if len(v) != 1024 { t.Fatalf("vector %d length expected 1024, got %d", i, len(v)) } // 验证单位化模长 (L2 norm ≈ 1.0) var sumSq float64 for _, val := range v { sumSq += float64(val * val) } norm := math.Sqrt(sumSq) if math.Abs(norm-1.0) > 1e-4 { t.Fatalf("vector %d not normalized, norm=%f", i, norm) } } // 2. 验证确定性:相同文本向量完全一致 for i := 0; i < 1024; i++ { if vecs[0][i] != vecs[2][i] { t.Fatalf("deterministic mismatch at index %d: %v vs %v", i, vecs[0][i], vecs[2][i]) } } // 3. 验证语义区别:不同文本余弦相似度应小于 0.95 var dotProduct float32 for i := 0; i < 1024; i++ { dotProduct += vecs[0][i] * vecs[1][i] } if dotProduct > 0.95 { t.Fatalf("distinct texts should not have near-identical embeddings: dotProduct=%f", dotProduct) } // 4. 验证 VectorLiteral lit := ctxmgr.VectorLiteral(vecs[0][:3]) if lit == "" || lit[0] != '[' || lit[len(lit)-1] != ']' { t.Fatalf("invalid vector literal: %s", lit) } }