openai_channel_restriction_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestOpenAISelectAccountForModelWithExclusions_ChannelMappedRestrictionRejectsEarly(t *testing.T) {
  9. t.Parallel()
  10. channelSvc := newTestChannelService(makeStandardRepo(Channel{
  11. ID: 1,
  12. Status: StatusActive,
  13. GroupIDs: []int64{10},
  14. RestrictModels: true,
  15. BillingModelSource: BillingModelSourceChannelMapped,
  16. ModelPricing: []ChannelModelPricing{
  17. {Platform: PlatformOpenAI, Models: []string{"gpt-4o"}},
  18. },
  19. ModelMapping: map[string]map[string]string{
  20. PlatformOpenAI: {"gpt-4.1": "o3-mini"},
  21. },
  22. }, map[int64]string{10: PlatformOpenAI}))
  23. svc := &OpenAIGatewayService{
  24. accountRepo: stubOpenAIAccountRepo{accounts: []Account{
  25. {ID: 1, Platform: PlatformOpenAI, Status: StatusActive, Schedulable: true},
  26. }},
  27. channelService: channelSvc,
  28. }
  29. groupID := int64(10)
  30. _, err := svc.SelectAccountForModelWithExclusions(context.Background(), &groupID, "", "gpt-4.1", nil)
  31. require.ErrorIs(t, err, ErrNoAvailableAccounts)
  32. require.Contains(t, err.Error(), "channel pricing restriction")
  33. }
  34. func TestOpenAISelectAccountForModelWithExclusions_UpstreamRestrictionSkipsDisallowedAccount(t *testing.T) {
  35. t.Parallel()
  36. channelSvc := newTestChannelService(makeStandardRepo(Channel{
  37. ID: 1,
  38. Status: StatusActive,
  39. GroupIDs: []int64{10},
  40. RestrictModels: true,
  41. BillingModelSource: BillingModelSourceUpstream,
  42. ModelPricing: []ChannelModelPricing{
  43. {Platform: PlatformOpenAI, Models: []string{"o3-mini"}},
  44. },
  45. }, map[int64]string{10: PlatformOpenAI}))
  46. svc := &OpenAIGatewayService{
  47. accountRepo: stubOpenAIAccountRepo{accounts: []Account{
  48. {
  49. ID: 1,
  50. Platform: PlatformOpenAI,
  51. Status: StatusActive,
  52. Schedulable: true,
  53. Priority: 10,
  54. Credentials: map[string]any{
  55. "model_mapping": map[string]any{"gpt-4.1": "gpt-4o"},
  56. },
  57. },
  58. {
  59. ID: 2,
  60. Platform: PlatformOpenAI,
  61. Status: StatusActive,
  62. Schedulable: true,
  63. Priority: 20,
  64. Credentials: map[string]any{
  65. "model_mapping": map[string]any{"gpt-4.1": "o3-mini"},
  66. },
  67. },
  68. }},
  69. channelService: channelSvc,
  70. }
  71. groupID := int64(10)
  72. account, err := svc.SelectAccountForModelWithExclusions(context.Background(), &groupID, "", "gpt-4.1", nil)
  73. require.NoError(t, err)
  74. require.NotNil(t, account)
  75. require.Equal(t, int64(2), account.ID)
  76. }
  77. func TestIsUpstreamModelRestrictedByChannel_CompactMappingMatchesForwardPath(t *testing.T) {
  78. t.Parallel()
  79. account := &Account{
  80. Platform: PlatformOpenAI,
  81. Credentials: map[string]any{
  82. "model_mapping": map[string]any{"gpt-5.4-channel": "gpt-5.4-account"},
  83. "compact_model_mapping": map[string]any{"gpt-5.4-account": "gpt-5.4-compact"},
  84. },
  85. }
  86. tests := []struct {
  87. name string
  88. allowedUpstreamModel string
  89. useCompactModelMapping bool
  90. }{
  91. {
  92. name: "legacy compact applies compact mapping after channel and account mapping",
  93. allowedUpstreamModel: "gpt-5.4-compact",
  94. useCompactModelMapping: true,
  95. },
  96. {
  97. name: "native v2 stops after channel and account mapping",
  98. allowedUpstreamModel: "gpt-5.4-account",
  99. useCompactModelMapping: false,
  100. },
  101. }
  102. for _, tt := range tests {
  103. tt := tt
  104. t.Run(tt.name, func(t *testing.T) {
  105. t.Parallel()
  106. channelSvc := newTestChannelService(makeStandardRepo(Channel{
  107. ID: 1,
  108. Status: StatusActive,
  109. GroupIDs: []int64{10},
  110. RestrictModels: true,
  111. BillingModelSource: BillingModelSourceUpstream,
  112. ModelPricing: []ChannelModelPricing{
  113. {Platform: PlatformOpenAI, Models: []string{tt.allowedUpstreamModel}},
  114. },
  115. ModelMapping: map[string]map[string]string{
  116. PlatformOpenAI: {"gpt-5.4": "gpt-5.4-channel"},
  117. },
  118. }, map[int64]string{10: PlatformOpenAI}))
  119. svc := &OpenAIGatewayService{channelService: channelSvc}
  120. mapping := channelSvc.ResolveChannelMapping(context.Background(), 10, "gpt-5.4")
  121. require.True(t, mapping.Mapped)
  122. require.Equal(t, "gpt-5.4-channel", mapping.MappedModel)
  123. ctx := WithOpenAIForwardModel(
  124. context.Background(),
  125. mapping.MappedModel,
  126. tt.useCompactModelMapping,
  127. )
  128. require.False(t, svc.isUpstreamModelRestrictedByChannel(
  129. ctx, 10, account, "gpt-5.4", true,
  130. ))
  131. require.True(t, svc.isUpstreamModelRestrictedByChannel(
  132. context.Background(), 10, account, "gpt-5.4", true,
  133. ), "without the forward-model context the restriction check follows a different chain")
  134. })
  135. }
  136. }
  137. func TestIsUpstreamModelRestrictedByChannel_PassthroughMatchesForwardPath(t *testing.T) {
  138. t.Parallel()
  139. account := &Account{
  140. Platform: PlatformOpenAI,
  141. Type: AccountTypeAPIKey,
  142. Credentials: map[string]any{
  143. "model_mapping": map[string]any{"gpt-5.4-channel": "gpt-5.4-account"},
  144. "compact_model_mapping": map[string]any{
  145. "gpt-5.4-channel": "gpt-5.4-compact",
  146. },
  147. },
  148. Extra: map[string]any{"openai_passthrough": true},
  149. }
  150. tests := []struct {
  151. name string
  152. allowedUpstreamModel string
  153. useCompactModelMapping bool
  154. }{
  155. {
  156. name: "native v2 keeps channel-mapped model and ignores normal account mapping",
  157. allowedUpstreamModel: "gpt-5.4-channel",
  158. useCompactModelMapping: false,
  159. },
  160. {
  161. name: "legacy compact applies compact mapping to channel-mapped model",
  162. allowedUpstreamModel: "gpt-5.4-compact",
  163. useCompactModelMapping: true,
  164. },
  165. }
  166. for _, tt := range tests {
  167. tt := tt
  168. t.Run(tt.name, func(t *testing.T) {
  169. t.Parallel()
  170. channelSvc := newTestChannelService(makeStandardRepo(Channel{
  171. ID: 1,
  172. Status: StatusActive,
  173. GroupIDs: []int64{10},
  174. RestrictModels: true,
  175. BillingModelSource: BillingModelSourceUpstream,
  176. ModelPricing: []ChannelModelPricing{
  177. {Platform: PlatformOpenAI, Models: []string{tt.allowedUpstreamModel}},
  178. },
  179. ModelMapping: map[string]map[string]string{
  180. PlatformOpenAI: {"gpt-5.4": "gpt-5.4-channel"},
  181. },
  182. }, map[int64]string{10: PlatformOpenAI}))
  183. svc := &OpenAIGatewayService{channelService: channelSvc}
  184. mapping := channelSvc.ResolveChannelMapping(context.Background(), 10, "gpt-5.4")
  185. require.True(t, mapping.Mapped)
  186. require.Equal(t, "gpt-5.4-channel", mapping.MappedModel)
  187. ctx := WithOpenAIForwardModel(
  188. context.Background(),
  189. mapping.MappedModel,
  190. tt.useCompactModelMapping,
  191. )
  192. require.False(t, svc.isUpstreamModelRestrictedByChannel(
  193. ctx, 10, account, "gpt-5.4", true,
  194. ))
  195. })
  196. }
  197. }
  198. func TestIsUpstreamModelRestrictedByChannel_PassthroughFlagWithRawChatFallbackMatchesForwardPath(t *testing.T) {
  199. t.Parallel()
  200. account := &Account{
  201. Platform: PlatformOpenAI,
  202. Type: AccountTypeAPIKey,
  203. Credentials: map[string]any{
  204. "model_mapping": map[string]any{"gpt-5.4-channel": "gpt-5.4-account"},
  205. "compact_model_mapping": map[string]any{
  206. "gpt-5.4-account": "gpt-5.4-compact",
  207. },
  208. },
  209. Extra: map[string]any{
  210. "openai_passthrough": true,
  211. "openai_responses_supported": false,
  212. },
  213. }
  214. for _, useCompactModelMapping := range []bool{false, true} {
  215. useCompactModelMapping := useCompactModelMapping
  216. name := "native v2"
  217. if useCompactModelMapping {
  218. name = "legacy compact"
  219. }
  220. t.Run(name, func(t *testing.T) {
  221. t.Parallel()
  222. channelSvc := newTestChannelService(makeStandardRepo(Channel{
  223. ID: 1,
  224. Status: StatusActive,
  225. GroupIDs: []int64{10},
  226. RestrictModels: true,
  227. BillingModelSource: BillingModelSourceUpstream,
  228. ModelPricing: []ChannelModelPricing{
  229. {Platform: PlatformOpenAI, Models: []string{"gpt-5.4-account"}},
  230. },
  231. ModelMapping: map[string]map[string]string{
  232. PlatformOpenAI: {"gpt-5.4": "gpt-5.4-channel"},
  233. },
  234. }, map[int64]string{10: PlatformOpenAI}))
  235. svc := &OpenAIGatewayService{channelService: channelSvc}
  236. ctx := WithOpenAIForwardModel(
  237. context.Background(),
  238. "gpt-5.4-channel",
  239. useCompactModelMapping,
  240. )
  241. require.False(t, svc.isUpstreamModelRestrictedByChannel(
  242. ctx, 10, account, "gpt-5.4", true,
  243. ))
  244. })
  245. }
  246. }
  247. func TestIsUpstreamModelRestrictedByChannel_ForwardModelContextMatchesNormalForwardPath(t *testing.T) {
  248. t.Parallel()
  249. account := &Account{
  250. Platform: PlatformOpenAI,
  251. Type: AccountTypeAPIKey,
  252. Credentials: map[string]any{
  253. "model_mapping": map[string]any{"gpt-5.4-channel": "gpt-5.4-account"},
  254. },
  255. Extra: map[string]any{
  256. "openai_passthrough": true,
  257. "openai_responses_supported": false,
  258. },
  259. }
  260. channelSvc := newTestChannelService(makeStandardRepo(Channel{
  261. ID: 1,
  262. Status: StatusActive,
  263. GroupIDs: []int64{10},
  264. RestrictModels: true,
  265. BillingModelSource: BillingModelSourceUpstream,
  266. ModelPricing: []ChannelModelPricing{
  267. {Platform: PlatformOpenAI, Models: []string{"gpt-5.4-account"}},
  268. },
  269. ModelMapping: map[string]map[string]string{
  270. PlatformOpenAI: {"gpt-5.4": "gpt-5.4-channel"},
  271. },
  272. }, map[int64]string{10: PlatformOpenAI}))
  273. svc := &OpenAIGatewayService{channelService: channelSvc}
  274. ctx := WithOpenAIForwardModel(context.Background(), "gpt-5.4-channel", false)
  275. require.False(t, svc.isUpstreamModelRestrictedByChannel(
  276. ctx, 10, account, "gpt-5.4", false,
  277. ))
  278. }
  279. func TestOpenAISelectAccountForModelWithExclusions_StickyRestrictedUpstreamFallsBack(t *testing.T) {
  280. t.Parallel()
  281. channelSvc := newTestChannelService(makeStandardRepo(Channel{
  282. ID: 1,
  283. Status: StatusActive,
  284. GroupIDs: []int64{10},
  285. RestrictModels: true,
  286. BillingModelSource: BillingModelSourceUpstream,
  287. ModelPricing: []ChannelModelPricing{
  288. {Platform: PlatformOpenAI, Models: []string{"o3-mini"}},
  289. },
  290. }, map[int64]string{10: PlatformOpenAI}))
  291. cache := &stubGatewayCache{
  292. sessionBindings: map[string]int64{"openai:sticky-session": 1},
  293. }
  294. svc := &OpenAIGatewayService{
  295. accountRepo: stubOpenAIAccountRepo{accounts: []Account{
  296. {
  297. ID: 1,
  298. Platform: PlatformOpenAI,
  299. Status: StatusActive,
  300. Schedulable: true,
  301. Priority: 10,
  302. Credentials: map[string]any{
  303. "model_mapping": map[string]any{"gpt-4.1": "gpt-4o"},
  304. },
  305. },
  306. {
  307. ID: 2,
  308. Platform: PlatformOpenAI,
  309. Status: StatusActive,
  310. Schedulable: true,
  311. Priority: 20,
  312. Credentials: map[string]any{
  313. "model_mapping": map[string]any{"gpt-4.1": "o3-mini"},
  314. },
  315. },
  316. }},
  317. channelService: channelSvc,
  318. cache: cache,
  319. }
  320. groupID := int64(10)
  321. account, err := svc.SelectAccountForModelWithExclusions(context.Background(), &groupID, "sticky-session", "gpt-4.1", nil)
  322. require.NoError(t, err)
  323. require.NotNil(t, account)
  324. require.Equal(t, int64(2), account.ID)
  325. require.Equal(t, 1, cache.deletedSessions["openai:sticky-session"])
  326. require.Equal(t, int64(2), cache.sessionBindings["openai:sticky-session"])
  327. }