Mirror of https://github.com/roostorg/coop github.com/roostorg/coop
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 98 lines 3.8 kB view raw
1import { randomUUID } from 'crypto'; 2import { 3 Kysely, 4 type CompiledQuery, 5 type DatabaseConnection, 6 type QueryResult, 7} from 'kysely'; 8 9import { type Dependencies } from '../../iocContainer/index.js'; 10import { type MockedFn } from '../../test/mockHelpers/jestMocks.js'; 11import { makeMockWarehouseDialect } from '../../test/stubs/makeMockWarehouseKyselyDialect.js'; 12import { safePick } from '../../utils/misc.js'; 13import { makeFetchUserActionStatistics } from './fetchUserActionStatistics.js'; 14 15describe('fetchUserActionStatistics', () => { 16 let warehouseMock: MockedFn< 17 (it: CompiledQuery) => Promise<QueryResult<unknown>> 18 >; 19 let sut: ReturnType<typeof makeFetchUserActionStatistics>; 20 21 beforeEach(() => { 22 // For these tests, configure the mock to always resolve w/ an empty query 23 // result, since our code doesn't branch on the query result, and our tests 24 // are just asserting what queries were issued. 25 // 26 // This mutation is safe (while we're not running tests concurrently) as 27 // it's local to the test suite. Consider using the `makeTestWithFixture` 28 // helper instead to make a local copy of this state for each test. 29 30 warehouseMock = jest 31 .fn<DatabaseConnection['executeQuery']>() 32 .mockResolvedValue({ rows: [] }); 33 34 // This mutation is safe (while we're not running tests concurrently) as 35 // it's local to the test suite. Consider using the `makeTestWithFixture` 36 // helper instead to make a local copy of this state for each test. 37 38 const kysely = new Kysely({ 39 dialect: makeMockWarehouseDialect(warehouseMock), 40 }); 41 const dialectMock: Dependencies['DataWarehouseDialect'] = { 42 getKyselyInstance: () => kysely, 43 destroy: jest.fn(async () => {}), 44 }; 45 46 sut = makeFetchUserActionStatistics(dialectMock); 47 }); 48 49 test('should generate proper query given user item identifiers', async () => { 50 await sut({ orgId: 'x', userItemIdentifiers: [{ id: '1', typeId: 'a' }] }); 51 await sut({ 52 orgId: 'x', 53 userItemIdentifiers: [ 54 { id: '1', typeId: 'a' }, 55 { id: '3', typeId: 'b' }, 56 ], 57 }); 58 expect(warehouseMock).toHaveBeenCalledTimes(2); 59 60 const queriesRan = warehouseMock.mock.calls.map((it) => 61 safePick(it[0], ['parameters', 'sql']), 62 ); 63 64 expect(queriesRan[0]).toMatchInlineSnapshot(` 65 { 66 "parameters": [ 67 "x", 68 "1", 69 "a", 70 ], 71 "sql": "select "USER_ID" as "userId", "USER_TYPE_ID" as "userTypeId", "ACTION_ID" as "actionId", "POLICY_ID" as "policyId", "ITEM_SUBMISSION_IDS" as "itemSubmissionIds", "ACTOR_ID" as "actorId", "COUNT" as "count" from "USER_STATISTICS_SERVICE"."LIFETIME_ACTION_STATS" where "ORG_ID" = :1 and ("USER_ID" = :2 and "USER_TYPE_ID" = :3)", 72 } 73 `); 74 expect(queriesRan[1]).toMatchInlineSnapshot(` 75 { 76 "parameters": [ 77 "x", 78 "1", 79 "a", 80 "3", 81 "b", 82 ], 83 "sql": "select "USER_ID" as "userId", "USER_TYPE_ID" as "userTypeId", "ACTION_ID" as "actionId", "POLICY_ID" as "policyId", "ITEM_SUBMISSION_IDS" as "itemSubmissionIds", "ACTOR_ID" as "actorId", "COUNT" as "count" from "USER_STATISTICS_SERVICE"."LIFETIME_ACTION_STATS" where "ORG_ID" = :1 and (("USER_ID" = :2 and "USER_TYPE_ID" = :3) or ("USER_ID" = :4 and "USER_TYPE_ID" = :5))", 84 } 85 `); 86 }); 87 88 test('should batch queries of more than 16,000 unique user ids', async () => { 89 const numUserIds = Math.floor(16_000 / Math.max(Math.random(), 0.05)); // some big int over 16,000 90 const largeUserIdList = Array.from({ length: numUserIds }, (_) => ({ 91 id: randomUUID(), 92 typeId: randomUUID(), 93 })); 94 95 await sut({ orgId: 'x', userItemIdentifiers: largeUserIdList }); 96 expect(warehouseMock.mock.calls.length).toBeGreaterThan(1); 97 }); 98});