|
| 1 | +import { parseResourceAttributes } from '@/config'; |
| 2 | + |
| 3 | +describe('parseResourceAttributes', () => { |
| 4 | + it('parses a standard comma-separated string', () => { |
| 5 | + const raw = |
| 6 | + 'service.namespace=observability,deployment.environment=prod,k8s.cluster.name=us-west-2'; |
| 7 | + expect(parseResourceAttributes(raw)).toEqual({ |
| 8 | + 'service.namespace': 'observability', |
| 9 | + 'deployment.environment': 'prod', |
| 10 | + 'k8s.cluster.name': 'us-west-2', |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + it('returns an empty object for an empty string', () => { |
| 15 | + expect(parseResourceAttributes('')).toEqual({}); |
| 16 | + }); |
| 17 | + |
| 18 | + it('handles a single key=value pair', () => { |
| 19 | + expect(parseResourceAttributes('foo=bar')).toEqual({ foo: 'bar' }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('handles values containing equals signs', () => { |
| 23 | + expect(parseResourceAttributes('url=https://example.com?a=1')).toEqual({ |
| 24 | + url: 'https://example.com?a=1', |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('skips malformed entries without an equals sign', () => { |
| 29 | + expect(parseResourceAttributes('good=value,badentry,ok=yes')).toEqual({ |
| 30 | + good: 'value', |
| 31 | + ok: 'yes', |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('skips entries where key is empty (leading equals)', () => { |
| 36 | + expect(parseResourceAttributes('=nokey,valid=value')).toEqual({ |
| 37 | + valid: 'value', |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('handles trailing commas gracefully', () => { |
| 42 | + expect(parseResourceAttributes('a=1,b=2,')).toEqual({ a: '1', b: '2' }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('handles leading commas gracefully', () => { |
| 46 | + expect(parseResourceAttributes(',a=1,b=2')).toEqual({ a: '1', b: '2' }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('allows empty values', () => { |
| 50 | + expect(parseResourceAttributes('key=')).toEqual({ key: '' }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('last value wins for duplicate keys', () => { |
| 54 | + expect(parseResourceAttributes('k=first,k=second')).toEqual({ |
| 55 | + k: 'second', |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('decodes percent-encoded commas in values', () => { |
| 60 | + expect(parseResourceAttributes('tags=a%2Cb%2Cc')).toEqual({ |
| 61 | + tags: 'a,b,c', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('decodes percent-encoded equals in values', () => { |
| 66 | + expect(parseResourceAttributes('expr=x%3D1')).toEqual({ |
| 67 | + expr: 'x=1', |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('decodes percent-encoded keys', () => { |
| 72 | + expect(parseResourceAttributes('my%2Ekey=value')).toEqual({ |
| 73 | + 'my.key': 'value', |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('round-trips values with both encoded commas and equals', () => { |
| 78 | + expect(parseResourceAttributes('q=a%3D1%2Cb%3D2,other=plain')).toEqual({ |
| 79 | + q: 'a=1,b=2', |
| 80 | + other: 'plain', |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments