// Proxy for the embedded-V8 runtime: a bare global context with no Node
// builtins. Load the IIFE bundle (which assigns globalThis.renderApp) and call
// it. What renders here renders in rusty_v8 — the engine swaps, the contract
// (bundle defines a global render fn over a bare context) does not.
const fs = require("fs")
const vm = require("vm")

const code = fs.readFileSync(__dirname + "/bundle.js", "utf8")

// The minimal host globals React's bundle touches at init / sync render. The
// rusty_v8 engine must provide the same set — this list is the spec for it.
const sandbox = {
  console, setTimeout, clearTimeout, queueMicrotask, MessageChannel, performance,
  TextEncoder, TextDecoder,
}
sandbox.globalThis = sandbox

vm.createContext(sandbox)
vm.runInContext(code, sandbox)

const html = sandbox.renderApp({ name: "World" })
console.log("RENDERED:", html)

const expected = '<div id="greeting">Hello, World!</div>'
if (html !== expected) {
  console.error("MISMATCH — expected:", expected)
  process.exit(1)
}
console.log("OK — React bundle renders in a bare JS context (V8 proxy)")
