From 8aae4e312fee959b39dac5c02d7b55f1dfe88de8 Mon Sep 17 00:00:00 2001 From: gc <30398469+gc@users.noreply.github.com> Date: Wed, 3 Jul 2024 10:45:07 +1000 Subject: [PATCH] fixes --- tests/integration/commands/open.test.ts | 16 +- tests/integration/grandExchange.test.ts | 44 +-- tests/integration/migrateUser.test.ts | 338 ++++++++++----------- tests/integration/tradeTransaction.test.ts | 5 - tests/integration/trading.test.ts | 2 +- vitest.integration.config.mts | 7 +- 6 files changed, 188 insertions(+), 224 deletions(-) diff --git a/tests/integration/commands/open.test.ts b/tests/integration/commands/open.test.ts index 0462f07c7f..02a5ac3c67 100644 --- a/tests/integration/commands/open.test.ts +++ b/tests/integration/commands/open.test.ts @@ -1,5 +1,5 @@ import { Bank } from 'oldschooljs'; -import { beforeEach, describe, expect, test } from 'vitest'; +import { describe, expect, test } from 'vitest'; import { openCommand } from '../../../src/mahoji/commands/open'; import { randomMock } from '../setup'; @@ -7,15 +7,10 @@ import { createTestUser, mockClient } from '../util'; describe('Open Command', async () => { await mockClient(); - const user = await createTestUser(); - - beforeEach(async () => { + test.concurrent('Open with no quantity', async () => { randomMock(); - await user.reset(); + const user = await createTestUser(); await user.addItemsToBank({ items: new Bank().add('Reward casket (beginner)', 100) }); - }); - - test('Open with no quantity', async () => { const result = await user.runCommand(openCommand, { name: 'reward casket (beginner)' }); expect(result).toMatchObject({ content: `You have now opened a total of 1x Reward casket (beginner) @@ -31,7 +26,10 @@ describe('Open Command', async () => { await user.clMatch(new Bank().add('Fire rune', 34)); }); - test('Open with quantity', async () => { + test.concurrent('Open with quantity', async () => { + randomMock(); + const user = await createTestUser(); + await user.addItemsToBank({ items: new Bank().add('Reward casket (beginner)', 100) }); await user.runCommand(openCommand, { name: 'reward casket (beginner)', quantity: 10 }); await user.bankAmountMatch('Reward casket (beginner)', 90); await user.openedBankMatch(new Bank().add('Reward casket (beginner)', 10)); diff --git a/tests/integration/grandExchange.test.ts b/tests/integration/grandExchange.test.ts index f968aa0c53..7e59fbee10 100644 --- a/tests/integration/grandExchange.test.ts +++ b/tests/integration/grandExchange.test.ts @@ -5,35 +5,19 @@ import { describe, expect, test } from 'vitest'; import { usernameCache } from '../../src/lib/constants'; import { GrandExchange } from '../../src/lib/grandExchange'; +import { prisma } from '../../src/lib/settings/prisma'; import { assert } from '../../src/lib/util'; import resolveItems from '../../src/lib/util/resolveItems'; import { geCommand } from '../../src/mahoji/commands/ge'; import { cancelUsersListings } from '../../src/mahoji/lib/abstracted_commands/cancelGEListingCommand'; import type { TestUser } from './util'; import { createTestUser, mockClient } from './util'; -import { Stopwatch } from '@sapphire/stopwatch'; - -const TICKS_TO_RUN = 40; +const TICKS_TO_RUN = 100; const AMOUNT_USERS = 20; -const quantities = [-1, 0, 100_000_000_000_000_000, 1, 2, 38, 1_000_000_000_000, 500, '5*5']; -const prices = [ - -1, - 0, - 100_000_000_000_000_000, - 1, - 2, - 1_000_000_000_000, - 99, - 100, - 101, - 1005, - 4005, - 5005, - 100_000, - '5*9999999' -]; +const quantities = [1, 2, 38, 500, '5*5']; +const prices = [1, 30, 33, 55]; const sampleBank = new Bank() .add('Coins', 1_000_000_000) @@ -68,11 +52,13 @@ describe('Grand Exchange', async () => { let users: TestUser[] = []; for (let i = 0; i < AMOUNT_USERS; i++) { - users.push((async () => { - const user = await createTestUser(); - await user.addItemsToBank({ items: sampleBank }); - return user; - })() as any) + users.push( + (async () => { + const user = await createTestUser(); + await user.addItemsToBank({ items: sampleBank }); + return user; + })() as any + ); } users = await Promise.all(users); console.log(`Finished initializing ${AMOUNT_USERS} users`); @@ -81,7 +67,8 @@ describe('Grand Exchange', async () => { const commandPromises = new PQueue({ concurrency: 20 }); for (const user of shuffleArr(users)) { commandPromises.add(async () => { - const method = randArrItem(['buy', 'sell']); + for (let i = 0; i < 5;i++) { + const method = randArrItem(['buy', 'sell']); const quantity = randArrItem(quantities); const price = randArrItem(prices); for (const item of itemPool) { @@ -93,6 +80,7 @@ describe('Grand Exchange', async () => { } }); } + } }); } await commandPromises.onEmpty(); @@ -105,6 +93,8 @@ describe('Grand Exchange', async () => { await GrandExchange.extensiveVerification(); } await waitForGEToBeEmpty(); + const count = await prisma.gETransaction.count(); + console.log(`Finished ticking ${TICKS_TO_RUN} times, made ${count} transactions`); // Cancel all remaining listings const cancelPromises = []; @@ -135,8 +125,6 @@ describe('Grand Exchange', async () => { const data = await GrandExchange.fetchData(); expect(data.isLocked).toEqual(false); - expect(data.taxBank, '1MS').toBeGreaterThan(0); - expect(data.totalTax, 'L1M').toBeGreaterThan(0); const totalTaxed = await global.prisma!.gETransaction.aggregate({ _sum: { diff --git a/tests/integration/migrateUser.test.ts b/tests/integration/migrateUser.test.ts index 4387b460bb..8f21bf54d4 100644 --- a/tests/integration/migrateUser.test.ts +++ b/tests/integration/migrateUser.test.ts @@ -1190,226 +1190,212 @@ async function buildBaseUser(userId: string) { const user = await createTestUser(startBank, userData); return user; } -describe('migrate user test', async () => { - await mockClient(); - vi.doMock('../../src/lib/util', async () => { - const actual: any = await vi.importActual('../../src/lib/util'); - return { - ...actual, - channelIsSendable: () => false - }; - }); - - const logResult = ( - result: { result: boolean; errors: string[] }, - sourceData: UserData, - newData: UserData, - srcHistory?: string[], - dstHistory?: string[] - ) => { - if (!result.result) { - if (srcHistory) { - console.log(`Source Command History: ${sourceData.id}`); - console.log(srcHistory); - } - if (dstHistory) { - console.log(`Target Command History: ${newData.id}`); - console.log(dstHistory); - } - console.log(`source: ${sourceData.id} dest: ${newData.id}`); - console.log(result.errors); - console.log(JSON.stringify(sourceData)); - console.log(JSON.stringify(newData)); - } +await mockClient(); +vi.doMock('../../src/lib/util', async () => { + const actual: any = await vi.importActual('../../src/lib/util'); + return { + ...actual, + channelIsSendable: () => false }; +}); - test('test preventing a double (clobber) robochimp migration (two bot-migration)', async () => { - const sourceUserId = mockedId(); - const destUserId = mockedId(); - - // Create source user, and populate data: - const sourceUser = await buildBaseUser(sourceUserId); - const srcHistory = await runRandomTestCommandsOnUser(sourceUser, 5, true); - - const sourceData = new UserData(sourceUser); - await sourceData.sync(); - - const migrateResult = await migrateUser(sourceUser.id, destUserId); - expect(migrateResult).toEqual(true); - - const destData = new UserData(destUserId); - await destData.sync(); - - const compareResult = sourceData.equals(destData); - logResult(compareResult, sourceData, destData, srcHistory, []); - expect(compareResult.result).toBe(true); - - // Now the actual test, everything above has to happen first... - await runAllTestCommandsOnUser(sourceUser); +const logResult = ( + result: { result: boolean; errors: string[] }, + sourceData: UserData, + newData: UserData, + srcHistory?: string[], + dstHistory?: string[] +) => { + if (!result.result) { + if (srcHistory) { + console.log(`Source Command History: ${sourceData.id}`); + console.log(srcHistory); + } + if (dstHistory) { + console.log(`Target Command History: ${newData.id}`); + console.log(dstHistory); + } + console.log(`source: ${sourceData.id} dest: ${newData.id}`); + console.log(result.errors); + console.log(JSON.stringify(sourceData)); + console.log(JSON.stringify(newData)); + } +}; - const newSourceData = new UserData(sourceUser); - await newSourceData.sync(); +test.concurrent('test preventing a double (clobber) robochimp migration (two bot-migration)', async () => { + const sourceUserId = mockedId(); + const destUserId = mockedId(); - const secondMigrateResult = await migrateUser(sourceUser.id, destUserId); - expect(secondMigrateResult).toEqual(true); + // Create source user, and populate data: + const sourceUser = await buildBaseUser(sourceUserId); + const srcHistory = await runRandomTestCommandsOnUser(sourceUser, 5, true); - const newDestData = new UserData(destUserId); - await newDestData.sync(); + const sourceData = new UserData(sourceUser); + await sourceData.sync(); - const newCompareResult = sourceData.equals(destData); - logResult(newCompareResult, newSourceData, newDestData); - expect(newCompareResult.result).toBe(true); + const migrateResult = await migrateUser(sourceUser.id, destUserId); + expect(migrateResult).toEqual(true); - expect(newDestData.githubId).toEqual(sourceData.githubId); - expect(newDestData.githubId).toEqual(destData.githubId); + const destData = new UserData(destUserId); + await destData.sync(); - // Make sure the 2nd transfer didn't overwrite robochimp: - expect(newDestData.githubId !== newSourceData.githubId).toBeTruthy(); + const compareResult = sourceData.equals(destData); + logResult(compareResult, sourceData, destData, srcHistory, []); + expect(compareResult.result).toBe(true); - // Verify migrated id is correct - expect(newDestData.migratedUserId).toEqual(BigInt(sourceData.id)); - }); - test('test migrating existing user to target with no records', async () => { - const sourceUser = await buildBaseUser(mockedId()); - await runAllTestCommandsOnUser(sourceUser); + // Now the actual test, everything above has to happen first... + await runAllTestCommandsOnUser(sourceUser); - const destUserId = mockedId(); + const newSourceData = new UserData(sourceUser); + await newSourceData.sync(); - const sourceData = new UserData(sourceUser); - await sourceData.sync(); + const secondMigrateResult = await migrateUser(sourceUser.id, destUserId); + expect(secondMigrateResult).toEqual(true); - const migrateResult = await migrateUser(sourceUser.id, destUserId); - expect(migrateResult).toEqual(true); + const newDestData = new UserData(destUserId); + await newDestData.sync(); - const newData = new UserData(destUserId); - await newData.sync(); + const newCompareResult = sourceData.equals(destData); + logResult(newCompareResult, newSourceData, newDestData); + expect(newCompareResult.result).toBe(true); - const compareResult = sourceData.equals(newData); - logResult(compareResult, sourceData, newData); + expect(newDestData.githubId).toEqual(sourceData.githubId); + expect(newDestData.githubId).toEqual(destData.githubId); - expect(compareResult.result).toBe(true); - }); + // Make sure the 2nd transfer didn't overwrite robochimp: + expect(newDestData.githubId !== newSourceData.githubId).toBeTruthy(); - test('test migrating full user on top of full profile', async () => { - const sourceUser = await buildBaseUser(mockedId()); - const destUser = await buildBaseUser(mockedId()); - await runAllTestCommandsOnUser(sourceUser); - await runAllTestCommandsOnUser(destUser); + // Verify migrated id is correct + expect(newDestData.migratedUserId).toEqual(BigInt(sourceData.id)); +}); +test.concurrent('test migrating existing user to target with no records', async () => { + const sourceUser = await buildBaseUser(mockedId()); + await runAllTestCommandsOnUser(sourceUser); - const sourceData = new UserData(sourceUser); - await sourceData.sync(); + const destUserId = mockedId(); - const migrateResult = await migrateUser(sourceUser.id, destUser.id); - expect(migrateResult).toEqual(true); + const sourceData = new UserData(sourceUser); + await sourceData.sync(); - const newData = new UserData(destUser.id); - await newData.sync(); - const compareResult = sourceData.equals(newData); - logResult(compareResult, sourceData, newData); + const migrateResult = await migrateUser(sourceUser.id, destUserId); + expect(migrateResult).toEqual(true); - expect(compareResult.result).toBe(true); + const newData = new UserData(destUserId); + await newData.sync(); - if (newData.poh) newData.poh.spellbook_altar = 33; - if (newData.userStats) newData.userStats.sacrificed_bank = new Bank().add('Cannonball').bank; - newData.skillsAsLevels!.cooking = 1_000_000; - newData.bingos = []; - newData.botItemSell = []; - if (newData.gear?.melee) newData.gear.melee.weapon = null; + const compareResult = sourceData.equals(newData); + logResult(compareResult, sourceData, newData); - const badResult = sourceData.equals(newData); - expect(badResult.result).toBe(false); + expect(compareResult.result).toBe(true); +}); - const expectedBadResult = [ - `Failed comparing ${sourceUser.id} vs ${destUser.id}:`, - "melee gear doesn't match", - "cooking level doesn't match. 1 vs 1000000", - "POH Object doesn't match: null !== 33", - 'User Stats doesn\'t match: {} !== {"2":1}', - 'Wrong number of BotItemSell rows. 1 vs 0' - ]; - expect(badResult.errors).toEqual(expectedBadResult); - }); +test.concurrent('test migrating full user on top of full profile', async () => { + const sourceUser = await buildBaseUser(mockedId()); + const destUser = await buildBaseUser(mockedId()); + await runAllTestCommandsOnUser(sourceUser); + await runAllTestCommandsOnUser(destUser); + + const sourceData = new UserData(sourceUser); + await sourceData.sync(); + + const migrateResult = await migrateUser(sourceUser.id, destUser.id); + expect(migrateResult).toEqual(true); + + const newData = new UserData(destUser.id); + await newData.sync(); + const compareResult = sourceData.equals(newData); + logResult(compareResult, sourceData, newData); + + expect(compareResult.result).toBe(true); + + if (newData.poh) newData.poh.spellbook_altar = 33; + if (newData.userStats) newData.userStats.sacrificed_bank = new Bank().add('Cannonball').bank; + newData.skillsAsLevels!.cooking = 1_000_000; + newData.bingos = []; + newData.botItemSell = []; + if (newData.gear?.melee) newData.gear.melee.weapon = null; + + const badResult = sourceData.equals(newData); + expect(badResult.result).toBe(false); + + const expectedBadResult = [ + `Failed comparing ${sourceUser.id} vs ${destUser.id}:`, + "melee gear doesn't match", + "cooking level doesn't match. 1 vs 1000000", + "POH Object doesn't match: null !== 33", + 'User Stats doesn\'t match: {} !== {"2":1}', + 'Wrong number of BotItemSell rows. 1 vs 0' + ]; + expect(badResult.errors).toEqual(expectedBadResult); +}); - test( - 'test migrating random user on top of empty profile', - async () => { - const sourceUser = await buildBaseUser(mockedId()); - const destUserId = mockedId(); +test.concurrent('test migrating random user on top of empty profile', async () => { + const sourceUser = await buildBaseUser(mockedId()); + const destUserId = mockedId(); - const sourceRolls = randInt(6, 11); - const cmdHistory = await runRandomTestCommandsOnUser(sourceUser, sourceRolls); + const sourceRolls = randInt(6, 11); + const cmdHistory = await runRandomTestCommandsOnUser(sourceUser, sourceRolls); - const sourceData = new UserData(sourceUser); - await sourceData.sync(); + const sourceData = new UserData(sourceUser); + await sourceData.sync(); - const result = await migrateUser(sourceUser, destUserId); + const result = await migrateUser(sourceUser, destUserId); - if (result !== true) throw new Error(`${sourceUser.id} - ${result}`); - expect(result).toEqual(true); + if (result !== true) throw new Error(`${sourceUser.id} - ${result}`); + expect(result).toEqual(true); - const newData = new UserData(destUserId); - await newData.sync(); + const newData = new UserData(destUserId); + await newData.sync(); - const compareResult = sourceData.equals(newData); - logResult(compareResult, sourceData, newData, cmdHistory, []); + const compareResult = sourceData.equals(newData); + logResult(compareResult, sourceData, newData, cmdHistory, []); - expect(compareResult.result).toBe(true); - }, - { repeats: 1 } - ); + expect(compareResult.result).toBe(true); +}); - test( - 'test migrating random user on top of random profile', - async () => { - const sourceUser = await buildBaseUser(mockedId()); - const destUser = await buildBaseUser(mockedId()); +test.concurrent('test migrating random user on top of random profile', async () => { + const sourceUser = await buildBaseUser(mockedId()); + const destUser = await buildBaseUser(mockedId()); - const sourceRolls = randInt(5, 12); - const destRolls = randInt(5, 12); + const sourceRolls = randInt(5, 12); + const destRolls = randInt(5, 12); - const srcHistory = await runRandomTestCommandsOnUser(sourceUser, sourceRolls); - const dstHistory = await runRandomTestCommandsOnUser(destUser, destRolls); + const srcHistory = await runRandomTestCommandsOnUser(sourceUser, sourceRolls); + const dstHistory = await runRandomTestCommandsOnUser(destUser, destRolls); - const sourceData = new UserData(sourceUser); - await sourceData.sync(); + const sourceData = new UserData(sourceUser); + await sourceData.sync(); - const result = await migrateUser(sourceUser, destUser); - expect(result).toEqual(true); + const result = await migrateUser(sourceUser, destUser); + expect(result).toEqual(true); - const newData = new UserData(destUser); - await newData.sync(); + const newData = new UserData(destUser); + await newData.sync(); - const compareResult = sourceData.equals(newData); - logResult(compareResult, sourceData, newData, srcHistory, dstHistory); + const compareResult = sourceData.equals(newData); + logResult(compareResult, sourceData, newData, srcHistory, dstHistory); - expect(compareResult.result).toBe(true); - }, - { repeats: 1 } - ); + expect(compareResult.result).toBe(true); +}); - test( - 'test migrating random user on top of full profile', - async () => { - const sourceUser = await buildBaseUser(mockedId()); - const destUser = await buildBaseUser(mockedId()); +test.concurrent('test migrating random user on top of full profile', async () => { + const sourceUser = await buildBaseUser(mockedId()); + const destUser = await buildBaseUser(mockedId()); - const cmdHistory = await runRandomTestCommandsOnUser(sourceUser); - await runAllTestCommandsOnUser(destUser); + const cmdHistory = await runRandomTestCommandsOnUser(sourceUser); + await runAllTestCommandsOnUser(destUser); - const sourceData = new UserData(sourceUser); - await sourceData.sync(); + const sourceData = new UserData(sourceUser); + await sourceData.sync(); - const result = await migrateUser(sourceUser, destUser); - expect(result).toEqual(true); + const result = await migrateUser(sourceUser, destUser); + expect(result).toEqual(true); - const newData = new UserData(destUser); - await newData.sync(); + const newData = new UserData(destUser); + await newData.sync(); - const compareResult = sourceData.equals(newData); - logResult(compareResult, sourceData, newData, cmdHistory, []); + const compareResult = sourceData.equals(newData); + logResult(compareResult, sourceData, newData, cmdHistory, []); - expect(compareResult.result).toBe(true); - }, - { repeats: 1 } - ); + expect(compareResult.result).toBe(true); }); diff --git a/tests/integration/tradeTransaction.test.ts b/tests/integration/tradeTransaction.test.ts index b684d57c15..0c47819447 100644 --- a/tests/integration/tradeTransaction.test.ts +++ b/tests/integration/tradeTransaction.test.ts @@ -133,12 +133,7 @@ describe('Transactionalized Trade Test', async () => { const expectedResult = { success: false, message: `<@${cyr}> doesn't own all items.` }; - expect(uCyr.GP).toBe(1_000_000); - expect(uMagna.GP).toBe(20_000_000); - - await uCyr.sync(); expect(result).toMatchObject(expectedResult); - console.log(cyrStartingBank.bank); expect(uCyr.bankWithGP.toString()).toEqual(cyrStartingBank.toString()); expect(uCyr.bankWithGP.equals(cyrStartingBank)).toBe(true); expect(uMagna.bankWithGP.equals(magnaStartingBank)).toBe(true); diff --git a/tests/integration/trading.test.ts b/tests/integration/trading.test.ts index 226e80246c..1cb9065481 100644 --- a/tests/integration/trading.test.ts +++ b/tests/integration/trading.test.ts @@ -14,7 +14,7 @@ test('Trade consistency', async () => { let users: TestUser[] = []; for (let i = 0; i < NUMBER_OF_USERS; i++) { - users.push( createTestUser(bank) as any); + users.push(createTestUser(bank) as any); } users = await Promise.all(users); diff --git a/vitest.integration.config.mts b/vitest.integration.config.mts index fe455cf7ca..918be23705 100644 --- a/vitest.integration.config.mts +++ b/vitest.integration.config.mts @@ -13,14 +13,11 @@ export default defineConfig({ testTimeout: 30_000, bail: 1, pool: 'threads', + maxConcurrency: 30, poolOptions: { - forks: { - maxForks: 30, - minForks: 20 - }, threads: { maxThreads: 30, - minThreads: 20 + minThreads: 10 } } }