243 lines
8.4 KiB
JavaScript
243 lines
8.4 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const vm = require("vm");
|
|
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
const sandbox = {
|
|
console: { log: function() {}, error: console.error, warn: console.warn },
|
|
window: { MSREditor: {} }
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(
|
|
fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8"),
|
|
sandbox,
|
|
{ filename: "muSRFitlib.js" }
|
|
);
|
|
vm.runInNewContext(
|
|
fs.readFileSync(path.join(repoRoot, "assets/model-recommender.js"), "utf8"),
|
|
sandbox,
|
|
{ filename: "model-recommender.js" }
|
|
);
|
|
|
|
function auditScrubbedIndex() {
|
|
const index = sandbox.window.muSRFlowModelIndex;
|
|
const availableModels = new Set(
|
|
vm.runInNewContext("Object.keys(funcDictionary)", sandbox)
|
|
);
|
|
assert.ok(index);
|
|
assert.ok(index.entries.length > 1000);
|
|
assert.strictEqual(index.weights.title, 0);
|
|
assert.strictEqual(
|
|
index.recipe_conventions.LEM.oscillating.frequency_evidence_records,
|
|
index.recipe_conventions.LEM.oscillating.oscillating_records
|
|
);
|
|
assert.ok(
|
|
index.recipe_conventions.LEM.SingleHist.phase_evidence_records > 800
|
|
);
|
|
const forbidden = new Set([
|
|
"source_id", "group_key", "sample", "path", "relpath", "quality",
|
|
"raw_title", "composition_status"
|
|
]);
|
|
function visit(value) {
|
|
if (Array.isArray(value)) {
|
|
value.forEach(visit);
|
|
return;
|
|
}
|
|
if (!value || typeof value !== "object") return;
|
|
Object.keys(value).forEach(function(key) {
|
|
assert.ok(!forbidden.has(key), "forbidden index key: " + key);
|
|
visit(value[key]);
|
|
});
|
|
}
|
|
visit(index);
|
|
index.entries.forEach(function(entry) {
|
|
assert.ok(entry.recipe.length > 0);
|
|
entry.recipe.forEach(function(model) {
|
|
assert.ok(availableModels.has(model), "unknown GUI recipe: " + model);
|
|
});
|
|
});
|
|
}
|
|
|
|
function testLEMRecipeExpansion() {
|
|
const input = {
|
|
instrument: "LEM",
|
|
n_runs: 2,
|
|
fittypes: [0, 0],
|
|
conditions: { field: { value: 250, unit: "G" } },
|
|
measurement: {
|
|
histogram_sets: [
|
|
{ all: [21, 25], forward: [21, 25], backward: [] },
|
|
{ all: [23, 27], forward: [23, 27], backward: [] }
|
|
]
|
|
}
|
|
};
|
|
const expanded = sandbox.buildSuggestedModelRecipe(
|
|
["GaussianCos"], input, sandbox.window.muSRFlowModelIndex
|
|
);
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(expanded.parsed.constraints)),
|
|
[
|
|
{ target: "Frq", expression: "gamma_mu * Field" },
|
|
{ target: "Phi", expression: "BasePhase + map1" }
|
|
]
|
|
);
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(expanded.parsed.functionMaps)),
|
|
{
|
|
"1": {
|
|
target: "Phi",
|
|
parameters: ["RelPhase1H1", "RelPhase1H2"]
|
|
}
|
|
}
|
|
);
|
|
assert.strictEqual(expanded.defaults.Field.value, 250);
|
|
assert.strictEqual(expanded.defaults.RelPhase1H1.value, 0);
|
|
assert.strictEqual(expanded.defaults.RelPhase1H2.value, 180);
|
|
assert.strictEqual(expanded.defaults.RelPhase1H1.error, 0);
|
|
|
|
const asymmetry = sandbox.buildSuggestedModelRecipe(
|
|
["ExponentialCos"],
|
|
Object.assign({}, input, { fittypes: [2], n_runs: 1 }),
|
|
sandbox.window.muSRFlowModelIndex
|
|
);
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(asymmetry.parsed.constraints)),
|
|
[{ target: "Frq", expression: "gamma_mu * Field" }]
|
|
);
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(asymmetry.parsed.functionMaps)), {});
|
|
}
|
|
|
|
function testUnitNormalizedScoring() {
|
|
const weights = Object.assign({}, sandbox.window.muSRFlowModelIndex.weights, {
|
|
field_numeric: 2,
|
|
field_regime: 1
|
|
});
|
|
const base = {
|
|
instrument: "GPS", n_runs: 1, fittypes: [2],
|
|
conditions: { field: { value: 1, unit: "kG" } },
|
|
measurement: { data_formats: ["PSI-BIN"], histogram_sets: [] }
|
|
};
|
|
const equivalent = {
|
|
instrument: "GPS", n_runs: 1, fittypes: [2],
|
|
conditions: { field: { value: 1000, unit: "G" } },
|
|
measurement: { data_formats: ["PSI-BIN"], histogram_sets: [] }
|
|
};
|
|
const different = {
|
|
instrument: "GPS", n_runs: 1, fittypes: [2],
|
|
conditions: { field: { value: 5, unit: "G" } },
|
|
measurement: { data_formats: ["PSI-BIN"], histogram_sets: [] }
|
|
};
|
|
assert.ok(
|
|
sandbox.scoreModelRecommendation(base, equivalent, weights)
|
|
> sandbox.scoreModelRecommendation(base, different, weights)
|
|
);
|
|
}
|
|
|
|
function testLEMGeometryScoring() {
|
|
const weights = Object.assign({}, sandbox.window.muSRFlowModelIndex.weights, {
|
|
lem_geometry: 6
|
|
});
|
|
const input = {
|
|
instrument: "LEM", n_runs: 1, fittypes: [0], conditions: {},
|
|
measurement: { histogram_sets: [], lem_geometry: "BPAR" }
|
|
};
|
|
const matching = {
|
|
instrument: "LEM", n_runs: 1, fittypes: [0], conditions: {},
|
|
measurement: { histogram_sets: [], lem_geometry: "BPAR" }
|
|
};
|
|
const different = {
|
|
instrument: "LEM", n_runs: 1, fittypes: [0], conditions: {},
|
|
measurement: { histogram_sets: [], lem_geometry: "WEW" }
|
|
};
|
|
assert.strictEqual(
|
|
sandbox.scoreModelRecommendation(input, matching, weights)
|
|
- sandbox.scoreModelRecommendation(input, different, weights),
|
|
6
|
|
);
|
|
}
|
|
|
|
function testExplicitRecipeApplication() {
|
|
let applied = null;
|
|
let rebuilt = 0;
|
|
let closed = 0;
|
|
let plotVisibilityUpdates = 0;
|
|
let clearedTabs = 0;
|
|
const parameterTab = { innerHTML: "old" };
|
|
const fitTypeSelect = { value: "AsymmetryGLB" };
|
|
const histogramInput = { value: "21 25,23 27" };
|
|
sandbox.document = {
|
|
getElementById: function(id) {
|
|
if (id === "Params") return parameterTab;
|
|
if (id === "fitAsyType") return fitTypeSelect;
|
|
if (id === "LRBF") return histogramInput;
|
|
return null;
|
|
}
|
|
};
|
|
sandbox.buildModelRecommendationInput = function(fitType) {
|
|
return {
|
|
instrument: "LEM", n_runs: 1, fittypes: [fitType], conditions: {},
|
|
measurement: { histogram_sets: [] }
|
|
};
|
|
};
|
|
sandbox.applyMSRTheoryModel = function(parsed) {
|
|
applied = parsed;
|
|
return 1;
|
|
};
|
|
sandbox.cfgParams = function() {
|
|
rebuilt++;
|
|
};
|
|
sandbox.closeModelSuggestions = function() {
|
|
closed++;
|
|
};
|
|
sandbox.updatePlotOptionVisibility = function() {
|
|
plotVisibilityUpdates++;
|
|
};
|
|
sandbox.clearTabs = function(reinitialize) {
|
|
assert.strictEqual(reinitialize, true);
|
|
clearedTabs++;
|
|
};
|
|
sandbox.applyModelSuggestion(
|
|
["GaussianCos", "Exponential"], 0, "21 25,22 26,23 27,24 28"
|
|
);
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(applied.components)),
|
|
["GaussianCos", "Exponential"]
|
|
);
|
|
assert.strictEqual(sandbox.All.valField1, "100");
|
|
assert.strictEqual(sandbox.All.fitAsyType, "SingleHist");
|
|
assert.strictEqual(sandbox.All.LRBF, "21 25,22 26,23 27,24 28");
|
|
assert.strictEqual(fitTypeSelect.value, "SingleHist");
|
|
assert.strictEqual(histogramInput.value, "21 25,22 26,23 27,24 28");
|
|
assert.strictEqual(parameterTab.innerHTML, "");
|
|
assert.strictEqual(rebuilt, 1);
|
|
assert.strictEqual(closed, 1);
|
|
assert.strictEqual(plotVisibilityUpdates, 1);
|
|
assert.strictEqual(clearedTabs, 1);
|
|
}
|
|
|
|
function testMarkupContract() {
|
|
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
|
|
assert.match(html, /assets\/model-recommender\.js/);
|
|
assert.match(html, /id="modelSuggestionDialog"/);
|
|
assert.match(html, /class="fit-suggest-button"[\s\S]*onclick="showModelSuggestions\(\);"/);
|
|
assert.doesNotMatch(html, /fit-model-suggest/);
|
|
const source = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
assert.match(source, /async function showModelSuggestions\(\)/);
|
|
assert.match(source, /Reading run metadata\.\.\./);
|
|
assert.match(source, /Run metadata unavailable:/);
|
|
assert.match(source, /await initializeLocalRunData\(\)/);
|
|
assert.match(source, /maximumSignificantDigits:\s*2/);
|
|
}
|
|
|
|
auditScrubbedIndex();
|
|
testUnitNormalizedScoring();
|
|
testLEMGeometryScoring();
|
|
testLEMRecipeExpansion();
|
|
testExplicitRecipeApplication();
|
|
testMarkupContract();
|
|
console.log("Model recommendation tests passed");
|