using ModelingToolkitBase, SciMLBase, Serialization, OrdinaryDiffEqSDIRK
using ModelingToolkitBase: t_nounits as t, D_nounits as D

missing_guess_value = MissingGuessValue.Constant(1.0)
@variables x(t)

@named sys = System([D(x) ~ -0.5 * x], t, initial_conditions = Dict(x => 1.0))
sys = complete(sys)
for prob in [
        eval(ModelingToolkitBase.ODEProblem{false}(sys, nothing, nothing)),
        eval(ModelingToolkitBase.ODEProblem{false}(sys, nothing, nothing; expression = Val{true})),
    ]
    _fn = tempname()

    open(_fn, "w") do f
        serialize(f, prob)
    end

    _cmd = "using ModelingToolkitBase, Serialization; deserialize(\"$_fn\")"

    run(`$(Base.julia_cmd()) -e $(_cmd)`)
end

include("common/rc_model.jl")
@unpack capacitor = rc_model
io = IOBuffer()
write(io, expand_connections(rc_model))
str = String(take!(io))

sys = include_string(@__MODULE__, str)
rc2 = expand_connections(rc_model)
@test issetequal(full_equations(sys), full_equations(rc2))
@test issetequal(unknowns(sys), unknowns(rc2))
@test issetequal(parameters(sys), parameters(rc2))

# check answer
ss = mtkcompile(rc_model)
all_obs = observables(ss)
prob = ODEProblem(ss, [capacitor.v => 0.0], (0, 0.1); missing_guess_value)
sol = solve(prob, ImplicitEuler())

## Check System with Observables ----------
ss_exp = ModelingToolkitBase.toexpr(ss)
ss_ = complete(eval(ss_exp))
prob_ = ODEProblem(ss_, [capacitor.v => 0.0], (0, 0.1); missing_guess_value)
sol_ = solve(prob_, ImplicitEuler())
@test sol[all_obs] == sol_[all_obs] skip = !@isdefined(ModelingToolkit)

## Check ODEProblemExpr with Observables -----------

# build the observable function expression
# ODEProblemExpr with observedfun_exp included
probexpr = ODEProblem{true}(ss, [capacitor.v => 0.0], (0, 0.1); expr = Val{true}, missing_guess_value);
prob_obs = eval(probexpr)
sol_obs = solve(prob_obs, ImplicitEuler())
@test sol_obs[all_obs] == sol[all_obs]
