feat: add active-game screen and firing interactions
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const test = require('node:test');
|
||||
const { createTargetActivator } = require('../../data/target_interaction.js');
|
||||
|
||||
test('a single tap selects a target without firing', () => {
|
||||
const selected = [];
|
||||
let shots = 0;
|
||||
const activator = createTargetActivator({ canFire: () => true, select: target => selected.push(target), fire: async () => { shots += 1; } });
|
||||
assert.equal(activator.tap({ x: 1, y: 6 }).action, 'select');
|
||||
assert.deepEqual(selected, [{ x: 1, y: 6 }]);
|
||||
assert.equal(shots, 0);
|
||||
});
|
||||
|
||||
test('a second tap on the same cell fires once while a different cell selects', async () => {
|
||||
let timeMs = 0;
|
||||
const selected = [];
|
||||
let shots = 0;
|
||||
const activator = createTargetActivator({ canFire: () => true, select: target => selected.push(target), fire: async () => { shots += 1; }, now: () => timeMs });
|
||||
activator.tap({ x: 1, y: 6 });
|
||||
timeMs = 200;
|
||||
const sameCell = activator.tap({ x: 1, y: 6 });
|
||||
await sameCell.promise;
|
||||
timeMs = 300;
|
||||
assert.equal(activator.tap({ x: 2, y: 6 }).action, 'select');
|
||||
assert.equal(shots, 1);
|
||||
assert.deepEqual(selected, [{ x: 1, y: 6 }, { x: 2, y: 6 }]);
|
||||
});
|
||||
|
||||
test('double activation and explicit fire are deduplicated while a shot is pending', async () => {
|
||||
let resolveShot;
|
||||
let shots = 0;
|
||||
const activator = createTargetActivator({ canFire: () => true, select: () => {}, fire: () => new Promise(resolve => { shots += 1; resolveShot = resolve; }) });
|
||||
const first = activator.doubleActivate({ x: 1, y: 6 });
|
||||
const duplicate = activator.doubleActivate({ x: 1, y: 6 });
|
||||
assert.equal(await duplicate.promise, false);
|
||||
assert.equal(shots, 1);
|
||||
resolveShot();
|
||||
await first.promise;
|
||||
});
|
||||
|
||||
test('out-of-turn or already-targeted cells cannot select or fire', () => {
|
||||
let selected = 0;
|
||||
let shots = 0;
|
||||
const activator = createTargetActivator({ canFire: () => false, select: () => { selected += 1; }, fire: async () => { shots += 1; } });
|
||||
assert.equal(activator.tap({ x: 1, y: 6 }).action, 'ignored');
|
||||
assert.equal(activator.keyboard({ x: 1, y: 6 }).action, 'ignored');
|
||||
assert.equal(selected, 0);
|
||||
assert.equal(shots, 0);
|
||||
});
|
||||
Reference in New Issue
Block a user