|
| 1 | +import * as E from 'fp-ts/lib/Either'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import test from 'node:test'; |
| 4 | + |
| 5 | +import { TestProject } from './testProject'; |
| 6 | +import { parsePlainInitializer, type Schema } from '../src'; |
| 7 | +import { KNOWN_IMPORTS, type KnownImports } from '../src/knownImports'; |
| 8 | + |
| 9 | +const ARROW_WITH_PARAMS_MAIN = ` |
| 10 | +import * as t from 'io-ts'; |
| 11 | +import { NonEmptyString } from './types'; |
| 12 | +import { arrayFromArrayOrSingle } from './codecs'; |
| 13 | +
|
| 14 | +export const UpdateShardKeyRequestBody = { |
| 15 | + shardKey: t.string, |
| 16 | + collectionType: t.union([ |
| 17 | + t.literal('user'), |
| 18 | + t.literal('enterprise'), |
| 19 | + t.literal('enterpriseTemplate') |
| 20 | + ]), |
| 21 | + ids: arrayFromArrayOrSingle(NonEmptyString), |
| 22 | + enterpriseUpdateUsers: t.boolean, |
| 23 | +} as const; |
| 24 | +`; |
| 25 | + |
| 26 | +const ARROW_WITH_PARAMS_TYPES = ` |
| 27 | +import * as t from 'io-ts'; |
| 28 | +
|
| 29 | +export const NonEmptyString = t.string; |
| 30 | +`; |
| 31 | + |
| 32 | +const ARROW_WITH_PARAMS_CODECS = ` |
| 33 | +import * as t from 'io-ts'; |
| 34 | +
|
| 35 | +export const arrayFromArrayOrSingle = <C extends t.Mixed>(codec: C) => |
| 36 | + t.array(codec); |
| 37 | +`; |
| 38 | + |
| 39 | +test('arrow function with parameters uses custom codec definition', async () => { |
| 40 | + const files = { |
| 41 | + '/src/main.ts': ARROW_WITH_PARAMS_MAIN, |
| 42 | + '/src/types.ts': ARROW_WITH_PARAMS_TYPES, |
| 43 | + '/src/codecs.ts': ARROW_WITH_PARAMS_CODECS, |
| 44 | + }; |
| 45 | + |
| 46 | + // Custom codec for arrayFromArrayOrSingle, merged with default io-ts codecs |
| 47 | + const knownImports: KnownImports = { |
| 48 | + ...KNOWN_IMPORTS, |
| 49 | + '.': { |
| 50 | + ...KNOWN_IMPORTS['.'], |
| 51 | + arrayFromArrayOrSingle: (_deref, innerSchema): E.Either<string, Schema> => |
| 52 | + E.right({ type: 'array', items: innerSchema }), |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + const project = new TestProject(files, knownImports); |
| 57 | + await project.parseEntryPoint('/src/main.ts'); |
| 58 | + const sourceFile = project.get('/src/main.ts'); |
| 59 | + |
| 60 | + assert.ok(sourceFile !== undefined, 'Source file not found'); |
| 61 | + |
| 62 | + const declaration = sourceFile.symbols.declarations.find( |
| 63 | + (s) => s.name === 'UpdateShardKeyRequestBody', |
| 64 | + ); |
| 65 | + assert.ok(declaration?.init !== undefined, 'Declaration not found'); |
| 66 | + |
| 67 | + const result = parsePlainInitializer(project, sourceFile, declaration.init); |
| 68 | + |
| 69 | + assert.ok( |
| 70 | + E.isRight(result), |
| 71 | + `Expected success, got: "${E.isLeft(result) ? result.left : ''}"`, |
| 72 | + ); |
| 73 | + assert.equal(result.right.type, 'object'); |
| 74 | + if (result.right.type === 'object') { |
| 75 | + assert.equal( |
| 76 | + result.right.properties?.ids?.type, |
| 77 | + 'array', |
| 78 | + 'ids field should be array (from custom codec)', |
| 79 | + ); |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +test('arrow function with parameters without custom codec returns ref', async () => { |
| 84 | + const files = { |
| 85 | + '/src/main.ts': ` |
| 86 | +import * as t from 'io-ts'; |
| 87 | +import { NonEmptyString } from './types'; |
| 88 | +import { arrayFromArrayOrSingle } from './codecs'; |
| 89 | +
|
| 90 | +export const TestBody = { |
| 91 | + ids: arrayFromArrayOrSingle(NonEmptyString), |
| 92 | +} as const; |
| 93 | +`, |
| 94 | + '/src/types.ts': ARROW_WITH_PARAMS_TYPES, |
| 95 | + '/src/codecs.ts': ARROW_WITH_PARAMS_CODECS, |
| 96 | + }; |
| 97 | + |
| 98 | + // No custom codec for arrayFromArrayOrSingle |
| 99 | + const project = new TestProject(files, KNOWN_IMPORTS); |
| 100 | + await project.parseEntryPoint('/src/main.ts'); |
| 101 | + const sourceFile = project.get('/src/main.ts'); |
| 102 | + |
| 103 | + assert.ok(sourceFile !== undefined, 'Source file not found'); |
| 104 | + |
| 105 | + const declaration = sourceFile.symbols.declarations.find( |
| 106 | + (s) => s.name === 'TestBody', |
| 107 | + ); |
| 108 | + assert.ok(declaration?.init !== undefined, 'Declaration not found'); |
| 109 | + |
| 110 | + const result = parsePlainInitializer(project, sourceFile, declaration.init); |
| 111 | + |
| 112 | + // Without custom codec, should return a ref (not an error) |
| 113 | + assert.ok( |
| 114 | + E.isRight(result), |
| 115 | + `Expected success, got: "${E.isLeft(result) ? result.left : ''}"`, |
| 116 | + ); |
| 117 | + assert.equal(result.right.type, 'object'); |
| 118 | + if (result.right.type === 'object') { |
| 119 | + assert.equal( |
| 120 | + result.right.properties?.ids?.type, |
| 121 | + 'ref', |
| 122 | + 'ids field should be ref (no custom codec)', |
| 123 | + ); |
| 124 | + } |
| 125 | +}); |
0 commit comments