bf9395e022
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled
89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Package testutil holds test-only helpers shared across event subsystem tests.
|
|
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/larksuite/cli/internal/event/transport"
|
|
)
|
|
|
|
// FakeTransport delegates to inner with a fixed addr, so tests can use t.TempDir paths.
|
|
type FakeTransport struct {
|
|
addr string
|
|
inner transport.IPC
|
|
mu sync.Mutex
|
|
cleaned bool
|
|
cleanups int
|
|
}
|
|
|
|
func NewWrappedFake(inner transport.IPC, addr string) *FakeTransport {
|
|
return &FakeTransport{addr: addr, inner: inner}
|
|
}
|
|
|
|
func (t *FakeTransport) Listen(_ string) (net.Listener, error) {
|
|
return t.inner.Listen(t.addr)
|
|
}
|
|
|
|
func (t *FakeTransport) Dial(_ string) (net.Conn, error) {
|
|
return t.inner.Dial(t.addr)
|
|
}
|
|
|
|
func (t *FakeTransport) Address(_ string) string { return t.addr }
|
|
|
|
func (t *FakeTransport) Cleanup(_ string) {
|
|
t.mu.Lock()
|
|
t.cleaned = true
|
|
t.cleanups++
|
|
t.mu.Unlock()
|
|
t.inner.Cleanup(t.addr)
|
|
}
|
|
|
|
func (t *FakeTransport) DidCleanup() bool {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.cleaned
|
|
}
|
|
|
|
func (t *FakeTransport) CleanupCount() int {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.cleanups
|
|
}
|
|
|
|
// StubAPIClient records the last call and returns Body or Err.
|
|
type StubAPIClient struct {
|
|
Body string
|
|
Err error
|
|
|
|
mu sync.Mutex
|
|
GotMethod string
|
|
GotPath string
|
|
GotBody interface{}
|
|
Calls int
|
|
}
|
|
|
|
func (s *StubAPIClient) CallAPI(_ context.Context, method, path string, body interface{}) (json.RawMessage, error) {
|
|
s.mu.Lock()
|
|
s.GotMethod = method
|
|
s.GotPath = path
|
|
s.GotBody = body
|
|
s.Calls++
|
|
s.mu.Unlock()
|
|
if s.Err != nil {
|
|
return nil, s.Err
|
|
}
|
|
if s.Body == "" {
|
|
return json.RawMessage("{}"), nil
|
|
}
|
|
return json.RawMessage(s.Body), nil
|
|
}
|
|
|
|
var ErrStubUnconfigured = errors.New("testutil.StubAPIClient: no body or err configured")
|