|
| 1 | +import { ok, fail } from './result.factory' |
| 2 | +import { AsyncResult } from './async-result' |
| 3 | + |
| 4 | +describe(AsyncResult.name, () => { |
| 5 | + it('should allow point-free async chaining with map/mapAsync/flatMap/flatMapAsync', async () => { |
| 6 | + const res = AsyncResult.ok<number, string>(2) |
| 7 | + .map(n => n + 1) // 3 |
| 8 | + .mapAsync(async n => n * 2) // 6 |
| 9 | + .flatMap(x => ok<string, string>(String(x))) // Ok('6') |
| 10 | + .flatMapAsync(async s => ok<string, string>(s + '!')) // Ok('6!') |
| 11 | + |
| 12 | + const final = await res.toPromise() |
| 13 | + expect(final.isOk()).toBe(true) |
| 14 | + expect(final.unwrap()).toBe('6!') |
| 15 | + }) |
| 16 | + |
| 17 | + it('should propagate failures across async boundaries', async () => { |
| 18 | + const res = AsyncResult.ok<number, string>(1) |
| 19 | + .mapAsync(async () => { |
| 20 | + throw 'bad' |
| 21 | + }) |
| 22 | + .map(n => n + 1) |
| 23 | + .flatMap(() => ok<number, string>(999)) |
| 24 | + |
| 25 | + const final = await res.toPromise() |
| 26 | + expect(final.isFail()).toBe(true) |
| 27 | + expect(final.unwrapFail()).toBe('bad') |
| 28 | + }) |
| 29 | + |
| 30 | + it('fromPromise should wrap resolve/reject into Result and support chaining', async () => { |
| 31 | + const okAr = AsyncResult.fromPromise<number, string>(Promise.resolve(5)).map(n => n * 2) |
| 32 | + const okFinal = await okAr.toPromise() |
| 33 | + expect(okFinal.isOk()).toBe(true) |
| 34 | + expect(okFinal.unwrap()).toBe(10) |
| 35 | + |
| 36 | + const failAr = AsyncResult.fromPromise<number, string>(Promise.reject('nope')).map(n => n * 2) |
| 37 | + const failFinal = await failAr.toPromise() |
| 38 | + expect(failFinal.isFail()).toBe(true) |
| 39 | + expect(failFinal.unwrapFail()).toBe('nope') |
| 40 | + }) |
| 41 | + |
| 42 | + it('flatMapAsync should flatten Promise<Result<...>>', async () => { |
| 43 | + const ar = AsyncResult.ok<number, string>(1).flatMapAsync(async (n) => { |
| 44 | + return n > 0 ? ok<number, string>(n + 1) : fail<number, string>('neg') |
| 45 | + }) |
| 46 | + |
| 47 | + const final = await ar.toPromise() |
| 48 | + expect(final.isOk()).toBe(true) |
| 49 | + expect(final.unwrap()).toBe(2) |
| 50 | + }) |
| 51 | + |
| 52 | + it('chain should accept a function returning AsyncResult and keep it point-free', async () => { |
| 53 | + const incAsync = (n: number): AsyncResult<number, string> => AsyncResult.ok<number, string>(n + 1) |
| 54 | + |
| 55 | + const ar = AsyncResult.ok<number, string>(3) |
| 56 | + .chain(incAsync) |
| 57 | + .chain(incAsync) |
| 58 | + |
| 59 | + const final = await ar.toPromise() |
| 60 | + expect(final.isOk()).toBe(true) |
| 61 | + expect(final.unwrap()).toBe(5) |
| 62 | + }) |
| 63 | + |
| 64 | + it('all should collect Ok values and fail on the first failure', async () => { |
| 65 | + const a = AsyncResult.ok<number, string>(1) |
| 66 | + const b = AsyncResult.fromPromise<number, string>(Promise.resolve(2)) |
| 67 | + const c = AsyncResult.ok<number, string>(3) |
| 68 | + |
| 69 | + const allOk = await AsyncResult.all([a, b, c]).toPromise() |
| 70 | + expect(allOk.isOk()).toBe(true) |
| 71 | + expect(allOk.unwrap()).toEqual([1, 2, 3]) |
| 72 | + |
| 73 | + const bad = AsyncResult.fail<number, string>('oops') |
| 74 | + const allFail = await AsyncResult.all([a, bad, c]).toPromise() |
| 75 | + expect(allFail.isFail()).toBe(true) |
| 76 | + expect(allFail.unwrapFail()).toBe('oops') |
| 77 | + }) |
| 78 | + |
| 79 | + it('fromResult and fromResultPromise should wrap existing Results', async () => { |
| 80 | + const syncOk = AsyncResult.fromResult(ok<number, string>(1)) |
| 81 | + const okFinal = await syncOk.toPromise() |
| 82 | + expect(okFinal.isOk()).toBe(true) |
| 83 | + expect(okFinal.unwrap()).toBe(1) |
| 84 | + |
| 85 | + const syncFail = AsyncResult.fromResult(fail<number, string>('e')) |
| 86 | + const failFinal = await syncFail.toPromise() |
| 87 | + expect(failFinal.isFail()).toBe(true) |
| 88 | + expect(failFinal.unwrapFail()).toBe('e') |
| 89 | + |
| 90 | + const p = Promise.resolve(ok<number, string>(7)) |
| 91 | + const fromPromiseResult = await AsyncResult.fromResultPromise(p).toPromise() |
| 92 | + expect(fromPromiseResult.isOk()).toBe(true) |
| 93 | + expect(fromPromiseResult.unwrap()).toBe(7) |
| 94 | + |
| 95 | + const viaFromResult = await AsyncResult.fromResult(Promise.resolve(ok<number, string>(42))).toPromise() |
| 96 | + expect(viaFromResult.isOk()).toBe(true) |
| 97 | + expect(viaFromResult.unwrap()).toBe(42) |
| 98 | + }) |
| 99 | + |
| 100 | + it('mapFail should transform the error', async () => { |
| 101 | + const ar = AsyncResult.fail<number, Error>(new Error('x')).mapFail(e => e.message) |
| 102 | + const final = await ar.toPromise() |
| 103 | + expect(final.isFail()).toBe(true) |
| 104 | + expect(final.unwrapFail()).toBe('x') |
| 105 | + }) |
| 106 | + |
| 107 | + it('flatMapAsync should catch thrown/rejected errors and convert to Fail', async () => { |
| 108 | + const ar = AsyncResult.ok<number, string>(1).flatMapAsync(async () => { |
| 109 | + throw 'oops' |
| 110 | + }) |
| 111 | + const final = await ar.toPromise() |
| 112 | + expect(final.isFail()).toBe(true) |
| 113 | + expect(final.unwrapFail()).toBe('oops') |
| 114 | + }) |
| 115 | + |
| 116 | + it('flatMapAsync and chain should short-circuit when initial AsyncResult is Fail', async () => { |
| 117 | + const fm = await AsyncResult.fail<number, string>('bad') |
| 118 | + .flatMapAsync(async n => ok<number, string>(n + 1)) |
| 119 | + .toPromise() |
| 120 | + expect(fm.isFail()).toBe(true) |
| 121 | + expect(fm.unwrapFail()).toBe('bad') |
| 122 | + |
| 123 | + const chained = await AsyncResult.fail<number, string>('bad') |
| 124 | + .chain(n => AsyncResult.ok<number, string>(n + 1)) |
| 125 | + .toPromise() |
| 126 | + expect(chained.isFail()).toBe(true) |
| 127 | + expect(chained.unwrapFail()).toBe('bad') |
| 128 | + }) |
| 129 | + |
| 130 | + it('match and matchAsync should resolve with the proper branch', async () => { |
| 131 | + const m1 = await AsyncResult.ok<number, string>(2).match({ ok: n => n * 2, fail: _ => -1 }) |
| 132 | + expect(m1).toBe(4) |
| 133 | + |
| 134 | + const m2 = await AsyncResult.ok<number, string>(3).matchAsync({ ok: async n => n * 3, fail: async _ => -1 }) |
| 135 | + expect(m2).toBe(9) |
| 136 | + |
| 137 | + const m3 = await AsyncResult.fail<number, string>('x').matchAsync({ ok: async _ => 0, fail: async e => e.length }) |
| 138 | + expect(m3).toBe(1) |
| 139 | + }) |
| 140 | +}) |
0 commit comments