Skip to content

Commit bf9c320

Browse files
committed
test: test fallback to custom codecs when arrow function inlining fails
Check if parseFunctionBody() succeeded before returning, allowing parser to use custom codec definitions when function contains parameter references.
1 parent 97bb096 commit bf9c320

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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(declaration !== undefined, 'UpdateShardKeyRequestBody declaration should exist');
56+
assert.ok(declaration.init !== undefined, 'UpdateShardKeyRequestBody should have init');
57+
58+
const result = parsePlainInitializer(project, sourceFile, declaration.init);
59+
60+
assert.ok(E.isRight(result), `Expected no errors, but got "${E.isLeft(result) ? result.left : ''}"`);
61+
62+
assert.ok(
63+
result.right.type === 'object',
64+
'Should parse UpdateShardKeyRequestBody as object',
65+
);
66+
67+
assert.ok(
68+
result.right.properties?.ids?.type === 'array',
69+
'Should parse ids field as array (from custom codec definition)',
70+
);
71+
});

0 commit comments

Comments
 (0)