Skip to content

Commit

Permalink
chore(sync-tools): stop exporting fetchLatestEcQuestion
Browse files Browse the repository at this point in the history
Refs: #10378

fix: rebase cleanup
  • Loading branch information
anilhelvaci committed Nov 15, 2024
1 parent a5baf9c commit d560d72
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 73 deletions.
27 changes: 13 additions & 14 deletions packages/client-utils/src/sync-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ export const waitUntilOfferExited = async (addr, offerId, io, options) => {
);
};

/// ////////// Make sure an election held by a given committee (see @agoric/governance) turned out as expected /////////////
/// ////////// Make sure an election held by a given committee //////////
/// ////////// (see @agoric/governance) turned out as expected //////////

/**
* @typedef {{
Expand All @@ -343,20 +344,19 @@ export const waitUntilOfferExited = async (addr, offerId, io, options) => {

/**
* @param {string} basePath
* @param {{follow: (...params: string[]) => Promise<object>; marshaller: import("@endo/marshal").Marshal<any>}} io
* @param {import('./vstorage-kit').VstorageKit} vstorage
* @returns {Promise<ElectionResult>}
*/
export const fetchLatestEcQuestion = async (basePath, io) => {
const { follow, marshaller } = io;
const pathOutcome = `:${basePath}.latestOutcome`;
const pathQuestion = `:${basePath}.latestQuestion`;
const fetchLatestEcQuestion = async (basePath, vstorage) => {
const pathOutcome = `${basePath}.latestOutcome`;
const pathQuestion = `${basePath}.latestQuestion`;

const [latestOutcome, latestQuestion] = await Promise.all([
follow('-lF', pathOutcome, '-o', 'text').then(outcomeRaw =>
marshaller.fromCapData(JSON.parse(outcomeRaw)),
/** @type {Promise<ElectionResult["latestOutcome"]>} */ (
vstorage.readLatestHead(pathOutcome)
),
follow('-lF', pathQuestion, '-o', 'text').then(questionRaw =>
marshaller.fromCapData(JSON.parse(questionRaw)),
/** @type {Promise<ElectionResult["latestQuestion"]>} */ (
vstorage.readLatestHead(pathQuestion)
),
]);

Expand Down Expand Up @@ -397,8 +397,7 @@ const checkCommitteeElectionResult = (electionResult, expectedResult) => {
* deadline: bigint;
* }} expectedResult
* @param {{
* follow: (...params: string[]) => Promise<object>;
* marshaller: import("@endo/marshal").Marshal<any>,
* vstorage: import('./vstorage-kit').VstorageKit;
* log: typeof console.log,
* setTimeout: typeof global.setTimeout
* }} io
Expand All @@ -410,13 +409,13 @@ export const waitUntilElectionResult = (
io,
options,
) => {
const { follow, marshaller, log, setTimeout } = io;
const { vstorage, log, setTimeout } = io;

const { maxRetries, retryIntervalMs, errorMessage } =
overrideDefaultOptions(options);

return retryUntilCondition(
() => fetchLatestEcQuestion(committeePathBase, { follow, marshaller }),
() => fetchLatestEcQuestion(committeePathBase, vstorage),
electionResult =>
checkCommitteeElectionResult(electionResult, expectedResult),
errorMessage,
Expand Down
1 change: 0 additions & 1 deletion packages/client-utils/test/snapshots/exports.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Generated by [AVA](https://avajs.dev).
[
'boardSlottingMarshaller',
'fetchLatestEcQuestion',
'makeAgoricNames',
'makeFromBoard',
'makeStargateClient',
Expand Down
Binary file modified packages/client-utils/test/snapshots/exports.test.js.snap
Binary file not shown.
96 changes: 38 additions & 58 deletions packages/client-utils/test/sync-tools.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env node */
// @ts-check
import test from 'ava';
import { makeMarshal, Far } from '@endo/marshal';
import { Far } from '@endo/marshal';
import {
waitUntilAccountFunded,
waitUntilContractDeployed,
Expand All @@ -15,43 +15,23 @@ import {
const retryIntervalMs = 10;
const DEFAULT_TIMEOUT = 30;

const makeSimpleMarshaller = () => {
const vals = [];
const fromVal = val => {
vals.push(val);
return vals.length - 1;
};
const toVal = slot => vals[slot];
return makeMarshal(fromVal, toVal, {
serializeBodyFormat: 'smallcaps',
marshalSaveError: err => {
throw err;
},
});
};
harden(makeSimpleMarshaller);

const makeFakeFollow = () => {
const marshaller = makeSimpleMarshaller();
const makeFakeVstorageKit = () => {
let value;

const setValue = newValue => (value = newValue);
// TODO remove this when we switch all sync-tools to use client-utils's vstorageKit
const follow = () => Promise.resolve(value);
/**
*
* @param {any} _
* @param {string} path Assumes the path will be something like 'published.auction.book0'
* where value = { book0: {...} }
*/
const followByPath = (_, path) => {
const propName = path.split('.').at(-1);
return Promise.resolve(
// @ts-expect-error path will be a sequence of strings joined by "."
JSON.stringify(marshaller.toCapData(value[propName])),
);
const readLatestHead = path => {
const key = path.split('.').at(-1);
// @ts-expect-error path will be a string joined by "."
return Promise.resolve(value[key]);
};

return { setValue, follow, followByPath, marshaller };
return { setValue, follow, readLatestHead };
};

const makeFakeBalanceQuery = () => {
Expand Down Expand Up @@ -79,7 +59,7 @@ const makeFakeBalanceQuery = () => {
};

test.serial('wait until contract is deployed', async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue([['arbitrary', true]]);
const waitP = waitUntilContractDeployed(
'name',
Expand Down Expand Up @@ -179,7 +159,7 @@ test.serial('wait until account funded, insufficient balance', async t => {
test.serial(
'wait until offer result, balance update - should throw',
async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ status: {}, updated: 'balance' });

const waitP = waitUntilOfferResult(
Expand All @@ -199,7 +179,7 @@ test.serial(
);

test.serial('wait until offer result, wrong id - should throw', async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ status: { id: 'your-offer' }, updated: 'offerStatus' });

const waitP = waitUntilOfferResult(
Expand All @@ -218,7 +198,7 @@ test.serial('wait until offer result, wrong id - should throw', async t => {
});

test.serial('wait until offer result, no "status" - should throw', async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ updated: 'offerStatus' });

const waitP = waitUntilOfferResult(
Expand All @@ -239,7 +219,7 @@ test.serial('wait until offer result, no "status" - should throw', async t => {
test.serial(
'wait until offer result, numWantsSatisfied not equals to 1 - should throw',
async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({
status: { id: 'my-offer', numWantsSatisfied: 0 },
updated: 'offerStatus',
Expand All @@ -262,7 +242,7 @@ test.serial(
);

test.serial('wait until offer result, do not wait for "payouts"', async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ status: { id: 'my-offer' }, updated: 'offerStatus' });

const waitP = waitUntilOfferResult(
Expand Down Expand Up @@ -298,7 +278,7 @@ test.serial('wait until offer result, do not wait for "payouts"', async t => {
});

test.serial('wait until offer result, wait for "payouts"', async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ status: { id: 'my-offer' }, updated: 'offerStatus' });

const waitP = waitUntilOfferResult(
Expand Down Expand Up @@ -349,7 +329,7 @@ test.serial('wait until offer result, wait for "payouts"', async t => {
test.serial(
'wait until invitation recevied, wrong "updated" value',
async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ updated: 'offerStatus' });

const waitP = waitUntilInvitationReceived(
Expand All @@ -369,7 +349,7 @@ test.serial(
test.serial(
'wait until invitation recevied, falty "currentAmount" object',
async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ updated: 'balance' });

const waitP = waitUntilInvitationReceived(
Expand All @@ -394,7 +374,7 @@ test.serial(
test.serial(
'wait until invitation recevied, brand string do not match',
async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({ updated: 'balance', currentAmount: { brand: 'foo bar foo' } });

const waitP = waitUntilInvitationReceived(
Expand All @@ -412,7 +392,7 @@ test.serial(
);

test.serial('wait until invitation recevied', async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({});

const waitP = waitUntilInvitationReceived(
Expand All @@ -438,7 +418,7 @@ test.serial('wait until invitation recevied', async t => {
});

test.serial('wait until offer exited', async t => {
const { setValue, follow } = makeFakeFollow();
const { setValue, follow } = makeFakeVstorageKit();
setValue({});

const waitP = waitUntilOfferExited(
Expand All @@ -455,11 +435,11 @@ test.serial('wait until offer exited', async t => {
await t.throwsAsync(waitP);
});

test.serial('wait election result: question handle is adhered', async t => {
test.serial('wait election result: question handle is respected', async t => {
const oldQuestionHandle = Far('Question 1', {});
const newQuestionHandle = Far('Question 2', {});

const { setValue, followByPath, marshaller } = makeFakeFollow();
const { setValue, readLatestHead } = makeFakeVstorageKit();
const initState = harden({
latestOutcome: { outcome: 'win', question: oldQuestionHandle },
latestQuestion: {
Expand All @@ -473,8 +453,8 @@ test.serial('wait election result: question handle is adhered', async t => {
'dummyPath',
{ outcome: 'win', deadline: 2n },
{
follow: followByPath,
marshaller,
// @ts-expect-error casting
vstorage: { readLatestHead },
log: console.log,
setTimeout,
},
Expand All @@ -492,8 +472,8 @@ test.serial('wait election result: question handle is adhered', async t => {
'dummyPath',
{ outcome: 'win', deadline: 2n },
{
follow: followByPath,
marshaller,
// @ts-expect-error casting
vstorage: { readLatestHead },
log: console.log,
setTimeout,
},
Expand All @@ -514,10 +494,10 @@ test.serial('wait election result: question handle is adhered', async t => {
await t.notThrowsAsync(waitHappyP);
});

test.serial('wait election result: deadline is adhered', async t => {
test.serial('wait election result: deadline is respected', async t => {
const questionHandle = Far('Question', {});

const { setValue, followByPath, marshaller } = makeFakeFollow();
const { setValue, readLatestHead } = makeFakeVstorageKit();
const initState = harden({
latestOutcome: { outcome: 'win', question: questionHandle },
latestQuestion: {
Expand All @@ -531,8 +511,8 @@ test.serial('wait election result: deadline is adhered', async t => {
'dummyPath',
{ outcome: 'win', deadline: 5n },
{
follow: followByPath,
marshaller,
// @ts-expect-error casting
vstorage: { readLatestHead },
log: console.log,
setTimeout,
},
Expand All @@ -550,8 +530,8 @@ test.serial('wait election result: deadline is adhered', async t => {
'dummyPath',
{ outcome: 'win', deadline: 5n },
{
follow: followByPath,
marshaller,
// @ts-expect-error casting
vstorage: { readLatestHead },
log: console.log,
setTimeout,
},
Expand All @@ -575,10 +555,10 @@ test.serial('wait election result: deadline is adhered', async t => {
await t.notThrowsAsync(waitHappyP);
});

test.serial('wait election result: outcome is adhered', async t => {
test.serial('wait election result: outcome is respected', async t => {
const questionHandle = Far('Question', {});

const { setValue, followByPath, marshaller } = makeFakeFollow();
const { setValue, readLatestHead } = makeFakeVstorageKit();
const initState = harden({
latestOutcome: { outcome: 'lose', question: questionHandle },
latestQuestion: {
Expand All @@ -592,8 +572,8 @@ test.serial('wait election result: outcome is adhered', async t => {
'dummyPath',
{ outcome: 'win', deadline: 5n },
{
follow: followByPath,
marshaller,
// @ts-expect-error casting
vstorage: { readLatestHead },
log: console.log,
setTimeout,
},
Expand All @@ -611,8 +591,8 @@ test.serial('wait election result: outcome is adhered', async t => {
'dummyPath',
{ outcome: 'win', deadline: 5n },
{
follow: followByPath,
marshaller,
// @ts-expect-error casting
vstorage: { readLatestHead },
log: console.log,
setTimeout,
},
Expand Down

0 comments on commit d560d72

Please sign in to comment.