1 import createAbortController, {
2 isAbortError,
3 getAbortError,
4 } from "../create-abort-controller"
5 import ProcessManager from "../process-manager"
6
7 describe("create-abort-controller", () => {
8 describe("createAbortController", () => {
9 let processManager: ProcessManager
10 let sigtermListeners: Array<(...args: any[]) => void>
11 let sigintListeners: Array<(...args: any[]) => void>
12
13 beforeEach(() => {
14 // Store existing listeners BEFORE creating ProcessManager
15 sigtermListeners = process.listeners("SIGTERM") as Array<
16 (...args: any[]) => void
17 >
18 sigintListeners = process.listeners("SIGINT") as Array<
19 (...args: any[]) => void
20 >
21 processManager = new ProcessManager()
22 })
23
24 afterEach(() => {
25 // Remove all listeners added during tests
26 process.removeAllListeners("SIGTERM")
27 process.removeAllListeners("SIGINT")
28 // Restore original listeners
29 sigtermListeners.forEach((listener) =>
30 process.on("SIGTERM", listener as any)
31 )
32 sigintListeners.forEach((listener) => process.on("SIGINT", listener as any))
33 })
34
35 it("should create and return an AbortController", () => {
36 const abortController = createAbortController(processManager)
37
38 expect(abortController).toBeInstanceOf(AbortController)
39 expect(abortController.signal).toBeDefined()
40 })
41
42 it("should abort the controller when process terminates with SIGTERM", () => {
43 const abortController = createAbortController(processManager)
44 const abortSpy = jest.spyOn(abortController, "abort")
45
46 process.emit("SIGTERM")
47
48 expect(abortSpy).toHaveBeenCalled()
49 })
50
51 it("should abort the controller when process terminates with SIGINT", () => {
52 const abortController = createAbortController(processManager)
53 const abortSpy = jest.spyOn(abortController, "abort")
54
55 process.emit("SIGINT")
56
57 expect(abortSpy).toHaveBeenCalled()
58 })
59
60 it("should not be aborted initially", () => {
61 const abortController = createAbortController(processManager)
62
63 expect(abortController.signal.aborted).toBe(false)
64 })
65 })
66
67 describe("isAbortError", () => {
68 it("should return true for abort error objects", () => {
69 const abortError = { code: "ABORT_ERR" }
70
71 expect(isAbortError(abortError)).toBe(true)
72 })
73
74 it("should return true for abort error from getAbortError", () => {
75 const abortError = getAbortError()
76
77 expect(isAbortError(abortError)).toBe(true)
78 })
79
80 it("should return false for null", () => {
81 expect(isAbortError(null)).toBe(false)
82 })
83
84 it("should return false for undefined", () => {
85 expect(isAbortError(undefined)).toBe(false)
86 })
87
88 it("should return false for string errors", () => {
89 expect(isAbortError("npm install failed with status null")).toBe(false)
90 })
91
92 it("should return false for Error objects without code", () => {
93 const error = new Error("Something went wrong")
94
95 expect(isAbortError(error)).toBe(false)
96 })
97
98 it("should return false for objects with wrong code", () => {
99 const error = { code: "ENOENT" }
100
101 expect(isAbortError(error)).toBe(false)
102 })
103
104 it("should return false for objects with code as different type", () => {
105 const error = { code: 123 }
106
107 expect(isAbortError(error)).toBe(false)
108 })
109
110 it("should return false for empty objects", () => {
111 expect(isAbortError({})).toBe(false)
112 })
113
114 it("should return false for numbers", () => {
115 expect(isAbortError(123)).toBe(false)
116 })
117
118 it("should return false for booleans", () => {
119 expect(isAbortError(true)).toBe(false)
120 expect(isAbortError(false)).toBe(false)
121 })
122
123 it("should return false for arrays", () => {
124 expect(isAbortError([])).toBe(false)
125 expect(isAbortError(["ABORT_ERR"])).toBe(false)
126 })
127
128 it("should handle objects with ABORT_ERR code and additional properties", () => {
129 const error = {
130 code: "ABORT_ERR",
131 message: "Operation aborted",
132 timestamp: Date.now(),
133 }
134
135 expect(isAbortError(error)).toBe(true)
136 })
137
138 it("should handle Error-like objects with ABORT_ERR code", () => {
139 const error = Object.assign(new Error("Aborted"), { code: "ABORT_ERR" })
140
141 expect(isAbortError(error)).toBe(true)
142 })
143 })
144
145 describe("getAbortError", () => {
146 it("should return an object with ABORT_ERR code", () => {
147 const error = getAbortError()
148
149 expect(error).toEqual({ code: "ABORT_ERR" })
150 })
151
152 it("should return an object that passes isAbortError check", () => {
153 const error = getAbortError()
154
155 expect(isAbortError(error)).toBe(true)
156 })
157
158 it("should return a new object on each call", () => {
159 const error1 = getAbortError()
160 const error2 = getAbortError()
161
162 expect(error1).not.toBe(error2)
163 expect(error1).toEqual(error2)
164 })
165 })
166 })