Add Online(), honor KB_REQUIRE_ONLINE/RUN_PI_E2E unless KB_ALLOW_OFFLINE, and refuse empty/wrong credentials without leaking secrets in logs. Co-authored-by: Cursor <cursoragent@cursor.com>
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package kb_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"chat-agent-system/internal/config"
|
|
"chat-agent-system/internal/kb"
|
|
)
|
|
|
|
func TestRequireOnlineDefaultsOff(t *testing.T) {
|
|
t.Setenv("KB_REQUIRE_ONLINE", "")
|
|
t.Setenv("RUN_PI_E2E", "")
|
|
t.Setenv("KB_ALLOW_OFFLINE", "")
|
|
if kb.RequireOnline() {
|
|
t.Fatal("expected RequireOnline false by default")
|
|
}
|
|
}
|
|
|
|
func TestRequireOnlineHonorsFlags(t *testing.T) {
|
|
t.Setenv("KB_ALLOW_OFFLINE", "")
|
|
t.Setenv("RUN_PI_E2E", "")
|
|
t.Setenv("KB_REQUIRE_ONLINE", "1")
|
|
if !kb.RequireOnline() {
|
|
t.Fatal("expected RequireOnline when KB_REQUIRE_ONLINE=1")
|
|
}
|
|
t.Setenv("KB_ALLOW_OFFLINE", "1")
|
|
if kb.RequireOnline() {
|
|
t.Fatal("KB_ALLOW_OFFLINE must override require-online")
|
|
}
|
|
}
|
|
|
|
func TestNewClientOfflineEmptyPassword(t *testing.T) {
|
|
t.Setenv("KB_REQUIRE_ONLINE", "")
|
|
t.Setenv("RUN_PI_E2E", "")
|
|
t.Setenv("KB_ALLOW_OFFLINE", "1")
|
|
cfg := &config.Config{
|
|
KbDbHost: "127.0.0.1",
|
|
KbDbPort: 5432,
|
|
KbDbUser: "regkb",
|
|
KbDbPassword: "",
|
|
KbDbName: "regulations",
|
|
}
|
|
client := kb.NewClient(cfg)
|
|
defer client.Close()
|
|
if client.Online() {
|
|
t.Fatal("expected offline when password empty")
|
|
}
|
|
}
|
|
|
|
func TestNewClientWrongPasswordStaysOffline(t *testing.T) {
|
|
t.Setenv("KB_REQUIRE_ONLINE", "")
|
|
t.Setenv("RUN_PI_E2E", "")
|
|
t.Setenv("KB_ALLOW_OFFLINE", "1")
|
|
cfg := &config.Config{
|
|
KbDbHost: "127.0.0.1",
|
|
KbDbPort: 5432,
|
|
KbDbUser: "regkb",
|
|
KbDbPassword: "shared_kb_secret_pass",
|
|
KbDbName: "regulations",
|
|
}
|
|
client := kb.NewClient(cfg)
|
|
defer client.Close()
|
|
if client.Online() {
|
|
t.Fatal("wrong default password must not silently appear online")
|
|
}
|
|
}
|
|
|
|
func TestOnlineReportsPool(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("short")
|
|
}
|
|
if os.Getenv("RUN_KB_INTEGRATION") != "1" {
|
|
t.Skip("set RUN_KB_INTEGRATION=1")
|
|
}
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
client := kb.NewClient(cfg)
|
|
defer client.Close()
|
|
if !client.Online() {
|
|
t.Fatal("expected online with real layered config")
|
|
}
|
|
}
|