-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.test.mjs
More file actions
142 lines (123 loc) · 5.42 KB
/
index.test.mjs
File metadata and controls
142 lines (123 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { strictEqual, deepStrictEqual } from 'node:assert';
import { describe, it } from 'node:test';
import {
parseYAMLIntoMetadata,
transformTypeToReferenceLink,
parseHeadingIntoMetadata,
normalizeYamlSyntax,
} from '../index.mjs';
describe('transformTypeToReferenceLink', () => {
it('should transform a JavaScript primitive type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('string'),
'[`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type)'
);
});
it('should transform a JavaScript global type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('Array'),
'[`<Array>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)'
);
});
it('should transform a type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('SomeOtherType', {
SomeOtherType: 'fromTypeMap',
}),
'[`<SomeOtherType>`](fromTypeMap)'
);
});
it('should transform a basic Generic type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('{Promise<string>}'),
'[`<Promise>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type)>'
);
});
it('should partially transform a Generic type if only one part is known', () => {
strictEqual(
transformTypeToReferenceLink('{CustomType<string>}', {}),
'`<CustomType>`<[`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type)>'
);
});
it('should transform a Generic type with an inner union like {Promise<string|boolean>}', () => {
strictEqual(
transformTypeToReferenceLink('{Promise<string|boolean>}', {}),
'[`<Promise>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) | [`<boolean>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type)>'
);
});
it('should transform multi-parameter generics like {Map<string, number>}', () => {
strictEqual(
transformTypeToReferenceLink('{Map<string, number>}', {}),
'[`<Map>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)<[`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type), [`<number>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type)>'
);
});
it('should handle outer unions with generics like {Promise<string|number> | boolean}', () => {
strictEqual(
transformTypeToReferenceLink('{Promise<string|number> | boolean}', {}),
'[`<Promise>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) | [`<number>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type)> | [`<boolean>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type)'
);
});
});
describe('normalizeYamlSyntax', () => {
it('should normalize YAML syntax by fixing noncompliant properties', () => {
const input = `introduced_in=v0.1.21
source_link=lib/test.js
type=module
name=test_module
llm_description=This is a test module`;
const normalizedYaml = normalizeYamlSyntax(input);
strictEqual(
normalizedYaml,
`introduced_in: v0.1.21
source_link: lib/test.js
type: module
name: test_module
llm_description: This is a test module`
);
});
it('should remove leading and trailing newlines', () => {
const input = '\nintroduced_in=v0.1.21\n';
const normalizedYaml = normalizeYamlSyntax(input);
strictEqual(normalizedYaml, 'introduced_in: v0.1.21');
});
});
describe('parseYAMLIntoMetadata', () => {
it('should parse a YAML string into a JavaScript object', () => {
const input = 'name: test\ntype: module\nintroduced_in: v1.0.0';
const expectedOutput = {
name: 'test',
type: 'module',
introduced_in: 'v1.0.0',
};
deepStrictEqual(parseYAMLIntoMetadata(input), expectedOutput);
});
it('should parse a YAML string with multiple versions into a JavaScript object', () => {
const input = 'name: test\ntype: module\nintroduced_in: [v1.0.0, v1.1.0]';
const expectedOutput = {
name: 'test',
type: 'module',
introduced_in: ['v1.0.0', 'v1.1.0'],
};
deepStrictEqual(parseYAMLIntoMetadata(input), expectedOutput);
});
it('should parse a YAML string with source_link into a JavaScript object', () => {
const input =
'name: test\ntype: module\nintroduced_in: v1.0.0\nsource_link: https://github.com/nodejs/node';
const expectedOutput = {
name: 'test',
type: 'module',
introduced_in: 'v1.0.0',
source_link: 'https://github.com/nodejs/node',
};
deepStrictEqual(parseYAMLIntoMetadata(input), expectedOutput);
});
it('should parse a raw Heading string into Heading metadata', () => {
const input = '## test';
const expectedOutput = {
text: '## test',
name: '## test',
depth: 2,
};
deepStrictEqual(parseHeadingIntoMetadata(input, 2), expectedOutput);
});
});