|
| 1 | +import { describe, expect, test } from 'bun:test'; |
| 2 | +import { validateValueAgainstTypeSpec } from '../../lib/operation-args'; |
| 3 | +import { CliError } from '../../lib/errors'; |
| 4 | +import type { CliTypeSpec } from '../../cli/types'; |
| 5 | + |
| 6 | +describe('validateValueAgainstTypeSpec – oneOf const enumeration', () => { |
| 7 | + const schema: CliTypeSpec = { |
| 8 | + oneOf: [ |
| 9 | + { const: 'headerRow' }, |
| 10 | + { const: 'totalRow' }, |
| 11 | + { const: 'firstColumn' }, |
| 12 | + { const: 'lastColumn' }, |
| 13 | + { const: 'bandedRows' }, |
| 14 | + { const: 'bandedColumns' }, |
| 15 | + ], |
| 16 | + }; |
| 17 | + |
| 18 | + test('accepts a valid const value', () => { |
| 19 | + expect(() => validateValueAgainstTypeSpec('headerRow', schema, 'flag')).not.toThrow(); |
| 20 | + expect(() => validateValueAgainstTypeSpec('bandedColumns', schema, 'flag')).not.toThrow(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('rejects an invalid value and lists all allowed values', () => { |
| 24 | + try { |
| 25 | + validateValueAgainstTypeSpec('lastRow', schema, 'tables set-style-option:flag'); |
| 26 | + throw new Error('Expected CliError to be thrown'); |
| 27 | + } catch (error) { |
| 28 | + expect(error).toBeInstanceOf(CliError); |
| 29 | + const cliError = error as CliError; |
| 30 | + expect(cliError.code).toBe('VALIDATION_ERROR'); |
| 31 | + expect(cliError.message).toBe( |
| 32 | + 'tables set-style-option:flag must be one of: headerRow, totalRow, firstColumn, lastColumn, bandedRows, bandedColumns.', |
| 33 | + ); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + test('preserves per-variant errors in details', () => { |
| 38 | + try { |
| 39 | + validateValueAgainstTypeSpec('invalid', schema, 'flag'); |
| 40 | + throw new Error('Expected CliError to be thrown'); |
| 41 | + } catch (error) { |
| 42 | + const cliError = error as CliError; |
| 43 | + const details = cliError.details as { errors: string[] }; |
| 44 | + expect(details.errors).toBeArrayOfSize(6); |
| 45 | + } |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('validateValueAgainstTypeSpec – oneOf with mixed schemas', () => { |
| 50 | + const mixedSchema: CliTypeSpec = { |
| 51 | + oneOf: [{ const: 'block' }, { type: 'object', properties: { kind: { const: 'inline' } }, required: ['kind'] }], |
| 52 | + }; |
| 53 | + |
| 54 | + test('falls back to generic message when variants are not all const', () => { |
| 55 | + try { |
| 56 | + validateValueAgainstTypeSpec('nope', mixedSchema, 'target'); |
| 57 | + throw new Error('Expected CliError to be thrown'); |
| 58 | + } catch (error) { |
| 59 | + const cliError = error as CliError; |
| 60 | + expect(cliError.message).toBe('target must match one of the allowed schema variants.'); |
| 61 | + } |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('validateValueAgainstTypeSpec – enum branch', () => { |
| 66 | + const enumSchema: CliTypeSpec = { |
| 67 | + type: 'string', |
| 68 | + enum: ['direct', 'tracked'], |
| 69 | + } as CliTypeSpec & { enum: string[] }; |
| 70 | + |
| 71 | + test('accepts a valid enum value', () => { |
| 72 | + expect(() => validateValueAgainstTypeSpec('direct', enumSchema, 'changeMode')).not.toThrow(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('rejects an invalid enum value with allowed list', () => { |
| 76 | + try { |
| 77 | + validateValueAgainstTypeSpec('bogus', enumSchema, 'changeMode'); |
| 78 | + throw new Error('Expected CliError to be thrown'); |
| 79 | + } catch (error) { |
| 80 | + const cliError = error as CliError; |
| 81 | + expect(cliError.message).toBe('changeMode must be one of: direct, tracked.'); |
| 82 | + } |
| 83 | + }); |
| 84 | +}); |
0 commit comments