|
| 1 | +import { expect, rs, test } from 'rstack/test'; |
| 2 | +import type { RstackConfig } from '../../src/config.ts'; |
| 3 | +import { flattenConfigLayers } from '../../src/configLayers.ts'; |
| 4 | + |
| 5 | +test('flattens nested configs in order and preserves repeated references without resolving factories', () => { |
| 6 | + const app = rs.fn(() => ({})); |
| 7 | + const base = { app, fmt: { printWidth: 80 } }; |
| 8 | + const left = { extends: [base], fmt: { singleQuote: true } }; |
| 9 | + const nested = { extends: [left], fmt: { semi: false } }; |
| 10 | + const right = { extends: [base], fmt: { tabWidth: 4 } }; |
| 11 | + const configs: readonly RstackConfig[] = [nested, right, base]; |
| 12 | + |
| 13 | + expect(flattenConfigLayers(configs)).toEqual([ |
| 14 | + base, |
| 15 | + { fmt: { singleQuote: true } }, |
| 16 | + { fmt: { semi: false } }, |
| 17 | + base, |
| 18 | + { fmt: { tabWidth: 4 } }, |
| 19 | + base, |
| 20 | + ]); |
| 21 | + expect(app).not.toHaveBeenCalled(); |
| 22 | + expect(left.extends).toEqual([base]); |
| 23 | + expect(nested.extends).toEqual([left]); |
| 24 | + expect(right.extends).toEqual([base]); |
| 25 | + expect(configs).toEqual([nested, right, base]); |
| 26 | +}); |
| 27 | + |
| 28 | +test('reports the inheritance positions of a direct cycle', () => { |
| 29 | + const config: RstackConfig = {}; |
| 30 | + config.extends = [config]; |
| 31 | + |
| 32 | + expect(() => flattenConfigLayers([config])).toThrow( |
| 33 | + 'Circular config inheritance at extends[0].extends[0]: references extends[0].', |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | +test('reports the inheritance positions of an indirect cycle', () => { |
| 38 | + const first: RstackConfig = {}; |
| 39 | + const second: RstackConfig = { extends: [first] }; |
| 40 | + first.extends = [second]; |
| 41 | + |
| 42 | + expect(() => flattenConfigLayers([first])).toThrow( |
| 43 | + 'Circular config inheritance at extends[0].extends[0].extends[0]: references extends[0].', |
| 44 | + ); |
| 45 | +}); |
0 commit comments