Create dedicated mocha runner with isolated vm context

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: Testing
    • 5
    • Not Needed
    • None
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Use Case

      As a... Node.js Driver Developer
      I want... to execute the driver's test suite within a constrained "Clean Room" environment that simulates non-Node.js runtimes (e.g., Browser, Edge).
      So that... I can verify that the codebase is environment-agnostic and prevent the accidental re-introduction of Node-specific globals (like Buffer or process) in future PRs.

      User Experience

      • Developers can run a specific command (e.g., npm run check:sandbox) which executes the integration test suite.
      • If the driver code uses a forbidden global (e.g., Buffer.from(...)), the test runner must fail explicitly with a ReferenceError, pinpointing the violation.
      • The system allows for an "Iterative Subtraction" workflow: developers can temporarily allow specific globals in the configuration to keep the build green while refactoring is in progress.

      Impact:

      • This is an internal tooling improvement to enable the "Pluggable I/O" feature. It blocks the ability to confidently release the driver for Edge environments.

      Dependencies

      • Upstream: None.
      • Downstream: This ticket is the prerequisite for all refactoring tickets (e.g., "Replace Buffer with Uint8Array"). We cannot verify those fixes without this harness.

      Risks/Unknowns

      • Performance: On-the-fly TypeScript transpilation (ts.transpileModule) inside the test runner may significantly slow down test execution compared to standard ts-node. Caching strategies might be needed.
      • 3rd Party Dependencies: Runtime dependencies (like bson) might crash in the sandbox if they rely on globals. We may need to update their code.
      • Tooling Compatibility: Ensuring mocha, chai, and sinon (which run in the Host) function correctly when assertions are triggered from within the Guest (Sandbox) context.

      Acceptance Criteria

      Implementation Requirements

      • Custom Runner: Develop a Node.js script (e.g., test/tools/sandbox_runner.js) that orchestrates the test execution.
      • Centralize all imports from `src` in the test directory (add back mongodb.ts)
      • Inside mongodb.ts, utilize vm.createContext to instantiate a V8 context.
        • The context should accept a configuration map to inject/hide globals. For now, it contains all globals.
        • Add a custom require() implementation for the context which can be configured to error if the driver attempts to require a restricted module. This custom require must allow 3rd party dependencies and builtins that are not prohibited by the disallow list.
        • This must be opt-in: by default, `mongodb.ts` simply re-exports src.
      • CI Integration: Create an Evergreen workflow that executes this runner.
        • CI strategy:
          • Bundle the driver.
          • In mongodb.ts, load the bundle inside the restricted VM.Context and re-export.
            • The bundle is used to simplify the custom `require()` implementation defined above.
        • Run all integration tests on all topologies + rapid server, with authentication.
      • For any pluggable adapter that has been completed before this PR merges, add that module to the restricted imports.
        • For now, that only includes `os`.

      Testing Requirements

      • All tests pass.

      Documentation Requirements

      • Add a section to test/readme.md explaining:
        • How to run the sandbox tests.
        • How the "Iterative Subtraction" strategy works (how to enable/disable globals in the runner config).

      Follow Up Requirements

      • Individual tickets for removing the globals that are currently allow-listed (e.g., "Refactor: Remove global.Buffer dependency") already filled.

            Assignee:
            Unassigned
            Reporter:
            Sergey Zelenov
            None
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: