Skip to content

Commit 5eb0658

Browse files
authored
coverage: increase to ~47% (#309)
1 parent aff9339 commit 5eb0658

File tree

11 files changed

+609
-9
lines changed

11 files changed

+609
-9
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import { generateFileList } from '../generateFileList.mjs';
5+
6+
describe('generateFileList', () => {
7+
it('should transform test.js files with updated require paths', () => {
8+
const codeBlocks = [
9+
{
10+
name: 'test.js',
11+
content: "const addon = require('./build/Release/addon');",
12+
},
13+
];
14+
15+
const result = generateFileList(codeBlocks);
16+
const testFile = result.find(file => file.name === 'test.js');
17+
18+
assert(testFile.content.includes("'use strict';"));
19+
assert(testFile.content.includes('`./build/${common.buildType}/addon`'));
20+
assert(!testFile.content.includes("'./build/Release/addon'"));
21+
});
22+
23+
it('should preserve other files unchanged', () => {
24+
const codeBlocks = [{ name: 'addon.cc', content: '#include <node.h>' }];
25+
26+
const result = generateFileList(codeBlocks);
27+
28+
assert.equal(
29+
result.find(file => file.name === 'addon.cc').content,
30+
'#include <node.h>'
31+
);
32+
});
33+
34+
it('should add binding.gyp file', () => {
35+
const codeBlocks = [{ name: 'addon.cc', content: 'code' }];
36+
37+
const result = generateFileList(codeBlocks);
38+
const bindingFile = result.find(file => file.name === 'binding.gyp');
39+
40+
assert(bindingFile);
41+
const config = JSON.parse(bindingFile.content);
42+
assert.equal(config.targets[0].target_name, 'addon');
43+
assert(config.targets[0].sources.includes('addon.cc'));
44+
});
45+
46+
it('should handle empty input', () => {
47+
const result = generateFileList([]);
48+
49+
assert.equal(result.length, 1);
50+
assert.equal(result[0].name, 'binding.gyp');
51+
});
52+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import {
5+
isBuildableSection,
6+
normalizeSectionName,
7+
generateSectionFolderName,
8+
} from '../section.mjs';
9+
10+
describe('isBuildableSection', () => {
11+
it('should return true when both .cc and .js files are present', () => {
12+
const codeBlocks = [
13+
{ name: 'addon.cc', content: 'C++ code' },
14+
{ name: 'test.js', content: 'JS code' },
15+
];
16+
17+
assert.equal(isBuildableSection(codeBlocks), true);
18+
});
19+
20+
it('should return false when only .cc file is present', () => {
21+
const codeBlocks = [{ name: 'addon.cc', content: 'C++ code' }];
22+
23+
assert.equal(isBuildableSection(codeBlocks), false);
24+
});
25+
26+
it('should return false when only .js file is present', () => {
27+
const codeBlocks = [{ name: 'test.js', content: 'JS code' }];
28+
29+
assert.equal(isBuildableSection(codeBlocks), false);
30+
});
31+
32+
it('should return false for empty array', () => {
33+
assert.equal(isBuildableSection([]), false);
34+
});
35+
});
36+
37+
describe('normalizeSectionName', () => {
38+
it('should convert to lowercase and replace spaces with underscores', () => {
39+
assert.equal(normalizeSectionName('Hello World'), 'hello_world');
40+
});
41+
42+
it('should remove non-word characters', () => {
43+
assert.equal(normalizeSectionName('Test-Section!@#'), 'testsection');
44+
});
45+
46+
it('should handle empty string', () => {
47+
assert.equal(normalizeSectionName(''), '');
48+
});
49+
50+
it('should handle mixed cases and special characters', () => {
51+
assert.equal(
52+
normalizeSectionName('My Test & Example #1'),
53+
'my_test__example_1'
54+
);
55+
});
56+
});
57+
58+
describe('generateSectionFolderName', () => {
59+
it('should generate folder name with padded index', () => {
60+
assert.equal(generateSectionFolderName('hello_world', 0), '01_hello_world');
61+
});
62+
63+
it('should pad single digit indices', () => {
64+
assert.equal(generateSectionFolderName('test', 5), '06_test');
65+
});
66+
67+
it('should not pad double digit indices', () => {
68+
assert.equal(generateSectionFolderName('example', 15), '16_example');
69+
});
70+
71+
it('should handle empty section name', () => {
72+
assert.equal(generateSectionFolderName('', 0), '01_');
73+
});
74+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import { findParent, buildHierarchy } from '../buildHierarchy.mjs';
5+
6+
describe('findParent', () => {
7+
it('finds parent with lower depth', () => {
8+
const entries = [{ heading: { depth: 1 } }, { heading: { depth: 2 } }];
9+
const parent = findParent(entries[1], entries, 0);
10+
assert.equal(parent, entries[0]);
11+
});
12+
13+
it('throws when no parent exists', () => {
14+
const entries = [{ heading: { depth: 2 } }];
15+
assert.throws(() => findParent(entries[0], entries, -1));
16+
});
17+
});
18+
19+
describe('buildHierarchy', () => {
20+
it('returns empty array for empty input', () => {
21+
assert.deepEqual(buildHierarchy([]), []);
22+
});
23+
24+
it('keeps root entries at top level', () => {
25+
const entries = [{ heading: { depth: 1 } }, { heading: { depth: 1 } }];
26+
const result = buildHierarchy(entries);
27+
assert.equal(result.length, 2);
28+
});
29+
30+
it('nests children under parents', () => {
31+
const entries = [{ heading: { depth: 1 } }, { heading: { depth: 2 } }];
32+
const result = buildHierarchy(entries);
33+
34+
assert.equal(result.length, 1);
35+
assert.equal(result[0].hierarchyChildren.length, 1);
36+
assert.equal(result[0].hierarchyChildren[0], entries[1]);
37+
});
38+
39+
it('handles multiple levels', () => {
40+
const entries = [
41+
{ heading: { depth: 1 } },
42+
{ heading: { depth: 2 } },
43+
{ heading: { depth: 3 } },
44+
];
45+
const result = buildHierarchy(entries);
46+
47+
assert.equal(result.length, 1);
48+
assert.equal(result[0].hierarchyChildren[0].hierarchyChildren.length, 1);
49+
});
50+
});
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import {
5+
transformTypeReferences,
6+
extractPattern,
7+
parseListItem,
8+
parseList,
9+
} from '../parseList.mjs';
10+
11+
describe('transformTypeReferences', () => {
12+
it('replaces template syntax with curly braces', () => {
13+
const result = transformTypeReferences('`<string>`');
14+
assert.equal(result, '{string}');
15+
});
16+
17+
it('normalizes multiple types', () => {
18+
const result = transformTypeReferences('`<string>` | `<number>`');
19+
assert.equal(result, '{string|number}');
20+
});
21+
});
22+
23+
describe('extractPattern', () => {
24+
it('extracts pattern and removes from text', () => {
25+
const current = {};
26+
const result = extractPattern(
27+
'name: test description',
28+
/name:\s*([^.\s]+)/,
29+
'name',
30+
current
31+
);
32+
33+
assert.equal(current.name, 'test');
34+
assert.equal(result, ' description');
35+
});
36+
37+
it('returns original text when pattern not found', () => {
38+
const current = {};
39+
const result = extractPattern(
40+
'no match',
41+
/missing:\s*([^.]+)/,
42+
'missing',
43+
current
44+
);
45+
46+
assert.equal(result, 'no match');
47+
assert.equal(current.missing, undefined);
48+
});
49+
});
50+
51+
describe('parseListItem', () => {
52+
it('parses basic list item', () => {
53+
const child = {
54+
children: [
55+
{
56+
type: 'paragraph',
57+
children: [{ type: 'text', value: 'param {string} description' }],
58+
},
59+
],
60+
};
61+
62+
const result = parseListItem(child);
63+
assert.equal(typeof result, 'object');
64+
assert.ok(result.textRaw);
65+
});
66+
67+
it('identifies return items', () => {
68+
const child = {
69+
children: [
70+
{
71+
type: 'paragraph',
72+
children: [{ type: 'text', value: 'Returns: something' }],
73+
},
74+
],
75+
};
76+
77+
const result = parseListItem(child);
78+
assert.equal(result.name, 'return');
79+
});
80+
});
81+
82+
describe('parseList', () => {
83+
it('processes property sections', () => {
84+
const section = { type: 'property', name: 'test' };
85+
const nodes = [
86+
{
87+
type: 'list',
88+
children: [
89+
{
90+
children: [
91+
{
92+
type: 'paragraph',
93+
children: [{ type: 'text', value: '{string} description' }],
94+
},
95+
],
96+
},
97+
],
98+
},
99+
];
100+
101+
parseList(section, nodes);
102+
assert.ok(section.textRaw);
103+
});
104+
105+
it('processes event sections', () => {
106+
const section = { type: 'event' };
107+
const nodes = [
108+
{
109+
type: 'list',
110+
children: [
111+
{
112+
children: [
113+
{
114+
type: 'paragraph',
115+
children: [{ type: 'text', value: 'param description' }],
116+
},
117+
],
118+
},
119+
],
120+
},
121+
];
122+
123+
parseList(section, nodes);
124+
assert.ok(Array.isArray(section.params));
125+
});
126+
});

0 commit comments

Comments
 (0)