Skip to content

Commit 52246d7

Browse files
committed
add tests
1 parent 31fa591 commit 52246d7

File tree

2 files changed

+168
-7
lines changed

2 files changed

+168
-7
lines changed

src/linter/tests/engine.test.mjs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,39 @@ describe('createLinterEngine', () => {
99
const rule1 = mock.fn(() => []);
1010
const rule2 = mock.fn(() => []);
1111

12-
const engine = createLinterEngine([rule1, rule2]);
12+
const engine = createLinterEngine({
13+
singleEntryRules: [rule1],
14+
multiEntryRules: [rule2],
15+
});
1316

14-
engine.lint(assertEntry);
17+
engine.lintAll([assertEntry]);
1518

1619
assert.strictEqual(rule1.mock.callCount(), 1);
1720
assert.strictEqual(rule2.mock.callCount(), 1);
1821

1922
assert.deepEqual(rule1.mock.calls[0].arguments, [assertEntry]);
20-
assert.deepEqual(rule2.mock.calls[0].arguments, [assertEntry]);
23+
assert.deepEqual(rule2.mock.calls[0].arguments, [[assertEntry]]);
2124
});
2225

2326
it('should return the aggregated issues from all rules', () => {
2427
const rule1 = mock.fn(() => [infoIssue, warnIssue]);
2528
const rule2 = mock.fn(() => [errorIssue]);
2629

27-
const engine = createLinterEngine([rule1, rule2]);
30+
const engine = createLinterEngine({
31+
singleEntryRules: [rule1],
32+
multiEntryRules: [rule2],
33+
});
2834

29-
const issues = engine.lint(assertEntry);
35+
const issues = engine.lintAll([assertEntry]);
3036

3137
assert.equal(issues.length, 3);
32-
assert.deepEqual(issues, [infoIssue, warnIssue, errorIssue]);
38+
assert.deepEqual(issues, [errorIssue, infoIssue, warnIssue]);
3339
});
3440

3541
it('should return an empty array when no issues are found', () => {
3642
const rule = () => [];
3743

38-
const engine = createLinterEngine([rule]);
44+
const engine = createLinterEngine({ singleEntryRules: [rule] });
3945

4046
const issues = engine.lint(assertEntry);
4147

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { describe, it } from 'node:test';
2+
import { deepStrictEqual } from 'assert';
3+
import { duplicateStabilityNodes } from '../../rules/duplicate-stability-nodes.mjs';
4+
import { LINT_MESSAGES } from '../../../constants.mjs';
5+
6+
// Mock data structure for creating test entries
7+
const createEntry = (
8+
depth,
9+
stabilityIndex,
10+
source = 'file.yaml',
11+
position = { line: 1, column: 1 }
12+
) => ({
13+
heading: { data: { depth } },
14+
stability: { children: [{ data: { index: stabilityIndex } }] },
15+
api_doc_source: source,
16+
yaml_position: position,
17+
});
18+
19+
describe('duplicateStabilityNodes', () => {
20+
it('returns empty array when there are no entries', () => {
21+
deepStrictEqual(duplicateStabilityNodes([]), []);
22+
});
23+
24+
it('returns empty array when there are no duplicate stability nodes', () => {
25+
const entries = [createEntry(1, 0), createEntry(2, 1), createEntry(3, 2)];
26+
deepStrictEqual(duplicateStabilityNodes(entries), []);
27+
});
28+
29+
it('detects duplicate stability nodes within a chain', () => {
30+
const entries = [
31+
createEntry(1, 0),
32+
createEntry(2, 0), // Duplicate stability node
33+
];
34+
35+
deepStrictEqual(duplicateStabilityNodes(entries), [
36+
{
37+
level: 'warn',
38+
message: LINT_MESSAGES.duplicateStabilityNode,
39+
location: {
40+
path: 'file.yaml',
41+
position: { line: 1, column: 1 },
42+
},
43+
},
44+
]);
45+
});
46+
47+
it('resets stability tracking when depth decreases', () => {
48+
const entries = [
49+
createEntry(1, 0),
50+
createEntry(2, 0), // This should trigger an issue
51+
createEntry(1, 1),
52+
createEntry(2, 1), // This should trigger another issue
53+
];
54+
55+
deepStrictEqual(duplicateStabilityNodes(entries), [
56+
{
57+
level: 'warn',
58+
message: LINT_MESSAGES.duplicateStabilityNode,
59+
location: {
60+
path: 'file.yaml',
61+
position: { line: 1, column: 1 },
62+
},
63+
},
64+
{
65+
level: 'warn',
66+
message: LINT_MESSAGES.duplicateStabilityNode,
67+
location: {
68+
path: 'file.yaml',
69+
position: { line: 1, column: 1 },
70+
},
71+
},
72+
]);
73+
});
74+
75+
it('handles missing stability nodes gracefully', () => {
76+
const entries = [
77+
createEntry(1, -1),
78+
createEntry(2, -1),
79+
createEntry(3, 0),
80+
createEntry(4, 0), // This should trigger an issue
81+
];
82+
83+
deepStrictEqual(duplicateStabilityNodes(entries), [
84+
{
85+
level: 'warn',
86+
message: LINT_MESSAGES.duplicateStabilityNode,
87+
location: {
88+
path: 'file.yaml',
89+
position: { line: 1, column: 1 },
90+
},
91+
},
92+
]);
93+
});
94+
95+
it('handles entries with no stability property gracefully', () => {
96+
const entries = [
97+
{
98+
heading: { data: { depth: 1 } },
99+
stability: { children: [] },
100+
api_doc_source: 'file.yaml',
101+
yaml_position: { line: 2, column: 5 },
102+
},
103+
createEntry(2, 0),
104+
];
105+
deepStrictEqual(duplicateStabilityNodes(entries), []);
106+
});
107+
108+
it('handles entries with undefined stability index', () => {
109+
const entries = [
110+
createEntry(1, undefined),
111+
createEntry(2, undefined),
112+
createEntry(3, 1),
113+
createEntry(4, 1), // This should trigger an issue
114+
];
115+
deepStrictEqual(duplicateStabilityNodes(entries), [
116+
{
117+
level: 'warn',
118+
message: LINT_MESSAGES.duplicateStabilityNode,
119+
location: {
120+
path: 'file.yaml',
121+
position: { line: 1, column: 1 },
122+
},
123+
},
124+
]);
125+
});
126+
127+
it('handles mixed depths and stability nodes correctly', () => {
128+
const entries = [
129+
createEntry(1, 0),
130+
createEntry(2, 1),
131+
createEntry(3, 1), // This should trigger an issue
132+
createEntry(2, 2),
133+
createEntry(3, 2), // This should trigger another issue
134+
];
135+
136+
deepStrictEqual(duplicateStabilityNodes(entries), [
137+
{
138+
level: 'warn',
139+
message: LINT_MESSAGES.duplicateStabilityNode,
140+
location: {
141+
path: 'file.yaml',
142+
position: { line: 1, column: 1 },
143+
},
144+
},
145+
{
146+
level: 'warn',
147+
message: LINT_MESSAGES.duplicateStabilityNode,
148+
location: {
149+
path: 'file.yaml',
150+
position: { line: 1, column: 1 },
151+
},
152+
},
153+
]);
154+
});
155+
});

0 commit comments

Comments
 (0)