|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { EventType } from '../../../src/envd/filesystem/filesystem_pb' |
| 4 | +import { |
| 5 | + FilesystemEventType, |
| 6 | + WatchHandle, |
| 7 | +} from '../../../src/sandbox/filesystem/watchHandle' |
| 8 | + |
| 9 | +function filesystemEvent(name: string, type: EventType = EventType.WRITE) { |
| 10 | + return { |
| 11 | + event: { |
| 12 | + case: 'filesystem' as const, |
| 13 | + value: { name, type, entry: undefined }, |
| 14 | + }, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function events(items: ReturnType<typeof filesystemEvent>[]) { |
| 19 | + async function* gen() { |
| 20 | + for (const item of items) { |
| 21 | + yield item as any |
| 22 | + } |
| 23 | + } |
| 24 | + return gen() |
| 25 | +} |
| 26 | + |
| 27 | +describe('WatchHandle', () => { |
| 28 | + it('awaits an async onEvent before finishing and firing onExit', async () => { |
| 29 | + let callbackStarted = false |
| 30 | + let releaseCallback: (() => void) | undefined |
| 31 | + const callbackBlocked = new Promise<void>((resolve) => { |
| 32 | + releaseCallback = resolve |
| 33 | + }) |
| 34 | + |
| 35 | + const onEvent = async () => { |
| 36 | + callbackStarted = true |
| 37 | + await callbackBlocked |
| 38 | + } |
| 39 | + |
| 40 | + let exitCalled = false |
| 41 | + const onExit = () => { |
| 42 | + exitCalled = true |
| 43 | + } |
| 44 | + |
| 45 | + new WatchHandle( |
| 46 | + () => {}, |
| 47 | + events([filesystemEvent('a.txt')]), |
| 48 | + onEvent, |
| 49 | + onExit |
| 50 | + ) |
| 51 | + |
| 52 | + await vi.waitFor(() => { |
| 53 | + expect(callbackStarted).toBe(true) |
| 54 | + }) |
| 55 | + // onExit must not fire while onEvent is still pending. |
| 56 | + expect(exitCalled).toBe(false) |
| 57 | + |
| 58 | + releaseCallback?.() |
| 59 | + await vi.waitFor(() => { |
| 60 | + expect(exitCalled).toBe(true) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('routes a rejecting async onEvent to onExit and stops the watch', async () => { |
| 65 | + const error = new Error('callback failed') |
| 66 | + |
| 67 | + let stopped = false |
| 68 | + let exitErr: Error | undefined | 'unset' = 'unset' |
| 69 | + |
| 70 | + new WatchHandle( |
| 71 | + () => { |
| 72 | + stopped = true |
| 73 | + }, |
| 74 | + events([filesystemEvent('a.txt')]), |
| 75 | + async () => { |
| 76 | + throw error |
| 77 | + }, |
| 78 | + (err) => { |
| 79 | + exitErr = err ?? undefined |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | + await vi.waitFor(() => { |
| 84 | + expect(exitErr).not.toBe('unset') |
| 85 | + }) |
| 86 | + expect(exitErr).toBe(error) |
| 87 | + expect(stopped).toBe(true) |
| 88 | + }) |
| 89 | + |
| 90 | + it('calls onExit with no argument when the stream ends cleanly', async () => { |
| 91 | + const received: FilesystemEventType[] = [] |
| 92 | + let exitArgs: unknown[] | undefined |
| 93 | + |
| 94 | + new WatchHandle( |
| 95 | + () => {}, |
| 96 | + events([filesystemEvent('a.txt')]), |
| 97 | + (event) => { |
| 98 | + received.push(event.type) |
| 99 | + }, |
| 100 | + (...args: unknown[]) => { |
| 101 | + exitArgs = args |
| 102 | + } |
| 103 | + ) |
| 104 | + |
| 105 | + await vi.waitFor(() => { |
| 106 | + expect(exitArgs).toBeDefined() |
| 107 | + }) |
| 108 | + // No error is passed on a clean exit — onExit is called with zero args. |
| 109 | + expect(exitArgs).toEqual([]) |
| 110 | + expect(received).toEqual([FilesystemEventType.WRITE]) |
| 111 | + }) |
| 112 | + |
| 113 | + it('does not let a throwing onExit become an unhandled rejection', async () => { |
| 114 | + let stopped = false |
| 115 | + |
| 116 | + new WatchHandle( |
| 117 | + () => { |
| 118 | + stopped = true |
| 119 | + }, |
| 120 | + events([filesystemEvent('a.txt')]), |
| 121 | + () => {}, |
| 122 | + () => { |
| 123 | + throw new Error('onExit failed') |
| 124 | + } |
| 125 | + ) |
| 126 | + |
| 127 | + // handleStop runs after onExit, so observing the stop confirms the loop |
| 128 | + // ran the throwing onExit to completion. A leaked rejection would fail the |
| 129 | + // test run instead. |
| 130 | + await vi.waitFor(() => { |
| 131 | + expect(stopped).toBe(true) |
| 132 | + }) |
| 133 | + }) |
| 134 | +}) |
0 commit comments