|
| 1 | +import { Code, ConnectError } from '@connectrpc/connect' |
1 | 2 | import { describe, expect, it, vi } from 'vitest' |
2 | 3 |
|
3 | 4 | import { EventType } from '../../../src/envd/filesystem/filesystem_pb' |
@@ -131,4 +132,83 @@ describe('WatchHandle', () => { |
131 | 132 | expect(stopped).toBe(true) |
132 | 133 | }) |
133 | 134 | }) |
| 135 | + |
| 136 | + it('calls onExit with no argument when the watch is stopped by the user', async () => { |
| 137 | + // Simulates the transport: the stream stays open until `handleStop` |
| 138 | + // aborts it, then surfaces the abort as a Connect cancellation (which |
| 139 | + // `handleRpcError` would map to `TimeoutError`). |
| 140 | + let abortStream: (() => void) | undefined |
| 141 | + const stream = (async function* () { |
| 142 | + await new Promise<never>((_, reject) => { |
| 143 | + abortStream = () => |
| 144 | + reject(new ConnectError('This operation was aborted', Code.Canceled)) |
| 145 | + }) |
| 146 | + })() |
| 147 | + |
| 148 | + let exitArgs: unknown[] | undefined |
| 149 | + |
| 150 | + const handle = new WatchHandle( |
| 151 | + () => abortStream?.(), |
| 152 | + stream, |
| 153 | + undefined, |
| 154 | + (...args: unknown[]) => { |
| 155 | + exitArgs = args |
| 156 | + } |
| 157 | + ) |
| 158 | + |
| 159 | + await handle.stop() |
| 160 | + |
| 161 | + await vi.waitFor(() => { |
| 162 | + expect(exitArgs).toBeDefined() |
| 163 | + }) |
| 164 | + // A user-initiated stop is a clean end - no error is passed to onExit. |
| 165 | + expect(exitArgs).toEqual([]) |
| 166 | + }) |
| 167 | + |
| 168 | + it('still reports stream errors when the watch was not stopped by the user', async () => { |
| 169 | + const failure = new Error('stream broke') |
| 170 | + |
| 171 | + const stream = (async function* () { |
| 172 | + throw failure |
| 173 | + })() |
| 174 | + |
| 175 | + let exitArgs: unknown[] | undefined |
| 176 | + |
| 177 | + new WatchHandle( |
| 178 | + () => {}, |
| 179 | + stream, |
| 180 | + undefined, |
| 181 | + (...args: unknown[]) => { |
| 182 | + exitArgs = args |
| 183 | + } |
| 184 | + ) |
| 185 | + |
| 186 | + await vi.waitFor(() => { |
| 187 | + expect(exitArgs).toBeDefined() |
| 188 | + }) |
| 189 | + expect(exitArgs).toEqual([failure]) |
| 190 | + }) |
| 191 | + |
| 192 | + it('still reports onEvent errors raised after the watch is stopped', async () => { |
| 193 | + const failure = new Error('onEvent failed') |
| 194 | + let handle: WatchHandle | undefined |
| 195 | + let exitArgs: unknown[] | undefined |
| 196 | + |
| 197 | + handle = new WatchHandle( |
| 198 | + () => {}, |
| 199 | + events([filesystemEvent('a.txt')]), |
| 200 | + async () => { |
| 201 | + await handle?.stop() |
| 202 | + throw failure |
| 203 | + }, |
| 204 | + (...args: unknown[]) => { |
| 205 | + exitArgs = args |
| 206 | + } |
| 207 | + ) |
| 208 | + |
| 209 | + await vi.waitFor(() => { |
| 210 | + expect(exitArgs).toBeDefined() |
| 211 | + }) |
| 212 | + expect(exitArgs).toEqual([failure]) |
| 213 | + }) |
134 | 214 | }) |
0 commit comments