Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 87 lines 2.89 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Deferred, Effect, Fiber } from 'effect';
3 import {
4 LISTENER_STOP,
5 makeShutdownController,
6 signalExitCode,
7 } from 'src/services/shutdown-signal';
8
9 describe('ShutdownSignal', () => {
10 it.effect('[Given] no command cooperates [Then] the runner must interrupt', () =>
11 Effect.sync(() => {
12 expect(makeShutdownController().offer('SIGINT')).toBe(false);
13 })
14 );
15
16 it.effect('[Given] a listener [Then] it takes SIGINT and SIGTERM but not SIGHUP', () =>
17 Effect.gen(function* () {
18 const takes = (signal: 'SIGINT' | 'SIGTERM' | 'SIGHUP') =>
19 Effect.gen(function* () {
20 const controller = makeShutdownController();
21 yield* controller.service.cooperate(LISTENER_STOP);
22 return controller.offer(signal);
23 }).pipe(Effect.scoped);
24
25 expect(yield* takes('SIGINT')).toBe(true);
26 expect(yield* takes('SIGTERM')).toBe(true);
27 expect(yield* takes('SIGHUP')).toBe(false);
28 })
29 );
30
31 it.effect(
32 '[Given] a cooperating command [When] it takes a signal [Then] `requested` yields it',
33 () =>
34 Effect.gen(function* () {
35 const controller = makeShutdownController();
36 yield* controller.service.cooperate(LISTENER_STOP);
37 const waiting = yield* Effect.forkChild(controller.service.requested);
38
39 expect(controller.offer('SIGTERM')).toBe(true);
40 expect(yield* Fiber.join(waiting)).toBe('SIGTERM');
41 })
42 );
43
44 it.effect('[Given] the cooperating scope closed [Then] a signal interrupts again', () =>
45 Effect.gen(function* () {
46 const controller = makeShutdownController();
47 yield* Effect.scoped(controller.service.cooperate(LISTENER_STOP));
48
49 expect(controller.offer('SIGINT')).toBe(false);
50 })
51 );
52
53 it.effect(
54 '[Given] a forced exit [Then] it runs every onForce and reports whether to re-raise',
55 () =>
56 Effect.gen(function* () {
57 const controller = makeShutdownController();
58 const forced = yield* Deferred.make<void>();
59 yield* controller.service.cooperate({
60 signals: ['SIGINT'],
61 onForce: () => Deferred.doneUnsafe(forced, Effect.void),
62 reraiseOnForce: true,
63 });
64
65 expect(controller.force()).toEqual({ reraise: true });
66 expect(Deferred.isDoneUnsafe(forced)).toBe(true);
67 })
68 );
69
70 it.effect('[Given] a listener only [Then] a forced exit does not re-raise', () =>
71 Effect.gen(function* () {
72 const controller = makeShutdownController();
73 yield* controller.service.cooperate(LISTENER_STOP);
74
75 expect(controller.force()).toEqual({ reraise: false });
76 })
77 );
78
79 it.each([
80 { signal: 'SIGHUP', code: 129 },
81 { signal: 'SIGINT', code: 130 },
82 { signal: 'SIGKILL', code: 137 },
83 { signal: 'SIGTERM', code: 143 },
84 ] as const)('signalExitCode($signal) is $code', ({ signal, code }) => {
85 expect(signalExitCode(signal)).toBe(code);
86 });
87 });