import {
getScopeMaskFeather,
getScopeMaskOpacity,
isScopeMaskEnabled,
setScopeMaskEnabled,
setScopeMaskFeather,
setScopeMaskOpacity,
} from '../scopeMask.js';
import assert from 'node:assert/strict';
import { readdirSync, readFileSync } from 'node:fs';
import test from 'node:test';
import {
HUD_LAYOUTS,
isLegacySonarHudLayout,
LEGACY_SONAR_HUD_LAYOUTS,
normalizeHudLayout,
} from '../hudLayouts.js';
import {
isCyberMapThemeActive,
isCyberSonarActive,
isCyberSonarEnabled,
setCyberSonarEnabled,
} from '../cyberSonar.js';
import { GEV_ACTION_SCHEMAS } from '../voice/actionSchemas.js';
import { createGevActionRunner } from '../voice/gevActions.js';
import { StyleManager } from './applicationShell.js';
import { setCyberSonarControls } from './cyberSonarControls.js';
import { createPinia } from 'pinia';
import { useDisplayStore } from './stores/display.js';
import { VisualSettings } from './visualSettings.js';
import { STYLES } from './visualPresets.js';
const read = (relative) =>
readFileSync(new URL(relative, import.meta.url), 'utf8');
function cssRuleBody(styles, selector) {
const match = styles.match(new RegExp(`${selector.source}\\s*\\{([^}]+)\\}`));
assert.ok(match, `Missing CSS rule: ${selector.source}`);
return match[1];
}
function styleOwner(t, initialVariant = 'tactical') {
const previous = { document: globalThis.document, window: globalThis.window };
const root = { dataset: {} };
globalThis.document = { documentElement: root, querySelectorAll: () => [] };
globalThis.window = { dispatchEvent() {} };
t.after(() => Object.assign(globalThis, previous));
let variant = initialVariant;
const calls = [];
const hud = {
getVariant: () => variant,
// Normalize like the real HUD so removed layouts fall back.
setVariant: (value) => (variant = normalizeHudLayout(value)),
setMode: (value) => calls.push(['mode', value]),
onStyleChange: (value) => calls.push(['style', value]),
visible: true,
};
hud.setVariant(initialVariant);
const owner = Object.create(VisualSettings.prototype);
Object.assign(owner, {
activeStyle: 'normal',
store: { publish() {} },
readHud: () => hud,
readShareLinks: () => ({ onStyleChange() {} }),
_visualEffects: {
stages: Object.fromEntries(
Object.entries(STYLES).map(([name, config]) => [
name,
{ uniforms: { ...config.uniforms, intensity: 0 } },
]),
),
},
services: {
governorRequestRender: (reason) => calls.push(['render', reason]),
setDetectionStyle() {},
},
_setBloomEnabled: (value) => calls.push(['bloom', value]),
_setSharpenEnabled: (value) => calls.push(['sharpen', value]),
_applyDetectionPreset: (value) => calls.push(['detection', value]),
_syncShareState: () => calls.push(['share', variant]),
});
for (const name of [
'setCelestialRingEnabled',
'_startTransition',
'_publishActiveStyle',
'_updateSliderPanel',
'_updateHudButtonState',
'_syncIrBoost',
'_syncCockpitInheritedStyle',
'_syncCyberSonarControl',
'_scheduleAdaptivePanelLayout',
'_setDetectionFade',
// Intensities: the fixture has no post-process stages to drive.
'_setBloomIntensity',
'_setSharpenIntensity',
])
owner[name] = () => {};
return { owner, hud, root, calls };
}
test('Tactical and Minimal are the HUD layouts; Cyber is only a Sonar alias', () => {
assert.deepEqual(HUD_LAYOUTS, ['tactical', 'minimal']);
assert.deepEqual(LEGACY_SONAR_HUD_LAYOUTS, ['cyber']);
assert.equal(normalizeHudLayout('cyber'), 'tactical');
assert.equal(normalizeHudLayout('CYBER'), 'tactical');
assert.equal(normalizeHudLayout('operator'), 'tactical');
assert.equal(normalizeHudLayout('Minimal'), 'minimal');
assert.equal(isLegacySonarHudLayout('Cyber'), true);
assert.equal(isLegacySonarHudLayout('operator'), false);
assert.equal(isLegacySonarHudLayout('tactical'), false);
});
test('a share link, scene or saved view naming Cyber opens Tactical with Sonar on', async (t) => {
const { owner, hud, root, calls } = styleOwner(t, 'minimal');
assert.equal(isCyberSonarEnabled(root), false);
await owner.restoreShareState({ hudVariant: 'cyber' });
assert.equal(hud.getVariant(), 'tactical');
assert.equal(root.dataset.cyberSonar, 'on');
assert.equal(root.dataset.uiTheme, undefined);
// Turning it on asks for a frame, so a parked map starts the sweep.
assert.ok(
calls.some(
([name, reason]) => name === 'render' && reason === 'cyber-sonar-toggle',
),
);
// The viewer's own layout choice afterwards leaves the Sonar style alone.
owner._setHudVariant('minimal');
assert.equal(hud.getVariant(), 'minimal');
assert.equal(isCyberSonarEnabled(root), true);
// A restored state is authoritative: one that does not name Cyber turns
// Sonar off, whether from a link or a scene shot.
await owner.restoreShareState({ hudVariant: 'tactical' });
assert.equal(isCyberSonarEnabled(root), false);
setCyberSonarEnabled(true, root);
await owner.applyVisualState({ hud: { variant: 'minimal' } });
assert.equal(hud.getVariant(), 'minimal');
assert.equal(isCyberSonarEnabled(root), false);
await owner.applyVisualState({ hud: { variant: 'cyber' } });
assert.equal(hud.getVariant(), 'tactical');
assert.equal(isCyberSonarEnabled(root), true);
});
test('links and scene shots record Tactical with Sonar as the Cyber code, and only that', (t) => {
const { owner, root } = styleOwner(t, 'tactical');
Object.assign(owner.services, {
getDetectionTuning: () => ({ allocationStrategy: 'ELASTIC' }),
getDetectionMode: () => 'OFF',
isScopeMaskEnabled: () => true,
getScopeMaskFeather: () => 1,
getScopeMaskOpacity: () => 1,
getScopeTerminusOverride: () => null,
});
owner.readCelestialRing = () => null;
const shared = () => owner._readShareState().options.hudVariant;
assert.equal(shared(), 'tactical');
owner._setCyberSonarEnabled(true);
assert.equal(shared(), 'cyber');
// Minimal has no code that carries Sonar; it stays local.
owner._setHudVariant('minimal');
assert.equal(shared(), 'minimal');
owner._setHudVariant('tactical');
assert.equal(shared(), 'cyber');
owner._setCyberSonarEnabled(false);
assert.equal(shared(), 'tactical');
assert.equal(isCyberSonarEnabled(root), false);
});
test('Sonar is opt-in and gated on its toggle, the HUD, Cockpit and motion, not a theme', () => {
const classes = new Set();
const root = { dataset: {} };
const body = { classList: { contains: (name) => classes.has(name) } };
const gate = (extra = {}) => ({
root,
body,
hudActive: true,
reducedMotion: false,
...extra,
});
assert.equal(isCyberSonarEnabled(root), false);
assert.equal(isCyberSonarActive(gate()), false);
assert.equal(isCyberMapThemeActive(gate()), false);
// The removed Cyber theme attribute no longer enables anything.
root.dataset.uiTheme = 'cyber';
assert.equal(isCyberSonarActive(gate()), false);
delete root.dataset.uiTheme;
assert.equal(setCyberSonarEnabled(true, root), true);
assert.equal(root.dataset.cyberSonar, 'on');
assert.equal(isCyberSonarActive(gate()), true);
assert.equal(isCyberMapThemeActive(gate()), true);
assert.equal(isCyberSonarActive(gate({ hudActive: false })), false);
assert.equal(isCyberMapThemeActive(gate({ hudActive: false })), false);
// Reduced motion keeps the static treatment and stops the sweep.
assert.equal(isCyberSonarActive(gate({ reducedMotion: true })), false);
assert.equal(isCyberMapThemeActive(gate({ reducedMotion: true })), true);
classes.add('cockpit-mode');
assert.equal(isCyberSonarActive(gate()), false);
assert.equal(isCyberMapThemeActive(gate()), false);
classes.delete('cockpit-mode');
assert.equal(setCyberSonarEnabled(false, root), false);
assert.equal(root.dataset.cyberSonar, 'off');
assert.equal(isCyberSonarActive(gate()), false);
assert.equal(isCyberMapThemeActive(gate()), false);
});
test('Sonar controls apply under Tactical and Minimal and still validate input', () => {
for (const layout of ['tactical', 'minimal']) {
const root = { dataset: {} };
const doc = {
documentElement: root,
body: { classList: { contains: () => false } },
getElementById: () => null,
};
const calls = [];
const owner = {
hud: { getVariant: () => layout, visible: true },
_setCyberSonarSetting: (name, value) => calls.push([name, value]),
_setCyberSonarEnabled: (value) => {
calls.push(['enabled', value]);
setCyberSonarEnabled(value, root);
},
};
const applied = setCyberSonarControls(
owner,
{ enabled: true, rings: 6, sectorDeg: 30 },
doc,
);
assert.equal(applied.ok, true, layout);
assert.equal(applied.sonar.enabled, true);
assert.equal('cyberSelected' in applied.sonar, false);
assert.deepEqual(calls, [
['rings', 6],
['sector', 30],
['enabled', true],
]);
assert.equal(
setCyberSonarControls(owner, { enabled: 'yes' }, doc).error,
'enabled must be a boolean.',
);
assert.equal(
setCyberSonarControls(owner, { pulse: 3 }, doc).error,
'Unknown sonar setting.',
);
assert.equal(
setCyberSonarControls(owner, { rings: 13 }, doc).error,
'rings must be an integer from 3 to 12.',
);
}
});
test('Display and voice HUD routes offer Tactical and Minimal, and Cyber turns Sonar on', async (t) => {
const makeOwner = () => {
const { owner, hud, root } = styleOwner(t, 'tactical');
owner.setStyle('noir');
owner.setHudLayout = (...args) =>
StyleManager.prototype.setHudLayout.call(owner, ...args);
owner.getControlState = () => ({
hud: { layout: hud.getVariant(), visible: true },
});
return { owner, hud, root };
};
const display = makeOwner();
const store = useDisplayStore(createPinia());
const token = store.connect({
setHudLayout: (value) => display.owner.setHudLayout(value),
setStyle: (value) => display.owner.setStyle(value),
});
store.act('setHudLayout', 'minimal');
assert.equal(display.hud.getVariant(), 'minimal');
// Switching layouts never changes the visual preset.
assert.equal(display.owner.activeStyle, 'noir');
store.act('setStyle', 'anime');
assert.equal(display.owner.activeStyle, 'anime');
store.detach(token);
const setHud = GEV_ACTION_SCHEMAS.find((tool) => tool.name === 'set_hud');
assert.deepEqual(setHud.parameters.properties.layout.enum, HUD_LAYOUTS);
const voice = makeOwner();
const eventSource = { addEventListener() {}, removeEventListener() {} };
const viewer = {
camera: { moveEnd: { addEventListener() {} } },
clock: { onTick: { addEventListener: () => () => {} } },
canvas: eventSource,
scene: { canvas: eventSource },
};
const runVoiceAction = createGevActionRunner({
viewer,
styleManager: voice.owner,
dataManager: {},
});
const minimal = await runVoiceAction('set_hud', { layout: 'minimal' });
assert.equal(minimal.ok, true);
assert.equal(voice.hud.getVariant(), 'minimal');
assert.equal(isCyberSonarEnabled(voice.root), false);
const legacy = await runVoiceAction('set_hud', { layout: 'cyber' });
assert.equal(legacy.ok, true);
assert.equal(legacy.layout, 'tactical');
assert.equal(voice.hud.getVariant(), 'tactical');
assert.equal(isCyberSonarEnabled(voice.root), true);
assert.equal(voice.owner.activeStyle, 'noir');
const unknown = await runVoiceAction('set_hud', { layout: 'operator' });
assert.equal(unknown.ok, false);
});
test('style presets keep their own HUD layout and never touch Sonar', (t) => {
const { owner, hud, root } = styleOwner(t);
setCyberSonarEnabled(true, root);
for (const variant of ['tactical', 'minimal']) {
owner._setHudVariant(variant);
assert.equal(hud.getVariant(), variant);
owner.setStyle('normal');
owner.setStyle('surveillance');
assert.equal(hud.getVariant(), 'tactical');
assert.equal(isCyberSonarEnabled(root), true);
}
});
test('the Display panel offers Sonar as an opt-in toggle under the HUD layout', () => {
const view = read('./views/panels/DisplayPanel.vue');
assert.doesNotMatch(view, /value="cyber"/);
assert.match(view, /