-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathautofix_service.test.ts
More file actions
142 lines (110 loc) · 4.59 KB
/
autofix_service.test.ts
File metadata and controls
142 lines (110 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { describe, it, expect, beforeEach, beforeAll, vi } from 'vitest'
import { Connection } from 'vscode-languageserver/node'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { AutofixService } from '../src/autofix_service'
import { Config } from '@herb-tools/config'
import { Herb } from '@herb-tools/node-wasm'
describe('AutofixService', () => {
let connection: Connection
let autofixService: AutofixService
beforeAll(async () => {
await Herb.load()
})
beforeEach(() => {
connection = {
console: {
log: vi.fn(),
error: vi.fn()
}
} as unknown as Connection
autofixService = new AutofixService(connection)
})
describe('autofix', () => {
it('should return empty array when there are no offenses', async () => {
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, '<div>test</div>\n')
const result = await autofixService.autofix(document)
expect(result).toEqual([])
})
it('should fix uppercase tag names', async () => {
const input = '<DIV><SPAN>test</SPAN></DIV>\n'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result = await autofixService.autofix(document)
expect(result).toHaveLength(1)
expect(result[0].newText).toBe('<div><span>test</span></div>\n')
})
it('should return empty array when text is unchanged after autofix', async () => {
const input = '<div>test</div>\n'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result = await autofixService.autofix(document)
expect(result).toEqual([])
})
it('should handle multiple fixable offenses', async () => {
const input = '<DIV><SPAN>test</SPAN><P>paragraph</P></DIV>\n'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result = await autofixService.autofix(document)
expect(result).toHaveLength(1)
expect(result[0].newText).toBe('<div><span>test</span><p>paragraph</p></div>\n')
})
it('should handle ERB content correctly', async () => {
const input = '<DIV><% if true %><SPAN>test</SPAN><% end %></DIV>\n'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result = await autofixService.autofix(document)
expect(result).toHaveLength(1)
expect(result[0].newText).toBe('<div><% if true %><span>test</span><% end %></div>\n')
})
it('should handle documents with no fixable offenses', async () => {
const input = '<h2>Content<h3>'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result = await autofixService.autofix(document)
expect(result).toEqual([])
})
it('should replace the full document range', async () => {
const input = '<DIV>test</DIV>\n'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result = await autofixService.autofix(document)
expect(result).toHaveLength(1)
expect(result[0].range).toEqual({
start: { line: 0, character: 0 },
end: { line: 1, character: 0 }
})
})
})
describe('with custom config', () => {
it('should respect config rules', async () => {
const config = Config.fromObject({
linter: {
enabled: true,
rules: {
'html-tag-name-lowercase': { enabled: false }
}
}
}, { projectPath: '/test', version: '0.9.1' })
autofixService.setConfig(config)
const input = '<DIV>test</DIV>\n'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result = await autofixService.autofix(document)
expect(result).toEqual([])
})
it('should rebuild linter when config changes', async () => {
const config1 = Config.fromObject({
linter: { enabled: true }
}, { projectPath: '/test', version: '0.9.1' })
autofixService.setConfig(config1)
const input = '<DIV>test</DIV>\n'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
const result1 = await autofixService.autofix(document)
expect(result1).toHaveLength(1)
const config2 = Config.fromObject({
linter: {
enabled: true,
rules: {
'html-tag-name-lowercase': { enabled: false }
}
}
}, { projectPath: '/test', version: '0.9.1' })
autofixService.setConfig(config2)
const result2 = await autofixService.autofix(document)
expect(result2).toEqual([])
})
})
})