|
| 1 | +import { deepStrictEqual, ok } from 'assert' |
| 2 | +import { test } from 'node:test' |
| 3 | +import { GenericError, MultipleErrors } from '../../src/errors.ts' |
| 4 | +import { runConcurrentCallbacks } from '../../src/apis/callbacks.ts' |
| 5 | + |
| 6 | +test('runConcurrentCallbacks - all operations succeed', (_, done) => { |
| 7 | + const collection = ['a', 'b', 'c'] |
| 8 | + |
| 9 | + runConcurrentCallbacks<string, string>( |
| 10 | + 'should not error', |
| 11 | + collection, |
| 12 | + (item, cb) => { |
| 13 | + cb(null, item.toUpperCase()) |
| 14 | + }, |
| 15 | + (error, results) => { |
| 16 | + deepStrictEqual(error, null) |
| 17 | + deepStrictEqual(results, ['A', 'B', 'C']) |
| 18 | + done() |
| 19 | + } |
| 20 | + ) |
| 21 | +}) |
| 22 | + |
| 23 | +test('runConcurrentCallbacks - all operations fail', (_, done) => { |
| 24 | + const collection = ['a', 'b'] |
| 25 | + |
| 26 | + runConcurrentCallbacks<string, string>( |
| 27 | + 'all failed', |
| 28 | + collection, |
| 29 | + (item, cb) => { |
| 30 | + cb(new GenericError('PLT_KFK_USER', `failed: ${item}`)) |
| 31 | + }, |
| 32 | + (error, results) => { |
| 33 | + ok(error) |
| 34 | + ok(MultipleErrors.isMultipleErrors(error)) |
| 35 | + const multi = error as MultipleErrors |
| 36 | + deepStrictEqual(multi.errors.length, 2) |
| 37 | + deepStrictEqual(multi.errors[0].message, 'failed: a') |
| 38 | + deepStrictEqual(multi.errors[1].message, 'failed: b') |
| 39 | + done() |
| 40 | + } |
| 41 | + ) |
| 42 | +}) |
| 43 | + |
| 44 | +test('runConcurrentCallbacks - partial failures should not contain undefined in errors', (_, done) => { |
| 45 | + const collection = ['a', 'b', 'c'] |
| 46 | + |
| 47 | + runConcurrentCallbacks<string, string>( |
| 48 | + 'partial failure', |
| 49 | + collection, |
| 50 | + (item, cb) => { |
| 51 | + if (item === 'b') { |
| 52 | + cb(new GenericError('PLT_KFK_USER', 'failed: b')) |
| 53 | + } else { |
| 54 | + cb(null, item.toUpperCase()) |
| 55 | + } |
| 56 | + }, |
| 57 | + (error, results) => { |
| 58 | + ok(error) |
| 59 | + ok(MultipleErrors.isMultipleErrors(error)) |
| 60 | + const multi = error as MultipleErrors |
| 61 | + |
| 62 | + // The errors array should only contain actual errors, no undefined entries |
| 63 | + deepStrictEqual(multi.errors.length, 1) |
| 64 | + deepStrictEqual(multi.errors[0].message, 'failed: b') |
| 65 | + |
| 66 | + for (const e of multi.errors) { |
| 67 | + ok(e !== undefined, 'errors array should not contain undefined entries') |
| 68 | + ok(e instanceof Error, 'every entry should be an Error') |
| 69 | + } |
| 70 | + |
| 71 | + // Successful results should still be available |
| 72 | + deepStrictEqual(results[0], 'A') |
| 73 | + deepStrictEqual(results[2], 'C') |
| 74 | + done() |
| 75 | + } |
| 76 | + ) |
| 77 | +}) |
| 78 | + |
| 79 | +test('runConcurrentCallbacks - empty collection', (_, done) => { |
| 80 | + runConcurrentCallbacks<string, string>( |
| 81 | + 'empty', |
| 82 | + [], |
| 83 | + (_item, _cb) => { |
| 84 | + throw new Error('should not be called') |
| 85 | + }, |
| 86 | + (error, results) => { |
| 87 | + deepStrictEqual(error, null) |
| 88 | + deepStrictEqual(results, []) |
| 89 | + done() |
| 90 | + } |
| 91 | + ) |
| 92 | +}) |
0 commit comments