|
| 1 | +import { describe, test, expect } from 'vitest'; |
| 2 | +import flattenConfigForTable from '../src/utils/flattenConfigForTable.js'; |
| 3 | + |
| 4 | +describe('flattenConfigForTable', () => { |
| 5 | + test('flattens nested objects into dotted keys', () => { |
| 6 | + const out = flattenConfigForTable({ general: { name: 'app', alias: { package: 'gdal3.js' } } }); |
| 7 | + expect(out['general.name']).toBe('app'); |
| 8 | + expect(out['general.alias.package']).toBe('gdal3.js'); |
| 9 | + }); |
| 10 | + |
| 11 | + test('joins arrays of primitives inline', () => { |
| 12 | + const out = flattenConfigForTable({ ext: { header: ['h', 'hpp', 'hxx'] } }); |
| 13 | + expect(out['ext.header']).toBe('h, hpp, hxx'); |
| 14 | + }); |
| 15 | + |
| 16 | + test('collapses arrays of dependency objects to their names', () => { |
| 17 | + const out = flattenConfigForTable({ |
| 18 | + allDependencies: [{ general: { name: 'gdal' } }, { package: { name: '@cpp.js/package-proj' } }], |
| 19 | + }); |
| 20 | + expect(out.allDependencies).toBe('gdal, @cpp.js/package-proj'); |
| 21 | + }); |
| 22 | + |
| 23 | + test('recurses arrays of nameless objects into indexed keys', () => { |
| 24 | + const out = flattenConfigForTable({ targetSpecs: [{ specs: { env: { FOO: '1' } } }] }); |
| 25 | + expect(out['targetSpecs.0.specs.env.FOO']).toBe('1'); |
| 26 | + }); |
| 27 | + |
| 28 | + test('summarizes nameless object arrays once depth is exhausted', () => { |
| 29 | + const out = flattenConfigForTable({ targetSpecs: [{ specs: {} }, { specs: {} }] }, { maxDepth: 1 }); |
| 30 | + expect(out.targetSpecs).toBe('<2 items>'); |
| 31 | + }); |
| 32 | + |
| 33 | + test('renders functions as [Function]', () => { |
| 34 | + const out = flattenConfigForTable({ functions: { isEnabled: () => true } }); |
| 35 | + expect(out['functions.isEnabled']).toBe('[Function]'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('shows keys for large maps of objects instead of expanding them', () => { |
| 39 | + const targets = {}; |
| 40 | + for (let i = 0; i < 10; i += 1) targets[`t${i}`] = { gdal: { root: '/x' } }; |
| 41 | + const out = flattenConfigForTable({ allDependencyPaths: targets }); |
| 42 | + expect(out.allDependencyPaths).toContain('t0'); |
| 43 | + expect(out['allDependencyPaths.t0']).toBeUndefined(); |
| 44 | + expect(out['allDependencyPaths.t0.gdal']).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + test('recurses small maps of objects to show their contents', () => { |
| 48 | + const out = flattenConfigForTable({ specs: { env: { FOO: '1', BAR: '2' } } }); |
| 49 | + expect(out['specs.env.FOO']).toBe('1'); |
| 50 | + expect(out['specs.env.BAR']).toBe('2'); |
| 51 | + }); |
| 52 | + |
| 53 | + test('caps nesting depth to keep output terse', () => { |
| 54 | + const out = flattenConfigForTable({ a: { b: { c: { d: 1 } } } }, { maxDepth: 2 }); |
| 55 | + expect(out['a.b']).toBe('{…}'); |
| 56 | + expect(out['a.b.c']).toBeUndefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('marks empty objects and arrays', () => { |
| 60 | + const out = flattenConfigForTable({ build: {}, binHeaders: [] }); |
| 61 | + expect(out.build).toBe('{}'); |
| 62 | + expect(out.binHeaders).toBe('[]'); |
| 63 | + }); |
| 64 | + |
| 65 | + test('relativizes absolute paths under base, leaving outside paths absolute', () => { |
| 66 | + const out = flattenConfigForTable( |
| 67 | + { paths: { native: '/repo/packages/core/native', systemConfig: '/home/u/.cppjs.json' } }, |
| 68 | + { base: '/repo' }, |
| 69 | + ); |
| 70 | + expect(out['paths.native']).toBe('packages/core/native'); |
| 71 | + expect(out['paths.systemConfig']).toBe('/home/u/.cppjs.json'); |
| 72 | + }); |
| 73 | + |
| 74 | + test('relativizes each entry of an absolute-path array (globs)', () => { |
| 75 | + const out = flattenConfigForTable( |
| 76 | + { dependencyParameters: { nativeGlob: ['/repo/src/*.c', '/repo/src/*.cpp'] } }, |
| 77 | + { base: '/repo' }, |
| 78 | + ); |
| 79 | + expect(out['dependencyParameters.nativeGlob']).toBe('src/*.c, src/*.cpp'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('truncates long values in the middle, preserving head and tail', () => { |
| 83 | + const long = `/repo/${'x'.repeat(100)}/CMakeLists.txt`; |
| 84 | + const out = flattenConfigForTable({ paths: { cmake: long } }, { base: '/repo', maxValueLength: 40 }); |
| 85 | + const value = out['paths.cmake']; |
| 86 | + expect(value.length).toBe(40); |
| 87 | + expect(value).toContain('…'); |
| 88 | + expect(value.endsWith('CMakeLists.txt')).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + test('never truncates the dependency name list', () => { |
| 92 | + const deps = Array.from({ length: 20 }, (_, i) => ({ general: { name: `dep${i}` } })); |
| 93 | + const out = flattenConfigForTable({ allDependencies: deps }, { maxValueLength: 20 }); |
| 94 | + expect(out.allDependencies).toContain('dep19'); |
| 95 | + expect(out.allDependencies).not.toContain('…'); |
| 96 | + }); |
| 97 | +}); |
0 commit comments