Skip to content

Commit 7f0264a

Browse files
test: use codebase style guide
1 parent 798cc5d commit 7f0264a

File tree

4 files changed

+233
-219
lines changed

4 files changed

+233
-219
lines changed

src/generators/addon-verify/__tests__/index.test.mjs

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
22
import { mkdtemp, readFile } from 'node:fs/promises';
33
import { tmpdir } from 'node:os';
44
import { join } from 'node:path';
5-
import test from 'node:test';
5+
import { describe, it } from 'node:test';
66

77
import { u } from 'unist-builder';
88

@@ -12,67 +12,72 @@ import {
1212
generateSectionFolderName,
1313
} from '../utils/section.mjs';
1414

15-
test('returns empty array when no code blocks match filename comment', async () => {
16-
const entry = {
17-
heading: { data: { name: 'Section A' } },
18-
content: u('root', [u('code', 'console.log("no filename header");')]),
19-
};
20-
21-
const result = await addon.generate([entry], {});
22-
23-
// No sections were buildable / no filenames extracted
24-
assert.deepEqual(result, []);
25-
});
26-
27-
test('ignores non-buildable sections (needs both .cc and .js)', async () => {
28-
// Only a .cc file present -> not buildable
29-
const entry = {
30-
heading: { data: { name: 'OnlyCC' } },
31-
content: u('root', [u('code', '// file1.cc\nint main() {}')]),
32-
};
33-
34-
const result = await addon.generate([entry], {});
35-
36-
assert.deepEqual(result, []);
37-
});
38-
39-
test('generates files array and writes files to disk when output provided', async () => {
40-
const sectionName = 'My Addon Section';
41-
42-
const entry = {
43-
heading: { data: { name: sectionName } },
44-
content: u('root', [
45-
u('code', '// file1.cc\nint main() {}'),
46-
u(
47-
'code',
48-
"// test.js\nmodule.exports = require('./build/Release/addon');"
49-
),
50-
]),
51-
};
52-
53-
const tmp = await mkdtemp(join(tmpdir(), 'doc-kit-'));
54-
55-
const returned = await addon.generate([entry], { output: tmp });
56-
57-
// Returned is an array of file arrays (one per section)
58-
assert.equal(Array.isArray(returned), true);
59-
assert.equal(returned.length, 1);
60-
61-
const files = returned[0];
62-
63-
assert.ok(files.some(f => f.name === 'file1.cc'));
64-
assert.ok(files.some(f => f.name === 'test.js'));
65-
assert.ok(files.some(f => f.name === 'binding.gyp'));
66-
67-
// Verify files were written to disk under the computed folder name
68-
const folderName = generateSectionFolderName(
69-
normalizeSectionName(sectionName),
70-
0
71-
);
72-
73-
const file1 = await readFile(join(tmp, folderName, 'file1.cc'), 'utf-8');
74-
const binding = await readFile(join(tmp, folderName, 'binding.gyp'), 'utf-8');
75-
76-
assert.match(file1, /int main/);
77-
assert.match(binding, /targets/);
15+
describe('generators/addon-verify', () => {
16+
it('returns empty array when no code blocks match filename comment', async () => {
17+
const entry = {
18+
heading: { data: { name: 'Section A' } },
19+
content: u('root', [u('code', 'console.log("no filename header");')]),
20+
};
21+
22+
const result = await addon.generate([entry], {});
23+
24+
// No sections were buildable / no filenames extracted
25+
assert.deepEqual(result, []);
26+
});
27+
28+
it('ignores non-buildable sections (needs both .cc and .js)', async () => {
29+
// Only a .cc file present -> not buildable
30+
const entry = {
31+
heading: { data: { name: 'OnlyCC' } },
32+
content: u('root', [u('code', '// file1.cc\nint main() {}')]),
33+
};
34+
35+
const result = await addon.generate([entry], {});
36+
37+
assert.deepEqual(result, []);
38+
});
39+
40+
it('generates files array and writes files to disk when output provided', async () => {
41+
const sectionName = 'My Addon Section';
42+
43+
const entry = {
44+
heading: { data: { name: sectionName } },
45+
content: u('root', [
46+
u('code', '// file1.cc\nint main() {}'),
47+
u(
48+
'code',
49+
"// test.js\nmodule.exports = require('./build/Release/addon');"
50+
),
51+
]),
52+
};
53+
54+
const tmp = await mkdtemp(join(tmpdir(), 'doc-kit-'));
55+
56+
const returned = await addon.generate([entry], { output: tmp });
57+
58+
// Returned is an array of file arrays (one per section)
59+
assert.equal(Array.isArray(returned), true);
60+
assert.equal(returned.length, 1);
61+
62+
const files = returned[0];
63+
64+
assert.ok(files.some(f => f.name === 'file1.cc'));
65+
assert.ok(files.some(f => f.name === 'test.js'));
66+
assert.ok(files.some(f => f.name === 'binding.gyp'));
67+
68+
// Verify files were written to disk under the computed folder name
69+
const folderName = generateSectionFolderName(
70+
normalizeSectionName(sectionName),
71+
0
72+
);
73+
74+
const file1 = await readFile(join(tmp, folderName, 'file1.cc'), 'utf-8');
75+
const binding = await readFile(
76+
join(tmp, folderName, 'binding.gyp'),
77+
'utf-8'
78+
);
79+
80+
assert.match(file1, /int main/);
81+
assert.match(binding, /targets/);
82+
});
7883
});

src/generators/llms-txt/__tests__/index.test.mjs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
22
import { readFile, mkdtemp } from 'node:fs/promises';
33
import { tmpdir } from 'node:os';
44
import { join } from 'node:path';
5-
import test from 'node:test';
5+
import { describe, it } from 'node:test';
66

77
import llms from '../index.mjs';
88

@@ -23,36 +23,38 @@ const makeEntry = ({
2323
llm_description,
2424
});
2525

26-
test('generate returns filled template including depth 1 entries', async () => {
27-
const entry = makeEntry({ title: 'Alpha', desc: 'Alpha description' });
26+
describe('generators/llms-txt', () => {
27+
it('returns filled template including depth 1 entries', async () => {
28+
const entry = makeEntry({ title: 'Alpha', desc: 'Alpha description' });
2829

29-
const result = await llms.generate([entry], {});
30+
const result = await llms.generate([entry], {});
3031

31-
assert.equal(typeof result, 'string');
32-
assert.match(result, /- \[Alpha\]/);
33-
assert.match(result, /Alpha description/);
34-
});
32+
assert.equal(typeof result, 'string');
33+
assert.match(result, /- \[Alpha\]/);
34+
assert.match(result, /Alpha description/);
35+
});
3536

36-
test('generate only includes depth 1 headings', async () => {
37-
const entry1 = makeEntry({ title: 'Top', depth: 1, desc: 'Top desc' });
38-
const entry2 = makeEntry({ title: 'Sub', depth: 2, desc: 'Sub desc' });
37+
it('only includes depth 1 headings', async () => {
38+
const entry1 = makeEntry({ title: 'Top', depth: 1, desc: 'Top desc' });
39+
const entry2 = makeEntry({ title: 'Sub', depth: 2, desc: 'Sub desc' });
3940

40-
const result = await llms.generate([entry1, entry2], {});
41+
const result = await llms.generate([entry1, entry2], {});
4142

42-
assert.match(result, /- \[Top\]/);
43-
assert.doesNotMatch(result, /- \[Sub\]/);
44-
});
43+
assert.match(result, /- \[Top\]/);
44+
assert.doesNotMatch(result, /- \[Sub\]/);
45+
});
4546

46-
test('generate writes llms.txt when output is provided', async () => {
47-
const entry = makeEntry({ title: 'WriteTest', desc: 'Write description' });
47+
it('writes llms.txt when output is provided', async () => {
48+
const entry = makeEntry({ title: 'WriteTest', desc: 'Write description' });
4849

49-
const tmp = await mkdtemp(join(tmpdir(), 'doc-kit-'));
50+
const tmp = await mkdtemp(join(tmpdir(), 'doc-kit-'));
5051

51-
const returned = await llms.generate([entry], { output: tmp });
52+
const returned = await llms.generate([entry], { output: tmp });
5253

53-
const file = await readFile(join(tmp, 'llms.txt'), 'utf-8');
54+
const file = await readFile(join(tmp, 'llms.txt'), 'utf-8');
5455

55-
assert.equal(returned, file);
56-
assert.match(file, /- \[WriteTest\]/);
57-
assert.match(file, /Write description/);
56+
assert.equal(returned, file);
57+
assert.match(file, /- \[WriteTest\]/);
58+
assert.match(file, /Write description/);
59+
});
5860
});

src/generators/man-page/__tests__/index.test.mjs

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
22
import { mkdtemp, readFile } from 'node:fs/promises';
33
import { tmpdir } from 'node:os';
44
import { join } from 'node:path';
5-
import test from 'node:test';
5+
import { describe, it } from 'node:test';
66

77
import { u } from 'unist-builder';
88

@@ -24,74 +24,76 @@ const createMock = ({
2424
content: u('root', [, u('paragraph', [textNode(desc)])]),
2525
});
2626

27-
test('throws when no cli documentation present', async () => {
28-
await assert.rejects(
29-
async () => {
30-
await manpage.generate([{ api: 'not-cli' }], {});
31-
},
32-
{ message: /Could not find any `cli` documentation/ }
33-
);
34-
});
27+
describe('generators/man-page', () => {
28+
it('throws when no cli documentation present', async () => {
29+
await assert.rejects(
30+
async () => {
31+
await manpage.generate([{ api: 'not-cli' }], {});
32+
},
33+
{ message: /Could not find any `cli` documentation/ }
34+
);
35+
});
3536

36-
test('generates mandoc including options and environment entries', async () => {
37-
const components = [
38-
createMock({ api: 'cli', slug: 'cli', depth: 1 }),
39-
createMock({ api: 'cli', slug: 'options', depth: 2 }),
40-
createMock({
41-
api: 'cli',
42-
slug: 'opt-a',
43-
depth: 3,
44-
headingText: '`-a`, `--all`',
45-
desc: 'Option A description',
46-
}),
47-
createMock({ api: 'cli', slug: 'environment-variables-1', depth: 2 }),
48-
createMock({
49-
api: 'cli',
50-
slug: 'env-foo',
51-
depth: 3,
52-
headingText: '`FOO=bar`',
53-
desc: 'Env FOO description',
54-
}),
55-
createMock({ api: 'cli', slug: 'after', depth: 2 }),
56-
];
37+
it('generates mandoc including options and environment entries', async () => {
38+
const components = [
39+
createMock({ api: 'cli', slug: 'cli', depth: 1 }),
40+
createMock({ api: 'cli', slug: 'options', depth: 2 }),
41+
createMock({
42+
api: 'cli',
43+
slug: 'opt-a',
44+
depth: 3,
45+
headingText: '`-a`, `--all`',
46+
desc: 'Option A description',
47+
}),
48+
createMock({ api: 'cli', slug: 'environment-variables-1', depth: 2 }),
49+
createMock({
50+
api: 'cli',
51+
slug: 'env-foo',
52+
depth: 3,
53+
headingText: '`FOO=bar`',
54+
desc: 'Env FOO description',
55+
}),
56+
createMock({ api: 'cli', slug: 'after', depth: 2 }),
57+
];
5758

58-
const result = await manpage.generate(components, {});
59+
const result = await manpage.generate(components, {});
5960

60-
// Ensure mandoc markers for options and environment variables are present
61-
assert.match(result, /\.It Fl/);
62-
assert.match(result, /Option A description/);
63-
assert.match(result, /\.It Ev/);
64-
assert.match(result, /Env FOO description/);
65-
});
61+
// Ensure mandoc markers for options and environment variables are present
62+
assert.match(result, /\.It Fl/);
63+
assert.match(result, /Option A description/);
64+
assert.match(result, /\.It Ev/);
65+
assert.match(result, /Env FOO description/);
66+
});
6667

67-
test('writes node.1 to output when provided', async () => {
68-
const components = [
69-
createMock({ api: 'cli', slug: 'options', depth: 2 }),
70-
createMock({
71-
api: 'cli',
72-
slug: 'opt-a',
73-
depth: 3,
74-
headingText: '`-a`',
75-
desc: 'desc',
76-
}),
77-
createMock({ api: 'cli', slug: 'environment-variables-1', depth: 2 }),
78-
createMock({
79-
api: 'cli',
80-
slug: 'env',
81-
depth: 3,
82-
headingText: '`X=`',
83-
desc: 'env desc',
84-
}),
85-
createMock({ api: 'cli', slug: 'end', depth: 2 }),
86-
];
68+
it('writes node.1 to output when provided', async () => {
69+
const components = [
70+
createMock({ api: 'cli', slug: 'options', depth: 2 }),
71+
createMock({
72+
api: 'cli',
73+
slug: 'opt-a',
74+
depth: 3,
75+
headingText: '`-a`',
76+
desc: 'desc',
77+
}),
78+
createMock({ api: 'cli', slug: 'environment-variables-1', depth: 2 }),
79+
createMock({
80+
api: 'cli',
81+
slug: 'env',
82+
depth: 3,
83+
headingText: '`X=`',
84+
desc: 'env desc',
85+
}),
86+
createMock({ api: 'cli', slug: 'end', depth: 2 }),
87+
];
8788

88-
const tmp = await mkdtemp(join(tmpdir(), 'doc-kit-'));
89+
const tmp = await mkdtemp(join(tmpdir(), 'doc-kit-'));
8990

90-
const returned = await manpage.generate(components, { output: tmp });
91+
const returned = await manpage.generate(components, { output: tmp });
9192

92-
const file = await readFile(join(tmp, 'node.1'), 'utf-8');
93+
const file = await readFile(join(tmp, 'node.1'), 'utf-8');
9394

94-
assert.equal(returned, file);
95-
assert.match(file, /desc/);
96-
assert.match(file, /env desc/);
95+
assert.equal(returned, file);
96+
assert.match(file, /desc/);
97+
assert.match(file, /env desc/);
98+
});
9799
});

0 commit comments

Comments
 (0)