1
+import { describe, expect, layer } from '@effect/vitest';
2
+import { Effect } from 'effect';
3
+import { TerminalUI } from 'src/services/terminal-ui';
4
+import { TestLive, MockConsole } from 'test/__utils__';
5
+
6
+describe('TerminalUI', () => {
7
+ layer(TestLive())(it => {
8
+ // -----------------------------------------------------------------------
9
+ // Log text capture
10
+ // -----------------------------------------------------------------------
11
+
12
+ it.scoped('log.info writes text capturable by MockConsole', () =>
13
+ Effect.gen(function* () {
14
+ const ui = yield* TerminalUI;
15
+ yield* ui.log.info('hello from info');
16
+
17
+ const lines = yield* MockConsole.getLines();
18
+ expect(lines).toContain('hello from info');
19
+ })
20
+ );
21
+
22
+ it.scoped('log.success writes text capturable by MockConsole', () =>
23
+ Effect.gen(function* () {
24
+ const ui = yield* TerminalUI;
25
+ yield* ui.log.success('operation succeeded');
26
+
27
+ const lines = yield* MockConsole.getLines();
28
+ expect(lines).toContain('operation succeeded');
29
+ })
30
+ );
31
+
32
+ it.scoped('log.warn writes text capturable by MockConsole', () =>
33
+ Effect.gen(function* () {
34
+ const ui = yield* TerminalUI;
35
+ yield* ui.log.warn('something is off');
36
+
37
+ const lines = yield* MockConsole.getLines();
38
+ expect(lines).toContain('something is off');
39
+ })
40
+ );
41
+
42
+ it.scoped('log.error writes text capturable by MockConsole', () =>
43
+ Effect.gen(function* () {
44
+ const ui = yield* TerminalUI;
45
+ yield* ui.log.error('something broke');
46
+
47
+ const lines = yield* MockConsole.getLines();
48
+ expect(lines).toContain('something broke');
49
+ })
50
+ );
51
+
52
+ it.scoped('log.step writes text capturable by MockConsole', () =>
53
+ Effect.gen(function* () {
54
+ const ui = yield* TerminalUI;
55
+ yield* ui.log.step('step completed');
56
+
57
+ const lines = yield* MockConsole.getLines();
58
+ expect(lines).toContain('step completed');
59
+ })
60
+ );
61
+
62
+ it.scoped('note writes title and message capturable by MockConsole', () =>
63
+ Effect.gen(function* () {
64
+ const ui = yield* TerminalUI;
65
+ yield* ui.note('api_key_123', 'API Key');
66
+
67
+ const lines = yield* MockConsole.getLines();
68
+ const output = lines.join('\n');
69
+ expect(output).toContain('api_key_123');
70
+ expect(output).toContain('API Key');
71
+ })
72
+ );
73
+
74
+ it.scoped('intro and outro write text capturable by MockConsole', () =>
75
+ Effect.gen(function* () {
76
+ const ui = yield* TerminalUI;
77
+ yield* ui.intro('my command');
78
+ yield* ui.outro('all done');
79
+
80
+ const lines = yield* MockConsole.getLines();
81
+ const output = lines.join('\n');
82
+ expect(output).toContain('my command');
83
+ expect(output).toContain('all done');
84
+ })
85
+ );
86
+
87
+ // -----------------------------------------------------------------------
88
+ // withSpinner lifecycle
89
+ // -----------------------------------------------------------------------
90
+
91
+ it.scoped('withSpinner captures success message', () =>
92
+ Effect.gen(function* () {
93
+ const ui = yield* TerminalUI;
94
+ const result = yield* ui.withSpinner('loading', Effect.succeed(42), {
95
+ successMessage: 'loaded 42 items',
96
+ });
97
+
98
+ expect(result).toBe(42);
99
+ const lines = yield* MockConsole.getLines();
100
+ expect(lines).toContain('loaded 42 items');
101
+ })
102
+ );
103
+
104
+ it.scoped('withSpinner propagates errors', () =>
105
+ Effect.gen(function* () {
106
+ const ui = yield* TerminalUI;
107
+ const exit = yield* Effect.exit(
108
+ ui.withSpinner('loading', Effect.fail(new Error('network error')))
109
+ );
110
+
111
+ expect(exit._tag).toBe('Failure');
112
+ })
113
+ );
114
+
115
+ // -----------------------------------------------------------------------
116
+ // useMakeSpinner — auto-cleanup on error
117
+ // -----------------------------------------------------------------------
118
+
119
+ it.scoped('useMakeSpinner auto-stops spinner on error', () =>
120
+ Effect.gen(function* () {
121
+ const ui = yield* TerminalUI;
122
+
123
+ const exit = yield* Effect.exit(
124
+ ui.useMakeSpinner('fetching data', _spinner => Effect.fail(new Error('API returned 400')))
125
+ );
126
+
127
+ expect(exit._tag).toBe('Failure');
128
+
129
+ // The spinner error message should be captured
130
+ const lines = yield* MockConsole.getLines();
131
+ expect(lines).toContain('fetching data');
132
+ })
133
+ );
134
+
135
+ it.scoped('useMakeSpinner does NOT double-stop if user already stopped', () =>
136
+ Effect.gen(function* () {
137
+ const ui = yield* TerminalUI;
138
+
139
+ yield* ui.useMakeSpinner('processing', spinner =>
140
+ Effect.gen(function* () {
141
+ yield* spinner.stop('done processing');
142
+ })
143
+ );
144
+
145
+ const lines = yield* MockConsole.getLines();
146
+ // Should see the user's stop message, not the auto-cleanup
147
+ expect(lines).toContain('done processing');
148
+ // Should NOT see the default message as an error (which would happen with double-stop)
149
+ expect(lines.filter(l => l === 'processing')).toHaveLength(0);
150
+ })
151
+ );
152
+
153
+ it.scoped('useMakeSpinner allows message updates before stop', () =>
154
+ Effect.gen(function* () {
155
+ const ui = yield* TerminalUI;
156
+
157
+ yield* ui.useMakeSpinner('starting', spinner =>
158
+ Effect.gen(function* () {
159
+ yield* spinner.message('step 1');
160
+ yield* spinner.message('step 2');
161
+ yield* spinner.stop('finished');
162
+ })
163
+ );
164
+
165
+ const lines = yield* MockConsole.getLines();
166
+ expect(lines).toContain('finished');
167
+ })
168
+ );
169
+
170
+ it.scoped('useMakeSpinner returns the value from the use callback', () =>
171
+ Effect.gen(function* () {
172
+ const ui = yield* TerminalUI;
173
+
174
+ const result = yield* ui.useMakeSpinner('computing', spinner =>
175
+ Effect.gen(function* () {
176
+ yield* spinner.stop('computed');
177
+ return 99;
178
+ })
179
+ );
180
+
181
+ expect(result).toBe(99);
182
+ })
183
+ );
184
+ });
185
+});