Back to Supersonic RC Revive
SOURCE / PINNED RELEASE

Made of little things.

Supersonic RC Revive

Release
1ba42f1ca1d6…
Author-recorded commit
baecad10b1cd…
License
LICENSE
Author’s source reference
nostr://npub1ye5ptcxfyyxl5vjvdjar2ua3f0hynkjzpx552mu5snj3qmx5pzjscpknpr/wss%3A%2F%2Fgit.napplet.soy%2F/n-143146b0d6f

Archive hash verified: 90d22b206672eba4…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tests/konami.test.mjs
// The secret code matcher (src/game/konami.ts): the classic sequence from
// keyboard codes and controller menu intents, restart on a wrong key, the
// suffix restart that keeps ↑↑↑… from eating the opening, and reset().
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { intentToToken, keyToToken, KonamiCode } from '../src/game/konami.ts';

const KEYBOARD = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA'];
const PAD = ['up', 'up', 'down', 'down', 'left', 'right', 'left', 'right', 'back', 'select'];
const TOKENS = ['up', 'up', 'down', 'down', 'left', 'right', 'left', 'right', 'b', 'a'];

/** Feed a token sequence into a fresh matcher; the indices at which it completes. */
function completions(tokens) {
  const matcher = new KonamiCode();
  const hits = [];
  tokens.forEach((token, i) => {
    if (matcher.feed(token)) hits.push(i);
  });
  return hits;
}

test('the keyboard codes spell the code', () => {
  assert.deepEqual(KEYBOARD.map(keyToToken), TOKENS);
});

test('the controller intents spell the code', () => {
  assert.deepEqual(PAD.map(intentToToken), TOKENS);
});

test('keys and buttons outside the code are not tokens', () => {
  for (const code of ['KeyW', 'KeyS', 'KeyD', 'Space', 'Escape', 'Enter', 'Digit1', 'KeyM', 'ShiftLeft']) assert.equal(keyToToken(code), undefined, code);
  assert.equal(intentToToken('start'), undefined);
});

test('the code completes exactly once, at its last token', () => {
  assert.deepEqual(completions(TOKENS), [9]);
});

test('a mixed keyboard/controller entry works', () => {
  const mixed = [
    keyToToken('ArrowUp'),
    intentToToken('up'),
    intentToToken('down'),
    keyToToken('ArrowDown'),
    keyToToken('ArrowLeft'),
    intentToToken('right'),
    keyToToken('ArrowLeft'),
    intentToToken('right'),
    keyToToken('KeyB'),
    intentToToken('select'),
  ];
  assert.deepEqual(completions(mixed), [9]);
});

test('a wrong token in the middle prevents completion', () => {
  const withSlip = [...TOKENS.slice(0, 4), 'b', ...TOKENS.slice(4)];
  assert.deepEqual(completions(withSlip), []);
});

test('a third ↑ restarts the match instead of wasting the opening', () => {
  assert.deepEqual(completions(['up', 'up', 'up', ...TOKENS.slice(2)]), [10]);
});

test('entering the code twice in a row completes twice', () => {
  assert.deepEqual(completions([...TOKENS, ...TOKENS]), [9, 19]);
});

test('reset() forgets a partial entry', () => {
  const matcher = new KonamiCode();
  const fed = [];
  for (const token of TOKENS.slice(0, 6)) fed.push(matcher.feed(token));
  matcher.reset();
  // Without the reset the remaining four tokens would complete the code.
  for (const token of TOKENS.slice(6)) fed.push(matcher.feed(token));
  assert.deepEqual(fed, Array.from({ length: 10 }, () => false));
});