|
| 1 | +const E = require('fp-ts/lib/Either'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | +const test = require('node:test'); |
| 4 | +const { TestProject } = require('./testProject'); |
| 5 | +const { parsePlainInitializer } = require('../src'); |
| 6 | + |
| 7 | +test('arrow function with parameters should use custom codec definition', async () => { |
| 8 | + const files = { |
| 9 | + '/api/v2/admin/migrateOrgShardKey.ts': ` |
| 10 | +import * as t from 'io-ts'; |
| 11 | +import { NonEmptyString } from './types'; |
| 12 | +import { arrayFromArrayOrSingle } from '../../../utils'; |
| 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 | + '/api/v2/admin/types.ts': ` |
| 26 | +import * as t from 'io-ts'; |
| 27 | +
|
| 28 | +export const NonEmptyString = t.string; |
| 29 | +`, |
| 30 | + '/utils/arrayFromArrayOrSingle.ts': ` |
| 31 | +import * as t from 'io-ts'; |
| 32 | +import { arrayFromSingle } from '@bitgo/public-types'; |
| 33 | +
|
| 34 | +export const arrayFromArrayOrSingle = <C extends t.Mixed>(codec: C) => |
| 35 | + t.union([t.array(codec), arrayFromSingle(codec)]); |
| 36 | +`, |
| 37 | + }; |
| 38 | + |
| 39 | + const knownImports = { |
| 40 | + '.': { |
| 41 | + arrayFromArrayOrSingle: (_: any, innerSchema: any) => |
| 42 | + E.right({ type: 'array', items: innerSchema }), |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + const project = new TestProject(files, knownImports); |
| 47 | + await project.parseEntryPoint('/api/v2/admin/migrateOrgShardKey.ts'); |
| 48 | + const sourceFile = project.get('/api/v2/admin/migrateOrgShardKey.ts'); |
| 49 | + |
| 50 | + assert.ok(sourceFile !== undefined, 'Source file should exist'); |
| 51 | + |
| 52 | + const declaration = sourceFile.symbols.declarations.find( |
| 53 | + (s: any) => s.name === 'UpdateShardKeyRequestBody', |
| 54 | + ); |
| 55 | + assert.ok( |
| 56 | + declaration !== undefined, |
| 57 | + 'UpdateShardKeyRequestBody declaration should exist', |
| 58 | + ); |
| 59 | + assert.ok( |
| 60 | + declaration.init !== undefined, |
| 61 | + 'UpdateShardKeyRequestBody should have init', |
| 62 | + ); |
| 63 | + |
| 64 | + const result = parsePlainInitializer(project, sourceFile, declaration.init); |
| 65 | + |
| 66 | + assert.ok( |
| 67 | + E.isRight(result), |
| 68 | + `Expected no errors, but got "${E.isLeft(result) ? result.left : ''}"`, |
| 69 | + ); |
| 70 | + |
| 71 | + assert.ok( |
| 72 | + result.right.type === 'object', |
| 73 | + 'Should parse UpdateShardKeyRequestBody as object', |
| 74 | + ); |
| 75 | + |
| 76 | + assert.ok( |
| 77 | + result.right.properties?.ids?.type === 'array', |
| 78 | + 'Should parse ids field as array (from custom codec definition)', |
| 79 | + ); |
| 80 | +}); |
0 commit comments