Skip to content

Commit

Permalink
improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Feb 25, 2024
1 parent 6bd4b3e commit 880b181
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/lib/grandExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,14 @@ ${type} ${toKMB(quantity)} ${item.name} for ${toKMB(price)} each, for a total of

debugLog(`Expected G.E Bank: ${shouldHave}`);
if (!currentBank.equals(shouldHave)) {
throw new Error(
`GE either has extra or insufficient items. Difference: ${shouldHave.difference(currentBank)}`
);
if (!currentBank.has(shouldHave)) {
throw new Error(
`GE is MISSING items to cover the ${
[...buyListings, ...sellListings].length
}x active listings. Difference: ${shouldHave.difference(currentBank)}`
);
}
throw new Error(`GE has EXTRA items. Difference: ${shouldHave.difference(currentBank)}`);
} else {
debugLog(
`GE has ${currentBank}, which is enough to cover the ${
Expand Down
18 changes: 13 additions & 5 deletions tests/integration/grandExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('Grand Exchange', async () => {
GrandExchange.calculateSlotsOfUser = async () => ({ slots: 500 } as any);
await mockClient();

async function waitForGEToBeEmpty() {
await GrandExchange.queue.onEmpty();
assert(GrandExchange.queue.size === 0, 'G.E Queue should be empty');
assert(!GrandExchange.locked, 'G.E should not be locked');
}

test(
'Fuzz',
async () => {
Expand All @@ -84,6 +90,7 @@ describe('Grand Exchange', async () => {
}
console.log(`Finished initializing ${amountOfUsers} users`);

// Run a bunch of commands to buy/sell
const commandPromises = new PQueue({ concurrency: 10 });
for (const user of shuffleArr(users)) {
const method = randArrItem(['buy', 'sell']);
Expand All @@ -101,33 +108,34 @@ describe('Grand Exchange', async () => {
}
});
}

await commandPromises.onEmpty();
await GrandExchange.queue.onEmpty();
await waitForGEToBeEmpty();

console.log('Finished running all commands');

// Tick the g.e to make some transactions
for (let i = 0; i < 100; i++) {
await GrandExchange.tick();
await Promise.all([
GrandExchange.checkGECanFullFilAllListings(),
GrandExchange.extensiveVerification()
]);
}
await waitForGEToBeEmpty();
console.log('Finished ticking 100 times');

const testBank = new Bank();
// Cancel all remaining listings
const cancelPromises = [];
for (const user of users) {
cancelPromises.push(cancelAllListings(user));
}

await Promise.all(cancelPromises);
await GrandExchange.queue.onEmpty();
await waitForGEToBeEmpty();
console.log('Finished cancelling');

await Promise.all(users.map(u => u.sync()));

const testBank = new Bank();
for (const user of users) {
testBank.add(user.bankWithGP);
}
Expand Down

0 comments on commit 880b181

Please sign in to comment.