4adb608ad1
CI / changes (pull_request) Successful in 1s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 14s
CI / ui (pull_request) Successful in 51s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m8s
- vs-AI: the comms hub drops the Chat tab (Dictionary only); a finished AI game has no comms at all, so its 💬 history-header entry is hidden too. - Confirm-move ✅: redone as an absolute overlay pinned to the right edge (its right edge sits under the preview caption), so the rack keeps a fixed tile size — this reverts the tile-shrink that resized the letters at 6 tiles. - Recall: dragging a placed tile back to the rack now drops it at the slot the pointer is over (drag-to-position, a gap opens), instead of snapping to its original slot; double-tap still recalls to the origin. New pure recallToSlot. Tests: placement recallToSlot units; e2e recall-to-position; vs-AI comms e2e updated. Docs: UI_DESIGN + FUNCTIONAL (+ru).
148 lines
5.4 KiB
TypeScript
148 lines
5.4 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
BLANK,
|
|
cellOccupied,
|
|
isBlankSlot,
|
|
newPlacement,
|
|
place,
|
|
placementFromHint,
|
|
rackView,
|
|
recallAt,
|
|
recallIndex,
|
|
recallToSlot,
|
|
reorderIndices,
|
|
reset,
|
|
toSubmit,
|
|
} from './placement';
|
|
|
|
const rack = ['A', 'Q', BLANK, 'N', 'I', 'W', 'E'];
|
|
|
|
describe('placement state machine', () => {
|
|
it('places a tile and marks the rack slot used', () => {
|
|
const p = place(newPlacement(rack), 0, 7, 7);
|
|
expect(p.pending).toHaveLength(1);
|
|
expect(rackView(p)[0].used).toBe(true);
|
|
expect(rackView(p)[1].used).toBe(false);
|
|
});
|
|
|
|
it('rejects reusing a slot or an occupied cell', () => {
|
|
let p = place(newPlacement(rack), 0, 7, 7);
|
|
p = place(p, 0, 7, 8); // same slot -> no-op
|
|
expect(p.pending).toHaveLength(1);
|
|
p = place(p, 1, 7, 7); // occupied cell -> no-op
|
|
expect(p.pending).toHaveLength(1);
|
|
});
|
|
|
|
it('requires a letter for a blank slot', () => {
|
|
const noLetter = place(newPlacement(rack), 2, 7, 7);
|
|
expect(noLetter.pending).toHaveLength(0);
|
|
const withLetter = place(newPlacement(rack), 2, 7, 7, 'x');
|
|
expect(withLetter.pending[0]).toMatchObject({ letter: 'X', blank: true });
|
|
});
|
|
|
|
it('recalls a tile by cell', () => {
|
|
let p = place(newPlacement(rack), 0, 7, 7);
|
|
p = recallAt(p, 7, 7);
|
|
expect(p.pending).toHaveLength(0);
|
|
expect(reset(place(p, 0, 7, 7)).pending).toHaveLength(0);
|
|
});
|
|
|
|
it('builds a sorted submit payload and returns null when empty', () => {
|
|
let p = place(newPlacement(rack), 1, 7, 9);
|
|
p = place(p, 0, 7, 7);
|
|
const sub = toSubmit(p);
|
|
expect(sub?.tiles.map((t) => t.col)).toEqual([7, 9]);
|
|
expect(toSubmit(newPlacement(rack))).toBeNull();
|
|
});
|
|
|
|
it('recalls a tile by rack index and reports occupied cells / blank slots', () => {
|
|
let p = place(newPlacement(rack), 0, 7, 7);
|
|
p = place(p, 1, 7, 8);
|
|
expect(cellOccupied(p, 7, 7)).toBe(true);
|
|
expect(cellOccupied(p, 6, 6)).toBe(false);
|
|
p = recallIndex(p, 0);
|
|
expect(p.pending.map((t) => t.rackIndex)).toEqual([1]);
|
|
expect(isBlankSlot(newPlacement(rack), 2)).toBe(true); // '?' slot
|
|
expect(isBlankSlot(newPlacement(rack), 0)).toBe(false);
|
|
});
|
|
|
|
it('submits a single tile as a one-tile payload', () => {
|
|
const sub = toSubmit(place(newPlacement(rack), 0, 7, 7));
|
|
expect(sub?.tiles).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe('placementFromHint', () => {
|
|
it('maps hint letters and blanks onto rack slots', () => {
|
|
const p = placementFromHint(
|
|
[
|
|
{ row: 7, col: 7, letter: 'C', blank: false },
|
|
{ row: 7, col: 8, letter: 'A', blank: false },
|
|
{ row: 7, col: 9, letter: 'B', blank: true },
|
|
],
|
|
['C', 'A', BLANK, 'T'],
|
|
);
|
|
expect(p.pending).toHaveLength(3);
|
|
expect(p.pending[0]).toMatchObject({ rackIndex: 0, letter: 'C', blank: false });
|
|
expect(p.pending[2]).toMatchObject({ rackIndex: 2, letter: 'B', blank: true });
|
|
});
|
|
|
|
it('falls back to a blank slot when the hint letter is not in the rack', () => {
|
|
const p = placementFromHint([{ row: 7, col: 7, letter: 'Z', blank: false }], ['A', BLANK]);
|
|
expect(p.pending).toHaveLength(1);
|
|
expect(p.pending[0]).toMatchObject({ rackIndex: 1, letter: 'Z', blank: true });
|
|
});
|
|
|
|
it('skips hint tiles once the rack is exhausted', () => {
|
|
const p = placementFromHint(
|
|
[
|
|
{ row: 7, col: 7, letter: 'A', blank: false },
|
|
{ row: 7, col: 8, letter: 'B', blank: false },
|
|
],
|
|
['A'],
|
|
);
|
|
expect(p.pending.map((t) => t.letter)).toEqual(['A']);
|
|
});
|
|
});
|
|
|
|
describe('reorderIndices', () => {
|
|
it('lifts an element and drops it at the given slot among the others', () => {
|
|
expect(reorderIndices(4, 0, 2)).toEqual([1, 2, 0, 3]);
|
|
expect(reorderIndices(4, 3, 0)).toEqual([3, 0, 1, 2]);
|
|
expect(reorderIndices(4, 1, 1)).toEqual([0, 1, 2, 3]); // back to identity
|
|
expect(reorderIndices(3, 0, 99)).toEqual([1, 2, 0]); // slot clamped to the end
|
|
});
|
|
});
|
|
|
|
describe('recallToSlot', () => {
|
|
it('reinserts the recalled tile at the chosen visible slot, reordering the rack', () => {
|
|
// Lift Q (slot 1) onto the board, then recall it to visible slot 4.
|
|
const p = place(newPlacement(rack), 1, 7, 7);
|
|
const { placement, order } = recallToSlot(p, 7, 7, 4);
|
|
expect(placement.pending).toHaveLength(0);
|
|
expect(placement.rack).toEqual(['A', BLANK, 'N', 'I', 'Q', 'W', 'E']);
|
|
expect(order).toEqual([0, 2, 3, 4, 1, 5, 6]);
|
|
});
|
|
|
|
it('counts only still-visible slots and remaps the remaining pending tiles', () => {
|
|
// A (slot 0) and N (slot 3) are on the board; recall A to visible slot 2 — the placed N is
|
|
// skipped, and N's rackIndex follows the rack permutation.
|
|
let p = place(newPlacement(rack), 0, 7, 7);
|
|
p = place(p, 3, 7, 8);
|
|
const { placement, order } = recallToSlot(p, 7, 7, 2);
|
|
expect(placement.rack).toEqual(['Q', BLANK, 'N', 'A', 'I', 'W', 'E']);
|
|
expect(order).toEqual([1, 2, 3, 0, 4, 5, 6]);
|
|
expect(placement.pending).toHaveLength(1);
|
|
expect(placement.pending[0].rackIndex).toBe(2);
|
|
expect(placement.rack[placement.pending[0].rackIndex]).toBe('N');
|
|
});
|
|
|
|
it('clamps the slot to the end and is an identity no-op when no tile sits at the cell', () => {
|
|
const p = place(newPlacement(rack), 1, 7, 7);
|
|
expect(recallToSlot(p, 7, 7, 99).placement.rack).toEqual(['A', BLANK, 'N', 'I', 'W', 'E', 'Q']);
|
|
const none = recallToSlot(p, 0, 0, 0);
|
|
expect(none.placement).toBe(p);
|
|
expect(none.order).toEqual([0, 1, 2, 3, 4, 5, 6]);
|
|
});
|
|
});
|