|
| 1 | +import { argsToQueryString } from './analytics'; |
| 2 | + |
| 3 | +describe('argsToQueryString', () => { |
| 4 | + it('should convert simple non-sensitive args to query string', () => { |
| 5 | + const result = argsToQueryString({ verbose: true, parallel: 3 }); |
| 6 | + expect(result).toBe('verbose=true¶llel=3'); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should handle array values by repeating the key', () => { |
| 10 | + const result = argsToQueryString({ |
| 11 | + targets: ['build', 'test'], |
| 12 | + }); |
| 13 | + expect(result).toBe('targets=build&targets=test'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should filter out internal yargs keys', () => { |
| 17 | + const result = argsToQueryString({ |
| 18 | + verbose: true, |
| 19 | + $0: 'nx', |
| 20 | + _: ['run-many'], |
| 21 | + __overrides_unparsed__: [], |
| 22 | + __overrides__: [], |
| 23 | + }); |
| 24 | + expect(result).toBe('verbose=true'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should skip null and undefined values', () => { |
| 28 | + const result = argsToQueryString({ |
| 29 | + verbose: true, |
| 30 | + missing: null, |
| 31 | + absent: undefined, |
| 32 | + }); |
| 33 | + expect(result).toBe('verbose=true'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should skip nested objects (non-array)', () => { |
| 37 | + const result = argsToQueryString({ |
| 38 | + verbose: true, |
| 39 | + nested: { deep: 'value' }, |
| 40 | + }); |
| 41 | + expect(result).toBe('verbose=true'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return empty string for empty args', () => { |
| 45 | + const result = argsToQueryString({}); |
| 46 | + expect(result).toBe(''); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return empty string when all keys are internal', () => { |
| 50 | + const result = argsToQueryString({ |
| 51 | + $0: 'nx', |
| 52 | + _: [], |
| 53 | + }); |
| 54 | + expect(result).toBe(''); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle numeric values for non-sensitive keys', () => { |
| 58 | + const result = argsToQueryString({ port: 4200 }); |
| 59 | + expect(result).toBe('port=4200'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle false boolean values', () => { |
| 63 | + const result = argsToQueryString({ verbose: false }); |
| 64 | + expect(result).toBe('verbose=false'); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('sensitive args sanitization', () => { |
| 68 | + it('should redact sensitive file paths', () => { |
| 69 | + const result = argsToQueryString({ file: '/path/to/file.json' }); |
| 70 | + expect(result).toBe('file=%3Credacted%3E'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should redact sensitive project identifiers', () => { |
| 74 | + const result = argsToQueryString({ |
| 75 | + project: 'my-secret-app', |
| 76 | + focus: 'internal-lib', |
| 77 | + }); |
| 78 | + expect(result).toBe('project=%3Credacted%3E&focus=%3Credacted%3E'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should preserve boolean values on sensitive keys', () => { |
| 82 | + const result = argsToQueryString({ runMigrations: true }); |
| 83 | + expect(result).toBe('runMigrations=true'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should preserve false boolean values on sensitive keys', () => { |
| 87 | + const result = argsToQueryString({ graph: false }); |
| 88 | + expect(result).toBe('graph=false'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should redact non-boolean values on sensitive keys', () => { |
| 92 | + const result = argsToQueryString({ |
| 93 | + runMigrations: 'migrations.json', |
| 94 | + }); |
| 95 | + expect(result).toBe('runMigrations=%3Credacted%3E'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should pass through non-sensitive keys unchanged', () => { |
| 99 | + const result = argsToQueryString({ |
| 100 | + verbose: true, |
| 101 | + port: 4200, |
| 102 | + parallel: 3, |
| 103 | + }); |
| 104 | + expect(result).toBe('verbose=true&port=4200¶llel=3'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should redact each element of array values for sensitive keys', () => { |
| 108 | + const result = argsToQueryString({ |
| 109 | + projects: ['secret-app', 'internal-lib'], |
| 110 | + }); |
| 111 | + expect(result).toBe('projects=%3Credacted%3E&projects=%3Credacted%3E'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should preserve each element of array values for non-sensitive keys', () => { |
| 115 | + const result = argsToQueryString({ |
| 116 | + targets: ['build', 'test'], |
| 117 | + }); |
| 118 | + expect(result).toBe('targets=build&targets=test'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should catch kebab-case sensitive keys', () => { |
| 122 | + const result = argsToQueryString({ 'output-path': 'dist/app' }); |
| 123 | + expect(result).toBe('output-path=%3Credacted%3E'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should handle a real-world graph command scenario', () => { |
| 127 | + const result = argsToQueryString({ |
| 128 | + file: 'output.json', |
| 129 | + focus: 'my-secret-app', |
| 130 | + exclude: 'internal-lib', |
| 131 | + host: '192.168.1.1', |
| 132 | + port: 4200, |
| 133 | + targets: 'build', |
| 134 | + watch: true, |
| 135 | + }); |
| 136 | + expect(result).toBe( |
| 137 | + 'file=%3Credacted%3E&focus=%3Credacted%3E&exclude=%3Credacted%3E&host=%3Credacted%3E&port=4200&targets=build&watch=true' |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should always redact credentials', () => { |
| 142 | + const result = argsToQueryString({ otp: '123456' }); |
| 143 | + expect(result).toBe('otp=%3Credacted%3E'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should redact git refs', () => { |
| 147 | + const result = argsToQueryString({ |
| 148 | + base: 'feature/secret-branch', |
| 149 | + head: 'main', |
| 150 | + }); |
| 151 | + expect(result).toBe('base=%3Credacted%3E&head=%3Credacted%3E'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should redact URLs and hosts', () => { |
| 155 | + const result = argsToQueryString({ |
| 156 | + baseUrl: 'https://internal.company.com', |
| 157 | + deployUrl: 'https://staging.company.com/app', |
| 158 | + }); |
| 159 | + expect(result).toBe('baseUrl=%3Credacted%3E&deployUrl=%3Credacted%3E'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should handle kebab-case config keys', () => { |
| 163 | + const result = argsToQueryString({ |
| 164 | + 'ts-config': 'tsconfig.app.json', |
| 165 | + 'webpack-config': 'webpack.config.js', |
| 166 | + }); |
| 167 | + expect(result).toBe( |
| 168 | + 'ts-config=%3Credacted%3E&webpack-config=%3Credacted%3E' |
| 169 | + ); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments