Skip to content

Commit f8824ed

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 f8824ed

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 actual: any = {};
53+
const errors: string[] = [];
54+
55+
for (const symbol of sourceFile.symbols.declarations) {
56+
if (symbol.init !== undefined) {
57+
const result = parsePlainInitializer(project, sourceFile, symbol.init);
58+
if (E.isLeft(result)) {
59+
errors.push(result.left);
60+
} else {
61+
actual[symbol.name] = result.right;
62+
}
63+
}
64+
}
65+
66+
assert.deepEqual(errors, [], `Expected no errors, but got "${errors[0]}"`);
67+
68+
assert.ok(
69+
actual.UpdateShardKeyRequestBody?.type === 'object',
70+
'Should parse UpdateShardKeyRequestBody as object',
71+
);
72+
73+
assert.ok(
74+
actual.UpdateShardKeyRequestBody?.properties?.ids?.type === 'array',
75+
'Should parse ids field as array (from custom codec definition)',
76+
);
77+
});

0 commit comments

Comments
 (0)