// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // Stage 13b: the best-practices switch. Off by default, one per area, remembered in the browser and nowhere else; // while it is off the rules are never called (a spy proves it), and nothing about them reaches a diagnostic. import test from 'node:test'; import assert from 'node:assert/strict'; import { Session } from '../dist/core/session.js'; import { stateForProblem, problemById } from '../dist/core/catalog.js'; import { practicesOn, setPracticesOn, forgetPractices, onPracticesChange } from '../dist/core/prefs.js'; import { findPractices, PRACTICES_ORIGIN } from '../core/dist/mojo/practices.js'; import { encodeState } from '../dist/core/state.js'; const store = new Map(); globalThis.localStorage = { getItem: (k) => store.get(k) ?? null, setItem: (k, v) => store.set(k, String(v)), removeItem: (k) => store.delete(k) }; const spy = () => { const f = (...a) => { f.calls++; return findPractices(...a); }; f.calls = 0; return f; }; const open = (id, area, practices) => new Session(stateForProblem(problemById(id)), { area, storageId: null, practices }); const noted = (s) => s.allDiagnostics().filter((d) => d.origin === PRACTICES_ORIGIN); test('off by default in every area, and the rules are never called while off', () => { forgetPractices(); for (const area of ['bridge', 'dev', 'hw', null]) assert.equal(practicesOn(area), false, String(area)); for (const area of ['bridge', 'dev', 'hw']) { const f = spy(); const s = open('count-base', area, f); s.setLang('mojo'); s.allDiagnostics(); s.diagnostics('mojo'); s.practices(); s.trace(); assert.equal(f.calls, 0, `${area}: no rule ran`); assert.deepEqual(s.practices(), []); assert.deepEqual(noted(s), []); } }); test('the switch is per area, remembered under its own key, and a fresh read finds it', () => { forgetPractices(); setPracticesOn('dev', true); assert.equal(store.get('pplus.practices.dev'), '1'); assert.equal(practicesOn('dev'), true); assert.equal(practicesOn('bridge'), false, 'the other area is unaffected'); assert.equal(practicesOn('hw'), false, 'Hardware has no switch'); setPracticesOn('hw', true); assert.equal(practicesOn('hw'), false); let told = 0; const off = onPracticesChange(() => told++); setPracticesOn('bridge', true); setPracticesOn('bridge', false); off(); setPracticesOn('bridge', true); assert.equal(told, 2); setPracticesOn('bridge', false); setPracticesOn('dev', false); assert.ok(!store.has('pplus.practices.dev') && !store.has('pplus.practices.bridge'), 'off is stored as absent'); }); test('on in the Developer area: findings are notes on the student\'s own lines; the Bridge, still off, stays quiet', () => { forgetPractices(); setPracticesOn('dev', true); const f = spy(); const dev = open('count-base', 'dev', f); dev.setSource('mojo', 'def main():\n var n: Int\n n = 5\n n = n + 1\n print(n)\n'); const notes = noted(dev); assert.ok(f.calls >= 1); assert.deepEqual(notes.map((d) => d.id).sort(), ['practice-augmented-assign', 'practice-declare-then-assign']); assert.ok(notes.every((d) => d.severity === 'note' && d.fix && d.lang === 'mojo')); const g = spy(); const bridge = open('count-base', 'bridge', g); bridge.allDiagnostics(); bridge.practices(); assert.equal(g.calls, 0); forgetPractices(); }); test('on in the Bridge: the generated Mojo draws findings in the helper\'s list, not in Problems', () => { forgetPractices(); setPracticesOn('bridge', true); const s = open('count-base', 'bridge'); const rules = new Set(s.practices().map((x) => x.rule)); for (const id of ['declare-then-assign', 'augmented-assign', 'codepoint-loop']) assert.ok(rules.has(id), id); assert.deepEqual(noted(s), [], 'Problems stays about the pseudo and the translation'); assert.equal(s.source('mojo'), s.derivedMojo().source, 'the Mojo is still the literal translation'); forgetPractices(); }); test('the switch is a browser preference: it is not in the state, so a link cannot turn it on', async () => { forgetPractices(); setPracticesOn('dev', true); const st = stateForProblem(problemById('count-base')); assert.ok(!/practices/i.test(JSON.stringify(st))); assert.ok(!/practices/i.test(await encodeState(st))); forgetPractices(); });