Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
486 lines (373 loc) · 13.1 KB
/
Copy pathtest.js
File metadata and controls
486 lines (373 loc) · 13.1 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
import { createAsPipes } from './index.js';
import { strict as assert } from 'node:assert';
import { test } from 'node:test';
test('basic example', async () => {
const { pipe, asPipe } = createAsPipes();
const upper = asPipe((s) => s.toUpperCase());
const result = pipe('hello');
result | upper;
assert.equal(await result.run(), 'HELLO');
});
test('createAsPipes returns pipe and asPipe functions', () => {
const { pipe, asPipe } = createAsPipes();
assert.equal(typeof pipe, 'function');
assert.equal(typeof asPipe, 'function');
});
test('basic string pipeline - uppercase', async () => {
const { pipe, asPipe } = createAsPipes();
const upper = asPipe((s) => s.toUpperCase());
const result = pipe('hello');
result | upper;
assert.equal(await result.run(), 'HELLO');
});
test('string pipeline with multiple operations', async () => {
const { pipe, asPipe } = createAsPipes();
const upper = asPipe((s) => s.toUpperCase());
const ex = asPipe((s, mark = '!') => s + mark);
const greeting = pipe('hello');
greeting | upper | ex('!!!');
assert.equal(await greeting.run(), 'HELLO!!!');
});
test('string pipeline with default parameter', async () => {
const { pipe, asPipe } = createAsPipes();
const upper = asPipe((s) => s.toUpperCase());
const ex = asPipe((s, mark = '!') => s + mark);
const greeting = pipe('hello');
greeting | upper | ex();
assert.equal(await greeting.run(), 'HELLO!');
});
test('numeric pipeline - increment', async () => {
const { pipe, asPipe } = createAsPipes();
const inc = asPipe((x) => x + 1);
const result = pipe(5);
result | inc;
assert.equal(await result.run(), 6);
});
test('numeric pipeline with multiplication', async () => {
const { pipe, asPipe } = createAsPipes();
const inc = asPipe((x) => x + 1);
const mul = asPipe((x, k) => x * k);
const calc = pipe(3);
calc | inc | mul(10);
assert.equal(await calc.run(), 40);
});
test('async pipeline', async () => {
const { pipe, asPipe } = createAsPipes();
const asyncDouble = asPipe(async (x) => {
return new Promise((resolve) => {
setTimeout(() => resolve(x * 2), 10);
});
});
const result = pipe(5);
result | asyncDouble;
assert.equal(await result.run(), 10);
});
test('mixed sync and async pipeline', async () => {
const { pipe, asPipe } = createAsPipes();
const inc = asPipe((x) => x + 1);
const asyncDouble = asPipe(async (x) => {
return new Promise((resolve) => {
setTimeout(() => resolve(x * 2), 10);
});
});
const add = asPipe((x, y) => x + y);
const result = pipe(5);
result | inc | asyncDouble | add(3);
assert.equal(await result.run(), 15); // (5 + 1) * 2 + 3 = 15
});
test('object pipeline', async () => {
const { pipe, asPipe } = createAsPipes();
const addProperty = asPipe((obj, key, value) => ({ ...obj, [key]: value }));
const result = pipe({});
result | addProperty('name', 'John') | addProperty('age', 30);
const final = await result.run();
assert.equal(final.name, 'John');
assert.equal(final.age, 30);
});
test('array pipeline', async () => {
const { pipe, asPipe } = createAsPipes();
const map = asPipe((arr, fn) => arr.map(fn));
const filter = asPipe((arr, fn) => arr.filter(fn));
const result = pipe([1, 2, 3, 4, 5]);
result | map((x) => x * 2) | filter((x) => x > 5);
const final = await result.run();
assert.deepEqual(final, [6, 8, 10]);
});
test('pick function for nested object access', async () => {
const { pipe, asPipe } = createAsPipes();
const pick = asPipe((o, ...keys) => keys.reduce((a, k) => a?.[k], o));
const obj = {
user: {
profile: {
name: 'Alice',
},
},
};
const result = pipe(obj);
result | pick('user', 'profile', 'name');
assert.equal(await result.run(), 'Alice');
});
test('trim function', async () => {
const { pipe, asPipe } = createAsPipes();
const trim = asPipe((s) => (typeof s === 'string' ? s.trim() : s));
const result = pipe(' hello ');
result | trim;
assert.equal(await result.run(), 'hello');
});
test('trim function with non-string', async () => {
const { pipe, asPipe } = createAsPipes();
const trim = asPipe((s) => (typeof s === 'string' ? s.trim() : s));
const result = pipe(42);
result | trim;
assert.equal(await result.run(), 42);
});
test('isolated pipeline contexts', async () => {
const { pipe, asPipe } = createAsPipes();
const inc = asPipe((x) => x + 1);
const pipeline1 = pipe(10);
const pipeline2 = pipe(20);
pipeline1 | inc;
pipeline2 | inc;
assert.equal(await pipeline1.run(), 11);
assert.equal(await pipeline2.run(), 21);
});
test('multiple isolated environments', async () => {
const env1 = createAsPipes();
const env2 = createAsPipes();
const inc1 = env1.asPipe((x) => x + 1);
const inc2 = env2.asPipe((x) => x + 2);
const result1 = env1.pipe(10);
const result2 = env2.pipe(10);
result1 | inc1;
result2 | inc2;
assert.equal(await result1.run(), 11);
assert.equal(await result2.run(), 12);
});
test('complex pipeline with multiple transformations', async () => {
const { pipe, asPipe } = createAsPipes();
const upper = asPipe((s) => s.toUpperCase());
const split = asPipe((s, delim) => s.split(delim));
const filter = asPipe((arr, fn) => arr.filter(fn));
const join = asPipe((arr, delim) => arr.join(delim));
const result = pipe('hello world test');
result | upper | split(' ') | filter((w) => w.length > 4) | join('-');
assert.equal(await result.run(), 'HELLO-WORLD');
});
test('error propagation in pipeline', async () => {
const { pipe, asPipe } = createAsPipes();
const throwError = asPipe(() => {
throw new Error('Test error');
});
const result = pipe(5);
result | throwError;
await assert.rejects(async () => await result.run(), {
message: 'Test error',
});
});
test('promise rejection propagation', async () => {
const { pipe, asPipe } = createAsPipes();
const rejectPromise = asPipe(async () => {
throw new Error('Async error');
});
const result = pipe(5);
result | rejectPromise;
await assert.rejects(async () => await result.run(), {
message: 'Async error',
});
});
test('identity pipeline', async () => {
const { pipe } = createAsPipes();
const result = pipe(42);
assert.equal(await result.run(), 42);
});
test('chaining with zero arguments', async () => {
const { pipe, asPipe } = createAsPipes();
const negate = asPipe((x) => -x);
const result = pipe(5);
result | negate;
assert.equal(await result.run(), -5);
});
test('pipeline with boolean values', async () => {
const { pipe, asPipe } = createAsPipes();
const not = asPipe((x) => !x);
const result = pipe(true);
result | not;
assert.equal(await result.run(), false);
});
test('pipeline with null/undefined handling', async () => {
const { pipe, asPipe } = createAsPipes();
const defaultValue = asPipe((x, def) => x ?? def);
const result1 = pipe(null);
result1 | defaultValue('default');
assert.equal(await result1.run(), 'default');
const result2 = pipe(undefined);
result2 | defaultValue('default');
assert.equal(await result2.run(), 'default');
const result3 = pipe('value');
result3 | defaultValue('default');
assert.equal(await result3.run(), 'value');
});
test('composable pipes - higher-order pipe composition', async () => {
const { pipe, asPipe } = createAsPipes();
// Basic building blocks
const add = asPipe((x, n) => x + n);
const multiply = asPipe((x, n) => x * n);
const square = asPipe((x) => x * x);
// Create a composed pipe - clean syntax without variable assignment
const complexCalc = asPipe((value) => {
const p = pipe(value);
p | add(5) | multiply(2) | square;
return p;
});
// Use the composed pipe in a new pipeline
const finalResult = pipe(3);
finalResult | complexCalc;
// (3 + 5) * 2 = 16, then 16^2 = 256
assert.equal(await finalResult.run(), 256);
});
test('composable pipes - bot-like pattern with mock data', async () => {
const { pipe, asPipe } = createAsPipes();
// Mock fetch-like operations
const postJson = asPipe((data, body) => ({
...data,
response: { result: body.input.toUpperCase() },
}));
const extract = asPipe((data, key) => data.response[key]);
const trim = asPipe((s) => (typeof s === 'string' ? s.trim() : s));
// Compose a reusable bot pipe - clean direct syntax
const botPipe = asPipe((endpoint, payload) => {
const p = pipe(endpoint);
p | postJson(payload) | extract('result') | trim;
return p;
});
// Use the composed bot pipe in a pipeline
const response = pipe({ url: 'mock-api', data: {} });
response | botPipe({ input: ' hello world ' });
assert.equal(await response.run(), 'HELLO WORLD');
});
test('composable pipes - pipe used directly as operator', async () => {
const { pipe, asPipe } = createAsPipes();
const add = asPipe((x, n) => x + n);
const mul = asPipe((x, n) => x * n);
// Create a composable pipe using direct pipeline expression
const calculate = asPipe((value) => {
const p = pipe(value);
p | add(10) | mul(2);
return p;
});
// Use in a larger pipeline
const final = pipe(5);
final | calculate | add(100);
// (5 + 10) * 2 = 30, then 30 + 100 = 130
assert.equal(await final.run(), 130);
});
test('asPipe with Math object - destructuring methods', async () => {
const { pipe, asPipe } = createAsPipes();
const { sqrt, floor, abs } = asPipe(Math);
const result = pipe(16.7);
result | sqrt | floor | abs;
// sqrt(16.7) ≈ 4.087, floor(4.087) = 4, abs(4) = 4
assert.equal(await result.run(), 4);
});
test('asPipe with custom object - all methods become pipeable', async () => {
const { pipe, asPipe } = createAsPipes();
const calculator = {
add(x, n) { return x + n; },
multiply(x, n) { return x * n; },
square(x) { return x * x; }
};
const { add, multiply, square } = asPipe(calculator);
const result = pipe(3);
result | add(2) | multiply(4) | square;
// (3 + 2) * 4 = 20, then 20^2 = 400
assert.equal(await result.run(), 400);
});
test('asPipe with object - non-function properties accessible', async () => {
const { asPipe } = createAsPipes();
const obj = {
value: 42,
getName() { return 'test'; }
};
const wrapped = asPipe(obj);
assert.equal(wrapped.value, 42);
assert.equal(typeof wrapped.getName, 'function');
});
// Tests for issue #9: Objects with .run() method should not be confused with pipe tokens
test('issue #9 - return object with run method', async () => {
const { pipe, asPipe } = createAsPipes();
// A regular object that happens to have a .run() method
const task = {
name: 'MyTask',
async run() {
return 'task executed';
},
};
const getTask = asPipe((x) => task);
const result = pipe(5);
result | getTask;
const output = await result.run();
// We expect to get the task object back, not the result of calling task.run()
assert.deepEqual(output, task, 'Should return the task object itself, not call its .run() method');
});
test('issue #9 - return multiple objects with run methods in sequence', async () => {
const { pipe, asPipe } = createAsPipes();
const command1 = {
id: 1,
async run() {
return 'command1 executed';
},
};
const command2 = {
id: 2,
async run() {
return 'command2 executed';
},
};
const createCommand = asPipe((x, id) => (id === 1 ? command1 : command2));
const result = pipe(0);
result | createCommand(1) | createCommand(2);
const output = await result.run();
// We expect the second command object, not "command2 executed"
assert.deepEqual(output, command2, 'Should return command2 object, not call its .run() method');
});
test('issue #9 - promise-like object with then method', async () => {
const { pipe, asPipe } = createAsPipes();
// An object that looks like a promise but isn't
const fakePromise = {
value: 42,
then(resolve) {
resolve('fake promise resolved');
},
};
const getFakePromise = asPipe((x) => fakePromise);
const result = pipe(5);
result | getFakePromise;
const output = await result.run();
// This might work correctly since Promise.resolve handles it,
// but we should still get the original object
assert.equal(output, 'fake promise resolved', 'Promise-like objects are handled by Promise.resolve');
});
test('issue #9 - distinguish between pipe token and regular object', async () => {
const { pipe, asPipe } = createAsPipes();
// Create an actual pipe token
const innerPipe = pipe(10);
innerPipe | asPipe((x) => x * 2);
// Create a function that returns this pipe token (should be unwrapped)
const getPipe = asPipe((x) => innerPipe);
const result1 = pipe(5);
result1 | getPipe;
const output1 = await result1.run();
assert.equal(output1, 20, 'Pipe tokens should be unwrapped and executed');
// Now create a regular object with run method (should NOT be unwrapped)
const regularObject = {
data: 100,
async run() {
return 200;
},
};
const getObject = asPipe((x) => regularObject);
const result2 = pipe(5);
result2 | getObject;
const output2 = await result2.run();
assert.deepEqual(output2, regularObject, 'Regular objects with run() should be returned as-is');
});