|
| 1 | +const { assertTableContents, describeMigration, rowsExistFor } = require('./utils'); |
| 2 | + |
| 3 | +describeMigration('20250910-02-backfill-last-login-at', ({ runMigrationBeingTested }) => { |
| 4 | + before(async () => { |
| 5 | + // Set up test data: create actors, users, and audit records |
| 6 | + await rowsExistFor('actees', |
| 7 | + { id: 'actee1', species: 'user' }, |
| 8 | + { id: 'actee2', species: 'user' }, |
| 9 | + { id: 'actee3', species: 'user' } |
| 10 | + ); |
| 11 | + |
| 12 | + await rowsExistFor('actors', |
| 13 | + { id: 1, type: 'user', acteeId: 'actee1', displayName: 'Alice', createdAt: new Date('2025-01-01T10:00:00Z') }, |
| 14 | + { id: 2, type: 'user', acteeId: 'actee2', displayName: 'Bob', createdAt: new Date('2025-01-01T11:00:00Z') }, |
| 15 | + { id: 3, type: 'user', acteeId: 'actee3', displayName: 'Charlie', createdAt: new Date('2025-01-01T12:00:00Z') } |
| 16 | + ); |
| 17 | + |
| 18 | + await rowsExistFor('users', |
| 19 | + { actorId: 1, email: '[email protected]', lastLoginAt: null }, |
| 20 | + { actorId: 2, email: '[email protected]', lastLoginAt: null }, |
| 21 | + { actorId: 3, email: '[email protected]', lastLoginAt: null } |
| 22 | + ); |
| 23 | + |
| 24 | + // Create audit records - Alice has multiple logins, Bob has one, Charlie has none |
| 25 | + await rowsExistFor('audits', |
| 26 | + // Alice's login sessions (most recent should be picked) |
| 27 | + { actorId: 1, action: 'user.session.create', acteeId: 'actee1', loggedAt: new Date('2025-01-10T10:00:00Z') }, |
| 28 | + { actorId: 1, action: 'user.session.create', acteeId: 'actee1', loggedAt: new Date('2025-01-15T14:30:00Z') }, |
| 29 | + { actorId: 1, action: 'user.session.create', acteeId: 'actee1', loggedAt: new Date('2025-01-12T09:15:00Z') }, |
| 30 | + |
| 31 | + // Bob's single login session |
| 32 | + { actorId: 2, action: 'user.session.create', acteeId: 'actee2', loggedAt: new Date('2025-01-08T16:45:00Z') }, |
| 33 | + ); |
| 34 | + |
| 35 | + await runMigrationBeingTested(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should backfill lastLoginAt with most recent login timestamp for users with login history', async () => { |
| 39 | + await assertTableContents('users', |
| 40 | + // Alice should get her most recent login time (2025-01-15T14:30:00Z) |
| 41 | + { actorId: 1, email: '[email protected]', lastLoginAt: 1736969400000 }, |
| 42 | + |
| 43 | + // Bob should get his only login time (2025-01-08T16:45:00Z) |
| 44 | + { actorId: 2, email: '[email protected]', lastLoginAt: 1736372700000 }, |
| 45 | + |
| 46 | + // Charlie should remain null (never logged in) |
| 47 | + { actorId: 3, email: '[email protected]', lastLoginAt: null } |
| 48 | + ); |
| 49 | + }); |
| 50 | +}); |
0 commit comments