Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843import { PostgreSqlContainer, StartedPostgreSqlContainer,} from '@testcontainers/postgresql';import postgres from 'postgres';import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';import { DrizzleUserStatsRepository } from '../../infrastructure/repositories/DrizzleUserStatsRepository';import { createTestSchema } from '../../../cards/tests/test-utils/createTestSchema';import { users } from '../../infrastructure/repositories/schema/user.sql';import { cards } from '../../../cards/infrastructure/repositories/schema/card.sql';import { collections } from '../../../cards/infrastructure/repositories/schema/collection.sql';import { connections } from '../../../cards/infrastructure/repositories/schema/connection.sql';import { follows } from '../../infrastructure/repositories/schema/follows.sql';import { collectionCards } from '../../../cards/infrastructure/repositories/schema/collection.sql';
describe('DrizzleUserStatsRepository', () => { let container: StartedPostgreSqlContainer; let db: PostgresJsDatabase; let statsRepository: DrizzleUserStatsRepository;
beforeAll(async () => { // Start PostgreSQL container container = await new PostgreSqlContainer('postgres:14').start();
// Create database connection const connectionString = container.getConnectionUri(); process.env.DATABASE_URL = connectionString; const client = postgres(connectionString); db = drizzle(client);
// Create repository statsRepository = new DrizzleUserStatsRepository(db);
// Create schema using helper function await createTestSchema(db); }, 60000); // Increase timeout for container startup
afterAll(async () => { await container.stop(); });
// Clear data between tests beforeEach(async () => { await db.delete(collectionCards); await db.delete(follows); await db.delete(connections); await db.delete(collections); await db.delete(cards); await db.delete(users); });
describe('getUserGrowthStats', () => { it('should return user growth statistics with cumulative totals', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Insert users at different times await db.insert(users).values([ { id: 'user1', handle: 'user1.test', linkedAt: new Date(baseTime.getTime()), // Apr 1 lastLoginAt: new Date(baseTime.getTime()), }, { id: 'user2', handle: 'user2.test', linkedAt: new Date(baseTime.getTime()), // Apr 1 lastLoginAt: new Date(baseTime.getTime()), }, { id: 'user3', handle: 'user3.test', linkedAt: new Date(baseTime.getTime() + 24 * 60 * 60 * 1000), // Apr 2 lastLoginAt: new Date(baseTime.getTime() + 24 * 60 * 60 * 1000), }, { id: 'user4', handle: 'user4.test', linkedAt: new Date(baseTime.getTime() + 2 * 24 * 60 * 60 * 1000), // Apr 3 lastLoginAt: new Date(baseTime.getTime() + 2 * 24 * 60 * 60 * 1000), }, ]);
const result = await statsRepository.getUserGrowthStats({ interval: 'day', limit: 10, });
expect(result.dataPoints).toHaveLength(3); expect(result.currentTotal).toBe(4);
// Check cumulative growth (chronological order) expect(result.dataPoints[0]?.newUsers).toBe(2); // Apr 1: 2 new users expect(result.dataPoints[0]?.totalUsers).toBe(2); // Total: 2
expect(result.dataPoints[1]?.newUsers).toBe(1); // Apr 2: 1 new user expect(result.dataPoints[1]?.totalUsers).toBe(3); // Total: 3
expect(result.dataPoints[2]?.newUsers).toBe(1); // Apr 3: 1 new user expect(result.dataPoints[2]?.totalUsers).toBe(4); // Total: 4 });
it('should return empty data when no users exist', async () => { const result = await statsRepository.getUserGrowthStats({ interval: 'day', limit: 10, });
expect(result.dataPoints).toHaveLength(0); expect(result.currentTotal).toBe(0); });
it('should support week interval aggregation', async () => { const baseTime = new Date('2026-04-01T00:00:00Z'); // Tuesday
// Insert users across two weeks await db.insert(users).values([ { id: 'user1', handle: 'user1.test', linkedAt: new Date(baseTime.getTime()), // Week 1 lastLoginAt: new Date(baseTime.getTime()), }, { id: 'user2', handle: 'user2.test', linkedAt: new Date(baseTime.getTime() + 7 * 24 * 60 * 60 * 1000), // Week 2 lastLoginAt: new Date(baseTime.getTime() + 7 * 24 * 60 * 60 * 1000), }, ]);
const result = await statsRepository.getUserGrowthStats({ interval: 'week', limit: 10, });
expect(result.dataPoints).toHaveLength(2); expect(result.currentTotal).toBe(2); }); });
describe('getUserEngagementStats', () => { beforeEach(async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create test users await db.insert(users).values([ { id: 'active-user-1', handle: 'active1.test', linkedAt: baseTime, lastLoginAt: baseTime, }, { id: 'active-user-2', handle: 'active2.test', linkedAt: baseTime, lastLoginAt: baseTime, }, { id: 'inactive-user', handle: 'inactive.test', linkedAt: baseTime, lastLoginAt: baseTime, }, ]); });
it('should calculate engagement metrics for active and inactive users', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Active user 1: has URL cards await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'active-user-1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Active user 2: has collections and follows await db.insert(collections).values([ { id: '20000000-0000-0000-0000-000000000001', authorId: 'active-user-2', name: 'Test Collection', accessType: 'OPEN', createdAt: baseTime, updatedAt: baseTime, }, ]);
await db.insert(follows).values([ { followerId: 'active-user-2', targetId: 'active-user-1', targetType: 'USER', createdAt: baseTime, }, ]);
// Inactive user: no content
const result = await statsRepository.getUserEngagementStats({});
expect(result.totalUsers).toBe(3); expect(result.activeUsers).toBe(2); expect(result.inactiveUsers).toBe(1); expect(result.usersWithCards).toBe(1); expect(result.usersWithCollections).toBe(1); expect(result.usersWithFollows).toBe(1); expect(result.activationRate).toBeCloseTo(2 / 3, 2); expect(result.avgActionsPerActiveUser).toBeGreaterThan(0); expect(result.dataPoints).toBeUndefined(); // No time series by default });
it('should only count URL cards, not NOTE or HIGHLIGHT cards', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// User with URL cards (should count) await db.insert(cards).values([ { id: '50000000-0000-0000-0000-000000000001', authorId: 'active-user-1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, ]);
// User with NOTE cards (should not count as having cards) await db.insert(cards).values([ { id: '40000000-0000-0000-0000-000000000001', authorId: 'active-user-2', type: 'NOTE', contentData: { text: 'A note' }, createdAt: baseTime, updatedAt: baseTime, }, ]);
const result = await statsRepository.getUserEngagementStats({});
expect(result.totalUsers).toBe(3); expect(result.usersWithCards).toBe(1); // Only active-user-1 has URL cards expect(result.activeUsers).toBe(1); // Only active-user-1 is active });
it('should include time series data when requested', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create URL cards at different times await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'active-user-1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: new Date(baseTime.getTime()), // Day 1 updatedAt: baseTime, }, { id: '10000000-0000-0000-0000-000000000002', authorId: 'active-user-2', type: 'URL', contentData: { url: 'https://example2.com' }, url: 'https://example2.com', urlType: 'video', createdAt: new Date(baseTime.getTime() + 24 * 60 * 60 * 1000), // Day 2 updatedAt: new Date(baseTime.getTime() + 24 * 60 * 60 * 1000), }, ]);
const result = await statsRepository.getUserEngagementStats({ includeTimeSeries: true, interval: 'day', limit: 10, });
expect(result.dataPoints).toBeDefined(); expect(result.dataPoints!.length).toBeGreaterThan(0); expect(result.dataPoints![0]).toHaveProperty('date'); expect(result.dataPoints![0]).toHaveProperty('activeUsers'); expect(result.dataPoints![0]).toHaveProperty('newlyActivatedUsers'); expect(result.dataPoints![0]).toHaveProperty('cumulativeActiveUsers'); });
it('should track users with contributions to others collections', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create a collection owned by active-user-1 await db.insert(collections).values([ { id: '20000000-0000-0000-0000-000000000001', authorId: 'active-user-1', name: 'Test Collection', accessType: 'OPEN', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Create a URL card await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'active-user-2', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Active-user-2 adds a card to active-user-1's collection await db.insert(collectionCards).values([ { id: '60000000-0000-0000-0000-000000000001', collectionId: '20000000-0000-0000-0000-000000000001', cardId: '10000000-0000-0000-0000-000000000001', addedBy: 'active-user-2', // Different from collection owner addedAt: baseTime, }, ]);
const result = await statsRepository.getUserEngagementStats({});
expect(result.usersWithContributions).toBe(1); // active-user-2 contributed to someone else's collection }); });
describe('getDailyActivityStats', () => { it('should return daily activity counts for all content types', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Day 1: Create URL cards await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'user1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, { id: '10000000-0000-0000-0000-000000000002', authorId: 'user2', type: 'URL', contentData: { url: 'https://example2.com' }, url: 'https://example2.com', urlType: 'video', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Day 1: Create collections await db.insert(collections).values([ { id: '20000000-0000-0000-0000-000000000001', authorId: 'user1', name: 'Collection 1', accessType: 'OPEN', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Day 2: Create connections and follows const day2Time = new Date(baseTime.getTime() + 24 * 60 * 60 * 1000);
await db.insert(connections).values([ { id: '30000000-0000-0000-0000-000000000001', curatorId: 'user1', sourceType: 'URL', sourceValue: 'https://example.com', targetType: 'URL', targetValue: 'https://example2.com', connectionType: 'SUPPORTS', createdAt: day2Time, updatedAt: day2Time, }, ]);
await db.insert(follows).values([ { followerId: 'user1', targetId: 'user2', targetType: 'USER', createdAt: day2Time, }, ]);
const result = await statsRepository.getDailyActivityStats({ interval: 'day', limit: 10, });
expect(result.dataPoints).toHaveLength(2);
// Find day 1 and day 2 data points const day1 = result.dataPoints.find((dp) => dp.date.startsWith('2026-04-01'), ); const day2 = result.dataPoints.find((dp) => dp.date.startsWith('2026-04-02'), );
expect(day1).toBeDefined(); expect(day1?.cardsCreated).toBe(2); expect(day1?.collectionsCreated).toBe(1); expect(day1?.connectionsCreated).toBe(0); expect(day1?.followsCreated).toBe(0); expect(day1?.totalActions).toBe(3);
expect(day2).toBeDefined(); expect(day2?.cardsCreated).toBe(0); expect(day2?.collectionsCreated).toBe(0); expect(day2?.connectionsCreated).toBe(1); expect(day2?.followsCreated).toBe(1); expect(day2?.totalActions).toBe(2);
// Check totals expect(result.totals.cardsCreated).toBe(2); expect(result.totals.collectionsCreated).toBe(1); expect(result.totals.connectionsCreated).toBe(1); expect(result.totals.followsCreated).toBe(1); expect(result.totals.totalActions).toBe(5); });
it('should only count URL cards, not NOTE or HIGHLIGHT cards', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create URL cards (should count) await db.insert(cards).values([ { id: '50000000-0000-0000-0000-000000000001', authorId: 'user1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Create NOTE cards (should not count) await db.insert(cards).values([ { id: '40000000-0000-0000-0000-000000000001', authorId: 'user2', type: 'NOTE', contentData: { text: 'A note' }, createdAt: baseTime, updatedAt: baseTime, }, ]);
const result = await statsRepository.getDailyActivityStats({ interval: 'day', limit: 10, });
expect(result.dataPoints).toHaveLength(1); expect(result.dataPoints[0]?.cardsCreated).toBe(1); // Only URL card expect(result.totals.cardsCreated).toBe(1); });
it('should return empty data when no activity exists', async () => { const result = await statsRepository.getDailyActivityStats({ interval: 'day', limit: 10, });
expect(result.dataPoints).toHaveLength(0); expect(result.totals.cardsCreated).toBe(0); expect(result.totals.totalActions).toBe(0); });
it('should support week interval aggregation', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create cards in two different weeks await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'user1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, // Week 1 updatedAt: baseTime, }, { id: '10000000-0000-0000-0000-000000000002', authorId: 'user2', type: 'URL', contentData: { url: 'https://example2.com' }, url: 'https://example2.com', urlType: 'video', createdAt: new Date(baseTime.getTime() + 7 * 24 * 60 * 60 * 1000), // Week 2 updatedAt: new Date(baseTime.getTime() + 7 * 24 * 60 * 60 * 1000), }, ]);
const result = await statsRepository.getDailyActivityStats({ interval: 'week', limit: 10, });
expect(result.dataPoints).toHaveLength(2); expect(result.totals.cardsCreated).toBe(2); }); });
describe('getContentBreakdownStats', () => { it('should return breakdown of URL cards by type over time', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Day 1: Create URL cards of different types await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'user1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, { id: '10000000-0000-0000-0000-000000000002', authorId: 'user2', type: 'URL', contentData: { url: 'https://example2.com' }, url: 'https://example2.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, { id: '10000000-0000-0000-0000-000000000003', authorId: 'user3', type: 'URL', contentData: { url: 'https://example3.com' }, url: 'https://example3.com', urlType: 'video', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Day 2: Create more URL cards const day2Time = new Date(baseTime.getTime() + 24 * 60 * 60 * 1000); await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000004', authorId: 'user1', type: 'URL', contentData: { url: 'https://example4.com' }, url: 'https://example4.com', urlType: 'tool', createdAt: day2Time, updatedAt: day2Time, }, ]);
const result = await statsRepository.getContentBreakdownStats({ interval: 'day', limit: 10, });
expect(result.dataPoints.length).toBeGreaterThan(0);
// Check current totals expect(result.currentTotals.urlCards.total).toBe(4); expect(result.currentTotals.urlCards.byType['article']).toBe(2); expect(result.currentTotals.urlCards.byType['video']).toBe(1); expect(result.currentTotals.urlCards.byType['tool']).toBe(1); });
it('should return breakdown of collections by access type', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create collections with different access types await db.insert(collections).values([ { id: '20000000-0000-0000-0000-000000000001', authorId: 'user1', name: 'Open Collection 1', accessType: 'OPEN', createdAt: baseTime, updatedAt: baseTime, }, { id: '20000000-0000-0000-0000-000000000002', authorId: 'user2', name: 'Open Collection 2', accessType: 'OPEN', createdAt: baseTime, updatedAt: baseTime, }, { id: '20000000-0000-0000-0000-000000000003', authorId: 'user3', name: 'Closed Collection', accessType: 'CLOSED', createdAt: baseTime, updatedAt: baseTime, }, ]);
const result = await statsRepository.getContentBreakdownStats({ interval: 'day', limit: 10, });
expect(result.currentTotals.collections.total).toBe(3); expect(result.currentTotals.collections.byAccessType['OPEN']).toBe(2); expect(result.currentTotals.collections.byAccessType['CLOSED']).toBe(1); });
it('should return breakdown of connections by type', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create connections with different types await db.insert(connections).values([ { id: '30000000-0000-0000-0000-000000000001', curatorId: 'user1', sourceType: 'URL', sourceValue: 'https://example.com', targetType: 'URL', targetValue: 'https://example2.com', connectionType: 'SUPPORTS', createdAt: baseTime, updatedAt: baseTime, }, { id: '30000000-0000-0000-0000-000000000002', curatorId: 'user2', sourceType: 'URL', sourceValue: 'https://example3.com', targetType: 'URL', targetValue: 'https://example4.com', connectionType: 'SUPPORTS', createdAt: baseTime, updatedAt: baseTime, }, { id: '30000000-0000-0000-0000-000000000003', curatorId: 'user3', sourceType: 'URL', sourceValue: 'https://example5.com', targetType: 'URL', targetValue: 'https://example6.com', connectionType: 'OPPOSES', createdAt: baseTime, updatedAt: baseTime, }, { id: '30000000-0000-0000-0000-000000000004', curatorId: 'user1', sourceType: 'URL', sourceValue: 'https://example7.com', targetType: 'URL', targetValue: 'https://example8.com', connectionType: null, // Should map to 'unspecified' createdAt: baseTime, updatedAt: baseTime, }, ]);
const result = await statsRepository.getContentBreakdownStats({ interval: 'day', limit: 10, });
expect(result.currentTotals.connections.total).toBe(4); expect(result.currentTotals.connections.byType['SUPPORTS']).toBe(2); expect(result.currentTotals.connections.byType['OPPOSES']).toBe(1); expect(result.currentTotals.connections.byType['unspecified']).toBe(1); });
it('should only count URL cards, not NOTE or HIGHLIGHT cards', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create URL cards (should count) await db.insert(cards).values([ { id: '50000000-0000-0000-0000-000000000001', authorId: 'user1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Create NOTE cards (should not count) await db.insert(cards).values([ { id: '40000000-0000-0000-0000-000000000001', authorId: 'user2', type: 'NOTE', contentData: { text: 'A note' }, createdAt: baseTime, updatedAt: baseTime, }, ]);
const result = await statsRepository.getContentBreakdownStats({ interval: 'day', limit: 10, });
expect(result.currentTotals.urlCards.total).toBe(1); // Only URL card });
it('should handle null urlType as unspecified', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Create URL card with null urlType await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'user1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: null, // Should map to 'unspecified' createdAt: baseTime, updatedAt: baseTime, }, ]);
const result = await statsRepository.getContentBreakdownStats({ interval: 'day', limit: 10, });
expect(result.currentTotals.urlCards.total).toBe(1); expect(result.currentTotals.urlCards.byType['unspecified']).toBe(1); });
it('should show cumulative growth over time periods', async () => { const baseTime = new Date('2026-04-01T00:00:00Z');
// Day 1: Create 2 URL cards await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000001', authorId: 'user1', type: 'URL', contentData: { url: 'https://example.com' }, url: 'https://example.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, { id: '10000000-0000-0000-0000-000000000002', authorId: 'user2', type: 'URL', contentData: { url: 'https://example2.com' }, url: 'https://example2.com', urlType: 'article', createdAt: baseTime, updatedAt: baseTime, }, ]);
// Day 2: Create 1 more URL card const day2Time = new Date(baseTime.getTime() + 24 * 60 * 60 * 1000); await db.insert(cards).values([ { id: '10000000-0000-0000-0000-000000000003', authorId: 'user1', type: 'URL', contentData: { url: 'https://example3.com' }, url: 'https://example3.com', urlType: 'video', createdAt: day2Time, updatedAt: day2Time, }, ]);
const result = await statsRepository.getContentBreakdownStats({ interval: 'day', limit: 10, });
expect(result.dataPoints.length).toBeGreaterThanOrEqual(2);
// Data should be in chronological order const sortedDataPoints = [...result.dataPoints].sort( (a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(), );
// First period should have cumulative count const firstPeriod = sortedDataPoints[0]; expect(firstPeriod).toBeDefined();
// Last period should have cumulative total matching current totals expect(result.currentTotals.urlCards.total).toBe(3); });
it('should return empty data when no content exists', async () => { const result = await statsRepository.getContentBreakdownStats({ interval: 'day', limit: 10, });
expect(result.dataPoints).toHaveLength(0); expect(result.currentTotals.urlCards.total).toBe(0); expect(result.currentTotals.collections.total).toBe(0); expect(result.currentTotals.connections.total).toBe(0); }); });});