File size: 1,721 Bytes
3e8ea5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import assert from 'node:assert/strict';
import { afterEach, describe, it } from 'node:test';

import { readPositiveIntegerEnv } from './env-utils.mjs';

const TEST_ENV_NAME = 'GPT_IMAGE_PLAYGROUND_TEST_INTEGER';

afterEach(() => {
    delete process.env[TEST_ENV_NAME];
});

describe('Environment utilities', () => {
    it('returns the default value when the env var is unset', () => {
        assert.equal(readPositiveIntegerEnv(TEST_ENV_NAME, 30), 30);
    });

    it('returns valid integer env values', () => {
        process.env[TEST_ENV_NAME] = '42';

        assert.equal(readPositiveIntegerEnv(TEST_ENV_NAME, 30), 42);
    });

    it('accepts values exactly equal to the configured minimum', () => {
        process.env[TEST_ENV_NAME] = '1000';

        assert.equal(readPositiveIntegerEnv(TEST_ENV_NAME, 30, 1000), 1000);
    });

    it('accepts zero when the configured minimum is zero', () => {
        process.env[TEST_ENV_NAME] = '0';

        assert.equal(readPositiveIntegerEnv(TEST_ENV_NAME, 30, 0), 0);
    });

    it('rejects non-digit values before parsing', () => {
        process.env[TEST_ENV_NAME] = '12ms';

        assert.throws(() => readPositiveIntegerEnv(TEST_ENV_NAME, 30), /formatted as digits/);
    });

    it('rejects values below the configured minimum', () => {
        process.env[TEST_ENV_NAME] = '999';

        assert.throws(() => readPositiveIntegerEnv(TEST_ENV_NAME, 30, 1000), /greater than or equal to 1000/);
    });

    it('rejects integers that exceed JavaScript safe integer range', () => {
        process.env[TEST_ENV_NAME] = String(Number.MAX_SAFE_INTEGER + 1);

        assert.throws(() => readPositiveIntegerEnv(TEST_ENV_NAME, 30), /safe integer/);
    });
});