How to send a string to be read by the REPL?

I’m trying to write tests for some code which creates a REPL mode and having a lot of trouble. to test the code I’d like to have a function that allows me to send a string to Base.active_repl and then returns the string that the REPL would normally print as the output. ie.

send_to_repl("echo hi") == "hi"

is there any way to do this?

The tests in REPL do something a bit like that, but it doesn’t look straightforward.

That seems to be all about creating a dummy repl and doing tests on it. I would like to access the actual active repl if possible.