import { describe, it, expect } from 'vitest'; import { shouldAcceptScore, resolveConflict } from '../conflict.js'; import { Score } from '../../shared/types.js'; function makeScore(overrides: Partial = {}): Score { return { jurorId: 'finke', teamId: 'dual', criterionId: 'gesamtidee', value: 7, updatedAt: '2026-05-19T18:00:00.000Z', ...overrides, }; } describe('shouldAcceptScore', () => { it('accepts when no existing score', () => { const incoming = makeScore(); const result = shouldAcceptScore(incoming, undefined); expect(result).toEqual({ accept: true }); }); it('accepts when incoming timestamp is newer', () => { const existing = makeScore({ updatedAt: '2026-05-19T18:00:00.000Z' }); const incoming = makeScore({ updatedAt: '2026-05-19T18:01:00.000Z', value: 9 }); const result = shouldAcceptScore(incoming, existing); expect(result).toEqual({ accept: true }); }); it('rejects when incoming timestamp is older', () => { const existing = makeScore({ updatedAt: '2026-05-19T18:01:00.000Z' }); const incoming = makeScore({ updatedAt: '2026-05-19T18:00:00.000Z', value: 3 }); const result = shouldAcceptScore(incoming, existing); expect(result).toEqual({ accept: false, reason: 'Newer score already exists' }); }); it('rejects when timestamps are equal', () => { const existing = makeScore({ updatedAt: '2026-05-19T18:00:00.000Z' }); const incoming = makeScore({ updatedAt: '2026-05-19T18:00:00.000Z', value: 5 }); const result = shouldAcceptScore(incoming, existing); expect(result).toEqual({ accept: false, reason: 'Newer score already exists' }); }); }); describe('resolveConflict', () => { it('accepts when force is true regardless of timestamps', () => { const existing = makeScore({ updatedAt: '2026-05-19T18:01:00.000Z' }); const incoming = makeScore({ updatedAt: '2026-05-19T18:00:00.000Z', value: 2 }); const result = resolveConflict(incoming, existing, true); expect(result).toEqual({ accept: true }); }); it('delegates to shouldAcceptScore when force is false', () => { const existing = makeScore({ updatedAt: '2026-05-19T18:01:00.000Z' }); const incoming = makeScore({ updatedAt: '2026-05-19T18:00:00.000Z', value: 2 }); const result = resolveConflict(incoming, existing, false); expect(result).toEqual({ accept: false, reason: 'Newer score already exists' }); }); it('delegates to shouldAcceptScore when force is undefined', () => { const incoming = makeScore(); const result = resolveConflict(incoming, undefined); expect(result).toEqual({ accept: true }); }); it('accepts with force even when no existing score', () => { const incoming = makeScore(); const result = resolveConflict(incoming, undefined, true); expect(result).toEqual({ accept: true }); }); });