setting_service_claude_oauth_system_prompt_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/config"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func resetGatewayForwardingSettingsCacheForTest(t *testing.T) {
  9. t.Helper()
  10. gatewayForwardingSF.Forget("gateway_forwarding")
  11. gatewayForwardingCache.Store(&cachedGatewayForwardingSettings{})
  12. t.Cleanup(func() {
  13. gatewayForwardingSF.Forget("gateway_forwarding")
  14. gatewayForwardingCache.Store(&cachedGatewayForwardingSettings{})
  15. })
  16. }
  17. func TestSettingService_GetClaudeOAuthSystemPromptInjectionSettings(t *testing.T) {
  18. t.Run("defaults to enabled with empty prompt", func(t *testing.T) {
  19. resetGatewayForwardingSettingsCacheForTest(t)
  20. svc := NewSettingService(&gatewayTTLSettingRepo{data: map[string]string{}}, &config.Config{})
  21. enabled, prompt, blocks := svc.GetClaudeOAuthSystemPromptInjectionSettings(context.Background())
  22. require.True(t, enabled)
  23. require.Empty(t, prompt)
  24. require.Empty(t, blocks)
  25. })
  26. t.Run("uses configured switch prompt and blocks", func(t *testing.T) {
  27. resetGatewayForwardingSettingsCacheForTest(t)
  28. const customPrompt = "custom prompt\n\nkeep spacing"
  29. const customBlocks = `[{"type":"text","text":"custom block","cache_control":true}]`
  30. svc := NewSettingService(&gatewayTTLSettingRepo{data: map[string]string{
  31. SettingKeyEnableClaudeOAuthSystemPromptInjection: "false",
  32. SettingKeyClaudeOAuthSystemPrompt: customPrompt,
  33. SettingKeyClaudeOAuthSystemPromptBlocks: customBlocks,
  34. }}, &config.Config{})
  35. enabled, prompt, blocks := svc.GetClaudeOAuthSystemPromptInjectionSettings(context.Background())
  36. require.False(t, enabled)
  37. require.Equal(t, customPrompt, prompt)
  38. require.Equal(t, customBlocks, blocks)
  39. })
  40. }