Take the chat client's port from Run's caller (closes #4)
check / check (push) Successful in 1m8s

The test that runs the whole bot started its stand-in chat client on
5225, the port a real simplex-chat uses, so on a machine where one
listens there the test would have sent it the bot's set-up commands.
Run now takes the chat client's port: the run command passes
bot.ChatPort (5225), and the test a port it found free.

Model: opus-5-5
This commit is contained in:
clawbot
2026-09-29 02:08:56 +00:00
parent 407eaf847a
commit 9ce902fb05
3 changed files with 22 additions and 18 deletions
+17 -13
View File
@@ -28,11 +28,11 @@ const DisplayName = "calc"
const Welcome = "Send me arithmetic, such as 2 + 2 or 5 * 5/2, " +
"and I will reply with the result."
const (
// chatPort is where the chat client serves its API, on localhost
// inside the bot's own container.
chatPort = 5225
// ChatPort is where the chat client serves its API, on localhost inside
// the bot's own container.
const ChatPort = 5225
const (
// connectTimeout bounds the wait for a freshly started chat client
// to open its API, which includes creating or migrating the
// database.
@@ -54,12 +54,15 @@ const (
var errExited = errors.New("simplex-chat exited")
// Run starts the chat client with its database in cfg.DataDir, connects
// to it, sets up the bot's address, then answers messages and serves the
// API until ctx is cancelled — which is a clean stop and returns nil —
// or until the chat client, the connection to it or the API's listener
// fails, which returns the error.
func Run(ctx context.Context, log *slog.Logger, cfg *config.Config) error {
// Run starts the chat client with its database in cfg.DataDir, serving
// its API on localhost at chatPort, connects to it, sets up the bot's
// address, then answers messages and serves the bot's API until ctx is
// cancelled — which is a clean stop and returns nil — or until the chat
// client, the connection to it or the API's listener fails, which
// returns the error.
func Run(
ctx context.Context, log *slog.Logger, cfg *config.Config, chatPort int,
) error {
err := os.MkdirAll(cfg.DataDir, dataDirMode)
if err != nil {
return fmt.Errorf("creating data directory: %w", err)
@@ -84,7 +87,7 @@ func Run(ctx context.Context, log *slog.Logger, cfg *config.Config) error {
<-cli.Done()
}()
client, err := connect(ctx, log, cli)
client, err := connect(ctx, log, cli, chatPort)
if err != nil {
return err
}
@@ -143,9 +146,10 @@ func stopAPI(ctx context.Context, log *slog.Logger, srv *http.Server) {
}
}
// connect waits for the chat client to open its API and connects to it.
// connect waits for the chat client to open its API on chatPort and
// connects to it.
func connect(
ctx context.Context, log *slog.Logger, cli *simplex.CLI,
ctx context.Context, log *slog.Logger, cli *simplex.CLI, chatPort int,
) (*simplex.Client, error) {
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
defer cancel()