anthropic_session_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //go:build unit
  2. package service
  3. import (
  4. "strings"
  5. "testing"
  6. )
  7. func mustParseAnthropicDigestRequest(t *testing.T, body string) *ParsedRequest {
  8. t.Helper()
  9. parsed, err := ParseGatewayRequest(NewRequestBodyRef([]byte(body)), "")
  10. if err != nil {
  11. t.Fatalf("ParseGatewayRequest failed: %v", err)
  12. }
  13. return parsed
  14. }
  15. func TestBuildAnthropicDigestChain_NilRequest(t *testing.T) {
  16. result := BuildAnthropicDigestChain(nil)
  17. if result != "" {
  18. t.Errorf("expected empty string for nil request, got: %s", result)
  19. }
  20. }
  21. func TestBuildAnthropicDigestChain_EmptyMessages(t *testing.T) {
  22. parsed := mustParseAnthropicDigestRequest(t, `{"messages":[]}`)
  23. result := BuildAnthropicDigestChain(parsed)
  24. if result != "" {
  25. t.Errorf("expected empty string for empty messages, got: %s", result)
  26. }
  27. }
  28. func TestBuildAnthropicDigestChain_SingleUserMessage(t *testing.T) {
  29. parsed := mustParseAnthropicDigestRequest(t, `{"messages":[{"role":"user","content":"hello"}]}`)
  30. result := BuildAnthropicDigestChain(parsed)
  31. parts := splitChain(result)
  32. if len(parts) != 1 {
  33. t.Fatalf("expected 1 part, got %d: %s", len(parts), result)
  34. }
  35. if !strings.HasPrefix(parts[0], "u:") {
  36. t.Errorf("expected prefix 'u:', got: %s", parts[0])
  37. }
  38. }
  39. func TestBuildAnthropicDigestChain_UserAndAssistant(t *testing.T) {
  40. parsed := mustParseAnthropicDigestRequest(t, `{"messages":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}]}`)
  41. result := BuildAnthropicDigestChain(parsed)
  42. parts := splitChain(result)
  43. if len(parts) != 2 {
  44. t.Fatalf("expected 2 parts, got %d: %s", len(parts), result)
  45. }
  46. if !strings.HasPrefix(parts[0], "u:") {
  47. t.Errorf("part[0] expected prefix 'u:', got: %s", parts[0])
  48. }
  49. if !strings.HasPrefix(parts[1], "a:") {
  50. t.Errorf("part[1] expected prefix 'a:', got: %s", parts[1])
  51. }
  52. }
  53. func TestBuildAnthropicDigestChain_WithSystemString(t *testing.T) {
  54. parsed := mustParseAnthropicDigestRequest(t, `{"system":"You are a helpful assistant","messages":[{"role":"user","content":"hello"}]}`)
  55. result := BuildAnthropicDigestChain(parsed)
  56. parts := splitChain(result)
  57. if len(parts) != 2 {
  58. t.Fatalf("expected 2 parts (s + u), got %d: %s", len(parts), result)
  59. }
  60. if !strings.HasPrefix(parts[0], "s:") {
  61. t.Errorf("part[0] expected prefix 's:', got: %s", parts[0])
  62. }
  63. if !strings.HasPrefix(parts[1], "u:") {
  64. t.Errorf("part[1] expected prefix 'u:', got: %s", parts[1])
  65. }
  66. }
  67. func TestBuildAnthropicDigestChain_WithSystemContentBlocks(t *testing.T) {
  68. parsed := mustParseAnthropicDigestRequest(t, `{"system":[{"type":"text","text":"You are a helpful assistant"}],"messages":[{"role":"user","content":"hello"}]}`)
  69. result := BuildAnthropicDigestChain(parsed)
  70. parts := splitChain(result)
  71. if len(parts) != 2 {
  72. t.Fatalf("expected 2 parts (s + u), got %d: %s", len(parts), result)
  73. }
  74. if !strings.HasPrefix(parts[0], "s:") {
  75. t.Errorf("part[0] expected prefix 's:', got: %s", parts[0])
  76. }
  77. }
  78. func TestBuildAnthropicDigestChain_ConversationPrefixRelationship(t *testing.T) {
  79. // 核心测试:验证对话增长时链的前缀关系
  80. // 上一轮的完整链一定是下一轮链的前缀
  81. round1 := mustParseAnthropicDigestRequest(t, `{"system":"You are a helpful assistant","messages":[{"role":"user","content":"hello"}]}`)
  82. chain1 := BuildAnthropicDigestChain(round1)
  83. round2 := mustParseAnthropicDigestRequest(t, `{"system":"You are a helpful assistant","messages":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"},{"role":"user","content":"how are you?"}]}`)
  84. chain2 := BuildAnthropicDigestChain(round2)
  85. round3 := mustParseAnthropicDigestRequest(t, `{"system":"You are a helpful assistant","messages":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"},{"role":"user","content":"how are you?"},{"role":"assistant","content":"I'm doing well"},{"role":"user","content":"great"}]}`)
  86. chain3 := BuildAnthropicDigestChain(round3)
  87. t.Logf("Chain1: %s", chain1)
  88. t.Logf("Chain2: %s", chain2)
  89. t.Logf("Chain3: %s", chain3)
  90. if !strings.HasPrefix(chain2, chain1) {
  91. t.Errorf("chain1 should be prefix of chain2:\n chain1: %s\n chain2: %s", chain1, chain2)
  92. }
  93. if !strings.HasPrefix(chain3, chain2) {
  94. t.Errorf("chain2 should be prefix of chain3:\n chain2: %s\n chain3: %s", chain2, chain3)
  95. }
  96. if !strings.HasPrefix(chain3, chain1) {
  97. t.Errorf("chain1 should be prefix of chain3:\n chain1: %s\n chain3: %s", chain1, chain3)
  98. }
  99. }
  100. func TestBuildAnthropicDigestChain_DifferentSystemProducesDifferentChain(t *testing.T) {
  101. parsed1 := mustParseAnthropicDigestRequest(t, `{"system":"System A","messages":[{"role":"user","content":"hello"}]}`)
  102. parsed2 := mustParseAnthropicDigestRequest(t, `{"system":"System B","messages":[{"role":"user","content":"hello"}]}`)
  103. chain1 := BuildAnthropicDigestChain(parsed1)
  104. chain2 := BuildAnthropicDigestChain(parsed2)
  105. if chain1 == chain2 {
  106. t.Error("Different system prompts should produce different chains")
  107. }
  108. parts1 := splitChain(chain1)
  109. parts2 := splitChain(chain2)
  110. if parts1[1] != parts2[1] {
  111. t.Error("Same user message should produce same hash regardless of system")
  112. }
  113. }
  114. func TestBuildAnthropicDigestChain_DifferentContentProducesDifferentChain(t *testing.T) {
  115. parsed1 := mustParseAnthropicDigestRequest(t, `{"messages":[{"role":"user","content":"hello"},{"role":"assistant","content":"ORIGINAL reply"},{"role":"user","content":"next"}]}`)
  116. parsed2 := mustParseAnthropicDigestRequest(t, `{"messages":[{"role":"user","content":"hello"},{"role":"assistant","content":"TAMPERED reply"},{"role":"user","content":"next"}]}`)
  117. chain1 := BuildAnthropicDigestChain(parsed1)
  118. chain2 := BuildAnthropicDigestChain(parsed2)
  119. if chain1 == chain2 {
  120. t.Error("Different content should produce different chains")
  121. }
  122. parts1 := splitChain(chain1)
  123. parts2 := splitChain(chain2)
  124. if parts1[0] != parts2[0] {
  125. t.Error("First user message hash should be the same")
  126. }
  127. if parts1[1] == parts2[1] {
  128. t.Error("Assistant reply hash should differ")
  129. }
  130. }
  131. func TestBuildAnthropicDigestChain_Deterministic(t *testing.T) {
  132. parsed := mustParseAnthropicDigestRequest(t, `{"system":"test system","messages":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi"}]}`)
  133. chain1 := BuildAnthropicDigestChain(parsed)
  134. chain2 := BuildAnthropicDigestChain(parsed)
  135. if chain1 != chain2 {
  136. t.Errorf("BuildAnthropicDigestChain not deterministic: %s vs %s", chain1, chain2)
  137. }
  138. }
  139. func TestBuildAnthropicDigestChain_CanonicalJSON(t *testing.T) {
  140. parsed1 := mustParseAnthropicDigestRequest(t, `{"system":[{"type":"text","text":"system"}],"messages":[{"role":"user","content":{"type":"text","text":"hello"}}]}`)
  141. parsed2 := mustParseAnthropicDigestRequest(t, `{"system":[{"text":"system","type":"text"}],"messages":[{"role":"user","content":{"text":"hello","type":"text"}}]}`)
  142. chain1 := BuildAnthropicDigestChain(parsed1)
  143. chain2 := BuildAnthropicDigestChain(parsed2)
  144. if chain1 != chain2 {
  145. t.Errorf("semantically equivalent JSON should produce same chain: %s vs %s", chain1, chain2)
  146. }
  147. }
  148. func TestGenerateAnthropicDigestSessionKey(t *testing.T) {
  149. tests := []struct {
  150. name string
  151. prefixHash string
  152. uuid string
  153. want string
  154. }{
  155. {
  156. name: "normal 16 char hash with uuid",
  157. prefixHash: "abcdefgh12345678",
  158. uuid: "550e8400-e29b-41d4-a716-446655440000",
  159. want: "anthropic:digest:abcdefgh:550e8400",
  160. },
  161. {
  162. name: "exactly 8 chars",
  163. prefixHash: "12345678",
  164. uuid: "abcdefgh",
  165. want: "anthropic:digest:12345678:abcdefgh",
  166. },
  167. {
  168. name: "short values",
  169. prefixHash: "abc",
  170. uuid: "xyz",
  171. want: "anthropic:digest:abc:xyz",
  172. },
  173. {
  174. name: "empty values",
  175. prefixHash: "",
  176. uuid: "",
  177. want: "anthropic:digest::",
  178. },
  179. }
  180. for _, tt := range tests {
  181. t.Run(tt.name, func(t *testing.T) {
  182. got := GenerateAnthropicDigestSessionKey(tt.prefixHash, tt.uuid)
  183. if got != tt.want {
  184. t.Errorf("GenerateAnthropicDigestSessionKey(%q, %q) = %q, want %q", tt.prefixHash, tt.uuid, got, tt.want)
  185. }
  186. })
  187. }
  188. t.Run("different uuid different key", func(t *testing.T) {
  189. hash := "sameprefix123456"
  190. result1 := GenerateAnthropicDigestSessionKey(hash, "uuid0001-session-a")
  191. result2 := GenerateAnthropicDigestSessionKey(hash, "uuid0002-session-b")
  192. if result1 == result2 {
  193. t.Errorf("Different UUIDs should produce different session keys: %s vs %s", result1, result2)
  194. }
  195. })
  196. }
  197. func TestAnthropicSessionTTL(t *testing.T) {
  198. ttl := AnthropicSessionTTL()
  199. if ttl.Seconds() != 300 {
  200. t.Errorf("expected 300 seconds, got: %v", ttl.Seconds())
  201. }
  202. }
  203. func TestBuildAnthropicDigestChain_ContentBlocks(t *testing.T) {
  204. parsed := mustParseAnthropicDigestRequest(t, `{"messages":[{"role":"user","content":[{"type":"text","text":"describe this image"},{"type":"image","source":{"type":"base64"}}]}]}`)
  205. result := BuildAnthropicDigestChain(parsed)
  206. parts := splitChain(result)
  207. if len(parts) != 1 {
  208. t.Fatalf("expected 1 part, got %d: %s", len(parts), result)
  209. }
  210. if !strings.HasPrefix(parts[0], "u:") {
  211. t.Errorf("expected prefix 'u:', got: %s", parts[0])
  212. }
  213. }