|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import assert from 'node:assert/strict'; |
| 8 | +import {describe, it} from 'node:test'; |
| 9 | + |
| 10 | +import type {cliOptions} from '../../src/cli.js'; |
| 11 | +import {computeFlagUsage} from '../../src/telemetry/flag-utils.js'; |
| 12 | + |
| 13 | +describe('computeFlagUsage', () => { |
| 14 | + const mockOptions = { |
| 15 | + boolFlag: { |
| 16 | + type: 'boolean' as const, |
| 17 | + description: 'A boolean flag', |
| 18 | + }, |
| 19 | + stringFlag: { |
| 20 | + type: 'string' as const, |
| 21 | + description: 'A string flag', |
| 22 | + }, |
| 23 | + enumFlag: { |
| 24 | + type: 'string' as const, |
| 25 | + description: 'An enum flag', |
| 26 | + choices: ['a', 'b'], |
| 27 | + }, |
| 28 | + flagWithDefault: { |
| 29 | + type: 'boolean' as const, |
| 30 | + description: 'A flag with a default value', |
| 31 | + default: false, |
| 32 | + }, |
| 33 | + } as unknown as typeof cliOptions; |
| 34 | + |
| 35 | + it('logs boolean flags directly with snake_case keys', () => { |
| 36 | + const args = {boolFlag: true}; |
| 37 | + const usage = computeFlagUsage(args, mockOptions); |
| 38 | + assert.equal(usage.bool_flag, true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('logs boolean flags as false when false', () => { |
| 42 | + const args = {boolFlag: false}; |
| 43 | + const usage = computeFlagUsage(args, mockOptions); |
| 44 | + assert.equal(usage.bool_flag, false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('logs enum flags as uppercase', () => { |
| 48 | + const args = {enumFlag: 'a'}; |
| 49 | + const usage = computeFlagUsage(args, mockOptions); |
| 50 | + assert.equal(usage.enum_flag, 'A'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('logs other flags as present with snake_case keys', () => { |
| 54 | + const args = {stringFlag: 'value'}; |
| 55 | + const usage = computeFlagUsage(args, mockOptions); |
| 56 | + assert.equal(usage.string_flag, undefined); |
| 57 | + assert.equal(usage.string_flag_present, true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('handles undefined/null values', () => { |
| 61 | + const args = {stringFlag: undefined}; |
| 62 | + const usage = computeFlagUsage(args, mockOptions); |
| 63 | + assert.equal(usage.string_flag_present, false); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('defaults behavior', () => { |
| 67 | + it('logs presence when default exists and user provides different value', () => { |
| 68 | + // Case 1: Default exists, and a value is provided by the user. |
| 69 | + // default is false, user provides true. |
| 70 | + const args = {flagWithDefault: true}; |
| 71 | + const usage = computeFlagUsage(args, mockOptions); |
| 72 | + assert.equal(usage.flag_with_default, true); |
| 73 | + assert.equal(usage.flag_with_default_present, true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('does not log presence when default exists and user provides no value', () => { |
| 77 | + // Case 2a: Default exists, and a value is not provided by the user. |
| 78 | + // Argument parsing would populate with default. |
| 79 | + const args = {flagWithDefault: false}; |
| 80 | + const usage = computeFlagUsage(args, mockOptions); |
| 81 | + assert.equal(usage.flag_with_default, false); |
| 82 | + assert.equal(usage.flag_with_default_present, undefined); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not log presence when default exists and user explicitly provides the default value', () => { |
| 86 | + // Case 2b: User explicitly provides 'false', which matches default. |
| 87 | + const args = {flagWithDefault: false}; |
| 88 | + const usage = computeFlagUsage(args, mockOptions); |
| 89 | + assert.equal(usage.flag_with_default, false); |
| 90 | + assert.equal(usage.flag_with_default_present, undefined); |
| 91 | + }); |
| 92 | + |
| 93 | + it('logs presence when no default exists and user provides value', () => { |
| 94 | + // Case 3: No default, user provides value. |
| 95 | + const args = {stringFlag: 'value'}; |
| 96 | + const usage = computeFlagUsage(args, mockOptions); |
| 97 | + assert.equal(usage.string_flag_present, true); |
| 98 | + }); |
| 99 | + |
| 100 | + it('logs non-presence when no default exists and user provides no value', () => { |
| 101 | + // Case 4: No default, user provides nothing. |
| 102 | + const args = {}; |
| 103 | + const usage = computeFlagUsage(args, mockOptions); |
| 104 | + assert.equal(usage.string_flag_present, false); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('matches snapshot for all current CLI options', async t => { |
| 109 | + // Import the real options to test against the actual CLI definition |
| 110 | + const {cliOptions} = await import('../../src/cli.js'); |
| 111 | + |
| 112 | + const mockArgs: Record<string, unknown> = {}; |
| 113 | + for (const [key, config] of Object.entries(cliOptions)) { |
| 114 | + if ('choices' in config && config.choices) { |
| 115 | + mockArgs[key] = config.choices[0]; |
| 116 | + } else if (config.type === 'boolean') { |
| 117 | + mockArgs[key] = true; |
| 118 | + } else if (config.type === 'string') { |
| 119 | + mockArgs[key] = '/mock/path'; |
| 120 | + } else if (config.type === 'array') { |
| 121 | + mockArgs[key] = ['--mock-arg']; |
| 122 | + } else { |
| 123 | + mockArgs[key] = 'mock-value'; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + const usage = computeFlagUsage(mockArgs, cliOptions); |
| 128 | + t.assert.snapshot(JSON.stringify(usage, null, 2)); |
| 129 | + }); |
| 130 | +}); |
0 commit comments