diff --git a/yarn-project/foundation/src/config/config.test.ts b/yarn-project/foundation/src/config/config.test.ts index c21021c8d067..a4e0f7ad17b2 100644 --- a/yarn-project/foundation/src/config/config.test.ts +++ b/yarn-project/foundation/src/config/config.test.ts @@ -1,6 +1,6 @@ import { jest } from '@jest/globals'; -import { type ConfigMappingsType, getConfigFromMappings, numberConfigHelper } from './index.js'; +import { type ConfigMappingsType, bigintConfigHelper, getConfigFromMappings, numberConfigHelper } from './index.js'; describe('Config', () => { describe('getConfigFromMappings', () => { @@ -131,4 +131,37 @@ describe('Config', () => { }); }); }); + + describe('bigintConfigHelper', () => { + it('parses plain integer strings', () => { + const { parseEnv } = bigintConfigHelper(); + expect(parseEnv!('123')).toBe(123n); + expect(parseEnv!('0')).toBe(0n); + expect(parseEnv!('200000000000000000000000')).toBe(200000000000000000000000n); + }); + + it('parses scientific notation', () => { + const { parseEnv } = bigintConfigHelper(); + expect(parseEnv!('1e+23')).toBe(100000000000000000000000n); + expect(parseEnv!('2E+23')).toBe(200000000000000000000000n); + expect(parseEnv!('1e23')).toBe(100000000000000000000000n); + expect(parseEnv!('5e18')).toBe(5000000000000000000n); + }); + + it('parses scientific notation with decimal mantissa', () => { + const { parseEnv } = bigintConfigHelper(); + expect(parseEnv!('1.5e10')).toBe(15000000000n); + expect(parseEnv!('2.5e5')).toBe(250000n); + }); + + it('returns default value for empty string', () => { + const { parseEnv } = bigintConfigHelper(42n); + expect(parseEnv!('')).toBe(42n); + }); + + it('throws for non-integer scientific notation results', () => { + const { parseEnv } = bigintConfigHelper(); + expect(() => parseEnv!('1e-3')).toThrow(); + }); + }); }); diff --git a/yarn-project/foundation/src/config/index.ts b/yarn-project/foundation/src/config/index.ts index 7ed7d3ef6ceb..8962dea3b1b0 100644 --- a/yarn-project/foundation/src/config/index.ts +++ b/yarn-project/foundation/src/config/index.ts @@ -177,6 +177,21 @@ export function bigintConfigHelper(defaultVal?: bigint): Pick