1
1
import { PackageIdentifier } from "../browser.js" ;
2
- import { markdownToRule } from "./markdownToRule.js" ;
2
+ import { getRuleName , markdownToRule } from "./markdownToRule.js" ;
3
3
4
4
describe ( "markdownToRule" , ( ) => {
5
5
// Use a mock PackageIdentifier for testing
@@ -36,7 +36,7 @@ This is a test rule.`;
36
36
const result = markdownToRule ( content , mockId ) ;
37
37
expect ( result . globs ) . toBe ( "**/test/**/*.kt" ) ;
38
38
expect ( result . rule ) . toBe ( "# Test Rule\n\nThis is a test rule." ) ;
39
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
39
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
40
40
} ) ;
41
41
42
42
it ( "should handle missing frontmatter" , ( ) => {
@@ -47,7 +47,7 @@ This is a test rule without frontmatter.`;
47
47
const result = markdownToRule ( content , mockId ) ;
48
48
expect ( result . globs ) . toBeUndefined ( ) ;
49
49
expect ( result . rule ) . toBe ( content ) ;
50
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
50
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
51
51
} ) ;
52
52
53
53
it ( "should handle empty frontmatter" , ( ) => {
@@ -63,7 +63,7 @@ This is a test rule with empty frontmatter.`;
63
63
expect ( result . rule ) . toBe (
64
64
"# Test Rule\n\nThis is a test rule with empty frontmatter." ,
65
65
) ;
66
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
66
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
67
67
} ) ;
68
68
69
69
it ( "should handle frontmatter with whitespace" , ( ) => {
@@ -78,7 +78,7 @@ This is a test rule.`;
78
78
const result = markdownToRule ( content , mockId ) ;
79
79
expect ( result . globs ) . toBe ( "**/test/**/*.kt" ) ;
80
80
expect ( result . rule ) . toBe ( "# Test Rule\n\nThis is a test rule." ) ;
81
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
81
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
82
82
} ) ;
83
83
84
84
it ( "should handle Windows line endings (CRLF)" , ( ) => {
@@ -95,7 +95,7 @@ This is a test rule.`;
95
95
expect ( result . globs ) . toBe ( "**/test/**/*.kt" ) ;
96
96
// The result should be normalized to \n
97
97
expect ( result . rule ) . toBe ( "# Test Rule\n\nThis is a test rule." ) ;
98
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
98
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
99
99
} ) ;
100
100
101
101
it ( "should handle malformed frontmatter" , ( ) => {
@@ -112,7 +112,7 @@ This is a test rule.`;
112
112
const result = markdownToRule ( content , mockId ) ;
113
113
expect ( result . globs ) . toBeUndefined ( ) ;
114
114
expect ( result . rule ) . toBe ( content ) ;
115
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
115
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
116
116
} ) ;
117
117
118
118
it ( "should use packageIdentifierToDisplayName if no heading or frontmatter name" , ( ) => {
@@ -125,7 +125,7 @@ globs: "**/test/**/*.kt"
125
125
This is a test rule.` ;
126
126
127
127
const result = markdownToRule ( content , mockId ) ;
128
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
128
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
129
129
} ) ;
130
130
131
131
it ( "should use packageIdentifierToDisplayName if no heading or frontmatter name (no heading)" , ( ) => {
@@ -136,7 +136,7 @@ globs: "**/test/**/*.kt"
136
136
This is a test rule without a heading.` ;
137
137
138
138
const result = markdownToRule ( content , mockId ) ;
139
- expect ( result . name ) . toBe ( "/path/ to/file" ) ; // Should use packageIdentifierToDisplayName result
139
+ expect ( result . name ) . toBe ( "to/file" ) ; // Should use last two path segments
140
140
} ) ;
141
141
142
142
it ( "should include description from frontmatter" , ( ) => {
@@ -171,3 +171,108 @@ This is a rule with alwaysApply explicitly set to false.`;
171
171
expect ( result . alwaysApply ) . toBe ( false ) ;
172
172
} ) ;
173
173
} ) ;
174
+
175
+ describe ( "getRuleName" , ( ) => {
176
+ it ( "should return frontmatter name when provided" , ( ) => {
177
+ const frontmatter = { name : "Custom Rule Name" } ;
178
+ const id : PackageIdentifier = {
179
+ uriType : "file" ,
180
+ filePath : "/path/to/my-rule.md" ,
181
+ } ;
182
+
183
+ const result = getRuleName ( frontmatter , id ) ;
184
+ expect ( result ) . toBe ( "Custom Rule Name" ) ;
185
+ } ) ;
186
+
187
+ it ( "should return last two path segments for file identifier when no name" , ( ) => {
188
+ const frontmatter = { } ;
189
+ const id : PackageIdentifier = {
190
+ uriType : "file" ,
191
+ filePath : "/long/path/to/rules/my-rule.md" ,
192
+ } ;
193
+
194
+ const result = getRuleName ( frontmatter , id ) ;
195
+ expect ( result ) . toBe ( "rules/my-rule.md" ) ;
196
+ } ) ;
197
+
198
+ it ( "should handle file paths with backslashes" , ( ) => {
199
+ const frontmatter = { } ;
200
+ const id : PackageIdentifier = {
201
+ uriType : "file" ,
202
+ filePath : "C:\\long\\path\\to\\rules\\my-rule.md" ,
203
+ } ;
204
+
205
+ const result = getRuleName ( frontmatter , id ) ;
206
+ expect ( result ) . toBe ( "rules/my-rule.md" ) ;
207
+ } ) ;
208
+
209
+ it ( "should handle mixed forward and back slashes" , ( ) => {
210
+ const frontmatter = { } ;
211
+ const id : PackageIdentifier = {
212
+ uriType : "file" ,
213
+ filePath : "/path/to\\rules/my-rule.md" ,
214
+ } ;
215
+
216
+ const result = getRuleName ( frontmatter , id ) ;
217
+ expect ( result ) . toBe ( "rules/my-rule.md" ) ;
218
+ } ) ;
219
+
220
+ it ( "should handle single segment path" , ( ) => {
221
+ const frontmatter = { } ;
222
+ const id : PackageIdentifier = {
223
+ uriType : "file" ,
224
+ filePath : "my-rule.md" ,
225
+ } ;
226
+
227
+ const result = getRuleName ( frontmatter , id ) ;
228
+ expect ( result ) . toBe ( "my-rule.md" ) ;
229
+ } ) ;
230
+
231
+ it ( "should handle two segment path" , ( ) => {
232
+ const frontmatter = { } ;
233
+ const id : PackageIdentifier = {
234
+ uriType : "file" ,
235
+ filePath : "rules/my-rule.md" ,
236
+ } ;
237
+
238
+ const result = getRuleName ( frontmatter , id ) ;
239
+ expect ( result ) . toBe ( "rules/my-rule.md" ) ;
240
+ } ) ;
241
+
242
+ it ( "should return display name for non-file identifiers" , ( ) => {
243
+ const frontmatter = { } ;
244
+ const id : PackageIdentifier = {
245
+ uriType : "slug" ,
246
+ fullSlug : {
247
+ ownerSlug : "test-owner" ,
248
+ packageSlug : "my-rule-package" ,
249
+ versionSlug : "v1.0.0" ,
250
+ } ,
251
+ } ;
252
+
253
+ const result = getRuleName ( frontmatter , id ) ;
254
+ expect ( result ) . toBe ( "my-rule-package" ) ; // packageIdentifierToDisplayName returns just the packageSlug
255
+ } ) ;
256
+
257
+ it ( "should prioritize frontmatter name over file path" , ( ) => {
258
+ const frontmatter = { name : "Override Name" } ;
259
+ const id : PackageIdentifier = {
260
+ uriType : "file" ,
261
+ filePath : "/very/long/path/to/rules/original-name.md" ,
262
+ } ;
263
+
264
+ const result = getRuleName ( frontmatter , id ) ;
265
+ expect ( result ) . toBe ( "Override Name" ) ;
266
+ } ) ;
267
+
268
+ it ( "should handle empty string name in frontmatter by falling back to path" , ( ) => {
269
+ const frontmatter = { name : "" } ;
270
+ const id : PackageIdentifier = {
271
+ uriType : "file" ,
272
+ filePath : "/path/to/rules/my-rule.md" ,
273
+ } ;
274
+
275
+ const result = getRuleName ( frontmatter , id ) ;
276
+ expect ( result ) . toBe ( "rules/my-rule.md" ) ; // Empty string is falsy, so falls back to path logic
277
+ } ) ;
278
+ } ) ;
0 commit comments