@@ -3,6 +3,7 @@ import { describe, test, expect } from 'bun:test'
33import {
44 isOutOfCreditsError ,
55 isFreeModeUnavailableError ,
6+ getCountryBlockFromFreeModeError ,
67 OUT_OF_CREDITS_MESSAGE ,
78 FREE_MODE_UNAVAILABLE_MESSAGE ,
89 createErrorMessage ,
@@ -70,7 +71,11 @@ describe('error-handling', () => {
7071
7172 describe ( 'isFreeModeUnavailableError' , ( ) => {
7273 test ( 'returns true for error with statusCode 403 and error free_mode_unavailable' , ( ) => {
73- const error = { statusCode : 403 , error : 'free_mode_unavailable' , message : 'Free mode is not available in your country.' }
74+ const error = {
75+ statusCode : 403 ,
76+ error : 'free_mode_unavailable' ,
77+ message : 'Free mode is not available in your country.' ,
78+ }
7479 expect ( isFreeModeUnavailableError ( error ) ) . toBe ( true )
7580 } )
7681
@@ -80,12 +85,20 @@ describe('error-handling', () => {
8085 } )
8186
8287 test ( 'returns false for 403 with different error code' , ( ) => {
83- const error = { statusCode : 403 , error : 'account_suspended' , message : 'Suspended' }
88+ const error = {
89+ statusCode : 403 ,
90+ error : 'account_suspended' ,
91+ message : 'Suspended' ,
92+ }
8493 expect ( isFreeModeUnavailableError ( error ) ) . toBe ( false )
8594 } )
8695
8796 test ( 'returns false for non-403 status with free_mode_unavailable error' , ( ) => {
88- const error = { statusCode : 400 , error : 'free_mode_unavailable' , message : 'Bad request' }
97+ const error = {
98+ statusCode : 400 ,
99+ error : 'free_mode_unavailable' ,
100+ message : 'Bad request' ,
101+ }
89102 expect ( isFreeModeUnavailableError ( error ) ) . toBe ( false )
90103 } )
91104
@@ -102,9 +115,51 @@ describe('error-handling', () => {
102115 } )
103116 } )
104117
118+ describe ( 'getCountryBlockFromFreeModeError' , ( ) => {
119+ test ( 'extracts country block details from free-mode unavailable errors' , ( ) => {
120+ const error = {
121+ statusCode : 403 ,
122+ error : 'free_mode_unavailable' ,
123+ countryCode : 'US' ,
124+ countryBlockReason : 'anonymous_network' ,
125+ ipPrivacySignals : [ 'vpn' , 'hosting' , 123 ] ,
126+ }
127+
128+ expect ( getCountryBlockFromFreeModeError ( error ) ) . toEqual ( {
129+ countryCode : 'US' ,
130+ countryBlockReason : 'anonymous_network' ,
131+ ipPrivacySignals : [ 'vpn' , 'hosting' ] ,
132+ } )
133+ } )
134+
135+ test ( 'defaults missing country code to UNKNOWN' , ( ) => {
136+ const error = {
137+ statusCode : 403 ,
138+ error : 'free_mode_unavailable' ,
139+ }
140+
141+ expect ( getCountryBlockFromFreeModeError ( error ) ) . toEqual ( {
142+ countryCode : 'UNKNOWN' ,
143+ countryBlockReason : undefined ,
144+ ipPrivacySignals : undefined ,
145+ } )
146+ } )
147+
148+ test ( 'returns null for non-free-mode errors' , ( ) => {
149+ expect (
150+ getCountryBlockFromFreeModeError ( {
151+ statusCode : 403 ,
152+ error : 'account_suspended' ,
153+ } ) ,
154+ ) . toBe ( null )
155+ } )
156+ } )
157+
105158 describe ( 'FREE_MODE_UNAVAILABLE_MESSAGE' , ( ) => {
106159 test ( 'mentions unavailability in country' , ( ) => {
107- expect ( FREE_MODE_UNAVAILABLE_MESSAGE . toLowerCase ( ) ) . toContain ( 'not available in your country' )
160+ expect ( FREE_MODE_UNAVAILABLE_MESSAGE . toLowerCase ( ) ) . toContain (
161+ 'not available in your country' ,
162+ )
108163 } )
109164 } )
110165
0 commit comments