-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vowTools): add asVow helper (#9577)
refs: #9449 ## Description - Add `asVow` helper to `VowTools` to ensure we always return vows, even in the event of early synchronous errors. - Implement the helper in `ChainAccountKit` against #9562
- Loading branch information
Showing
4 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// @ts-check | ||
import test from 'ava'; | ||
|
||
import { E } from '@endo/far'; | ||
import { makeHeapZone } from '@agoric/base-zone/heap.js'; | ||
|
||
import { prepareVowTools } from '../src/tools.js'; | ||
import { getVowPayload, isVow } from '../src/vow-utils.js'; | ||
|
||
test('asVow takes a function that throws/returns synchronously and returns a vow', async t => { | ||
const { watch, when, asVow } = prepareVowTools(makeHeapZone()); | ||
|
||
const fnThatThrows = () => { | ||
throw Error('fail'); | ||
}; | ||
|
||
const vowWithRejection = asVow(fnThatThrows); | ||
t.true(isVow(vowWithRejection)); | ||
await t.throwsAsync( | ||
when(vowWithRejection), | ||
{ message: 'fail' }, | ||
'error should propogate as promise rejection', | ||
); | ||
|
||
const isWatchAble = watch(asVow(fnThatThrows)); | ||
t.true(isVow(vowWithRejection)); | ||
await t.throwsAsync(when(isWatchAble), { message: 'fail' }); | ||
|
||
const fnThatReturns = () => { | ||
return 'early return'; | ||
}; | ||
const vowWithReturn = asVow(fnThatReturns); | ||
t.true(isVow(vowWithReturn)); | ||
t.is(await when(vowWithReturn), 'early return'); | ||
t.is(await when(watch(vowWithReturn)), 'early return'); | ||
}); | ||
|
||
test('asVow does not resolve a vow to a vow', async t => { | ||
const { watch, when, asVow } = prepareVowTools(makeHeapZone()); | ||
|
||
const testVow = watch(Promise.resolve('payload')); | ||
const testVowAsVow = asVow(() => testVow); | ||
|
||
const vowPayload = getVowPayload(testVowAsVow); | ||
assert(vowPayload?.vowV0, 'testVowAsVow is a vow'); | ||
const unwrappedOnce = await E(vowPayload.vowV0).shorten(); | ||
t.false( | ||
isVow(unwrappedOnce), | ||
'vows passed to asVow are not rewrapped as vows', | ||
); | ||
t.is(unwrappedOnce, 'payload'); | ||
|
||
t.is(await when(testVow), await when(testVowAsVow), 'result is preserved'); | ||
}); |