-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.test.js
More file actions
651 lines (573 loc) · 20.5 KB
/
Copy pathindex.test.js
File metadata and controls
651 lines (573 loc) · 20.5 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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import { deepStrictEqual, ok, strictEqual } from "node:assert";
import test from "node:test";
import {
createReadableStream,
pipejoin,
pipeline,
streamToArray,
} from "@datastream/core";
import stringDefault, {
stringCountStream,
stringLengthStream,
stringMinimumChunkSize,
stringMinimumFirstChunkSize,
stringReadableStream,
stringReplaceStream,
stringSkipConsecutiveDuplicates,
stringSplitStream,
} from "@datastream/string";
let variant = "unknown";
for (const execArgv of process.execArgv) {
const flag = "--conditions=";
if (execArgv.includes("--conditions=")) {
variant = execArgv.replace(flag, "");
}
}
// *** stringReadableStream *** //
test(`${variant}: stringReadableStream should read in initial chunks`, async (_t) => {
const input = "abc";
const streams = [stringReadableStream(input)];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, [input]);
});
// *** stringLengthStream *** //
test(`${variant}: stringLengthStream should count length of chunks`, async (_t) => {
const input = ["1", "2", "3"];
const streams = [createReadableStream(input), stringLengthStream()];
const result = await pipeline(streams);
const { key, value } = streams[1].result();
strictEqual(key, "length");
strictEqual(result.length, 3);
strictEqual(value, 3);
});
test(`${variant}: stringSizeStream should count length of chunks with custom key`, async (_t) => {
const input = ["1", "2", "3"];
const streams = [
createReadableStream(input),
stringLengthStream({ resultKey: "string" }),
];
const result = await pipeline(streams);
const { key, value } = streams[1].result();
strictEqual(key, "string");
strictEqual(result.string, 3);
strictEqual(value, 3);
});
// *** stringSkipConsecutiveDuplicates *** //
test(`${variant}: stringSkipConsecutiveDuplicates should skip consecutive duplicates`, async (_t) => {
const input = ["1", "2", "2", "3"];
const streams = [
createReadableStream(input),
stringSkipConsecutiveDuplicates(),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["1", "2", "3"]);
});
// *** stringSplitStream *** //
test(`${variant}: stringSplitStream should split into empty strings`, async (_t) => {
const input = [",,", ",,"];
const streams = [
createReadableStream(input),
stringSplitStream({ separator: "," }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["", "", "", "", ""]);
});
test(`${variant}: stringSplitStream should split across chunk boundaries`, async (_t) => {
const input = ["a,b", "c,d"];
const streams = [
createReadableStream(input),
stringSplitStream({ separator: "," }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["a", "bc", "d"]);
});
// *** stringCountStream *** //
test(`${variant}: stringCountStream should count occurrences of substring`, async (_t) => {
const input = ["hello world", "hello universe"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "hello" }),
];
const result = await pipeline(streams);
const { key, value } = streams[1].result();
strictEqual(key, "count");
strictEqual(result.count, 2);
strictEqual(value, 2);
});
test(`${variant}: stringCountStream should count multiple occurrences in single chunk`, async (_t) => {
const input = ["aaa aaa aaa"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "a" }),
];
await pipeline(streams);
const { value } = streams[1].result();
strictEqual(value, 9);
});
test(`${variant}: stringCountStream should use custom result key`, async (_t) => {
const input = ["test test"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "test", resultKey: "matches" }),
];
const result = await pipeline(streams);
const { key } = streams[1].result();
strictEqual(key, "matches");
strictEqual(result.matches, 2);
});
// *** stringReplaceStream *** //
test(`${variant}: stringReplaceStream should replace pattern across chunks`, async (_t) => {
const input = ["hello world", "hello universe"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: /hello/g, replacement: "hi" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
// First chunk: previousChunk="", outputs "", previousChunk becomes "hi world"
// Second chunk: previousChunk="hi world", newChunk = "hi worldhello universe".replace() = "hi worldhi universe"
// outputs "hi world" (substring 0, 8), previousChunk becomes "hi universe"
// flush outputs "hi universe"
deepStrictEqual(output, ["", "hi world", "hi universe"]);
});
test(`${variant}: stringReplaceStream should handle pattern spanning chunks`, async (_t) => {
const input = ["hel", "lo world"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: /hello/g, replacement: "hi" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
// Implementation buffers and matches across chunks:
// Chunk 1: "hel" -> enqueue "", previousChunk="hel"
// Chunk 2: "hel"+"lo world" = "hello world" -> replace = "hi world" -> enqueue "hi ", previousChunk="world"
// Flush: enqueue "world"
deepStrictEqual(output, ["", "hi ", "world"]);
});
test(`${variant}: stringReplaceStream should replace with string pattern`, async (_t) => {
const input = ["hello world"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: "hello", replacement: "hi" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
// Output includes empty string from first chunk (previousChunk.length was 0)
deepStrictEqual(output, ["", "hi world"]);
});
// *** stringMinimumFirstChunkSize *** //
test(`${variant}: stringMinimumFirstChunkSize should buffer until chunkSize reached`, async (_t) => {
const input = ["ab", "cd", "ef"];
const streams = [
createReadableStream(input),
stringMinimumFirstChunkSize({ chunkSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["abcd", "ef"]);
});
test(`${variant}: stringMinimumFirstChunkSize should flush small input`, async (_t) => {
const input = ["ab"];
const streams = [
createReadableStream(input),
stringMinimumFirstChunkSize({ chunkSize: 100 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["ab"]);
});
test(`${variant}: stringMinimumFirstChunkSize should pass through after first chunk met`, async (_t) => {
const input = ["abcdef", "gh", "ij"];
const streams = [
createReadableStream(input),
stringMinimumFirstChunkSize({ chunkSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["abcdef", "gh", "ij"]);
});
// *** stringMinimumChunkSize *** //
test(`${variant}: stringMinimumChunkSize should buffer until chunkSize reached`, async (_t) => {
const input = ["ab", "cd", "ef"];
const streams = [
createReadableStream(input),
stringMinimumChunkSize({ chunkSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["abcd", "ef"]);
});
test(`${variant}: stringMinimumChunkSize should flush small input`, async (_t) => {
const input = ["ab"];
const streams = [
createReadableStream(input),
stringMinimumChunkSize({ chunkSize: 100 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["ab"]);
});
test(`${variant}: stringMinimumChunkSize should buffer all chunks to minimum size`, async (_t) => {
const input = ["abcdef", "gh", "ij"];
const streams = [
createReadableStream(input),
stringMinimumChunkSize({ chunkSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["abcdef", "ghij"]);
});
// *** stringReplaceStream buffer limit regression *** //
test(`${variant}: stringReplaceStream should throw when buffer exceeds maxBufferSize`, async (_t) => {
const input = ["aaaa", "bbbb", "cccc"];
const streams = [
createReadableStream(input),
stringReplaceStream({
pattern: "zzz",
replacement: "yyy",
maxBufferSize: 3,
}),
];
try {
await pipeline(streams);
throw new Error("Should have thrown");
} catch (e) {
ok(e.message.includes("maxBufferSize"));
}
});
// *** stringSplitStream buffer limit regression *** //
test(`${variant}: stringSplitStream should throw when buffer exceeds maxBufferSize`, async (_t) => {
const input = ["aaaaaa", "bbbbbb", "cccccc"];
const streams = [
createReadableStream(input),
stringSplitStream({ separator: "zzz", maxBufferSize: 10 }),
];
try {
await pipeline(streams);
throw new Error("Should have thrown");
} catch (e) {
ok(e.message.includes("maxBufferSize"));
}
});
// *** stringSplitStream empty separator guard *** //
test(`${variant}: stringSplitStream should throw on empty separator at construction`, (_t) => {
let threw = false;
try {
stringSplitStream({ separator: "" });
} catch (e) {
threw = true;
ok(
e.message.includes("non-empty separator"),
`expected non-empty separator in error, got: ${e.message}`,
);
}
ok(threw, "stringSplitStream({ separator: '' }) should throw");
});
// *** stringCountStream cross-chunk boundary *** //
test(`${variant}: stringCountStream should count occurrences spanning chunk boundaries`, async (_t) => {
const input = ["hel", "lo"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "hello" }),
];
await pipeline(streams);
const { value } = streams[1].result();
strictEqual(value, 1);
});
// *** default export *** //
test(`${variant}: default export should include all stream functions`, (_t) => {
deepStrictEqual(Object.keys(stringDefault).sort(), [
"countStream",
"lengthStream",
"readableStream",
"replaceStream",
"skipConsecutiveDuplicates",
"splitStream",
]);
});
// *** stringCountStream guard: typeof check *** //
test(`${variant}: stringCountStream should throw when substr is not a string (number)`, (_t) => {
let threw = false;
try {
stringCountStream({ substr: 42 });
} catch (e) {
threw = true;
strictEqual(e.message, "stringCountStream requires a non-empty substr");
}
ok(threw, "stringCountStream({ substr: 42 }) should throw");
});
test(`${variant}: stringCountStream should throw when substr is not a string (undefined)`, (_t) => {
let threw = false;
try {
stringCountStream({});
} catch (e) {
threw = true;
strictEqual(e.message, "stringCountStream requires a non-empty substr");
}
ok(threw, "stringCountStream({}) should throw");
});
test(`${variant}: stringCountStream should throw on empty substr`, (_t) => {
let threw = false;
try {
stringCountStream({ substr: "" });
} catch (e) {
threw = true;
strictEqual(e.message, "stringCountStream requires a non-empty substr");
}
ok(threw, "stringCountStream({ substr: '' }) should throw");
});
// *** stringCountStream: cursor boundary (< vs <=) *** //
test(`${variant}: stringCountStream should count match at the very end of combined string`, async (_t) => {
// substr "ab" with combined exactly ending in "ab" — exercises cursor at length boundary
const input = ["xab"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "ab" }),
];
await pipeline(streams);
const { value } = streams[1].result();
strictEqual(value, 1);
});
// *** stringCountStream: carry arithmetic (substr.length - 1 vs + 1) *** //
test(`${variant}: stringCountStream should NOT double-count across chunk boundary when carry is exact`, async (_t) => {
// substr = "ab", chunk1 = "a", chunk2 = "b" => carry="a", combined="ab" => 1 match
// if carry used -(length+1) it would incorrectly carry "xa" or extra chars, changing count
const input = ["a", "b", "ab"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "ab" }),
];
await pipeline(streams);
const { value } = streams[1].result();
strictEqual(value, 2);
});
test(`${variant}: stringCountStream carry should not include extra characters that cause false positives`, async (_t) => {
// substr = "ab" (length 2), carry should be 1 char (length-1)
// chunk1="xxa", chunk2="b" => combined="xxab", carry after chunk1 = "a" (1 char)
// if carry were 2 chars ("xa"), combined in chunk2 = "xab", still finds "ab" once
// but with "aab" boundary: chunk1="aab", chunk2="c" => should count 1 not 2
const input = ["aab", "c"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "ab" }),
];
await pipeline(streams);
const { value } = streams[1].result();
strictEqual(value, 1);
});
test(`${variant}: stringCountStream carry boundary with 3-char substr`, async (_t) => {
// substr = "abc" (length 3), carry should be 2 chars (length-1=2)
// chunk1="xab", chunk2="cd" => carry="ab" (2 chars), combined="abcd", finds "abc" once total
// if carry were 3 chars "xab", combined="xabcd", still finds "abc" once - same
// Better: chunk1="abc" chunk2="abc" => 2 matches; carry from chunk1 = "bc" (2 chars)
// combined in chunk2 = "bcabc" => finds "abc" at pos 2 => total = 2
const input = ["abc", "abc"];
const streams = [
createReadableStream(input),
stringCountStream({ substr: "abc" }),
];
await pipeline(streams);
const { value } = streams[1].result();
strictEqual(value, 2);
});
// *** stringMinimumFirstChunkSize: buffer reset after emit *** //
test(`${variant}: stringMinimumFirstChunkSize should output exact chunk size when met and not carry leftovers`, async (_t) => {
// chunkSize=4, input ["ab","cd","ef"]
// After emitting "abcd", buffer must be "" so next chunk "ef" is passed through as-is
// If buffer="" mutant used "Stryker was here!", subsequent chunk would be wrong
const input = ["ab", "cd", "ef"];
const streams = [
createReadableStream(input),
stringMinimumFirstChunkSize({ chunkSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["abcd", "ef"]);
});
test(`${variant}: stringMinimumFirstChunkSize should not emit in flush when done=true and buffer empty`, async (_t) => {
// When chunkSize is met exactly, done=true and buffer="" => flush should emit nothing
const input = ["abcd"];
const streams = [
createReadableStream(input),
stringMinimumFirstChunkSize({ chunkSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
// "abcd" meets chunkSize=4 exactly, emitted in transform; flush emits nothing (buffer empty, done=true)
deepStrictEqual(output, ["abcd"]);
});
test(`${variant}: stringMinimumFirstChunkSize should not flush empty buffer when done=false`, async (_t) => {
// Input is empty => done=false, buffer="" => flush should NOT emit empty string
const input = [""];
const streams = [
createReadableStream(input),
stringMinimumFirstChunkSize({ chunkSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
// buffer is "" — flush condition `buffer.length > 0` must prevent enqueue
deepStrictEqual(output, []);
});
// *** stringReplaceStream: RegExp guard *** //
test(`${variant}: stringReplaceStream should throw on RegExp without g or y flag`, (_t) => {
let threw = false;
try {
stringReplaceStream({ pattern: /hello/, replacement: "hi" });
} catch (e) {
threw = true;
strictEqual(
e.message,
"RegExp pattern must include the global (g) or sticky (y) flag",
);
}
ok(threw, "stringReplaceStream with /hello/ (no flags) should throw");
});
test(`${variant}: stringReplaceStream should NOT throw on RegExp with g flag`, async (_t) => {
const input = ["hello"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: /hello/g, replacement: "hi" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["", "hi"]);
});
test(`${variant}: stringReplaceStream should NOT throw on RegExp with y flag`, async (_t) => {
const input = ["hello"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: /hello/y, replacement: "hi" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["", "hi"]);
});
test(`${variant}: stringReplaceStream should NOT throw on RegExp with both g and y flags`, async (_t) => {
const input = ["hello"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: /hello/gy, replacement: "hi" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["", "hi"]);
});
// *** stringReplaceStream: useReplaceAll (string vs RegExp path) *** //
test(`${variant}: stringReplaceStream should replace ALL occurrences with string pattern (replaceAll path)`, async (_t) => {
// String pattern uses replaceAll, regex uses replace
// With string "a", input "aaa" => all 3 replaced
const input = ["aaa"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: "a", replacement: "b" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["", "bbb"]);
});
test(`${variant}: stringReplaceStream with regex /a/g should replace all occurrences (replace path with global)`, async (_t) => {
const input = ["aaa"];
const streams = [
createReadableStream(input),
stringReplaceStream({ pattern: /a/g, replacement: "b" }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["", "bbb"]);
});
// *** stringReplaceStream: maxBufferSize exact boundary (> vs >=) *** //
test(`${variant}: stringReplaceStream should NOT throw when buffer equals maxBufferSize exactly`, async (_t) => {
// previousChunk.length === maxBufferSize should NOT throw (only > maxBufferSize should)
// Use pattern that never matches so buffer grows; first chunk "aaaa" (4 chars) with maxBufferSize=4
// After first chunk: combined="aaaa", enqueue substring(0,0)="", previousChunk="aaaa" (4 chars)
// 4 > 4 is false => should NOT throw
const input = ["aaaa"];
const streams = [
createReadableStream(input),
stringReplaceStream({
pattern: "zzz",
replacement: "yyy",
maxBufferSize: 4,
}),
];
// Should not throw for 4-char buffer with maxBufferSize=4
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["", "aaaa"]);
});
test(`${variant}: stringReplaceStream should throw when buffer exceeds maxBufferSize by 1`, async (_t) => {
// previousChunk.length = 5 > maxBufferSize = 4 => throws
const input = ["aaaaa"];
const streams = [
createReadableStream(input),
stringReplaceStream({
pattern: "zzz",
replacement: "yyy",
maxBufferSize: 4,
}),
];
try {
await pipeline(streams);
throw new Error("Should have thrown");
} catch (e) {
ok(e.message.includes("maxBufferSize"));
}
});
// *** stringSplitStream: typeof separator guard *** //
test(`${variant}: stringSplitStream should throw when separator is not a string (number)`, (_t) => {
let threw = false;
try {
stringSplitStream({ separator: 42 });
} catch (e) {
threw = true;
ok(
e.message.includes("non-empty separator"),
`expected non-empty separator error, got: ${e.message}`,
);
}
ok(threw, "stringSplitStream({ separator: 42 }) should throw");
});
test(`${variant}: stringSplitStream should throw when separator is undefined`, (_t) => {
let threw = false;
try {
stringSplitStream({});
} catch (e) {
threw = true;
ok(
e.message.includes("non-empty separator"),
`expected non-empty separator error, got: ${e.message}`,
);
}
ok(threw, "stringSplitStream({}) should throw");
});
// *** stringSplitStream: maxBufferSize exact boundary (> vs >=) *** //
test(`${variant}: stringSplitStream should NOT throw when buffer equals maxBufferSize exactly`, async (_t) => {
// separator "zzz" won't be found; chunk "aaaa" (4 chars), maxBufferSize=4
// previousChunk.length=4, 4 > 4 is false => should NOT throw
const input = ["aaaa"];
const streams = [
createReadableStream(input),
stringSplitStream({ separator: "zzz", maxBufferSize: 4 }),
];
const stream = pipejoin(streams);
const output = await streamToArray(stream);
deepStrictEqual(output, ["aaaa"]);
});
test(`${variant}: stringSplitStream should throw when buffer exceeds maxBufferSize by 1`, async (_t) => {
// chunk "aaaaa" (5 chars), maxBufferSize=4; 5 > 4 => throws
const input = ["aaaaa"];
const streams = [
createReadableStream(input),
stringSplitStream({ separator: "zzz", maxBufferSize: 4 }),
];
try {
await pipeline(streams);
throw new Error("Should have thrown");
} catch (e) {
ok(e.message.includes("maxBufferSize"));
}
});