summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models/relative_positioning_shared_examples.rb
blob: e4668926d743528471e6545e7cb6c072a94f28e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# frozen_string_literal: true

RSpec.shared_examples 'a class that supports relative positioning' do
  let(:item1) { create_item }
  let(:item2) { create_item }
  let(:new_item) { create_item }

  def create_item(params = {})
    create(factory, params.merge(default_params))
  end

  def create_items_with_positions(positions)
    positions.map do |position|
      create_item(relative_position: position)
    end
  end

  describe '.move_nulls_to_end' do
    let(:item3) { create_item }

    it 'moves items with null relative_position to the end' do
      item1.update!(relative_position: 1000)
      item2.update!(relative_position: nil)
      item3.update!(relative_position: nil)

      items = [item1, item2, item3]
      expect(described_class.move_nulls_to_end(items)).to be(2)

      expect(items.sort_by(&:relative_position)).to eq(items)
      expect(item1.relative_position).to be(1000)
      expect(item1.prev_relative_position).to be_nil
      expect(item1.next_relative_position).to eq(item2.relative_position)
      expect(item2.next_relative_position).to eq(item3.relative_position)
      expect(item3.next_relative_position).to be_nil
    end

    it 'preserves relative position' do
      item1.update!(relative_position: nil)
      item2.update!(relative_position: nil)

      described_class.move_nulls_to_end([item1, item2])

      expect(item1.relative_position).to be < item2.relative_position
    end

    it 'moves the item near the start position when there are no existing positions' do
      item1.update!(relative_position: nil)

      described_class.move_nulls_to_end([item1])
      expect(item1.reset.relative_position).to eq(described_class::START_POSITION + described_class::IDEAL_DISTANCE)
    end

    it 'does not perform any moves if all items have their relative_position set' do
      item1.update!(relative_position: 1)

      expect(described_class.move_nulls_to_start([item1])).to be(0)
      expect(item1.reload.relative_position).to be(1)
    end

    it 'manages to move nulls to the end even if there is a sequence at the end' do
      bunch = create_items_with_positions(run_at_end)
      item1.update!(relative_position: nil)

      described_class.move_nulls_to_end([item1])

      items = [*bunch, item1]
      items.each(&:reset)

      expect(items.map(&:relative_position)).to all(be_valid_position)
      expect(items.sort_by(&:relative_position)).to eq(items)
    end

    it 'does not have an N+1 issue' do
      create_items_with_positions(10..12)

      a, b, c, d, e, f = create_items_with_positions([nil, nil, nil, nil, nil, nil])

      baseline = ActiveRecord::QueryRecorder.new do
        described_class.move_nulls_to_end([a, e])
      end

      expect { described_class.move_nulls_to_end([b, c, d]) }
        .not_to exceed_query_limit(baseline)

      expect { described_class.move_nulls_to_end([f]) }
        .not_to exceed_query_limit(baseline.count)
    end
  end

  describe '.move_nulls_to_start' do
    let(:item3) { create_item }

    it 'moves items with null relative_position to the start' do
      item1.update!(relative_position: nil)
      item2.update!(relative_position: nil)
      item3.update!(relative_position: 1000)

      items = [item1, item2, item3]
      expect(described_class.move_nulls_to_start(items)).to be(2)
      items.map(&:reload)

      expect(items.sort_by(&:relative_position)).to eq(items)
      expect(item1.prev_relative_position).to eq nil
      expect(item1.next_relative_position).to eq item2.relative_position
      expect(item2.next_relative_position).to eq item3.relative_position
      expect(item3.next_relative_position).to eq nil
      expect(item3.relative_position).to be(1000)
    end

    it 'moves the item near the start position when there are no existing positions' do
      item1.update!(relative_position: nil)

      described_class.move_nulls_to_start([item1])

      expect(item1.relative_position).to eq(described_class::START_POSITION - described_class::IDEAL_DISTANCE)
    end

    it 'preserves relative position' do
      item1.update!(relative_position: nil)
      item2.update!(relative_position: nil)

      described_class.move_nulls_to_start([item1, item2])

      expect(item1.relative_position).to be < item2.relative_position
    end

    it 'does not perform any moves if all items have their relative_position set' do
      item1.update!(relative_position: 1)

      expect(described_class.move_nulls_to_start([item1])).to be(0)
      expect(item1.reload.relative_position).to be(1)
    end
  end

  describe '#max_relative_position' do
    it 'returns maximum position' do
      expect(item1.max_relative_position).to eq item2.relative_position
    end
  end

  describe '#prev_relative_position' do
    it 'returns previous position if there is an item above' do
      item1.update!(relative_position: 5)
      item2.update!(relative_position: 15)

      expect(item2.prev_relative_position).to eq item1.relative_position
    end

    it 'returns nil if there is no item above' do
      expect(item1.prev_relative_position).to eq nil
    end
  end

  describe '#next_relative_position' do
    it 'returns next position if there is an item below' do
      item1.update!(relative_position: 5)
      item2.update!(relative_position: 15)

      expect(item1.next_relative_position).to eq item2.relative_position
    end

    it 'returns nil if there is no item below' do
      expect(item2.next_relative_position).to eq nil
    end
  end

  describe '#find_next_gap_before' do
    context 'there is no gap' do
      let(:items) { create_items_with_positions(run_at_start) }

      it 'returns nil' do
        items.each do |item|
          expect(item.send(:find_next_gap_before)).to be_nil
        end
      end
    end

    context 'there is a sequence ending at MAX_POSITION' do
      let(:items) { create_items_with_positions(run_at_end) }

      let(:gaps) do
        items.map { |item| item.send(:find_next_gap_before) }
      end

      it 'can find the gap at the start for any item in the sequence' do
        gap = { start: items.first.relative_position, end: RelativePositioning::MIN_POSITION }

        expect(gaps).to all(eq(gap))
      end

      it 'respects lower bounds' do
        gap = { start: items.first.relative_position, end: 10 }
        new_item.update!(relative_position: 10)

        expect(gaps).to all(eq(gap))
      end
    end

    specify do
      item1.update!(relative_position: 5)

      (0..10).each do |pos|
        item2.update!(relative_position: pos)

        gap = item2.send(:find_next_gap_before)

        expect(gap[:start]).to be <= item2.relative_position
        expect((gap[:end] - gap[:start]).abs).to be >= RelativePositioning::MIN_GAP
        expect(gap[:start]).to be_valid_position
        expect(gap[:end]).to be_valid_position
      end
    end

    it 'deals with there not being any items to the left' do
      create_items_with_positions([1, 2, 3])
      new_item.update!(relative_position: 0)

      expect(new_item.send(:find_next_gap_before)).to eq(start: 0, end: RelativePositioning::MIN_POSITION)
    end

    it 'finds the next gap to the left, skipping adjacent values' do
      create_items_with_positions([1, 9, 10])
      new_item.update!(relative_position: 11)

      expect(new_item.send(:find_next_gap_before)).to eq(start: 9, end: 1)
    end

    it 'finds the next gap to the left' do
      create_items_with_positions([2, 10])

      new_item.update!(relative_position: 15)
      expect(new_item.send(:find_next_gap_before)).to eq(start: 15, end: 10)

      new_item.update!(relative_position: 11)
      expect(new_item.send(:find_next_gap_before)).to eq(start: 10, end: 2)

      new_item.update!(relative_position: 9)
      expect(new_item.send(:find_next_gap_before)).to eq(start: 9, end: 2)

      new_item.update!(relative_position: 5)
      expect(new_item.send(:find_next_gap_before)).to eq(start: 5, end: 2)
    end
  end

  describe '#find_next_gap_after' do
    context 'there is no gap' do
      let(:items) { create_items_with_positions(run_at_end) }

      it 'returns nil' do
        items.each do |item|
          expect(item.send(:find_next_gap_after)).to be_nil
        end
      end
    end

    context 'there is a sequence starting at MIN_POSITION' do
      let(:items) { create_items_with_positions(run_at_start) }

      let(:gaps) do
        items.map { |item| item.send(:find_next_gap_after) }
      end

      it 'can find the gap at the end for any item in the sequence' do
        gap = { start: items.last.relative_position, end: RelativePositioning::MAX_POSITION }

        expect(gaps).to all(eq(gap))
      end

      it 'respects upper bounds' do
        gap = { start: items.last.relative_position, end: 10 }
        new_item.update!(relative_position: 10)

        expect(gaps).to all(eq(gap))
      end
    end

    specify do
      item1.update!(relative_position: 5)

      (0..10).each do |pos|
        item2.update!(relative_position: pos)

        gap = item2.send(:find_next_gap_after)

        expect(gap[:start]).to be >= item2.relative_position
        expect((gap[:end] - gap[:start]).abs).to be >= RelativePositioning::MIN_GAP
        expect(gap[:start]).to be_valid_position
        expect(gap[:end]).to be_valid_position
      end
    end

    it 'deals with there not being any items to the right' do
      create_items_with_positions([1, 2, 3])
      new_item.update!(relative_position: 5)

      expect(new_item.send(:find_next_gap_after)).to eq(start: 5, end: RelativePositioning::MAX_POSITION)
    end

    it 'finds the next gap to the right, skipping adjacent values' do
      create_items_with_positions([1, 2, 10])
      new_item.update!(relative_position: 0)

      expect(new_item.send(:find_next_gap_after)).to eq(start: 2, end: 10)
    end

    it 'finds the next gap to the right' do
      create_items_with_positions([2, 10])

      new_item.update!(relative_position: 0)
      expect(new_item.send(:find_next_gap_after)).to eq(start: 0, end: 2)

      new_item.update!(relative_position: 1)
      expect(new_item.send(:find_next_gap_after)).to eq(start: 2, end: 10)

      new_item.update!(relative_position: 3)
      expect(new_item.send(:find_next_gap_after)).to eq(start: 3, end: 10)

      new_item.update!(relative_position: 5)
      expect(new_item.send(:find_next_gap_after)).to eq(start: 5, end: 10)
    end
  end

  describe '#move_before' do
    let(:item3) { create(factory, default_params) }

    it 'moves item before' do
      [item2, item1].each do |item|
        item.move_to_end
        item.save!
      end

      expect(item1.relative_position).to be > item2.relative_position

      item1.move_before(item2)

      expect(item1.relative_position).to be < item2.relative_position
    end

    context 'when there is no space' do
      before do
        item1.update!(relative_position: 1000)
        item2.update!(relative_position: 1001)
        item3.update!(relative_position: 1002)
      end

      it 'moves items correctly' do
        item3.move_before(item2)

        expect(item3.relative_position).to be_between(item1.reload.relative_position, item2.reload.relative_position).exclusive
      end
    end

    it 'can move the item before an item at the start' do
      item1.update!(relative_position: RelativePositioning::START_POSITION)

      new_item.move_before(item1)

      expect(new_item.relative_position).to be_valid_position
      expect(new_item.relative_position).to be < item1.reload.relative_position
    end

    it 'can move the item before an item at MIN_POSITION' do
      item1.update!(relative_position: RelativePositioning::MIN_POSITION)

      new_item.move_before(item1)

      expect(new_item.relative_position).to be >= RelativePositioning::MIN_POSITION
      expect(new_item.relative_position).to be < item1.reload.relative_position
    end

    it 'can move the item before an item bunched up at MIN_POSITION' do
      item1, item2, item3 = create_items_with_positions(run_at_start)

      new_item.move_before(item3)
      new_item.save!

      items = [item1, item2, new_item, item3]

      items.each do |item|
        expect(item.reset.relative_position).to be_valid_position
      end

      expect(items.sort_by(&:relative_position)).to eq(items)
    end

    context 'leap-frogging to the left' do
      before do
        start = RelativePositioning::START_POSITION
        item1.update!(relative_position: start - RelativePositioning::IDEAL_DISTANCE * 0)
        item2.update!(relative_position: start - RelativePositioning::IDEAL_DISTANCE * 1)
        item3.update!(relative_position: start - RelativePositioning::IDEAL_DISTANCE * 2)
      end

      let(:item3) { create(factory, default_params) }

      def leap_frog(steps)
        a = item1
        b = item2

        steps.times do |i|
          a.move_before(b)
          a.save!
          a, b = b, a
        end
      end

      it 'can leap-frog STEPS - 1 times before needing to rebalance' do
        # This is less efficient than going right, due to the flooring of
        # integer division
        expect { leap_frog(RelativePositioning::STEPS - 1) }
          .not_to change { item3.reload.relative_position }
      end

      it 'rebalances after leap-frogging STEPS times' do
        expect { leap_frog(RelativePositioning::STEPS) }
          .to change { item3.reload.relative_position }
      end
    end
  end

  describe '#move_after' do
    it 'moves item after' do
      [item1, item2].each(&:move_to_end)

      item1.move_after(item2)

      expect(item1.relative_position).to be > item2.relative_position
    end

    context 'when there is no space' do
      let(:item3) { create(factory, default_params) }

      before do
        item1.update!(relative_position: 1000)
        item2.update!(relative_position: 1001)
        item3.update!(relative_position: 1002)
      end

      it 'can move the item after an item at MAX_POSITION' do
        item1.update!(relative_position: RelativePositioning::MAX_POSITION)

        new_item.move_after(item1)
        expect(new_item.relative_position).to be_valid_position
        expect(new_item.relative_position).to be > item1.reset.relative_position
      end

      it 'moves items correctly' do
        item1.move_after(item2)

        expect(item1.relative_position).to be_between(item2.reload.relative_position, item3.reload.relative_position).exclusive
      end
    end

    it 'can move the item after an item bunched up at MAX_POSITION' do
      item1, item2, item3 = create_items_with_positions(run_at_end)

      new_item.move_after(item1)
      new_item.save!

      items = [item1, new_item, item2, item3]

      items.each do |item|
        expect(item.reset.relative_position).to be_valid_position
      end

      expect(items.sort_by(&:relative_position)).to eq(items)
    end

    context 'leap-frogging' do
      before do
        start = RelativePositioning::START_POSITION
        item1.update!(relative_position: start + RelativePositioning::IDEAL_DISTANCE * 0)
        item2.update!(relative_position: start + RelativePositioning::IDEAL_DISTANCE * 1)
        item3.update!(relative_position: start + RelativePositioning::IDEAL_DISTANCE * 2)
      end

      let(:item3) { create(factory, default_params) }

      def leap_frog(steps)
        a = item1
        b = item2

        steps.times do |i|
          a.move_after(b)
          a.save!
          a, b = b, a
        end
      end

      it 'can leap-frog STEPS times before needing to rebalance' do
        expect { leap_frog(RelativePositioning::STEPS) }
          .not_to change { item3.reload.relative_position }
      end

      it 'rebalances after leap-frogging STEPS+1 times' do
        expect { leap_frog(RelativePositioning::STEPS + 1) }
          .to change { item3.reload.relative_position }
      end
    end
  end

  describe '#move_to_start' do
    before do
      [item1, item2].each do |item1|
        item1.move_to_start && item1.save!
      end
    end

    it 'moves item to the end' do
      new_item.move_to_start

      expect(new_item.relative_position).to be < item2.relative_position
    end

    it 'rebalances when there is already an item at the MIN_POSITION' do
      item2.update!(relative_position: RelativePositioning::MIN_POSITION)

      new_item.move_to_start
      item2.reset

      expect(new_item.relative_position).to be < item2.relative_position
      expect(new_item.relative_position).to be >= RelativePositioning::MIN_POSITION
    end

    it 'deals with a run of elements at the start' do
      item1.update!(relative_position: RelativePositioning::MIN_POSITION + 1)
      item2.update!(relative_position: RelativePositioning::MIN_POSITION)

      new_item.move_to_start
      item1.reset
      item2.reset

      expect(item2.relative_position).to be < item1.relative_position
      expect(new_item.relative_position).to be < item2.relative_position
      expect(new_item.relative_position).to be >= RelativePositioning::MIN_POSITION
    end
  end

  describe '#move_to_end' do
    before do
      [item1, item2].each do |item1|
        item1.move_to_end && item1.save!
      end
    end

    it 'moves item to the end' do
      new_item.move_to_end

      expect(new_item.relative_position).to be > item2.relative_position
    end

    it 'rebalances when there is already an item at the MAX_POSITION' do
      item2.update!(relative_position: RelativePositioning::MAX_POSITION)

      new_item.move_to_end
      item2.reset

      expect(new_item.relative_position).to be > item2.relative_position
      expect(new_item.relative_position).to be <= RelativePositioning::MAX_POSITION
    end

    it 'deals with a run of elements at the end' do
      item1.update!(relative_position: RelativePositioning::MAX_POSITION - 1)
      item2.update!(relative_position: RelativePositioning::MAX_POSITION)

      new_item.move_to_end
      item1.reset
      item2.reset

      expect(item2.relative_position).to be > item1.relative_position
      expect(new_item.relative_position).to be > item2.relative_position
      expect(new_item.relative_position).to be <= RelativePositioning::MAX_POSITION
    end
  end

  describe '#move_between' do
    before do
      [item1, item2].each do |item|
        item.move_to_end && item.save!
      end
    end

    shared_examples 'moves item between' do
      it 'moves the middle item to between left and right' do
        expect do
          middle.move_between(left, right)
          middle.save!
        end.to change { between_exclusive?(left, middle, right) }.from(false).to(true)
      end
    end

    it 'positions item between two other' do
      new_item.move_between(item1, item2)

      expect(new_item.relative_position).to be > item1.relative_position
      expect(new_item.relative_position).to be < item2.relative_position
    end

    it 'positions item between on top' do
      new_item.move_between(nil, item1)

      expect(new_item.relative_position).to be < item1.relative_position
    end

    it 'positions item between to end' do
      new_item.move_between(item2, nil)

      expect(new_item.relative_position).to be > item2.relative_position
    end

    it 'positions items even when after and before positions are the same' do
      item2.update! relative_position: item1.relative_position

      new_item.move_between(item1, item2)
      [item1, item2].each(&:reset)

      expect(new_item.relative_position).to be > item1.relative_position
      expect(item1.relative_position).to be < item2.relative_position
    end

    context 'the two items are next to each other' do
      let(:left) { item1 }
      let(:middle) { new_item }
      let(:right) { create_item(relative_position: item1.relative_position + 1) }

      it_behaves_like 'moves item between'
    end

    it 'positions item in the middle of other two if distance is big enough' do
      item1.update! relative_position: 6000
      item2.update! relative_position: 10000

      new_item.move_between(item1, item2)

      expect(new_item.relative_position).to eq(8000)
    end

    it 'positions item closer to the middle if we are at the very top' do
      item1.update!(relative_position: 6001)
      item2.update!(relative_position: 6000)

      new_item.move_between(nil, item2)

      expect(new_item.relative_position).to eq(6000 - RelativePositioning::IDEAL_DISTANCE)
    end

    it 'positions item closer to the middle if we are at the very bottom' do
      new_item.update!(relative_position: 1)
      item1.update!(relative_position: 6000)
      item2.update!(relative_position: 5999)

      new_item.move_between(item1, nil)

      expect(new_item.relative_position).to eq(6000 + RelativePositioning::IDEAL_DISTANCE)
    end

    it 'positions item in the middle of other two' do
      item1.update! relative_position: 100
      item2.update! relative_position: 400

      new_item.move_between(item1, item2)

      expect(new_item.relative_position).to eq(250)
    end

    context 'there is no space' do
      let(:middle) { new_item }
      let(:left) { create_item(relative_position: 100) }
      let(:right) { create_item(relative_position: 101) }

      it_behaves_like 'moves item between'
    end

    context 'there is a bunch of items' do
      let(:items) { create_items_with_positions(100..104) }
      let(:left) { items[1] }
      let(:middle) { items[3] }
      let(:right) { items[2] }

      it_behaves_like 'moves item between'

      it 'handles bunches correctly' do
        middle.move_between(left, right)
        middle.save!

        expect(items.first.reset.relative_position).to be < middle.relative_position
      end
    end

    it 'positions item right if we pass non-sequential parameters' do
      item1.update! relative_position: 99
      item2.update! relative_position: 101
      item3 = create_item(relative_position: 102)
      new_item.update! relative_position: 103

      new_item.move_between(item1, item3)
      new_item.save!

      expect(new_item.relative_position).to be(100)
    end

    it 'avoids N+1 queries when rebalancing other items' do
      items = create_items_with_positions([100, 101, 102])

      count = ActiveRecord::QueryRecorder.new do
        new_item.move_between(items[-2], items[-1])
      end

      items = create_items_with_positions([150, 151, 152, 153, 154])

      expect { new_item.move_between(items[-2], items[-1]) }.not_to exceed_query_limit(count)
    end
  end

  describe '#move_sequence_before' do
    it 'moves the whole sequence of items to the middle of the nearest gap' do
      items = create_items_with_positions([90, 100, 101, 102])

      items.last.move_sequence_before
      items.last.save!

      positions = items.map { |item| item.reload.relative_position }
      expect(positions).to eq([90, 95, 96, 102])
    end

    it 'raises an error if there is no space' do
      items = create_items_with_positions(run_at_start)

      expect { items.last.move_sequence_before }.to raise_error(RelativePositioning::NoSpaceLeft)
    end

    it 'finds a gap if there are unused positions' do
      items = create_items_with_positions([100, 101, 102])

      items.last.move_sequence_before
      items.last.save!

      positions = items.map { |item| item.reload.relative_position }

      expect(positions.last - positions.second).to be > RelativePositioning::MIN_GAP
    end
  end

  describe '#move_sequence_after' do
    it 'moves the whole sequence of items to the middle of the nearest gap' do
      items = create_items_with_positions([100, 101, 102, 110])

      items.first.move_sequence_after
      items.first.save!

      positions = items.map { |item| item.reload.relative_position }
      expect(positions).to eq([100, 105, 106, 110])
    end

    it 'finds a gap if there are unused positions' do
      items = create_items_with_positions([100, 101, 102])

      items.first.move_sequence_after
      items.first.save!

      positions = items.map { |item| item.reload.relative_position }
      expect(positions.second - positions.first).to be > RelativePositioning::MIN_GAP
    end

    it 'raises an error if there is no space' do
      items = create_items_with_positions(run_at_end)

      expect { items.first.move_sequence_after }.to raise_error(RelativePositioning::NoSpaceLeft)
    end
  end

  def be_valid_position
    be_between(RelativePositioning::MIN_POSITION, RelativePositioning::MAX_POSITION)
  end

  def between_exclusive?(left, middle, right)
    a, b, c = [left, middle, right].map { |item| item.reset.relative_position }
    return false if a.nil? || b.nil?
    return a < b if c.nil?

    a < b && b < c
  end

  def run_at_end(size = 3)
    (RelativePositioning::MAX_POSITION - size)..RelativePositioning::MAX_POSITION
  end

  def run_at_start(size = 3)
    (RelativePositioning::MIN_POSITION..).take(size)
  end
end