Files
visual_debugger/tests/js/load-visualizer.js
mynameisdeleted 9556aa08a7 Add unit tests for py_debug_graph module
This commit introduces a comprehensive suite of unit tests for the
py_debug_graph module, which is responsible for building debug graphs
in Python. The tests cover various scenarios including linked lists,
cycles, scalar values, and the handling of local and global variables
in the debugging context. Additionally, it verifies the behavior of
the debug_graph function and ensures that the builtins.root patch
works as intended. These tests aim to enhance the reliability and
robustness of the debugging functionality.
2026-07-09 19:31:14 -04:00

68 lines
2.6 KiB
JavaScript

"use strict";
// Loads vis-plugins/node-table-visualizer.js's internal pure functions
// (layeredPositions, createSimulation, boxEdgePoint, subtreeSize, rootKind)
// for direct unit testing, WITHOUT modifying the real file on disk: the
// module keeps every function private inside `module.exports = function
// (register, lib) {...}`, so we read its source as text, splice in one
// harmless capture statement in an in-memory copy only, and run that copy
// in a fresh vm context. The file the extension host actually loads is
// never touched.
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const SOURCE_PATH = path.join(__dirname, "..", "..", "vis-plugins", "node-table-visualizer.js");
function loadInternals() {
const original = fs.readFileSync(SOURCE_PATH, "utf8");
const anchor = "register({";
const idx = original.indexOf(anchor);
if (idx === -1) {
throw new Error(
"load-visualizer.js: could not find the `register({` anchor to inject test hooks before -- " +
"has node-table-visualizer.js's shape changed?"
);
}
const hookCapture = [
"__TEST_HOOKS__.layeredPositions = layeredPositions;",
"__TEST_HOOKS__.createSimulation = createSimulation;",
"__TEST_HOOKS__.boxEdgePoint = boxEdgePoint;",
"__TEST_HOOKS__.subtreeSize = subtreeSize;",
"__TEST_HOOKS__.rootKind = rootKind;",
"",
].join("\n");
const patched = original.slice(0, idx) + hookCapture + original.slice(idx);
const hooks = {};
const sandbox = { module: {}, __TEST_HOOKS__: hooks, console };
vm.createContext(sandbox);
vm.runInContext(patched, sandbox, { filename: SOURCE_PATH });
// The file only *assigns* module.exports = function(register, lib) {...}
// at the top level; the closure body (and our injected capture inside
// it) doesn't run until that function is actually called. `register`
// and `lib` are the real extension host's objects in production -- here
// just enough of a stub to get past the schema setup and toolbar/DOM
// code without ever calling into `document`/`window` (register itself
// is a no-op, so the DOM-heavy render() is defined but never invoked).
const noop = function () {};
const passthroughSchema = function () {
return {};
};
const fakeLib = {
semanticJson: {
sOpenObject: passthroughSchema,
sString: passthroughSchema,
sOptionalProp: passthroughSchema,
sBoolean: passthroughSchema,
sArrayOf: passthroughSchema,
sLiteral: passthroughSchema,
},
};
sandbox.module.exports(noop, fakeLib);
return hooks;
}
module.exports = { loadInternals };