Skip to content

Commit dc39eb7

Browse files
committed
est: 49%
1 parent 6b8b731 commit dc39eb7

File tree

3 files changed

+205
-1
lines changed

3 files changed

+205
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import { getEntryDescription, buildApiDocLink } from '../buildApiDocLink.mjs';
5+
6+
describe('getEntryDescription', () => {
7+
it('returns llm_description when available', () => {
8+
const entry = {
9+
llm_description: 'LLM generated description',
10+
content: { children: [] },
11+
};
12+
13+
const result = getEntryDescription(entry);
14+
assert.equal(result, 'LLM generated description');
15+
});
16+
17+
it('extracts first paragraph when no llm_description', () => {
18+
const entry = {
19+
content: {
20+
children: [
21+
{
22+
type: 'paragraph',
23+
children: [{ type: 'text', value: 'First paragraph' }],
24+
},
25+
],
26+
},
27+
};
28+
29+
const result = getEntryDescription(entry);
30+
assert.ok(result.length > 0);
31+
});
32+
33+
it('returns empty string when no paragraph found', () => {
34+
const entry = {
35+
content: {
36+
children: [
37+
{ type: 'heading', children: [{ type: 'text', value: 'Title' }] },
38+
],
39+
},
40+
};
41+
42+
const result = getEntryDescription(entry);
43+
assert.equal(result, '');
44+
});
45+
46+
it('removes newlines from description', () => {
47+
const entry = {
48+
content: {
49+
children: [
50+
{
51+
type: 'paragraph',
52+
children: [{ type: 'text', value: 'Line 1\nLine 2\r\nLine 3' }],
53+
},
54+
],
55+
},
56+
};
57+
58+
const result = getEntryDescription(entry);
59+
assert.equal(result.includes('\n'), false);
60+
assert.equal(result.includes('\r'), false);
61+
});
62+
});
63+
64+
describe('buildApiDocLink', () => {
65+
it('builds markdown link with description', () => {
66+
const entry = {
67+
heading: { data: { name: 'Test API' } },
68+
api_doc_source: 'doc/api/test.md',
69+
llm_description: 'Test description',
70+
};
71+
72+
const result = buildApiDocLink(entry);
73+
assert.ok(result.includes('[Test API]'));
74+
assert.ok(result.includes('/docs/latest/api/test.md'));
75+
assert.ok(result.includes('Test description'));
76+
});
77+
78+
it('handles doc path replacement', () => {
79+
const entry = {
80+
heading: { data: { name: 'API Method' } },
81+
api_doc_source: 'doc/some/path.md',
82+
content: { children: [] },
83+
};
84+
85+
const result = buildApiDocLink(entry);
86+
assert.ok(result.includes('/docs/latest/some/path.md'));
87+
});
88+
});

src/generators/llms-txt/utils/buildApiDocLink.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { transformNodeToString } from '../../../utils/unist.mjs';
99
* @param {ApiDocMetadataEntry} entry
1010
* @returns {string}
1111
*/
12-
const getEntryDescription = entry => {
12+
export const getEntryDescription = entry => {
1313
if (entry.llm_description) {
1414
return entry.llm_description;
1515
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import {
5+
groupNodesByModule,
6+
getVersionFromSemVer,
7+
coerceSemVer,
8+
getCompatibleVersions,
9+
sortChanges,
10+
} from '../generators.mjs';
11+
12+
describe('groupNodesByModule', () => {
13+
it('groups nodes by api property', () => {
14+
const nodes = [
15+
{ api: 'fs', name: 'readFile' },
16+
{ api: 'http', name: 'createServer' },
17+
{ api: 'fs', name: 'writeFile' },
18+
];
19+
20+
const result = groupNodesByModule(nodes);
21+
assert.equal(result.get('fs').length, 2);
22+
assert.equal(result.get('http').length, 1);
23+
});
24+
25+
it('handles empty array', () => {
26+
const result = groupNodesByModule([]);
27+
assert.equal(result.size, 0);
28+
});
29+
});
30+
31+
describe('getVersionFromSemVer', () => {
32+
it('returns major.x for minor 0', () => {
33+
const version = { major: 18, minor: 0, patch: 0 };
34+
const result = getVersionFromSemVer(version);
35+
assert.equal(result, '18.x');
36+
});
37+
38+
it('returns major.minor.x for non-zero minor', () => {
39+
const version = { major: 18, minor: 5, patch: 2 };
40+
const result = getVersionFromSemVer(version);
41+
assert.equal(result, '18.5.x');
42+
});
43+
});
44+
45+
describe('coerceSemVer', () => {
46+
it('returns valid semver unchanged', () => {
47+
const result = coerceSemVer('1.2.3');
48+
assert.equal(result.version, '1.2.3');
49+
});
50+
51+
it('coerces invalid version to fallback', () => {
52+
const result = coerceSemVer('invalid');
53+
assert.equal(result.version, '0.0.0');
54+
});
55+
56+
it('handles null input', () => {
57+
const result = coerceSemVer(null);
58+
assert.equal(result.version, '0.0.0');
59+
});
60+
});
61+
62+
describe('getCompatibleVersions', () => {
63+
it('filters releases by major version', () => {
64+
const releases = [
65+
{ version: { major: 16 } },
66+
{ version: { major: 18 } },
67+
{ version: { major: 20 } },
68+
];
69+
70+
const result = getCompatibleVersions('18.0.0', releases);
71+
assert.equal(result.length, 2);
72+
assert.equal(result[0].version.major, 18);
73+
assert.equal(result[1].version.major, 20);
74+
});
75+
76+
it('includes all releases when introduced version is old', () => {
77+
const releases = [{ version: { major: 16 } }, { version: { major: 18 } }];
78+
79+
const result = getCompatibleVersions('14.0.0', releases);
80+
assert.equal(result.length, 2);
81+
});
82+
});
83+
84+
describe('sortChanges', () => {
85+
it('sorts changes by version', () => {
86+
const changes = [
87+
{ version: '18.5.0' },
88+
{ version: '16.2.0' },
89+
{ version: '20.1.0' },
90+
];
91+
92+
const result = sortChanges(changes);
93+
assert.equal(result[0].version, '16.2.0');
94+
assert.equal(result[1].version, '18.5.0');
95+
assert.equal(result[2].version, '20.1.0');
96+
});
97+
98+
it('handles array versions', () => {
99+
const changes = [
100+
{ version: ['18.5.0', '18.4.0'] },
101+
{ version: ['16.2.0'] },
102+
];
103+
104+
const result = sortChanges(changes);
105+
assert.equal(result[0].version[0], '16.2.0');
106+
assert.equal(result[1].version[0], '18.5.0');
107+
});
108+
109+
it('sorts by custom key', () => {
110+
const changes = [{ customVersion: '18.0.0' }, { customVersion: '16.0.0' }];
111+
112+
const result = sortChanges(changes, 'customVersion');
113+
assert.equal(result[0].customVersion, '16.0.0');
114+
assert.equal(result[1].customVersion, '18.0.0');
115+
});
116+
});

0 commit comments

Comments
 (0)