-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstreams.test.ts
More file actions
603 lines (482 loc) · 16.9 KB
/
streams.test.ts
File metadata and controls
603 lines (482 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
import * as assert from "@std/assert";
import { createChunk } from "./chunk.ts";
import { defineLayer } from "./define-layer.ts";
import { output } from "./output.ts";
import { pipeline } from "./pipeline.ts";
import { PipelineError } from "./types.ts";
import type { Chunk, ErrorContext } from "./types.ts";
// Sinks
import { buffer } from "./sinks/buffer.ts";
import { nullSink } from "./sinks/null.ts";
import { multiplex } from "./sinks/multiplex.ts";
// Sources
import { values } from "./sources/values.ts";
// Layers
import { map } from "./layers/map.ts";
import { filter } from "./layers/filter.ts";
import { tap } from "./layers/tap.ts";
import { tee } from "./layers/tee.ts";
// =============================================================================
// Chunk Tests
// =============================================================================
Deno.test("createChunk should create text chunk for strings", () => {
const chunk = createChunk("hello");
assert.assertEquals(chunk.data, "hello");
assert.assertEquals(chunk.meta.kind, "text");
assert.assertEquals(chunk.meta.channel, "stdout");
assert.assertExists(chunk.meta.timestamp);
});
Deno.test("createChunk should create structured chunk for objects", () => {
const chunk = createChunk({ name: "test" });
assert.assertEquals(chunk.meta.kind, "structured");
});
Deno.test("createChunk should create bytes chunk for Uint8Array", () => {
const chunk = createChunk(new Uint8Array([1, 2, 3]));
assert.assertEquals(chunk.meta.kind, "bytes");
});
Deno.test("createChunk should handle null data", () => {
const chunk = createChunk(null);
assert.assertEquals(chunk.data, null);
assert.assertEquals(chunk.meta.kind, "structured");
});
Deno.test("createChunk should handle undefined data", () => {
const chunk = createChunk(undefined);
assert.assertEquals(chunk.data, undefined);
assert.assertEquals(chunk.meta.kind, "structured");
});
Deno.test("createChunk should accept meta overrides", () => {
const chunk = createChunk("hello", { channel: "stderr" });
assert.assertEquals(chunk.meta.channel, "stderr");
assert.assertEquals(chunk.meta.kind, "text");
});
// =============================================================================
// defineLayer Tests
// =============================================================================
Deno.test("defineLayer should create a named layer", () => {
const layer = defineLayer({
name: "test-layer",
create: () => ({
transform(chunk, controller) {
controller.enqueue(chunk);
},
}),
});
assert.assertEquals(layer.name, "test-layer");
assert.assertExists(layer.transform);
});
Deno.test("defineLayer should produce a working TransformStream", async () => {
const doubler = defineLayer<number, number>({
name: "doubler",
create: () => ({
transform(chunk, controller) {
controller.enqueue({ data: chunk.data * 2, meta: chunk.meta });
},
}),
});
// Test via pipeline (avoids manual stream lifecycle issues)
const result = await pipeline()
.from(values(5))
.through(doubler)
.collect<number>();
assert.assertEquals(result, [10]);
});
// =============================================================================
// Buffer Sink Tests
// =============================================================================
Deno.test("buffer sink should collect chunks", async () => {
const buf = buffer<string>();
const writer = buf.writable.getWriter();
await writer.write(createChunk("hello"));
await writer.write(createChunk("world"));
await writer.close();
assert.assertEquals(buf.items().length, 2);
assert.assertEquals(buf.items()[0], "hello");
assert.assertEquals(buf.items()[1], "world");
});
Deno.test("buffer sink chunks() should include metadata", async () => {
const buf = buffer<string>();
const writer = buf.writable.getWriter();
await writer.write(createChunk("hello"));
await writer.close();
assert.assertEquals(buf.chunks()[0]!.meta.kind, "text");
});
Deno.test("buffer sink clear() should empty the collection", async () => {
const buf = buffer<string>();
const writer = buf.writable.getWriter();
await writer.write(createChunk("hello"));
assert.assertEquals(buf.items().length, 1);
buf.clear();
assert.assertEquals(buf.items().length, 0);
await writer.close();
});
// =============================================================================
// Null Sink Tests
// =============================================================================
Deno.test("null sink should accept and discard chunks", async () => {
const sink = nullSink();
const writer = sink.writable.getWriter();
await writer.write(createChunk("discarded"));
await writer.close();
// No assertion needed — it didn't throw
});
// =============================================================================
// Multiplex Sink Tests
// =============================================================================
Deno.test("multiplex sink should write to all sinks", async () => {
const buf1 = buffer<string>();
const buf2 = buffer<string>();
const mux = multiplex(buf1, buf2);
const writer = mux.writable.getWriter();
await writer.write(createChunk("hello"));
await writer.write(createChunk("world"));
await writer.close();
assert.assertEquals(buf1.items().length, 2);
assert.assertEquals(buf2.items().length, 2);
assert.assertEquals(buf1.items()[0], "hello");
assert.assertEquals(buf2.items()[1], "world");
});
// =============================================================================
// Values Source Tests
// =============================================================================
Deno.test("values source should emit all items", async () => {
const source = values("a", "b", "c");
const reader = source.readable.getReader();
const results: string[] = [];
while (true) {
const { done, value } = await reader.read();
if (done) break;
results.push(value.data as string);
}
assert.assertEquals(results, ["a", "b", "c"]);
});
Deno.test("values source should handle empty input", async () => {
const source = values();
const reader = source.readable.getReader();
const { done } = await reader.read();
assert.assertEquals(done, true);
});
// =============================================================================
// Layer Tests — map
// =============================================================================
Deno.test("map layer should transform chunk data", async () => {
const result = await pipeline()
.from(values(1, 2, 3))
.through(map((n: number) => n * 10))
.collect<number>();
assert.assertEquals(result, [10, 20, 30]);
});
// =============================================================================
// Layer Tests — filter
// =============================================================================
Deno.test("filter layer should drop non-matching chunks", async () => {
const result = await pipeline()
.from(values(1, 2, 3, 4, 5))
.through(filter((n: number) => n > 3))
.collect<number>();
assert.assertEquals(result, [4, 5]);
});
Deno.test("filter layer should pass all if predicate always true", async () => {
const result = await pipeline()
.from(values("a", "b"))
.through(filter(() => true))
.collect<string>();
assert.assertEquals(result, ["a", "b"]);
});
// =============================================================================
// Layer Tests — tap
// =============================================================================
Deno.test("tap layer should call side-effect without modifying chunk", async () => {
const observed: string[] = [];
const result = await pipeline()
.from(values("hello", "world"))
.through(tap((chunk: Chunk<string>) => {
observed.push(chunk.data);
}))
.collect<string>();
assert.assertEquals(result, ["hello", "world"]);
assert.assertEquals(observed, ["hello", "world"]);
});
// =============================================================================
// Layer Tests — tee
// =============================================================================
Deno.test("tee layer should send to extra sink and pass through", async () => {
const teeBuf = buffer<string>();
const result = await pipeline()
.from(values("a", "b", "c"))
.through(tee(teeBuf))
.collect<string>();
assert.assertEquals(result, ["a", "b", "c"]);
assert.assertEquals(teeBuf.items(), ["a", "b", "c"]);
});
// =============================================================================
// Pipeline Tests
// =============================================================================
Deno.test("pipeline should chain from → through → to → run", async () => {
const buf = buffer<number>();
await pipeline()
.from(values(1, 2, 3))
.through(map((n: number) => n + 100))
.to(buf)
.run();
assert.assertEquals(buf.items(), [101, 102, 103]);
});
Deno.test("pipeline collect() should return data array", async () => {
const result = await pipeline()
.from(values("x", "y"))
.collect<string>();
assert.assertEquals(result, ["x", "y"]);
});
Deno.test("pipeline should handle empty source", async () => {
const result = await pipeline()
.from(values())
.collect();
assert.assertEquals(result, []);
});
Deno.test("pipeline should throw PipelineError with no source", async () => {
await assert.assertRejects(
() => pipeline().to(buffer()).run(),
PipelineError,
"no source",
);
});
Deno.test("pipeline should throw PipelineError with no sink", async () => {
await assert.assertRejects(
() => pipeline().from(values(1)).run(),
PipelineError,
"no sink",
);
});
Deno.test("pipeline should support multiple layers", async () => {
const result = await pipeline()
.from(values(1, 2, 3, 4, 5))
.through(
filter((n: number) => n % 2 === 0),
map((n: number) => n * 10),
)
.collect<number>();
assert.assertEquals(result, [20, 40]);
});
Deno.test("pipeline should propagate layer errors", async () => {
const failLayer = defineLayer({
name: "fail",
create: () => ({
transform(_chunk, _controller) {
throw new Error("intentional failure");
},
}),
});
await assert.assertRejects(
() =>
pipeline()
.from(values("test"))
.through(failLayer)
.to(nullSink())
.run(),
PipelineError,
);
});
Deno.test({
name: "pipeline timeout should reject with TimeoutError",
sanitizeOps: false,
sanitizeResources: false,
async fn() {
// Use a slow layer that delays longer than the timeout
const slowLayer = defineLayer({
name: "slow",
create: () => ({
transform(_chunk, _controller) {
return new Promise((resolve) => setTimeout(resolve, 10000));
},
}),
});
await assert.assertRejects(
() =>
pipeline()
.from(values("test"))
.through(slowLayer)
.to(nullSink())
.run({ timeout: 50 }),
Error,
);
},
});
// =============================================================================
// Output Tests
// =============================================================================
Deno.test("output write() should enqueue to sink via buffer", async () => {
const buf = buffer<unknown>();
const out = output({ sink: buf });
out.write("hello");
out.write("world");
await out.flush();
assert.assertEquals(buf.items().length, 2);
});
Deno.test("output writeln() should append newline", async () => {
const buf = buffer<unknown>();
const out = output({ sink: buf });
out.writeln("hello");
await out.flush();
assert.assertEquals(buf.items()[0], "hello\n");
});
Deno.test("output flush() on empty buffer should be no-op", async () => {
const buf = buffer<unknown>();
const out = output({ sink: buf });
await out.flush(); // Should not throw
assert.assertEquals(buf.items().length, 0);
});
Deno.test("output close() should flush and close", async () => {
const buf = buffer<unknown>();
const out = output({ sink: buf });
out.write("data");
await out.close();
assert.assertEquals(buf.items().length, 1);
});
Deno.test("output with layers should transform data", async () => {
const buf = buffer<unknown>();
const out = output({
sink: buf,
layers: [map((s: unknown) => `[${String(s)}]`)],
});
out.write("test");
await out.flush();
assert.assertEquals(buf.items()[0], "[test]");
});
Deno.test({
name: "output error handler should receive context",
sanitizeOps: false,
sanitizeResources: false,
async fn() {
let receivedContext: ErrorContext | undefined;
const failLayer = defineLayer({
name: "fail",
create: () => ({
transform(_chunk, _controller) {
throw new Error("boom");
},
}),
});
const out = output({
sink: buffer(),
layers: [failLayer],
onError: (ctx) => {
receivedContext = ctx;
},
});
out.write("trigger");
await out.flush();
// Give async error propagation time to fire
await new Promise((r) => setTimeout(r, 100));
assert.assertExists(receivedContext);
assert.assertExists(receivedContext!.error);
},
});
Deno.test("output pipe() should return a new independent output", () => {
const out = output();
const piped = out.pipe(map((s: unknown) => `[${String(s)}]`));
// pipe() returns a new Output (not the same reference)
assert.assertNotEquals(out, piped);
});
// =============================================================================
// Coverage Gap Tests — edge cases from eng review
// =============================================================================
Deno.test("output write after close should be ignored", async () => {
const buf = buffer<unknown>();
const out = output({ sink: buf });
out.write("before");
await out.close();
out.write("after"); // should be silently ignored
await new Promise((r) => setTimeout(r, 20));
assert.assertEquals(buf.items().length, 1);
assert.assertEquals(buf.items()[0], "before");
});
Deno.test("output write() with multiple args renders to single chunk", async () => {
const buf = buffer<unknown>();
const out = output({ sink: buf });
out.write("a", "b", "c");
await out.flush();
// Multiple string args are normalized to Spans and rendered as one chunk
assert.assertEquals(buf.items().length, 1);
assert.assertEquals(buf.items()[0], "abc");
});
Deno.test("defineLayer with start and flush callbacks", async () => {
const events: string[] = [];
const traced = defineLayer({
name: "traced",
create: () => ({
start() {
events.push("start");
},
transform(chunk, controller) {
events.push("transform");
controller.enqueue(chunk);
},
flush() {
events.push("flush");
},
}),
});
await pipeline()
.from(values("x"))
.through(traced)
.to(nullSink())
.run();
assert.assertEquals(events, ["start", "transform", "flush"]);
});
Deno.test("pipeline with zero layers (source → sink directly)", async () => {
const result = await pipeline()
.from(values(42))
.collect<number>();
assert.assertEquals(result, [42]);
});
Deno.test("multiplex sink close() should close all sinks", async () => {
const buf1 = buffer<string>();
const buf2 = buffer<string>();
const mux = multiplex(buf1, buf2);
const writer = mux.writable.getWriter();
await writer.write(createChunk("data"));
await writer.close();
// Both sinks should have received the data
assert.assertEquals(buf1.items().length, 1);
assert.assertEquals(buf2.items().length, 1);
});
// =============================================================================
// Integration Tests — multi-layer pipelines
// =============================================================================
Deno.test("integration: values → filter → map → tap → buffer", async () => {
const tapped: number[] = [];
const result = await pipeline()
.from(values(1, 2, 3, 4, 5, 6))
.through(
filter((n: number) => n % 2 === 0),
map((n: number) => n * 100),
tap((chunk: Chunk<number>) => {
tapped.push(chunk.data);
}),
)
.collect<number>();
assert.assertEquals(result, [200, 400, 600]);
assert.assertEquals(tapped, [200, 400, 600]);
});
Deno.test("integration: values → tee → map → buffer", async () => {
const teeBuf = buffer<number>();
const result = await pipeline()
.from(values(10, 20, 30))
.through(
tee(teeBuf),
map((n: number) => n + 1),
)
.collect<number>();
// tee gets original values, collect gets mapped
assert.assertEquals(teeBuf.items(), [10, 20, 30]);
assert.assertEquals(result, [11, 21, 31]);
});
Deno.test("integration: tight write loop should not crash", async () => {
const buf = buffer<unknown>();
const out = output({ sink: buf });
for (let i = 0; i < 1000; i++) {
out.write(String(i));
}
await out.flush();
assert.assertEquals(buf.items().length, 1000);
});