@madeinoz67/voice-server 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,298 @@
1
+ /**
2
+ * Integration tests for FIFO Notification Queue API
3
+ *
4
+ * Note: These tests require MLX-audio to be installed.
5
+ * They will be skipped if MLX-audio is not available.
6
+ */
7
+
8
+ import { describe, test, expect, beforeAll, afterAll } from "bun:test";
9
+
10
+ // Test server configuration
11
+ const SERVER_HOST = "127.0.0.1";
12
+ const SERVER_PORT = 8899; // Use different port for tests
13
+ const SERVER_URL = `http://${SERVER_HOST}:${SERVER_PORT}`;
14
+
15
+ let serverProcess: ReturnType<typeof Bun.spawn> | null = null;
16
+
17
+ // Check if MLX-audio is available
18
+ async function checkMLXAudio(): Promise<boolean> {
19
+ try {
20
+ const proc = Bun.spawn(["which", "mlx_tts"], {
21
+ stdout: "pipe",
22
+ stderr: "pipe",
23
+ });
24
+ await proc.exited;
25
+ return proc.exitCode === 0;
26
+ } catch {
27
+ return false;
28
+ }
29
+ }
30
+
31
+ const hasMLXAudio = await checkMLXAudio();
32
+
33
+ describe.skipIf(!hasMLXAudio)("Queue API Integration Tests (skipped - MLX-audio not available)", () => {
34
+ beforeAll(async () => {
35
+ // Start test server
36
+ serverProcess = Bun.spawn(["bun", "run", "src/ts/server.ts"], {
37
+ env: {
38
+ ...process.env,
39
+ PORT: SERVER_PORT.toString(),
40
+ NODE_ENV: "test",
41
+ ENABLE_MACOS_NOTIFICATIONS: "false", // Disable macOS notifications in tests
42
+ },
43
+ stdout: "pipe",
44
+ stderr: "pipe",
45
+ });
46
+
47
+ // Wait for server to be ready
48
+ let retries = 50;
49
+ while (retries > 0) {
50
+ try {
51
+ const response = await fetch(`${SERVER_URL}/health`);
52
+ if (response.ok) {
53
+ break;
54
+ }
55
+ } catch {
56
+ // Server not ready yet
57
+ }
58
+ await new Promise((resolve) => setTimeout(resolve, 100));
59
+ retries--;
60
+ }
61
+
62
+ if (retries === 0) {
63
+ throw new Error("Server failed to start");
64
+ }
65
+ }, 10000);
66
+
67
+ afterAll(() => {
68
+ if (serverProcess) {
69
+ serverProcess.kill();
70
+ }
71
+ });
72
+
73
+ describe("POST /notify", () => {
74
+ test("should return 201 Accepted for valid notification", async () => {
75
+ const response = await fetch(`${SERVER_URL}/notify`, {
76
+ method: "POST",
77
+ headers: { "Content-Type": "application/json" },
78
+ body: JSON.stringify({
79
+ message: "Test notification",
80
+ voice_id: "marrvin",
81
+ }),
82
+ });
83
+
84
+ expect(response.status).toBe(201);
85
+ const data = await response.json();
86
+ expect(data.status).toBe("success");
87
+ expect(data.message).toContain("accepted");
88
+ });
89
+
90
+ test("should return 400 Bad Request for missing message", async () => {
91
+ const response = await fetch(`${SERVER_URL}/notify`, {
92
+ method: "POST",
93
+ headers: { "Content-Type": "application/json" },
94
+ body: JSON.stringify({ voice_id: "marrvin" }),
95
+ });
96
+
97
+ expect(response.status).toBe(400);
98
+ const data = await response.json();
99
+ expect(data.status).toBe("error");
100
+ });
101
+
102
+ test("should return 400 Bad Request for invalid JSON", async () => {
103
+ const response = await fetch(`${SERVER_URL}/notify`, {
104
+ method: "POST",
105
+ headers: { "Content-Type": "application/json" },
106
+ body: "invalid json",
107
+ });
108
+
109
+ expect(response.status).toBe(400);
110
+ });
111
+
112
+ test("should return 201 for notification with title", async () => {
113
+ const response = await fetch(`${SERVER_URL}/notify`, {
114
+ method: "POST",
115
+ headers: { "Content-Type": "application/json" },
116
+ body: JSON.stringify({
117
+ title: "Test Title",
118
+ message: "Test notification with title",
119
+ voice_id: "marrvin",
120
+ }),
121
+ });
122
+
123
+ expect(response.status).toBe(201);
124
+ const data = await response.json();
125
+ expect(data.status).toBe("success");
126
+ });
127
+
128
+ test("should process multiple requests sequentially", async () => {
129
+ // Send multiple requests rapidly
130
+ const requests = [];
131
+ for (let i = 0; i < 5; i++) {
132
+ requests.push(
133
+ fetch(`${SERVER_URL}/notify`, {
134
+ method: "POST",
135
+ headers: { "Content-Type": "application/json" },
136
+ body: JSON.stringify({
137
+ message: `Sequential test ${i}`,
138
+ voice_id: "marrvin",
139
+ }),
140
+ })
141
+ );
142
+ }
143
+
144
+ const responses = await Promise.all(requests);
145
+
146
+ // All should return 201 Accepted
147
+ for (const response of responses) {
148
+ expect(response.status).toBe(201);
149
+ const data = await response.json();
150
+ expect(data.status).toBe("success");
151
+ }
152
+
153
+ // Wait for queue to process (with voice_enabled=false for speed)
154
+ await new Promise((resolve) => setTimeout(resolve, 500));
155
+ });
156
+ });
157
+
158
+ describe("GET /queue/status", () => {
159
+ test("should return queue status", async () => {
160
+ const response = await fetch(`${SERVER_URL}/queue/status`);
161
+
162
+ expect(response.status).toBe(200);
163
+ const data = await response.json();
164
+ expect(data.status).toBe("success");
165
+ expect(data.depth).toBeGreaterThanOrEqual(0);
166
+ expect(data.processingStatus).toMatch(/^(idle|active|draining)$/);
167
+ expect(data.health).toMatch(/^(healthy|degraded|unavailable)$/);
168
+ });
169
+
170
+ test("should include metrics in status", async () => {
171
+ const response = await fetch(`${SERVER_URL}/queue/status`);
172
+
173
+ expect(response.status).toBe(200);
174
+ const data = await response.json();
175
+ expect(data.metrics).toBeDefined();
176
+ expect(data.metrics.itemsProcessed).toBeGreaterThanOrEqual(0);
177
+ expect(data.metrics.itemsFailed).toBeGreaterThanOrEqual(0);
178
+ });
179
+
180
+ test("should reflect queue state after enqueue", async () => {
181
+ // Send a notification
182
+ await fetch(`${SERVER_URL}/notify`, {
183
+ method: "POST",
184
+ headers: { "Content-Type": "application/json" },
185
+ body: JSON.stringify({
186
+ message: "Status check test",
187
+ voice_id: "marrvin",
188
+ voice_enabled: false, // Skip TTS for faster test
189
+ }),
190
+ });
191
+
192
+ // Check status
193
+ const response = await fetch(`${SERVER_URL}/queue/status`);
194
+ expect(response.status).toBe(200);
195
+ const data = await response.json();
196
+ expect(data.status).toBe("success");
197
+ // Queue might be empty or processing depending on timing
198
+ expect(data.depth).toBeGreaterThanOrEqual(0);
199
+ });
200
+ });
201
+
202
+ describe("Queue Health Indicators", () => {
203
+ test("should show healthy when queue is empty", async () => {
204
+ // Wait for any pending items to process
205
+ await new Promise((resolve) => setTimeout(resolve, 500));
206
+
207
+ const response = await fetch(`${SERVER_URL}/queue/status`);
208
+ const data = await response.json();
209
+
210
+ expect(data.health).toBe("healthy");
211
+ });
212
+
213
+ test("should show degraded when queue has many items", async () => {
214
+ // This test would require filling the queue beyond degradedThreshold
215
+ // For now, we just verify the endpoint works
216
+ const response = await fetch(`${SERVER_URL}/queue/status`);
217
+ expect(response.status).toBe(200);
218
+ });
219
+ });
220
+
221
+ describe("CORS", () => {
222
+ test("should include CORS headers for /notify", async () => {
223
+ const response = await fetch(`${SERVER_URL}/notify`, {
224
+ method: "POST",
225
+ headers: {
226
+ Origin: "http://localhost:3000",
227
+ "Content-Type": "application/json",
228
+ },
229
+ body: JSON.stringify({
230
+ message: "CORS test",
231
+ voice_id: "marrvin",
232
+ voice_enabled: false,
233
+ }),
234
+ });
235
+
236
+ expect(response.headers.get("Access-Control-Allow-Origin")).toBeDefined();
237
+ });
238
+
239
+ test("should include CORS headers for /queue/status", async () => {
240
+ const response = await fetch(`${SERVER_URL}/queue/status`, {
241
+ headers: { Origin: "http://localhost:3000" },
242
+ });
243
+
244
+ expect(response.headers.get("Access-Control-Allow-Origin")).toBeDefined();
245
+ });
246
+
247
+ test("should handle OPTIONS preflight for /notify", async () => {
248
+ const response = await fetch(`${SERVER_URL}/notify`, {
249
+ method: "OPTIONS",
250
+ headers: {
251
+ Origin: "http://localhost:3000",
252
+ "Access-Control-Request-Method": "POST",
253
+ },
254
+ });
255
+
256
+ expect(response.status).toBe(204);
257
+ });
258
+ });
259
+
260
+ describe("Response Time", () => {
261
+ test("should return 201 within 100ms", async () => {
262
+ const start = Date.now();
263
+
264
+ const response = await fetch(`${SERVER_URL}/notify`, {
265
+ method: "POST",
266
+ headers: { "Content-Type": "application/json" },
267
+ body: JSON.stringify({
268
+ message: "Response time test",
269
+ voice_id: "marrvin",
270
+ voice_enabled: false,
271
+ }),
272
+ });
273
+
274
+ const elapsed = Date.now() - start;
275
+
276
+ expect(response.status).toBe(201);
277
+ expect(elapsed).toBeLessThan(100);
278
+ });
279
+
280
+ test("should return /queue/status within 50ms", async () => {
281
+ const start = Date.now();
282
+
283
+ const response = await fetch(`${SERVER_URL}/queue/status`);
284
+
285
+ const elapsed = Date.now() - start;
286
+
287
+ expect(response.status).toBe(200);
288
+ expect(elapsed).toBeLessThan(50);
289
+ });
290
+ });
291
+ });
292
+
293
+ // If MLX-audio is not available, add a placeholder test
294
+ describe.if(hasMLXAudio)("Integration tests", () => {
295
+ test("placeholder - MLX-audio is available", () => {
296
+ expect(true).toBe(true);
297
+ });
298
+ });