fix issues

This commit is contained in:
2026-01-02 00:21:24 +01:00
parent 79913bb45e
commit 8693dde21e
46 changed files with 1680 additions and 302 deletions

View File

@@ -196,9 +196,20 @@ describe('AdminVoteSession', () => {
});
it('should throw error if session is closed', () => {
session.close();
// Close the session by first casting votes within the window, then closing
// But we need to be within the voting window, so use the current date
const currentSession = AdminVoteSession.create({
voteSessionId: 'vote-123',
leagueId: 'league-456',
adminId: 'admin-789',
startDate: new Date(Date.now() - 86400000), // Yesterday
endDate: new Date(Date.now() + 86400000), // Tomorrow
eligibleVoters: ['user-1', 'user-2', 'user-3'],
});
expect(() => session.castVote('user-1', true, now))
currentSession.close();
expect(() => currentSession.castVote('user-1', true, new Date()))
.toThrow(IdentityDomainInvariantError);
});
@@ -213,12 +224,30 @@ describe('AdminVoteSession', () => {
});
it('should update updatedAt timestamp', () => {
const originalUpdatedAt = session.updatedAt;
// Create a session with explicit timestamps
const createdAt = new Date('2025-01-01T00:00:00Z');
const updatedAt = new Date('2025-01-01T00:00:00Z');
const sessionWithTimestamps = AdminVoteSession.rehydrate({
voteSessionId: 'vote-123',
leagueId: 'league-456',
adminId: 'admin-789',
startDate: now,
endDate: tomorrow,
eligibleVoters: ['user-1', 'user-2', 'user-3'],
votes: [],
closed: false,
createdAt: createdAt,
updatedAt: updatedAt,
});
const originalUpdatedAt = sessionWithTimestamps.updatedAt;
// Wait a tiny bit to ensure different timestamp
const voteTime = new Date(now.getTime() + 1000);
sessionWithTimestamps.castVote('user-1', true, voteTime);
session.castVote('user-1', true, voteTime);
expect(session.updatedAt).not.toEqual(originalUpdatedAt);
// The updatedAt should be different (set to current time when vote is cast)
expect(sessionWithTimestamps.updatedAt.getTime()).toBeGreaterThan(originalUpdatedAt.getTime());
});
});
@@ -226,20 +255,25 @@ describe('AdminVoteSession', () => {
let session: AdminVoteSession;
beforeEach(() => {
// Use current dates so close() works
const startDate = new Date(Date.now() - 86400000); // Yesterday
const endDate = new Date(Date.now() + 86400000); // Tomorrow
session = AdminVoteSession.create({
voteSessionId: 'vote-123',
leagueId: 'league-456',
adminId: 'admin-789',
startDate: now,
endDate: tomorrow,
startDate,
endDate,
eligibleVoters: ['user-1', 'user-2', 'user-3', 'user-4'],
});
});
it('should close session and calculate positive outcome', () => {
session.castVote('user-1', true, now);
session.castVote('user-2', true, now);
session.castVote('user-3', false, now);
const voteTime = new Date();
session.castVote('user-1', true, voteTime);
session.castVote('user-2', true, voteTime);
session.castVote('user-3', false, voteTime);
const outcome = session.close();
@@ -254,9 +288,10 @@ describe('AdminVoteSession', () => {
});
it('should calculate negative outcome', () => {
session.castVote('user-1', false, now);
session.castVote('user-2', false, now);
session.castVote('user-3', true, now);
const voteTime = new Date();
session.castVote('user-1', false, voteTime);
session.castVote('user-2', false, voteTime);
session.castVote('user-3', true, voteTime);
const outcome = session.close();
@@ -265,8 +300,9 @@ describe('AdminVoteSession', () => {
});
it('should calculate tie outcome', () => {
session.castVote('user-1', true, now);
session.castVote('user-2', false, now);
const voteTime = new Date();
session.castVote('user-1', true, voteTime);
session.castVote('user-2', false, voteTime);
const outcome = session.close();
@@ -306,10 +342,11 @@ describe('AdminVoteSession', () => {
});
it('should round percentPositive to 2 decimal places', () => {
session.castVote('user-1', true, now);
session.castVote('user-2', true, now);
session.castVote('user-3', true, now);
session.castVote('user-4', false, now);
const voteTime = new Date();
session.castVote('user-1', true, voteTime);
session.castVote('user-2', true, voteTime);
session.castVote('user-3', true, voteTime);
session.castVote('user-4', false, voteTime);
const outcome = session.close();
@@ -321,19 +358,24 @@ describe('AdminVoteSession', () => {
let session: AdminVoteSession;
beforeEach(() => {
// Use current dates so close() works
const startDate = new Date(Date.now() - 86400000); // Yesterday
const endDate = new Date(Date.now() + 86400000); // Tomorrow
session = AdminVoteSession.create({
voteSessionId: 'vote-123',
leagueId: 'league-456',
adminId: 'admin-789',
startDate: now,
endDate: tomorrow,
startDate,
endDate,
eligibleVoters: ['user-1', 'user-2'],
});
});
describe('hasVoted', () => {
it('should return true if voter has voted', () => {
session.castVote('user-1', true, now);
const voteTime = new Date();
session.castVote('user-1', true, voteTime);
expect(session.hasVoted('user-1')).toBe(true);
});
@@ -344,7 +386,8 @@ describe('AdminVoteSession', () => {
describe('getVote', () => {
it('should return vote if exists', () => {
session.castVote('user-1', true, now);
const voteTime = new Date();
session.castVote('user-1', true, voteTime);
const vote = session.getVote('user-1');
expect(vote).toBeDefined();
@@ -361,51 +404,61 @@ describe('AdminVoteSession', () => {
it('should return correct count', () => {
expect(session.getVoteCount()).toBe(0);
session.castVote('user-1', true, now);
const voteTime = new Date();
session.castVote('user-1', true, voteTime);
expect(session.getVoteCount()).toBe(1);
session.castVote('user-2', false, now);
session.castVote('user-2', false, voteTime);
expect(session.getVoteCount()).toBe(2);
});
});
describe('isVotingWindowOpen', () => {
it('should return true during voting window', () => {
expect(session.isVotingWindowOpen(now)).toBe(true);
const voteTime = new Date();
expect(session.isVotingWindowOpen(voteTime)).toBe(true);
const midPoint = new Date((now.getTime() + tomorrow.getTime()) / 2);
// Midpoint of the voting window
const sessionStart = session.startDate.getTime();
const sessionEnd = session.endDate.getTime();
const midPoint = new Date((sessionStart + sessionEnd) / 2);
expect(session.isVotingWindowOpen(midPoint)).toBe(true);
});
it('should return false before voting window', () => {
const before = new Date('2024-12-31T23:59:59Z');
const before = new Date(Date.now() - 86400000 * 2); // 2 days ago
expect(session.isVotingWindowOpen(before)).toBe(false);
});
it('should return false after voting window', () => {
const after = new Date('2025-01-02T00:00:01Z');
const after = new Date(Date.now() + 86400000 * 2); // 2 days from now
expect(session.isVotingWindowOpen(after)).toBe(false);
});
it('should return false if session is closed', () => {
session.close();
expect(session.isVotingWindowOpen(now)).toBe(false);
expect(session.isVotingWindowOpen(new Date())).toBe(false);
});
});
});
describe('toJSON', () => {
it('should serialize to JSON correctly', () => {
// Use current dates so close() works
const startDate = new Date(Date.now() - 86400000); // Yesterday
const endDate = new Date(Date.now() + 86400000); // Tomorrow
const session = AdminVoteSession.create({
voteSessionId: 'vote-123',
leagueId: 'league-456',
adminId: 'admin-789',
startDate: now,
endDate: tomorrow,
startDate,
endDate,
eligibleVoters: ['user-1', 'user-2'],
});
session.castVote('user-1', true, now);
const voteTime = new Date();
session.castVote('user-1', true, voteTime);
session.close();
const json = session.toJSON();