import { describe, expect, it } from 'vitest' import { B2B_THRESHOLD_MIN, findB2BLinks, findB2BSides } from '@/lib/timetable/b2b' import type { Performance } from '@/types/timetable' function p(overrides: Partial = {}): Performance { return { id: 'p1', engagement_id: 'e1', event_id: 'ev1', stage_id: 's1', lane: 0, lane_resolved: 0, start_at: '2026-07-10T18:00:00.000Z', end_at: '2026-07-10T19:00:00.000Z', version: 0, notes: null, warnings: [], created_at: null, updated_at: null, deleted_at: null, ...overrides, } } describe('findB2BLinks', () => { it('returns empty when no consecutive pair exists', () => { expect(findB2BLinks([p({ id: 'a' })])).toEqual([]) }) it('marks 0-min gap as B2B', () => { const links = findB2BLinks([ p({ id: 'a', start_at: '2026-07-10T18:00:00Z', end_at: '2026-07-10T19:00:00Z' }), p({ id: 'b', start_at: '2026-07-10T19:00:00Z', end_at: '2026-07-10T20:00:00Z' }), ]) expect(links).toHaveLength(1) expect(links[0]).toEqual({ leftId: 'a', rightId: 'b', gapMin: 0 }) }) it('marks 2:59 gap as B2B (under 3-min threshold)', () => { const links = findB2BLinks([ p({ id: 'a', start_at: '2026-07-10T18:00:00Z', end_at: '2026-07-10T19:00:00Z' }), p({ id: 'b', start_at: '2026-07-10T19:02:59Z', end_at: '2026-07-10T20:00:00Z' }), ]) expect(links).toHaveLength(1) }) it('does NOT mark 3:01 gap as B2B', () => { const links = findB2BLinks([ p({ id: 'a', start_at: '2026-07-10T18:00:00Z', end_at: '2026-07-10T19:00:00Z' }), p({ id: 'b', start_at: '2026-07-10T19:03:01Z', end_at: '2026-07-10T20:00:00Z' }), ]) expect(links).toHaveLength(0) }) it('overlap (negative gap) is NOT a B2B link', () => { const links = findB2BLinks([ p({ id: 'a', start_at: '2026-07-10T18:00:00Z', end_at: '2026-07-10T19:00:00Z' }), p({ id: 'b', start_at: '2026-07-10T18:30:00Z', end_at: '2026-07-10T19:30:00Z' }), ]) expect(links).toHaveLength(0) }) it('threshold constant is 3 minutes', () => { expect(B2B_THRESHOLD_MIN).toBe(3) }) }) describe('findB2BSides', () => { it('produces left+right sets reflecting neighbour position', () => { const { leftSet, rightSet } = findB2BSides([ p({ id: 'a', start_at: '2026-07-10T18:00:00Z', end_at: '2026-07-10T19:00:00Z' }), p({ id: 'b', start_at: '2026-07-10T19:00:00Z', end_at: '2026-07-10T20:00:00Z' }), ]) expect(rightSet.has('a')).toBe(true) expect(leftSet.has('b')).toBe(true) expect(leftSet.has('a')).toBe(false) expect(rightSet.has('b')).toBe(false) }) })