2024-09-23 17:04:14 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"isle/daemon/jsonrpc2"
|
2024-12-06 14:42:37 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
2024-09-23 17:04:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const metaKeyNetworkSearchStr = "daemon.networkSearchStr"
|
|
|
|
|
|
|
|
// WithNetwork returns the Context so that, when used against a daemon RPC
|
|
|
|
// endpoint, the endpoint knows which network is being targetted for the call.
|
|
|
|
// The network can be identified by its ID, name, or domain.
|
|
|
|
func WithNetwork(ctx context.Context, searchStr string) context.Context {
|
|
|
|
return jsonrpc2.WithMeta(ctx, metaKeyNetworkSearchStr, searchStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkSearchStr(ctx context.Context) string {
|
|
|
|
v, _ := jsonrpc2.GetMeta(ctx)[metaKeyNetworkSearchStr].(string)
|
|
|
|
return v
|
|
|
|
}
|
2024-12-06 14:42:37 +00:00
|
|
|
|
|
|
|
// MockContextWithNetwork returns a value which can be used with the
|
|
|
|
// tesstify/mock package to match a context which has a search string added to
|
|
|
|
// it by WithNetwork.
|
|
|
|
func MockContextWithNetwork(searchStr string) any {
|
|
|
|
return mock.MatchedBy(func(ctx context.Context) bool {
|
|
|
|
return getNetworkSearchStr(ctx) == searchStr
|
|
|
|
})
|
|
|
|
}
|