package store import ( "context" "os" "path/filepath" "strings" "testing" ) func TestAPathIsStillSQLite(t *testing.T) { // Every existing configuration names a file, and every one of them has to // keep working without being touched. for _, addr := range []string{ "/var/lib/llmgw/llmgw.db", "llmgw.db", "./data/llmgw.db", } { if isPostgres(addr) { t.Errorf("%q was read as a PostgreSQL address", addr) } } } func TestAPostgresURLIsRecognised(t *testing.T) { for _, addr := range []string{ "postgres://u:p@h:5432/db", "postgresql://u@h/db?sslmode=disable", } { if !isPostgres(addr) { t.Errorf("%q was not read as a PostgreSQL address", addr) } } } func TestAnAddressFileKeepsThePasswordOutOfTheConfig(t *testing.T) { // A DSN carries a password, and a configuration file gets copied, diffed // and pasted into issues. The indirection is what lets the secret live // somewhere with its own permissions. dir := t.TempDir() p := filepath.Join(dir, "dsn") // With the trailing newline every editor and every echo adds, which must // not become part of the password. if err := os.WriteFile(p, []byte("postgres://u:secret@h/db\n"), 0o600); err != nil { t.Fatal(err) } got, err := resolveAddress("@" + p) if err != nil { t.Fatal(err) } if got != "postgres://u:secret@h/db" { t.Errorf("resolved to %q", got) } } func TestAMissingOrEmptyAddressFileSaysSo(t *testing.T) { // Rather than falling back to creating a SQLite file named "@/etc/...", // which is what a silent failure here would produce. if _, err := resolveAddress("@/nonexistent/dsn"); err == nil { t.Error("a missing address file was accepted") } dir := t.TempDir() p := filepath.Join(dir, "empty") if err := os.WriteFile(p, []byte(" \n"), 0o600); err != nil { t.Fatal(err) } _, err := resolveAddress("@" + p) if err == nil || !strings.Contains(err.Error(), "empty") { t.Errorf("an empty address file gave %v", err) } } func TestOpenStillCreatesASQLiteFile(t *testing.T) { p := filepath.Join(t.TempDir(), "llmgw.db") s, err := Open(context.Background(), p) if err != nil { t.Fatal(err) } defer s.Close() if s.dialect != SQLiteDialect { t.Errorf("a path opened as %q", s.dialect) } if _, err := os.Stat(p); err != nil { t.Errorf("no database file was created: %v", err) } }