-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoload-plugin.test.ts
More file actions
151 lines (117 loc) · 4.9 KB
/
autoload-plugin.test.ts
File metadata and controls
151 lines (117 loc) · 4.9 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
143
144
145
146
147
148
149
150
151
import { beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock(import('./generate-autoloader.js'))
vi.mock(import('./scan-commands.js'))
const { generateStaticAutoloader } = await import('./generate-autoloader.js')
const { scanCommandsDir } = await import('./scan-commands.js')
const { createAutoloadPlugin } = await import('./autoload-plugin.js')
const mockGenerateStaticAutoloader = vi.mocked(generateStaticAutoloader)
const mockScanCommandsDir = vi.mocked(scanCommandsDir)
beforeEach(() => {
vi.clearAllMocks()
})
describe('createAutoloadPlugin', () => {
describe('transform hook', () => {
it('should return null for non-kidd dist files', () => {
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const result = plugin.transform('some code', '/other/package/dist/index.js')
expect(result).toBeNull()
})
it('should return null when no region start marker found', () => {
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const code = 'const x = 1\n//#endregion\n'
const result = plugin.transform(code, '/node_modules/kidd/dist/index.js')
expect(result).toBeNull()
})
it('should return null when no region end marker found', () => {
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const code = 'const x = 1\n//#region src/autoload.ts\nsome content'
const result = plugin.transform(code, '/node_modules/kidd/dist/index.js')
expect(result).toBeNull()
})
it('should replace region with static import when markers found in kidd dist', () => {
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const code = [
'const before = 1',
'//#region src/autoload.ts',
'async function autoload() { return {} }',
'//#endregion',
'const after = 2',
].join('\n')
const result = plugin.transform(code, '/node_modules/kidd/dist/index.js')
expect(result).toContain('const before = 1')
expect(result).toContain('const after = 2')
expect(result).toContain('//#region src/autoload.ts (static)')
expect(result).toContain("await import('virtual:kidd-static-commands')")
expect(result).toContain('return mod.autoload()')
expect(result).not.toContain('async function autoload() { return {} }')
})
})
describe('resolveId hook', () => {
it('should resolve virtual module ID to prefixed ID', () => {
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const result = plugin.resolveId('virtual:kidd-static-commands')
expect(result).toBe('\0virtual:kidd-static-commands')
})
it('should return null for non-virtual module IDs', () => {
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const result = plugin.resolveId('./some-module.js')
expect(result).toBeNull()
})
})
describe('load hook', () => {
it('should return null for non-virtual module IDs', async () => {
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const result = await plugin.load('./some-module.js')
expect(result).toBeNull()
})
it('should call scanCommandsDir and generateStaticAutoloader for virtual module', async () => {
const scanResult = { dirs: [], files: [] }
mockScanCommandsDir.mockResolvedValueOnce(scanResult)
mockGenerateStaticAutoloader.mockReturnValueOnce('generated code')
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
const result = await plugin.load('\0virtual:kidd-static-commands')
expect(result).toBe('generated code')
expect(mockScanCommandsDir).toHaveBeenCalledOnce()
expect(mockGenerateStaticAutoloader).toHaveBeenCalledOnce()
})
it('should pass commandsDir and tagModulePath to generators', async () => {
const scanResult = { dirs: [], files: [] }
mockScanCommandsDir.mockResolvedValueOnce(scanResult)
mockGenerateStaticAutoloader.mockReturnValueOnce('generated code')
const plugin = createAutoloadPlugin({
commandsDir: '/project/commands',
tagModulePath: '/project/tag.js',
})
await plugin.load('\0virtual:kidd-static-commands')
expect(mockScanCommandsDir).toHaveBeenCalledWith('/project/commands')
expect(mockGenerateStaticAutoloader).toHaveBeenCalledWith({
scan: scanResult,
tagModulePath: '/project/tag.js',
})
})
})
})