aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Instances.hs
blob: c7a3624c4620da4b71134acb20f76ccca69b528b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-unused-matches #-}

module Instances where

import G4fClient.Model
import G4fClient.Core

import qualified Data.Aeson as A
import qualified Data.ByteString.Lazy as BL
import qualified Data.HashMap.Strict as HM
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Time as TI
import qualified Data.Vector as V
import Data.String (fromString)

import Control.Monad
import Data.Char (isSpace)
import Data.List (sort)
import Test.QuickCheck

import ApproxEq

instance Arbitrary T.Text where
  arbitrary = T.pack <$> arbitrary

instance Arbitrary TI.Day where
  arbitrary = TI.ModifiedJulianDay . (2000 +) <$> arbitrary
  shrink = (TI.ModifiedJulianDay <$>) . shrink . TI.toModifiedJulianDay

instance Arbitrary TI.UTCTime where
  arbitrary =
    TI.UTCTime <$> arbitrary <*> (TI.secondsToDiffTime <$> choose (0, 86401))

instance Arbitrary BL.ByteString where
    arbitrary = BL.pack <$> arbitrary
    shrink xs = BL.pack <$> shrink (BL.unpack xs)

instance Arbitrary ByteArray where
    arbitrary = ByteArray <$> arbitrary
    shrink (ByteArray xs) = ByteArray <$> shrink xs

instance Arbitrary Binary where
    arbitrary = Binary <$> arbitrary
    shrink (Binary xs) = Binary <$> shrink xs

instance Arbitrary DateTime where
    arbitrary = DateTime <$> arbitrary
    shrink (DateTime xs) = DateTime <$> shrink xs

instance Arbitrary Date where
    arbitrary = Date <$> arbitrary
    shrink (Date xs) = Date <$> shrink xs

#if MIN_VERSION_aeson(2,0,0)
#else
-- | A naive Arbitrary instance for A.Value:
instance Arbitrary A.Value where
  arbitrary = arbitraryValue
#endif

arbitraryValue :: Gen A.Value
arbitraryValue =
  frequency [(3, simpleTypes), (1, arrayTypes), (1, objectTypes)]
    where
      simpleTypes :: Gen A.Value
      simpleTypes =
        frequency
          [ (1, return A.Null)
          , (2, liftM A.Bool (arbitrary :: Gen Bool))
          , (2, liftM (A.Number . fromIntegral) (arbitrary :: Gen Int))
          , (2, liftM (A.String . T.pack) (arbitrary :: Gen String))
          ]
      mapF (k, v) = (fromString k, v)
      simpleAndArrays = frequency [(1, sized sizedArray), (4, simpleTypes)]
      arrayTypes = sized sizedArray
      objectTypes = sized sizedObject
      sizedArray n = liftM (A.Array . V.fromList) $ replicateM n simpleTypes
      sizedObject n =
        liftM (A.object . map mapF) $
        replicateM n $ (,) <$> (arbitrary :: Gen String) <*> simpleAndArrays

-- | Checks if a given list has no duplicates in _O(n log n)_.
hasNoDups
  :: (Ord a)
  => [a] -> Bool
hasNoDups = go Set.empty
  where
    go _ [] = True
    go s (x:xs)
      | s' <- Set.insert x s
      , Set.size s' > Set.size s = go s' xs
      | otherwise = False

instance ApproxEq TI.Day where
  (=~) = (==)

arbitraryReduced :: Arbitrary a => Int -> Gen a
arbitraryReduced n = resize (n `div` 2) arbitrary

arbitraryReducedMaybe :: Arbitrary a => Int -> Gen (Maybe a)
arbitraryReducedMaybe 0 = elements [Nothing]
arbitraryReducedMaybe n = arbitraryReduced n

arbitraryReducedMaybeValue :: Int -> Gen (Maybe A.Value)
arbitraryReducedMaybeValue 0 = elements [Nothing]
arbitraryReducedMaybeValue n = do
  generated <- arbitraryReduced n
  if generated == Just A.Null
    then return Nothing
    else return generated

-- * Models

instance Arbitrary AnyType where
  arbitrary = AnyType <$> arbitrary

instance Arbitrary ApiKey where
  arbitrary = sized genApiKey

genApiKey :: Int -> Gen ApiKey
genApiKey n =
  
  pure ApiKey
   
instance Arbitrary AudioResponseModel where
  arbitrary = sized genAudioResponseModel

genAudioResponseModel :: Int -> Gen AudioResponseModel
genAudioResponseModel n =
  AudioResponseModel
    <$> arbitrary -- audioResponseModelData :: Text
    <*> arbitraryReducedMaybe n -- audioResponseModelTranscript :: Maybe Text
  
instance Arbitrary AudioSpeechConfig where
  arbitrary = sized genAudioSpeechConfig

genAudioSpeechConfig :: Int -> Gen AudioSpeechConfig
genAudioSpeechConfig n =
  AudioSpeechConfig
    <$> arbitrary -- audioSpeechConfigInput :: Text
    <*> arbitraryReducedMaybe n -- audioSpeechConfigModel :: Maybe Text
    <*> arbitraryReducedMaybe n -- audioSpeechConfigProvider :: Maybe Text
    <*> arbitraryReducedMaybe n -- audioSpeechConfigVoice :: Maybe Text
    <*> arbitraryReducedMaybe n -- audioSpeechConfigInstrcutions :: Maybe Text
    <*> arbitraryReducedMaybe n -- audioSpeechConfigResponseFormat :: Maybe Text
    <*> arbitraryReducedMaybe n -- audioSpeechConfigLanguage :: Maybe Text
    <*> arbitraryReducedMaybe n -- audioSpeechConfigDownloadMedia :: Maybe Bool
  
instance Arbitrary ChatCompletion where
  arbitrary = sized genChatCompletion

genChatCompletion :: Int -> Gen ChatCompletion
genChatCompletion n =
  ChatCompletion
    <$> arbitrary -- chatCompletionId :: Text
    <*> arbitrary -- chatCompletionObject :: Text
    <*> arbitrary -- chatCompletionCreated :: Int
    <*> arbitrary -- chatCompletionModel :: Text
    <*> arbitrary -- chatCompletionProvider :: Text
    <*> arbitraryReduced n -- chatCompletionChoices :: [ChatCompletionChoice]
    <*> arbitraryReduced n -- chatCompletionUsage :: UsageModel
    <*> arbitraryReduced n -- chatCompletionConversation :: (Map.Map String AnyType)
  
instance Arbitrary ChatCompletionChoice where
  arbitrary = sized genChatCompletionChoice

genChatCompletionChoice :: Int -> Gen ChatCompletionChoice
genChatCompletionChoice n =
  ChatCompletionChoice
    <$> arbitrary -- chatCompletionChoiceIndex :: Int
    <*> arbitraryReduced n -- chatCompletionChoiceMessage :: ChatCompletionMessage
    <*> arbitrary -- chatCompletionChoiceFinishReason :: Text
  
instance Arbitrary ChatCompletionMessage where
  arbitrary = sized genChatCompletionMessage

genChatCompletionMessage :: Int -> Gen ChatCompletionMessage
genChatCompletionMessage n =
  ChatCompletionMessage
    <$> arbitrary -- chatCompletionMessageRole :: Text
    <*> arbitrary -- chatCompletionMessageContent :: Text
    <*> arbitraryReducedMaybe n -- chatCompletionMessageReasoning :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionMessageToolCalls :: Maybe [ToolCallModel]
    <*> arbitraryReducedMaybe n -- chatCompletionMessageAudio :: Maybe AudioResponseModel
  
instance Arbitrary ChatCompletionsConfig where
  arbitrary = sized genChatCompletionsConfig

genChatCompletionsConfig :: Int -> Gen ChatCompletionsConfig
genChatCompletionsConfig n =
  ChatCompletionsConfig
    <$> arbitraryReducedMaybe n -- chatCompletionsConfigModel :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigProvider :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigMedia :: Maybe [[AnyType]]
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigModalities :: Maybe [Text]
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigTemperature :: Maybe Double
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigPresencePenalty :: Maybe Double
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigFrequencyPenalty :: Maybe Double
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigTopP :: Maybe Double
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigMaxTokens :: Maybe Int
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigStop :: Maybe Stop
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigApiKey :: Maybe ApiKey
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigBaseUrl :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigWebSearch :: Maybe Bool
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigProxy :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigConversation :: Maybe (Map.Map String AnyType)
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigTimeout :: Maybe Int
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigStreamTimeout :: Maybe Int
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigToolCalls :: Maybe [AnyType]
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigReasoningEffort :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigLogitBias :: Maybe (Map.Map String AnyType)
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigAudio :: Maybe (Map.Map String AnyType)
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigResponseFormat :: Maybe (Map.Map String AnyType)
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigDownloadMedia :: Maybe Bool
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigRaw :: Maybe Bool
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigExtraBody :: Maybe (Map.Map String AnyType)
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigToolEmulation :: Maybe Bool
    <*> arbitraryReduced n -- chatCompletionsConfigMessages :: [Message]
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigStream :: Maybe Bool
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigImage :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigImageName :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigImages :: Maybe [[AnyType]]
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigTools :: Maybe [AnyType]
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigParallelToolCalls :: Maybe Bool
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigToolChoice :: Maybe Text
    <*> arbitraryReducedMaybe n -- chatCompletionsConfigConversationId :: Maybe Text
  
instance Arbitrary CompletionTokenDetails where
  arbitrary = sized genCompletionTokenDetails

genCompletionTokenDetails :: Int -> Gen CompletionTokenDetails
genCompletionTokenDetails n =
  CompletionTokenDetails
    <$> arbitrary -- completionTokenDetailsReasoningTokens :: Int
    <*> arbitrary -- completionTokenDetailsImageTokens :: Int
    <*> arbitrary -- completionTokenDetailsAudioTokens :: Int
  
instance Arbitrary Content where
  arbitrary = sized genContent

genContent :: Int -> Gen Content
genContent n =
  
  pure Content
   
instance Arbitrary ContentPart where
  arbitrary = sized genContentPart

genContentPart :: Int -> Gen ContentPart
genContentPart n =
  ContentPart
    <$> arbitraryReducedMaybe n -- contentPartType :: Maybe Text
    <*> arbitraryReducedMaybe n -- contentPartText :: Maybe Text
    <*> arbitraryReducedMaybe n -- contentPartImageUrl :: Maybe (Map.Map String Text)
    <*> arbitraryReducedMaybe n -- contentPartInputAudio :: Maybe (Map.Map String Text)
    <*> arbitraryReducedMaybe n -- contentPartBucketId :: Maybe Text
    <*> arbitraryReducedMaybe n -- contentPartName :: Maybe Text
  
instance Arbitrary ErrorResponseMessageModel where
  arbitrary = sized genErrorResponseMessageModel

genErrorResponseMessageModel :: Int -> Gen ErrorResponseMessageModel
genErrorResponseMessageModel n =
  ErrorResponseMessageModel
    <$> arbitrary -- errorResponseMessageModelMessage :: Text
  
instance Arbitrary ErrorResponseModel where
  arbitrary = sized genErrorResponseModel

genErrorResponseModel :: Int -> Gen ErrorResponseModel
genErrorResponseModel n =
  ErrorResponseModel
    <$> arbitraryReduced n -- errorResponseModelError :: ErrorResponseMessageModel
    <*> arbitraryReducedMaybe n -- errorResponseModelModel :: Maybe Text
    <*> arbitraryReducedMaybe n -- errorResponseModelProvider :: Maybe Text
  
instance Arbitrary FileResponseModel where
  arbitrary = sized genFileResponseModel

genFileResponseModel :: Int -> Gen FileResponseModel
genFileResponseModel n =
  FileResponseModel
    <$> arbitrary -- fileResponseModelFilename :: Text
  
instance Arbitrary HTTPValidationError where
  arbitrary = sized genHTTPValidationError

genHTTPValidationError :: Int -> Gen HTTPValidationError
genHTTPValidationError n =
  HTTPValidationError
    <$> arbitraryReducedMaybe n -- hTTPValidationErrorDetail :: Maybe [ValidationError]
  
instance Arbitrary Image where
  arbitrary = sized genImage

genImage :: Int -> Gen Image
genImage n =
  Image
    <$> arbitrary -- imageUrl :: Text
    <*> arbitrary -- imageB64Json :: Text
    <*> arbitrary -- imageRevisedPrompt :: Text
  
instance Arbitrary ImageGenerationConfig where
  arbitrary = sized genImageGenerationConfig

genImageGenerationConfig :: Int -> Gen ImageGenerationConfig
genImageGenerationConfig n =
  ImageGenerationConfig
    <$> arbitrary -- imageGenerationConfigPrompt :: Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigModel :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigProvider :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigResponseFormat :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigApiKey :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigProxy :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigWidth :: Maybe Int
    <*> arbitraryReducedMaybe n -- imageGenerationConfigHeight :: Maybe Int
    <*> arbitraryReducedMaybe n -- imageGenerationConfigNumInferenceSteps :: Maybe Int
    <*> arbitraryReducedMaybe n -- imageGenerationConfigSeed :: Maybe Int
    <*> arbitraryReducedMaybe n -- imageGenerationConfigGuidanceScale :: Maybe Int
    <*> arbitraryReducedMaybe n -- imageGenerationConfigAspectRatio :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigN :: Maybe Int
    <*> arbitraryReducedMaybe n -- imageGenerationConfigNegativePrompt :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigResolution :: Maybe Text
    <*> arbitraryReducedMaybe n -- imageGenerationConfigAudio :: Maybe (Map.Map String AnyType)
    <*> arbitraryReducedMaybe n -- imageGenerationConfigDownloadMedia :: Maybe Bool
  
instance Arbitrary ImagesResponse where
  arbitrary = sized genImagesResponse

genImagesResponse :: Int -> Gen ImagesResponse
genImagesResponse n =
  ImagesResponse
    <$> arbitraryReduced n -- imagesResponseData :: [Image]
    <*> arbitrary -- imagesResponseModel :: Text
    <*> arbitrary -- imagesResponseProvider :: Text
    <*> arbitrary -- imagesResponseCreated :: Int
  
instance Arbitrary Message where
  arbitrary = sized genMessage

genMessage :: Int -> Gen Message
genMessage n =
  Message
    <$> arbitrary -- messageRole :: Text
    <*> arbitraryReduced n -- messageContent :: Content
  
instance Arbitrary ModelResponseModel where
  arbitrary = sized genModelResponseModel

genModelResponseModel :: Int -> Gen ModelResponseModel
genModelResponseModel n =
  ModelResponseModel
    <$> arbitrary -- modelResponseModelId :: Text
    <*> arbitraryReducedMaybe n -- modelResponseModelObject :: Maybe Text
    <*> arbitrary -- modelResponseModelCreated :: Int
    <*> arbitrary -- modelResponseModelOwnedBy :: Text
  
instance Arbitrary PromptTokenDetails where
  arbitrary = sized genPromptTokenDetails

genPromptTokenDetails :: Int -> Gen PromptTokenDetails
genPromptTokenDetails n =
  PromptTokenDetails
    <$> arbitrary -- promptTokenDetailsCachedTokens :: Int
    <*> arbitrary -- promptTokenDetailsAudioTokens :: Int
  
instance Arbitrary ProviderResponseDetailModel where
  arbitrary = sized genProviderResponseDetailModel

genProviderResponseDetailModel :: Int -> Gen ProviderResponseDetailModel
genProviderResponseDetailModel n =
  ProviderResponseDetailModel
    <$> arbitrary -- providerResponseDetailModelId :: Text
    <*> arbitraryReducedMaybe n -- providerResponseDetailModelObject :: Maybe Text
    <*> arbitrary -- providerResponseDetailModelCreated :: Int
    <*> arbitrary -- providerResponseDetailModelUrl :: Text
    <*> arbitrary -- providerResponseDetailModelLabel :: Text
    <*> arbitrary -- providerResponseDetailModelModels :: [Text]
    <*> arbitrary -- providerResponseDetailModelImageModels :: [Text]
    <*> arbitrary -- providerResponseDetailModelVisionModels :: [Text]
    <*> arbitrary -- providerResponseDetailModelParams :: [Text]
  
instance Arbitrary ProviderResponseModel where
  arbitrary = sized genProviderResponseModel

genProviderResponseModel :: Int -> Gen ProviderResponseModel
genProviderResponseModel n =
  ProviderResponseModel
    <$> arbitrary -- providerResponseModelId :: Text
    <*> arbitraryReducedMaybe n -- providerResponseModelObject :: Maybe Text
    <*> arbitrary -- providerResponseModelCreated :: Int
    <*> arbitrary -- providerResponseModelUrl :: Text
    <*> arbitrary -- providerResponseModelLabel :: Text
  
instance Arbitrary Stop where
  arbitrary = sized genStop

genStop :: Int -> Gen Stop
genStop n =
  
  pure Stop
   
instance Arbitrary ToolCallModel where
  arbitrary = sized genToolCallModel

genToolCallModel :: Int -> Gen ToolCallModel
genToolCallModel n =
  ToolCallModel
    <$> arbitrary -- toolCallModelId :: Text
    <*> arbitrary -- toolCallModelType :: Text
    <*> arbitraryReduced n -- toolCallModelFunction :: ToolFunctionModel
  
instance Arbitrary ToolFunctionModel where
  arbitrary = sized genToolFunctionModel

genToolFunctionModel :: Int -> Gen ToolFunctionModel
genToolFunctionModel n =
  ToolFunctionModel
    <$> arbitrary -- toolFunctionModelName :: Text
    <*> arbitrary -- toolFunctionModelArguments :: Text
  
instance Arbitrary TranscriptionResponseModel where
  arbitrary = sized genTranscriptionResponseModel

genTranscriptionResponseModel :: Int -> Gen TranscriptionResponseModel
genTranscriptionResponseModel n =
  TranscriptionResponseModel
    <$> arbitrary -- transcriptionResponseModelText :: Text
    <*> arbitrary -- transcriptionResponseModelModel :: Text
    <*> arbitrary -- transcriptionResponseModelProvider :: Text
  
instance Arbitrary UsageModel where
  arbitrary = sized genUsageModel

genUsageModel :: Int -> Gen UsageModel
genUsageModel n =
  UsageModel
    <$> arbitrary -- usageModelPromptTokens :: Int
    <*> arbitrary -- usageModelCompletionTokens :: Int
    <*> arbitrary -- usageModelTotalTokens :: Int
    <*> arbitraryReduced n -- usageModelPromptTokensDetails :: PromptTokenDetails
    <*> arbitraryReduced n -- usageModelCompletionTokensDetails :: CompletionTokenDetails
    <*> arbitraryReducedMaybe n -- usageModelCache :: Maybe Text
  
instance Arbitrary ValidationError where
  arbitrary = sized genValidationError

genValidationError :: Int -> Gen ValidationError
genValidationError n =
  ValidationError
    <$> arbitraryReduced n -- validationErrorLoc :: [ValidationErrorLocInner]
    <*> arbitrary -- validationErrorMsg :: Text
    <*> arbitrary -- validationErrorType :: Text
    <*> arbitraryReduced n -- validationErrorInput :: AnyType
    <*> arbitraryReduced n -- validationErrorCtx :: AnyType
  
instance Arbitrary ValidationErrorLocInner where
  arbitrary = sized genValidationErrorLocInner

genValidationErrorLocInner :: Int -> Gen ValidationErrorLocInner
genValidationErrorLocInner n =
  
  pure ValidationErrorLocInner