This repository was archived by the owner on Jan 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.test.js
More file actions
151 lines (123 loc) · 4.09 KB
/
install.test.js
File metadata and controls
151 lines (123 loc) · 4.09 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
// Mock modules
jest.mock('fs');
jest.mock('os');
describe('install.js', () => {
let consoleLogSpy;
let consoleErrorSpy;
let processExitSpy;
let fs;
let os;
beforeEach(() => {
// Reset modules to clear require cache
jest.resetModules();
// Re-require the mocked modules
fs = require('fs');
os = require('os');
// Clear all mocks before each test
jest.clearAllMocks();
// Spy on console methods
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
processExitSpy = jest.spyOn(process, 'exit').mockImplementation();
// Default mock implementations
os.homedir.mockReturnValue('/mock/home');
fs.existsSync.mockReturnValue(true);
fs.mkdirSync.mockImplementation();
fs.copyFileSync.mockImplementation();
});
afterEach(() => {
consoleLogSpy.mockRestore();
consoleErrorSpy.mockRestore();
processExitSpy.mockRestore();
});
test('successfully installs agent when directory exists', () => {
// Mock directory already exists
fs.existsSync.mockReturnValue(true);
// Run the install script
require('./install.js');
// Verify directory creation was not called
expect(fs.mkdirSync).not.toHaveBeenCalled();
// Verify file was copied
expect(fs.copyFileSync).toHaveBeenCalledWith(
expect.stringContaining('agent/typescript-linting.md'),
'/mock/home/.config/opencode/agent/typescript-linting.md'
);
// Verify success message
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining(
'✓ TypeScript Linting agent installed successfully!'
)
);
});
test('creates directory when it does not exist', () => {
// Mock directory does not exist
fs.existsSync.mockReturnValue(false);
// Run the install script
require('./install.js');
// Verify directory creation
expect(fs.mkdirSync).toHaveBeenCalledWith(
'/mock/home/.config/opencode/agent',
{ recursive: true }
);
// Verify file was copied
expect(fs.copyFileSync).toHaveBeenCalled();
// Verify success message
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining(
'✓ TypeScript Linting agent installed successfully!'
)
);
});
test('handles copy error gracefully', () => {
const errorMessage = 'Permission denied';
fs.copyFileSync.mockImplementation(() => {
throw new Error(errorMessage);
});
// Run the install script
require('./install.js');
// Verify error message
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to install TypeScript Linting agent:',
errorMessage
);
// Verify process exit with error code
expect(processExitSpy).toHaveBeenCalledWith(1);
});
test('displays correct usage instructions', () => {
// Run the install script
require('./install.js');
// Verify all usage instructions are displayed
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining('Usage:')
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining('Run "opencode" in any TypeScript project')
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining('Manual invocation:')
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining(
'@typescript-linting help me set up linting for this project'
)
);
});
test('uses correct target path', () => {
os.homedir.mockReturnValue('/custom/home');
// Run the install script
require('./install.js');
// Verify correct target path
expect(fs.copyFileSync).toHaveBeenCalledWith(
expect.any(String),
'/custom/home/.config/opencode/agent/typescript-linting.md'
);
});
test('source file path is constructed correctly', () => {
// Run the install script
require('./install.js');
// Verify source file includes correct path
const copyCall = fs.copyFileSync.mock.calls[0][0];
expect(copyCall).toContain('agent');
expect(copyCall).toContain('typescript-linting.md');
});
});