80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"sub2api-cn-relay-manager/internal/config"
|
|
)
|
|
|
|
type errWriter struct {
|
|
err error
|
|
}
|
|
|
|
func (w errWriter) Write([]byte) (int, error) {
|
|
return 0, w.err
|
|
}
|
|
|
|
func TestExecuteWritesConfigSummaryAfterBootstrap(t *testing.T) {
|
|
var output bytes.Buffer
|
|
loadCalled := false
|
|
|
|
err := execute(context.Background(), &output, func(context.Context) (config.StartupConfig, error) {
|
|
loadCalled = true
|
|
return config.StartupConfig{
|
|
Server: config.ServerConfig{ListenAddr: ":9191"},
|
|
Database: config.DatabaseConfig{
|
|
SQLiteDSN: "file:test.db?_foreign_keys=on",
|
|
},
|
|
}, nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("execute() returned error: %v", err)
|
|
}
|
|
|
|
if !loadCalled {
|
|
t.Fatal("execute() did not load config")
|
|
}
|
|
|
|
got := output.String()
|
|
if !strings.Contains(got, "sub2api-cn-relay-manager cli ready") {
|
|
t.Fatalf("execute() output = %q, want readiness line", got)
|
|
}
|
|
|
|
if !strings.Contains(got, "listen_addr=:9191") {
|
|
t.Fatalf("execute() output = %q, want listen addr summary", got)
|
|
}
|
|
|
|
if !strings.Contains(got, "sqlite_dsn=file:test.db?_foreign_keys=on") {
|
|
t.Fatalf("execute() output = %q, want sqlite dsn summary", got)
|
|
}
|
|
}
|
|
|
|
func TestExecuteReturnsBootstrapError(t *testing.T) {
|
|
wantErr := errors.New("load config failed")
|
|
|
|
err := execute(context.Background(), &bytes.Buffer{}, func(context.Context) (config.StartupConfig, error) {
|
|
return config.StartupConfig{}, wantErr
|
|
})
|
|
if !errors.Is(err, wantErr) {
|
|
t.Fatalf("execute() error = %v, want %v", err, wantErr)
|
|
}
|
|
}
|
|
|
|
func TestExecuteReturnsWriteError(t *testing.T) {
|
|
wantErr := errors.New("write failed")
|
|
|
|
err := execute(context.Background(), errWriter{err: wantErr}, func(context.Context) (config.StartupConfig, error) {
|
|
return config.StartupConfig{
|
|
Server: config.ServerConfig{ListenAddr: ":9292"},
|
|
Database: config.DatabaseConfig{SQLiteDSN: "file:test.db"},
|
|
}, nil
|
|
})
|
|
if !errors.Is(err, wantErr) {
|
|
t.Fatalf("execute() error = %v, want %v", err, wantErr)
|
|
}
|
|
}
|