atagen

malice

public
Star0
Watch0
d67da9aec471fb9247bdddcbce01c951d6ae9854
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/test/reorder.test.ts

5059ed753f8batagen2026-10-27 07:20 UTC1
import assert from 'node:assert/strict';
5059ed753f8batagen2026-10-27 07:20 UTC2
import { test } from 'node:test';
5059ed753f8batagen2026-10-27 07:20 UTC3
import { dropSlot, moveBy, moveToSlot, type Box } from '../src/lib/reorder-model.ts';
5059ed753f8batagen2026-10-27 07:20 UTC4
5059ed753f8batagen2026-10-27 07:20 UTC5
const box = (left: number, top: number): Box => ({ left, top, width: 30, height: 24 });
5059ed753f8batagen2026-10-27 07:20 UTC6
5059ed753f8batagen2026-10-27 07:20 UTC7
const wrapped = [box(0, 0), box(34, 0), box(68, 0), box(0, 28), box(34, 28)];
5059ed753f8batagen2026-10-27 07:20 UTC8
5059ed753f8batagen2026-10-27 07:20 UTC9
test('a pointer left of a chip centre lands before it', () => {
5059ed753f8batagen2026-10-27 07:20 UTC10
  assert.deepEqual(dropSlot(wrapped, 40, 10), { slot: 1, after: false });
5059ed753f8batagen2026-10-27 07:20 UTC11
  assert.deepEqual(dropSlot(wrapped, 2, 10), { slot: 0, after: false });
5059ed753f8batagen2026-10-27 07:20 UTC12
});
5059ed753f8batagen2026-10-27 07:20 UTC13
5059ed753f8batagen2026-10-27 07:20 UTC14
test('a pointer right of every chip in a row lands after its last chip', () => {
5059ed753f8batagen2026-10-27 07:20 UTC15
  assert.deepEqual(dropSlot(wrapped, 200, 10), { slot: 3, after: true });
5059ed753f8batagen2026-10-27 07:20 UTC16
  assert.deepEqual(dropSlot(wrapped, 200, 30), { slot: 5, after: true });
5059ed753f8batagen2026-10-27 07:20 UTC17
});
5059ed753f8batagen2026-10-27 07:20 UTC18
5059ed753f8batagen2026-10-27 07:20 UTC19
test('the nearest row wins when the pointer is above or below the chips', () => {
5059ed753f8batagen2026-10-27 07:20 UTC20
  assert.deepEqual(dropSlot(wrapped, 40, -50), { slot: 1, after: false });
5059ed753f8batagen2026-10-27 07:20 UTC21
  assert.deepEqual(dropSlot(wrapped, 40, 90), { slot: 4, after: false });
5059ed753f8batagen2026-10-27 07:20 UTC22
  assert.deepEqual(dropSlot(wrapped, 10, 27), { slot: 3, after: false });
5059ed753f8batagen2026-10-27 07:20 UTC23
});
5059ed753f8batagen2026-10-27 07:20 UTC24
5059ed753f8batagen2026-10-27 07:20 UTC25
test('chips of slightly different heights share a row', () => {
5059ed753f8batagen2026-10-27 07:20 UTC26
  const mixed = [box(0, 0), { left: 34, top: 2, width: 12, height: 20 }, box(50, 0)];
5059ed753f8batagen2026-10-27 07:20 UTC27
  assert.deepEqual(dropSlot(mixed, 100, 10), { slot: 3, after: true });
5059ed753f8batagen2026-10-27 07:20 UTC28
});
5059ed753f8batagen2026-10-27 07:20 UTC29
5059ed753f8batagen2026-10-27 07:20 UTC30
test('an empty list always lands at the start', () => {
5059ed753f8batagen2026-10-27 07:20 UTC31
  assert.deepEqual(dropSlot([], 10, 10), { slot: 0, after: false });
5059ed753f8batagen2026-10-27 07:20 UTC32
});
5059ed753f8batagen2026-10-27 07:20 UTC33
5059ed753f8batagen2026-10-27 07:20 UTC34
test('moving to a slot after the source accounts for the removed item', () => {
5059ed753f8batagen2026-10-27 07:20 UTC35
  assert.deepEqual(moveToSlot(['a', 'b', 'c', 'd'], 0, 3), ['b', 'c', 'a', 'd']);
5059ed753f8batagen2026-10-27 07:20 UTC36
  assert.deepEqual(moveToSlot(['a', 'b', 'c', 'd'], 0, 4), ['b', 'c', 'd', 'a']);
5059ed753f8batagen2026-10-27 07:20 UTC37
  assert.deepEqual(moveToSlot(['a', 'b', 'c', 'd'], 3, 1), ['a', 'd', 'b', 'c']);
5059ed753f8batagen2026-10-27 07:20 UTC38
});
5059ed753f8batagen2026-10-27 07:20 UTC39
5059ed753f8batagen2026-10-27 07:20 UTC40
test('moving onto its own slot returns the same list', () => {
5059ed753f8batagen2026-10-27 07:20 UTC41
  const list = ['a', 'b', 'c'];
5059ed753f8batagen2026-10-27 07:20 UTC42
  assert.equal(moveToSlot(list, 1, 1), list);
5059ed753f8batagen2026-10-27 07:20 UTC43
  assert.equal(moveToSlot(list, 1, 2), list);
5059ed753f8batagen2026-10-27 07:20 UTC44
  assert.equal(moveToSlot(list, 5, 0), list);
5059ed753f8batagen2026-10-27 07:20 UTC45
  assert.equal(moveToSlot(list, -1, 0), list);
5059ed753f8batagen2026-10-27 07:20 UTC46
});
5059ed753f8batagen2026-10-27 07:20 UTC47
5059ed753f8batagen2026-10-27 07:20 UTC48
test('moving never mutates the input and clamps the slot', () => {
5059ed753f8batagen2026-10-27 07:20 UTC49
  const list = ['a', 'b', 'c'];
5059ed753f8batagen2026-10-27 07:20 UTC50
  assert.deepEqual(moveToSlot(list, 0, 99), ['b', 'c', 'a']);
5059ed753f8batagen2026-10-27 07:20 UTC51
  assert.deepEqual(moveToSlot(list, 2, -4), ['c', 'a', 'b']);
5059ed753f8batagen2026-10-27 07:20 UTC52
  assert.deepEqual(list, ['a', 'b', 'c']);
5059ed753f8batagen2026-10-27 07:20 UTC53
});
5059ed753f8batagen2026-10-27 07:20 UTC54
5059ed753f8batagen2026-10-27 07:20 UTC55
test('moving by a step swaps neighbours and stops at the ends', () => {
5059ed753f8batagen2026-10-27 07:20 UTC56
  const list = ['a', 'b', 'c'];
5059ed753f8batagen2026-10-27 07:20 UTC57
  assert.deepEqual(moveBy(list, 0, 1), ['b', 'a', 'c']);
5059ed753f8batagen2026-10-27 07:20 UTC58
  assert.deepEqual(moveBy(list, 2, -1), ['a', 'c', 'b']);
5059ed753f8batagen2026-10-27 07:20 UTC59
  assert.equal(moveBy(list, 0, -1), list);
5059ed753f8batagen2026-10-27 07:20 UTC60
  assert.equal(moveBy(list, 2, 1), list);
5059ed753f8batagen2026-10-27 07:20 UTC61
});