import { asUint16, expectType, Result } from 'ts-data-forge'; import { type Uint16 } from 'ts-type-forge'; import { type TypeOf } from '../../../type.mjs'; import { validationErrorsToMessages } from '../../../utils/index.mjs'; import { uint16 } from './uint16.mjs'; describe(uint16, () => { const targetType = uint16(asUint16(0)); type TargetType = TypeOf; expectType('='); expectType('='); describe('is', () => { test('truthy case', () => { const x: unknown = 30_000; const isTarget = targetType.is(x); if (isTarget) { expectType('='); } else { expectType('='); } assert.isTrue(isTarget); }); test('truthy case - zero', () => { const x: unknown = 0; assert.isTrue(targetType.is(x)); }); test('falsy case - negative', () => { const x: unknown = -1; const isTarget = targetType.is(x); if (isTarget) { expectType('='); } else { expectType('='); } assert.isFalse(isTarget); }); test('falsy case - too large', () => { const x: unknown = 70_000; assert.isFalse(targetType.is(x)); }); test('falsy case - float', () => { const x: unknown = 123.456; assert.isFalse(targetType.is(x)); }); }); describe('validate', () => { test('truthy case', () => { const result = targetType.validate(50_000); assert.isTrue(Result.isOk(result)); const resultValue = Result.unwrapThrow(result); expect(resultValue).toBe(50_000); }); test('validate returns input as-is for OK cases', () => { const input = 30_000; const result = targetType.validate(input); assert.isTrue(Result.isOk(result)); const resultValue1 = Result.unwrapThrow(result); expect(resultValue1).toBe(input); // ✅ same reference }); test('falsy case - negative', () => { const result = targetType.validate(-5); assert.isTrue(Result.isErr(result)); const resultError = Result.unwrapErrThrow(result); assert.deepStrictEqual(resultError, [ { path: [], actualValue: -5, expectedType: 'Uint16', typeName: 'Uint16', details: undefined, }, ]); assert.deepStrictEqual(validationErrorsToMessages(resultError), [ 'Error: expected type but type value `-5` was passed.', ]); }); }); describe('cast', () => { test('truthy case', () => { const x: unknown = 40_000; expect(targetType.cast(x)).toBe(40_000); }); test('falsy case', () => { const x: unknown = 'invalid'; expect(() => targetType.cast(x)).toThrow('Error'); }); }); describe('fill', () => { test('noop', () => { const x: unknown = 25_000; expect(targetType.fill(x)).toBe(25_000); }); test('fill with the default value', () => { const x: unknown = 'not a uint16'; expect(targetType.fill(x)).toBe(0); }); }); });