|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { convertJsonToEnv } from './json-to-env.service'; |
| 3 | + |
| 4 | +describe('json-to-env service', () => { |
| 5 | + describe('convertJsonToEnv', () => { |
| 6 | + it('converts a flat JSON object to .env lines', () => { |
| 7 | + const json = JSON.stringify({ |
| 8 | + ACCESS_KEY: 'mySecretAccessKey', |
| 9 | + APP_ENV: 'prod', |
| 10 | + }); |
| 11 | + |
| 12 | + expect(convertJsonToEnv({ json })).toMatchInlineSnapshot(` |
| 13 | + "ACCESS_KEY=mySecretAccessKey |
| 14 | + APP_ENV=prod" |
| 15 | + `); |
| 16 | + }); |
| 17 | + |
| 18 | + it('uppercases keys by default', () => { |
| 19 | + expect(convertJsonToEnv({ json: '{"app_env": "prod"}' })).toBe('APP_ENV=prod'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('keeps original case when uppercaseKeys is false', () => { |
| 23 | + expect(convertJsonToEnv({ json: '{"appEnv": "prod"}', uppercaseKeys: false })).toBe('appEnv=prod'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('quotes values that contain spaces', () => { |
| 27 | + expect(convertJsonToEnv({ json: '{"greeting": "hello world"}' })).toBe('GREETING="hello world"'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('quotes values that contain double quotes and escapes them', () => { |
| 31 | + expect(convertJsonToEnv({ json: '{"q": "he said \\"hi\\""}' })).toBe('Q="he said \\"hi\\""'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('quotes values that contain # so they are not read as comments', () => { |
| 35 | + expect(convertJsonToEnv({ json: '{"color": "#ff0000"}' })).toBe('COLOR="#ff0000"'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('quotes empty string values', () => { |
| 39 | + expect(convertJsonToEnv({ json: '{"empty": ""}' })).toBe('EMPTY=""'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('converts null and undefined to empty values', () => { |
| 43 | + expect(convertJsonToEnv({ json: '{"a": null}' })).toBe('A=""'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('stringifies numbers and booleans', () => { |
| 47 | + expect(convertJsonToEnv({ json: '{"port": 8080, "debug": true}' })).toMatchInlineSnapshot(` |
| 48 | + "PORT=8080 |
| 49 | + DEBUG=true" |
| 50 | + `); |
| 51 | + }); |
| 52 | + |
| 53 | + it('serializes nested objects and arrays as JSON strings', () => { |
| 54 | + const result = convertJsonToEnv({ json: '{"list": [1, 2, 3], "obj": {"a": 1}}' }); |
| 55 | + expect(result).toMatchInlineSnapshot(` |
| 56 | + "LIST=\\"[1,2,3]\\" |
| 57 | + OBJ=\\"{\\\\\\"a\\\\\\":1}\\"" |
| 58 | + `); |
| 59 | + }); |
| 60 | + |
| 61 | + it('escapes newlines in values', () => { |
| 62 | + expect(convertJsonToEnv({ json: '{"multi": "line1\\nline2"}' })).toBe('MULTI="line1\\nline2"'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns empty string on empty input', () => { |
| 66 | + expect(convertJsonToEnv({ json: '' })).toBe(''); |
| 67 | + expect(convertJsonToEnv({ json: ' ' })).toBe(''); |
| 68 | + }); |
| 69 | + |
| 70 | + it('throws when JSON is an array', () => { |
| 71 | + expect(() => convertJsonToEnv({ json: '[1, 2, 3]' })).toThrow(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('throws when JSON is a primitive', () => { |
| 75 | + expect(() => convertJsonToEnv({ json: '"just a string"' })).toThrow(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('throws on invalid JSON', () => { |
| 79 | + expect(() => convertJsonToEnv({ json: '{not json}' })).toThrow(); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments