1414
1515import { describe , it } from 'mocha' ;
1616import { expect } from 'chai' ;
17- import { isPlainObject } from '../src/util' ;
17+ import { isPlainObject , tryGetPreferRestEnvironmentVariable } from '../src/util' ;
18+ import * as sinon from 'sinon' ;
1819
1920describe ( 'isPlainObject()' , ( ) => {
2021 it ( 'allows Object.create()' , ( ) => {
@@ -33,4 +34,71 @@ describe('isPlainObject()', () => {
3334 expect ( isPlainObject ( new Foo ( ) ) ) . to . be . false ;
3435 expect ( isPlainObject ( Object . create ( new Foo ( ) ) ) ) . to . be . false ;
3536 } ) ;
37+
38+ describe ( 'tryGetPreferRestEnvironmentVariable' , ( ) => {
39+ const sandbox = sinon . createSandbox ( ) ;
40+
41+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42+ let warnSpy : any ;
43+ let originalValue : string | undefined ;
44+
45+ beforeEach ( ( ) => {
46+ warnSpy = sandbox . spy ( console , 'warn' ) ;
47+ originalValue = process . env . FIRESTORE_PREFER_REST ;
48+ } ) ;
49+
50+ afterEach ( ( ) => {
51+ sandbox . restore ( ) ;
52+ if ( originalValue === undefined ) {
53+ delete process . env . FIRESTORE_PREFER_REST ;
54+ } else {
55+ process . env . FIRESTORE_PREFER_REST = originalValue ;
56+ }
57+ } ) ;
58+
59+ it ( 'reads true' , async ( ) => {
60+ process . env . FIRESTORE_PREFER_REST = 'true' ;
61+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . true ;
62+ } ) ;
63+
64+ it ( 'reads 1' , async ( ) => {
65+ process . env . FIRESTORE_PREFER_REST = '1' ;
66+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . true ;
67+ } ) ;
68+
69+ it ( 'reads false' , async ( ) => {
70+ process . env . FIRESTORE_PREFER_REST = 'false' ;
71+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . false ;
72+ } ) ;
73+
74+ it ( 'reads 0' , async ( ) => {
75+ process . env . FIRESTORE_PREFER_REST = '0' ;
76+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . false ;
77+ } ) ;
78+
79+ it ( 'ignores case' , async ( ) => {
80+ process . env . FIRESTORE_PREFER_REST = 'True' ;
81+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . true ;
82+ } ) ;
83+
84+ it ( 'trims whitespace' , async ( ) => {
85+ process . env . FIRESTORE_PREFER_REST = ' true ' ;
86+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . true ;
87+ } ) ;
88+
89+ it ( 'returns undefined when the environment variable is not set' , async ( ) => {
90+ delete process . env . FIRESTORE_PREFER_REST ;
91+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . undefined ;
92+ expect ( warnSpy . calledOnce ) . to . be . false ;
93+ } ) ;
94+
95+ it ( 'returns undefined and warns when the environment variable is set to an unsupported value' , async ( ) => {
96+ process . env . FIRESTORE_PREFER_REST = 'enable' ;
97+ expect ( tryGetPreferRestEnvironmentVariable ( ) ) . to . be . undefined ;
98+ expect ( warnSpy . calledOnce ) . to . be . true ;
99+ expect ( warnSpy . getCall ( 0 ) . args [ 0 ] ) . to . match (
100+ / u n s u p p o r t e d v a l u e .* F I R E S T O R E _ P R E F E R _ R E S T /
101+ ) ;
102+ } ) ;
103+ } ) ;
36104} ) ;
0 commit comments