|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + createYamlExpansionBudget, |
| 7 | + isYamlExpansionBudgetExhausted, |
| 8 | + measureYamlExpansion, |
| 9 | + type YamlExpansionLimits, |
| 10 | +} from '@/lib/file-parsers/yaml-limits' |
| 11 | + |
| 12 | +const LIMITS: YamlExpansionLimits = { |
| 13 | + maxNodes: 1000, |
| 14 | + maxSerializedBytes: 64 * 1024, |
| 15 | + maxDepth: 10, |
| 16 | +} |
| 17 | + |
| 18 | +const limits = (overrides: Partial<YamlExpansionLimits> = {}): YamlExpansionLimits => ({ |
| 19 | + ...LIMITS, |
| 20 | + ...overrides, |
| 21 | +}) |
| 22 | + |
| 23 | +describe('measureYamlExpansion', () => { |
| 24 | + it('reports the depth of the expanded tree', () => { |
| 25 | + expect(measureYamlExpansion('scalar', LIMITS)).toEqual({ within: true, depth: 0 }) |
| 26 | + expect(measureYamlExpansion([1, 2, 3], LIMITS)).toEqual({ within: true, depth: 1 }) |
| 27 | + expect(measureYamlExpansion({ a: { b: { c: 1 } } }, LIMITS)).toEqual({ within: true, depth: 3 }) |
| 28 | + }) |
| 29 | + |
| 30 | + it('counts an empty container as a level', () => { |
| 31 | + expect(measureYamlExpansion([], LIMITS)).toEqual({ within: true, depth: 1 }) |
| 32 | + expect(measureYamlExpansion({ a: {} }, LIMITS)).toEqual({ within: true, depth: 2 }) |
| 33 | + }) |
| 34 | + |
| 35 | + it('charges an aliased subtree once per path that reaches it', () => { |
| 36 | + const shared = [1, 2, 3, 4, 5] |
| 37 | + const aliased = { a: shared, b: shared, c: shared } |
| 38 | + |
| 39 | + const measured = measureYamlExpansion(aliased, limits({ maxNodes: 20 })) |
| 40 | + expect(measured).toEqual({ within: true, depth: 2 }) |
| 41 | + // 18 nodes if every reach is charged; 8 if the shared array were counted once. |
| 42 | + expect(measureYamlExpansion(aliased, limits({ maxNodes: 12 })).within).toBe(false) |
| 43 | + }) |
| 44 | + |
| 45 | + it('terminates on a self-referential anchor instead of recursing forever', () => { |
| 46 | + const cyclic: Record<string, unknown> = {} |
| 47 | + cyclic.self = cyclic |
| 48 | + |
| 49 | + const measured = measureYamlExpansion(cyclic, LIMITS) |
| 50 | + |
| 51 | + expect(measured.within).toBe(false) |
| 52 | + if (!measured.within) expect(measured.reason).toContain('nesting depth') |
| 53 | + }) |
| 54 | + |
| 55 | + it('rejects a wide fan-out of containers without enumerating it first', () => { |
| 56 | + // The traversal holds one frame per level, not one per pending node, so a node |
| 57 | + // whose fan-out dwarfs the budget trips the cap part way through rather than |
| 58 | + // after building a frame for every sibling. |
| 59 | + const wide = Array.from({ length: 100_000 }, () => ({ a: 1 })) |
| 60 | + |
| 61 | + const measured = measureYamlExpansion(wide, limits({ maxNodes: 50 })) |
| 62 | + |
| 63 | + expect(measured.within).toBe(false) |
| 64 | + if (!measured.within) expect(measured.reason).toContain('expanded nodes') |
| 65 | + }) |
| 66 | + |
| 67 | + it('charges a long number its serialized length, not the flat allowance', () => { |
| 68 | + // Each serializes to 24 characters; the flat non-string allowance is 16. |
| 69 | + const longNumbers = Array.from({ length: 200 }, () => -1.2345678901234567e-308) |
| 70 | + const shortNumbers = Array.from({ length: 200 }, () => 1) |
| 71 | + |
| 72 | + // Sits between what 200 short numbers cost (~4.4 KB) and what 200 long ones do |
| 73 | + // (~6 KB); under the flat allowance both would land on the same side of it. |
| 74 | + const byteCap = limits({ maxSerializedBytes: 5000 }) |
| 75 | + |
| 76 | + expect(measureYamlExpansion(shortNumbers, byteCap).within).toBe(true) |
| 77 | + expect(measureYamlExpansion(longNumbers, byteCap).within).toBe(false) |
| 78 | + }) |
| 79 | + |
| 80 | + it('charges object keys, so an aliased object with long keys cannot bypass the cap', () => { |
| 81 | + const key = 'k'.repeat(500) |
| 82 | + const shared = { [key]: 1 } |
| 83 | + const aliased = Array.from({ length: 50 }, () => shared) |
| 84 | + |
| 85 | + const measured = measureYamlExpansion(aliased, limits({ maxSerializedBytes: 10_000 })) |
| 86 | + |
| 87 | + expect(measured.within).toBe(false) |
| 88 | + if (!measured.within) expect(measured.reason).toContain('serialized size') |
| 89 | + }) |
| 90 | + |
| 91 | + it('draws several documents down one shared budget', () => { |
| 92 | + const budget = createYamlExpansionBudget(limits({ maxNodes: 30 })) |
| 93 | + const doc = Array.from({ length: 10 }, (_, i) => i) |
| 94 | + |
| 95 | + expect(measureYamlExpansion(doc, limits({ maxNodes: 30 }), budget).within).toBe(true) |
| 96 | + expect(isYamlExpansionBudgetExhausted(budget)).toBe(false) |
| 97 | + expect(measureYamlExpansion(doc, limits({ maxNodes: 30 }), budget).within).toBe(true) |
| 98 | + // The third pass runs out: 3 x 11 nodes exceeds the 30 the budget was created with. |
| 99 | + expect(measureYamlExpansion(doc, limits({ maxNodes: 30 }), budget).within).toBe(false) |
| 100 | + expect(isYamlExpansionBudgetExhausted(budget)).toBe(true) |
| 101 | + }) |
| 102 | + |
| 103 | + it('leaves a shared budget usable after a depth rejection', () => { |
| 104 | + // Depth costs only its own nesting, so one over-deep document must not bankrupt |
| 105 | + // the documents that share its budget. |
| 106 | + const budget = createYamlExpansionBudget(limits({ maxDepth: 2 })) |
| 107 | + const deep = { a: { b: { c: { d: 1 } } } } |
| 108 | + |
| 109 | + expect(measureYamlExpansion(deep, limits({ maxDepth: 2 }), budget).within).toBe(false) |
| 110 | + expect(isYamlExpansionBudgetExhausted(budget)).toBe(false) |
| 111 | + expect(measureYamlExpansion({ ok: 1 }, limits({ maxDepth: 2 }), budget).within).toBe(true) |
| 112 | + }) |
| 113 | + |
| 114 | + it('ignores inherited properties when walking an object', () => { |
| 115 | + const parent = { inherited: 'x'.repeat(5000) } |
| 116 | + const child = Object.create(parent) as Record<string, unknown> |
| 117 | + child.own = 1 |
| 118 | + |
| 119 | + const measured = measureYamlExpansion(child, limits({ maxSerializedBytes: 200 })) |
| 120 | + |
| 121 | + expect(measured).toEqual({ within: true, depth: 1 }) |
| 122 | + }) |
| 123 | +}) |
0 commit comments