-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli-adapter.spec.ts
More file actions
270 lines (216 loc) · 8.16 KB
/
cli-adapter.spec.ts
File metadata and controls
270 lines (216 loc) · 8.16 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { CLIAdapter } from './cli-adapter.js';
import { createInstallerEventEmitter } from '../events.js';
// Mock console.log to capture styled output
const mockConsoleLog = vi.spyOn(console, 'log').mockImplementation(() => {});
// Mock clack
vi.mock('../../utils/clack.js', () => ({
default: {
intro: vi.fn(),
log: {
step: vi.fn(),
success: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
info: vi.fn(),
message: vi.fn(),
},
spinner: vi.fn(() => ({
start: vi.fn(),
stop: vi.fn(),
message: vi.fn(),
})),
confirm: vi.fn(),
text: vi.fn(),
password: vi.fn(),
isCancel: vi.fn(() => false),
outro: vi.fn(),
},
}));
vi.mock('../settings.js', () => ({
getConfig: vi.fn(() => ({
branding: {
showAsciiArt: false,
useCompact: true,
compactAsciiArt: 'Test Installer',
asciiArt: 'Big Art',
},
})),
}));
// Mock cli-symbols to avoid chalk color codes in test assertions
vi.mock('../../utils/cli-symbols.js', () => ({
styled: {
success: (text: string) => `✓ ${text}`,
error: (text: string) => `✗ ${text}`,
warning: (text: string) => `! ${text}`,
info: (text: string) => `ℹ ${text}`,
action: (text: string) => `→ ${text}`,
label: (label: string, value: string) => `${label} ${value}`,
phase: (num: number, total: number, name: string) => `${'▓'.repeat(num)}${'░'.repeat(total - num)} ${name}`,
bullet: (text: string) => ` • ${text}`,
},
symbols: {
success: '✓',
error: '✗',
warning: '!',
info: 'ℹ',
arrow: '→',
bullet: '•',
progressFilled: '▓',
progressEmpty: '░',
},
}));
describe('CLIAdapter', () => {
let emitter: ReturnType<typeof createInstallerEventEmitter>;
let sendEvent: ReturnType<typeof vi.fn>;
let adapter: CLIAdapter;
beforeEach(() => {
emitter = createInstallerEventEmitter();
sendEvent = vi.fn();
adapter = new CLIAdapter({ emitter, sendEvent });
});
afterEach(async () => {
await adapter.stop();
vi.clearAllMocks();
mockConsoleLog.mockClear();
});
describe('start', () => {
it('subscribes to events on start', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
// Emit auth:success - uses clack.log.success
emitter.emit('auth:success', {});
expect(clack.default.log.success).toHaveBeenCalledWith('Authenticated');
});
it('shows intro on start', async () => {
const clack = await import('../../utils/clack.js');
await adapter.start();
expect(clack.default.intro).toHaveBeenCalledWith('Welcome to the WorkOS AuthKit installer');
});
it('is idempotent', async () => {
const clack = await import('../../utils/clack.js');
await adapter.start();
await adapter.start(); // Second call should be no-op
expect(clack.default.intro).toHaveBeenCalledTimes(1);
});
});
describe('stop', () => {
it('unsubscribes from events on stop', async () => {
await adapter.start();
await adapter.stop();
const clack = await import('../../utils/clack.js');
vi.clearAllMocks();
// Emit an event - handler should NOT be called
emitter.emit('auth:checking', {});
expect(clack.default.log.step).not.toHaveBeenCalled();
});
it('is idempotent', async () => {
await adapter.start();
await adapter.stop();
await adapter.stop(); // Second call should be no-op
// Should not throw
});
});
describe('event handling', () => {
it('shows detection complete message', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
emitter.emit('detection:complete', { integration: 'nextjs' });
// Uses clack.log.success
expect(clack.default.log.success).toHaveBeenCalled();
});
it('shows spinner on agent:start', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
emitter.emit('agent:start', {});
expect(clack.default.spinner).toHaveBeenCalled();
});
it('updates spinner on agent:progress', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
const spinnerMock = {
start: vi.fn(),
stop: vi.fn(),
message: vi.fn(),
};
vi.mocked(clack.default.spinner).mockReturnValue(spinnerMock);
emitter.emit('agent:start', {});
emitter.emit('agent:progress', { step: 'Installing', detail: 'packages' });
expect(spinnerMock.message).toHaveBeenCalledWith('Installing: packages');
});
it('sends GIT_CONFIRMED on confirm', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
vi.mocked(clack.default.confirm).mockResolvedValue(true);
emitter.emit('git:dirty', { files: ['file1.ts'] });
// Wait for async handler
await new Promise((r) => setTimeout(r, 10));
expect(sendEvent).toHaveBeenCalledWith({ type: 'GIT_CONFIRMED' });
});
it('sends GIT_CANCELLED on decline', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
vi.mocked(clack.default.confirm).mockResolvedValue(false);
emitter.emit('git:dirty', { files: ['file1.ts'] });
await new Promise((r) => setTimeout(r, 10));
expect(sendEvent).toHaveBeenCalledWith({ type: 'GIT_CANCELLED' });
});
it('sends CREDENTIALS_SUBMITTED on credentials form', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
vi.mocked(clack.default.text).mockResolvedValueOnce('client_123'); // clientId
vi.mocked(clack.default.password).mockResolvedValueOnce('sk_test'); // apiKey (now uses password input)
emitter.emit('credentials:request', { requiresApiKey: true });
await new Promise((r) => setTimeout(r, 10));
expect(sendEvent).toHaveBeenCalledWith({
type: 'CREDENTIALS_SUBMITTED',
apiKey: 'sk_test',
clientId: 'client_123',
});
});
it('sends CANCEL when credentials form is cancelled', async () => {
await adapter.start();
const clack = await import('../../utils/clack.js');
vi.mocked(clack.default.isCancel).mockReturnValue(true);
vi.mocked(clack.default.text).mockResolvedValue(Symbol('cancel'));
emitter.emit('credentials:request', { requiresApiKey: false });
await new Promise((r) => setTimeout(r, 10));
expect(sendEvent).toHaveBeenCalledWith({ type: 'CANCEL' });
});
it('shows success summary box on complete', async () => {
await adapter.start();
const consoleSpy = vi.spyOn(console, 'log');
emitter.emit('complete', { success: true, summary: 'All done!' });
const output = consoleSpy.mock.calls.map((c) => String(c[0])).join('\n');
expect(output).toContain('WorkOS AuthKit Installed');
consoleSpy.mockRestore();
});
it('shows error summary box on failure complete', async () => {
await adapter.start();
const consoleSpy = vi.spyOn(console, 'log');
emitter.emit('complete', { success: false, summary: 'Something went wrong' });
const output = consoleSpy.mock.calls.map((c) => String(c[0])).join('\n');
expect(output).toContain('Installation Failed');
expect(output).toContain('Something went wrong');
consoleSpy.mockRestore();
});
it('keeps npx in auth recovery hints when launched through npm exec', async () => {
const originalNpmCommand = process.env.npm_command;
process.env.npm_command = 'exec';
try {
await adapter.start();
const clack = await import('../../utils/clack.js');
emitter.emit('error', { message: 'authentication failed', stack: undefined });
expect(clack.default.log.info).toHaveBeenCalledWith(
'Try running: npx workos@latest auth logout && npx workos@latest install',
);
} finally {
if (originalNpmCommand === undefined) {
delete process.env.npm_command;
} else {
process.env.npm_command = originalNpmCommand;
}
}
});
});
});