feedBack/tests/js/tour_engine.test.js
Bret Mogilefsky af2949677a
rename: slopsmith → feedBack, byron → got-feedBack (#537)
* Update GitHub repo references from feedback* to feedBack*

* rename: slopsmith -> feedBack, byron -> got-feedBack

Renames across the entire codebase:
- slopsmith/Slopsmith/SLOPSMITH/SlopSmith -> feedBack/FeedBack/FEEDBACK/FeedBack
- byron/Byron/Byrongamatos -> got-feedBack/got-feedBack/got-feedBack
- /home/byron/ -> /opt/got-feedBack/
- byron@ougsoft.com -> hi@got-feedBack.org
- github.com/byrongamatos/ -> github.com/got-feedback/
- com.byron. -> com.got-feedback.
- SLOPSMITH_ env vars -> FEEDBACK_ with backward-compat fallback
- Protocol/storage strings migrated with read-old/write-new pattern
- window.slopsmith JS API -> window.feedBack (canonical) + backward-compat alias

Refs: #rename-slopsmith

* rename: complete regen against current main + fix backward-compat alias

Regenerated the slopsmith->feedBack / byron->got-feedBack rename on top of
current main (3 commits had landed since the branch: #572/#554/#574),
resolving the four content conflicts in favour of main's newer content
(autoplay/auto-exit, accuracy-badge, Virtuoso re-home, feedpak badge).

Completion fixes on top of the mechanical rename:
- Re-apply rename to post-branch content the original rename never saw:
  window.slopsmith(.Tour) consumers in lessons.js / notifications.js /
  onboarding-tour.js, and the matching JS + python tests (autoplay_exit,
  progression_*, test_feedpak_extension FEEDBACK_* env vars). The test env
  vars now match server.py (which reads FEEDBACK_SYNC_STARTUP /
  FEEDBACK_SKIP_STARTUP_TASKS), so the sync-startup test exercises the real
  path again.
- Restore the window.slopsmith backward-compat alias dropped during conflict
  resolution, and move the bus aliases to AFTER the _feedBackExisting merge
  block so they reference the fully-assembled object (also fixes the
  loop_api.test.js API-surface regex, which the original PR latently broke).
- Drop the stray empty data/web_library.db (runtime DB lives in CONFIG_DIR)
  and gitignore it.
- Fix stale tone-source test: feed[dB]ack -> fee[dB]ack to match shipped
  source labels.

Verified locally (org CI billing-blocked): JS 819/819 pass; pytest 1669
passed / 1683 collected with 0 import errors; zero residual slopsmith/byron
except the two intentional window.slopsmith aliases.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* rename: implement advertised backward-compat + prune dead community plugins

Address gaps where PR #537's "Backward compatibility" section was advertised
but not implemented, and clean up the community plugin list.

Env vars (FEEDBACK_* canonical, legacy SLOPSMITH_* honoured):
- New lib/env_compat.py (getenv_compat / env_flag_compat) + tests. server.py
  (_env_flag + all FEEDBACK_* reads), diagnostics_hardware, gp2midi and
  tailwind_rebuild now resolve the legacy alias, so existing SLOPSMITH_UI /
  SLOPSMITH_PLUGINS_DIR / etc. deployments keep working.
- Fix the rename collapsing plugins/__init__.py and minigames/routes.py from
  `FEEDBACK_PLUGINS_DIR or SLOPSMITH_PLUGINS_DIR` into a redundant
  `FEEDBACK_ or FEEDBACK_` (the fallback was silently lost).

Storage (app.js update-channel):
- Read feedBack-update-channel, fall back to legacy slopsmith-update-channel,
  and clear the legacy key on write — so a user's update-channel preference
  survives the rename instead of resetting to "stable".

Community plugin list (README): the rename rewrote third-party repo URLs we
don't own. Probed every one; their owners never renamed, so:
- Restore the 13 live community plugins to their real slopsmith-* names.
- Prune 6 that are 404 to the public (topkoa splitscreen/stems, OmikronApex
  tuner, Jafz2001 nam-rig-builder, DeathlySin song-preview, Erikcb91 shuffle).
- Fix a pre-existing Guitar Theory clone-command typo (nam-tone -> guitar-theory).

Verified: env_compat 7/7, JS 819/819, pytest 1690 collected / 0 import errors,
rename-sensitive + startup suites green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: byrongamatos <xasiklas@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 11:03:01 +02:00

173 lines
9.7 KiB
JavaScript

// Source-level guards for the consolidated tour menu (feedBack#272).
// The engine lives in a DOMContentLoaded handler that wires window.feedBack,
// localStorage, and Shepherd — too much browser surface to reproduce cleanly
// in a vm sandbox. These checks lock in the contract (viz relevance gating,
// complete-vs-cancel semantics, waitFor validation, focus management,
// dedup, etc.) instead, so regressions land as failed assertions rather
// than silently-broken UX.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const tourJs = path.join(__dirname, '..', '..', 'static', 'tour-engine.js');
const SRC = fs.readFileSync(tourJs, 'utf8');
function extractBlock(src, signature) {
const start = src.indexOf(signature);
assert.ok(start !== -1, `signature '${signature}' not found`);
const openBrace = src.indexOf('{', start);
assert.ok(openBrace !== -1, `opening brace after '${signature}' not found`);
let depth = 1;
let i = openBrace + 1;
while (i < src.length && depth > 0) {
const ch = src[i];
if (ch === '{') depth++;
else if (ch === '}') depth--;
i++;
}
assert.ok(depth === 0, `unbalanced braces after '${signature}'`);
return src.slice(start, i);
}
test('viz tours on player are gated on the currently active viz', () => {
const fn = extractBlock(SRC, 'function _isRelevant(pluginId, screenId, activeVizId)');
// Gate must check is_viz on player, not just screen membership.
assert.match(fn, /meta\.is_viz/, '_isRelevant must read meta.is_viz');
assert.match(fn, /screenId\s*===\s*'player'/, '_isRelevant must gate the viz check on the player screen');
assert.match(fn, /activeVizId\s*===\s*pluginId/, '_isRelevant must compare activeVizId to pluginId');
});
test('_relevantPlugins computes the active viz id once per refresh', () => {
const fn = extractBlock(SRC, 'function _relevantPlugins(screenId)');
// _currentVizPluginId must be called exactly once at the top, not
// inside the per-plugin filter callback.
const callMatches = fn.match(/_currentVizPluginId\(\)/g) || [];
assert.equal(callMatches.length, 1, '_currentVizPluginId() must be called exactly once per refresh');
// And gated on the player screen — no need to evaluate matchesArrangement
// for irrelevant screens.
assert.match(fn, /screenId\s*===\s*'player'/, 'active viz lookup must be gated on the player screen');
});
test('_currentVizPluginId reads #viz-picker before localStorage', () => {
const fn = extractBlock(SRC, 'function _currentVizPluginId()');
const pickerIdx = fn.search(/getElementById\(\s*['"]viz-picker['"]/);
const lsIdx = fn.search(/localStorage\.getItem\(\s*['"]vizSelection['"]/);
assert.ok(pickerIdx !== -1, 'must read #viz-picker');
assert.ok(lsIdx !== -1, 'must read localStorage.vizSelection');
assert.ok(pickerIdx < lsIdx, '#viz-picker must be consulted before localStorage (app.js treats picker as source of truth)');
});
test('tour completion → markSeen, cancel → markDismissed', () => {
const fn = extractBlock(SRC, 'async function start(pluginId)');
// complete handler must call _markSeen, cancel handler must call _markDismissed.
// The two paths must not collapse into a single shared cleanup (else cancel
// would silently mark the tour as completed, mis-labeling the badge).
assert.match(fn, /tour\.on\(\s*['"]complete['"][\s\S]*?_markSeen\(\s*pluginId\s*\)/,
'complete handler must _markSeen');
assert.match(fn, /tour\.on\(\s*['"]cancel['"][\s\S]*?_markDismissed\(\s*pluginId\s*\)/,
'cancel handler must _markDismissed (not _markSeen)');
});
test('register() ignores legacy injectTriggerInto / injectTriggerOpts with a deduped warning', () => {
const fn = extractBlock(SRC, 'function register(pluginId, opts)');
assert.match(fn, /'injectTriggerInto'\s+in\s+opts/, 'must detect legacy injectTriggerInto');
assert.match(fn, /'injectTriggerOpts'\s+in\s+opts/, 'must detect legacy injectTriggerOpts');
assert.match(fn, /_deprecationWarned\.has\(pluginId\)/, 'must check dedup Set before warning');
assert.match(fn, /_deprecationWarned\.add\(pluginId\)/, 'must add to dedup Set so we warn once per plugin');
assert.match(fn, /console\.warn/, 'must emit a console.warn');
// The deprecated options must NOT be re-introduced anywhere — they were
// explicitly dropped from _registry storage and from injectTrigger calls.
assert.doesNotMatch(fn, /injectTriggerInto\s*:/, 'register() must not re-introduce injectTriggerInto storage');
});
test('waitFor validates string + try/catch protects querySelector', () => {
// Find the step-mapping block that handles waitFor.
const map = extractBlock(SRC, 'function _mapSteps(rawSteps, tourInstance)');
assert.match(map, /typeof\s+raw\.waitFor\s*===\s*['"]string['"]/, 'must validate waitFor is a string');
assert.match(map, /raw\.waitFor/, 'must reference raw.waitFor');
// The selector must be probed inside a try/catch before beforeShowPromise
// is installed — a malformed selector should warn + skip the wait, not
// hang the tour.
assert.match(map, /try\s*\{[^}]*document\.querySelector\(\s*sel\s*\)[^}]*\}\s*catch/,
'must try/catch the upfront querySelector probe');
assert.match(map, /_WAIT_FOR_TIMEOUT_MS/, 'must use the timeout constant');
});
test('_maybeShowToast guards against active tour and open popover', () => {
const fn = extractBlock(SRC, 'function _maybeShowToast()');
assert.match(fn, /if\s*\(_activeTour\)\s*return/, 'must early-return when a tour is running');
assert.match(fn, /_menuPopover\.style\.display\s*!==\s*'none'/,
'must early-return when the popover is already visible');
});
test('_updateMenuVisibility dismisses orphan toast when relevance drops to zero', () => {
const fn = extractBlock(SRC, 'function _updateMenuVisibility()');
// When plugins.length === 0 we hide the button AND must dismiss any
// active toast (otherwise it'd float at the now-vacant button anchor).
assert.match(fn, /_hideMenu\(\)[\s\S]*_dismissToast\(\)/,
'must call _dismissToast() alongside _hideMenu() when relevance drops to zero');
// And rebuild the open popover when relevance is still non-zero, so
// NEW/✓ badges flip live without a close-and-reopen.
assert.match(fn, /_rebuildMenuItems\(\)/, 'must rebuild open popover on visibility refresh');
});
test('popover has role=dialog with aria-controls wired from the trigger', () => {
const fn = extractBlock(SRC, 'function _ensureMenu()');
assert.match(fn, /setAttribute\(\s*['"]aria-controls['"]\s*,\s*['"]feedBack-tour-menu-popover['"]/,
'trigger must wire aria-controls to the popover id');
assert.match(fn, /_menuPopover\.id\s*=\s*['"]feedBack-tour-menu-popover['"]/,
'popover must carry the matching id');
assert.match(fn, /setAttribute\(\s*['"]role['"]\s*,\s*['"]dialog['"]/,
'popover must use role=dialog (not the menu role we don\'t implement)');
});
test('toast Yes handler defers persistence to start() — no double-marking', () => {
const fn = extractBlock(SRC, 'function _maybeShowToast()');
// Find just the yesBtn click handler within the toast. We must NOT
// see _markDismissed or _markSeen inside the Yes path — start()'s
// own Shepherd handlers own that state transition. Calling
// _markDismissed here would falsely flip hasDismissed() to true
// while the tour is still running and after a successful complete.
const yesIdx = fn.search(/yesBtn\.addEventListener/);
const noIdx = fn.search(/noBtn\.addEventListener/);
assert.ok(yesIdx !== -1 && noIdx !== -1, 'must find both yes and no handlers');
const yesBlock = fn.slice(yesIdx, noIdx);
assert.doesNotMatch(yesBlock, /_markDismissed\s*\(/,
'Yes handler must not call _markDismissed (start() does it on cancel)');
assert.doesNotMatch(yesBlock, /_markSeen\s*\(/,
'Yes handler must not call _markSeen (start() does it on complete)');
});
test('_hideMenu skips focus return when the trigger button is hidden', () => {
const fn = extractBlock(SRC, 'function _hideMenu()');
// The refocus path must check _menuBtn.style.display !== 'none'
// so we don't try to focus a hidden trigger (no-op, leaves focus
// stuck on the about-to-be-hidden popover).
assert.match(fn, /_menuBtn\.style\.display\s*!==\s*['"]none['"]/,
'_hideMenu must skip focus return when the button itself is hidden');
});
test('_showMenu moves focus into the dialog; _hideMenu returns it to the trigger', () => {
const show = extractBlock(SRC, 'function _showMenu()');
assert.match(show, /\.tour-menu-item['"]?\s*\)?[\s\S]*\.focus\(\)/,
'_showMenu must focus the first tour item');
const hide = extractBlock(SRC, 'function _hideMenu()');
assert.match(hide, /_menuBtn\.focus\(\)/, '_hideMenu must return focus to the trigger button');
// The return-focus path must be gated on focus actually being inside
// the dialog, so a programmatic _hideMenu doesn't steal focus from
// elsewhere on the page.
assert.match(hide, /_menuPopover\.contains\(\s*document\.activeElement\s*\)/,
'_hideMenu must only return focus when focus was inside the dialog');
});
test('esc() actually HTML-escapes — Shepherd renders title via innerHTML', () => {
const fn = extractBlock(SRC, 'function esc(s)');
// The pre-existing String() coercion was a no-op; the live esc() must
// map &<>"' to entities.
assert.match(fn, /replace\(/, 'esc() must call replace() to escape characters');
assert.match(SRC, /_ESC_MAP\s*=\s*\{[^}]*'&':\s*'&amp;'[^}]*'<':\s*'&lt;'[^}]*'>':\s*'&gt;'[^}]*'"':\s*'&quot;'[^}]*"'":\s*'&#39;'/,
'must map all five HTML-significant characters');
});