package platform import ( "context" "maps" "sync" ) // Call is one recorded call to a Fake. Method is "SetEnv", "CreateInstance" // or "DeleteInstance"; unused fields are zero. type Call struct { Method string App string Host string Environment string Vars map[string]string } // Fake is an in-memory Client for tests. It is safe for concurrent use. // // f := platform.NewFake() // f.Fail("CreateInstance", errBoom) // that call now returns errBoom // f.Fail("CreateInstance", nil) // clears it // f.Calls() // every call so far, in order // // A failing call is still recorded. type Fake struct { mu sync.Mutex calls []Call fail map[string]error } var _ Client = (*Fake)(nil) // NewFake returns an empty Fake. func NewFake() *Fake { return &Fake{fail: map[string]error{}} } // Fail makes the named method return err from now on. A nil err clears it. func (f *Fake) Fail(method string, err error) { f.mu.Lock() defer f.mu.Unlock() if err == nil { delete(f.fail, method) return } f.fail[method] = err } // Calls returns a copy of the recorded calls, oldest first. func (f *Fake) Calls() []Call { f.mu.Lock() defer f.mu.Unlock() out := make([]Call, len(f.calls)) for i, c := range f.calls { c.Vars = maps.Clone(c.Vars) out[i] = c } return out } func (f *Fake) record(c Call) error { f.mu.Lock() defer f.mu.Unlock() f.calls = append(f.calls, c) return f.fail[c.Method] } func (f *Fake) SetEnv(_ context.Context, app, environment string, vars map[string]string) error { return f.record(Call{Method: "SetEnv", App: app, Environment: environment, Vars: maps.Clone(vars)}) } func (f *Fake) CreateInstance(_ context.Context, app, host, environment string) error { return f.record(Call{Method: "CreateInstance", App: app, Host: host, Environment: environment}) } func (f *Fake) DeleteInstance(_ context.Context, app, host string) error { return f.record(Call{Method: "DeleteInstance", App: app, Host: host}) }