build: add makefile, dockerfile, ci and development stack

- build: add makefile, dockerfile, ci and development stack
- feat(config): add environment configuration and logger
- feat(server): add server lifecycle with health and version routes
- feat(db): add connection, contract check, migrations and stores
- feat(cli): add command dispatch and user management
- test(db): add integration tests against mysql
This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 45d1e307c6
commit 7089a6f408
46 files changed
+8456

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"bytes"
"context"
"log/slog"
"strings"
"testing"
"time"
)
func TestParseLogLevel(t *testing.T) {
cases := map[string]slog.Level{
"ERROR": slog.LevelError, "error": slog.LevelError,
"WARN": slog.LevelWarn, "wArN": slog.LevelWarn,
"INFO": slog.LevelInfo, "info": slog.LevelInfo,
"DEBUG": slog.LevelDebug, "Debug": slog.LevelDebug,
"TRACE": LevelTrace, "trace": LevelTrace,
}
for s, want := range cases {
got, err := parseLogLevel(s)
if err != nil || got != want {
t.Errorf("%s: %v %v", s, got, err)
}
}
for _, s := range []string{"", "WARNING", "INFO ", "tracé", "ınfo", "-8", "FATAL"} {
if _, err := parseLogLevel(s); err == nil {
t.Errorf("%q accepted", s)
}
}
}
func TestLogLevelName(t *testing.T) {
if logLevelName(LevelTrace) != "TRACE" || logLevelName(slog.LevelWarn) != "WARN" {
t.Fatal("unexpected names")
}
if logLevelName(slog.Level(2)) != slog.Level(2).String() {
t.Fatal("fallback")
}
}
func TestLoggerFiltering(t *testing.T) {
for _, tc := range []struct {
level slog.Level
want []string
}{
{LevelTrace, []string{"level=TRACE", "level=DEBUG", "level=INFO", "level=WARN", "level=ERROR"}},
{slog.LevelDebug, []string{"level=DEBUG", "level=INFO", "level=WARN", "level=ERROR"}},
{slog.LevelInfo, []string{"level=INFO", "level=WARN", "level=ERROR"}},
{slog.LevelWarn, []string{"level=WARN", "level=ERROR"}},
{slog.LevelError, []string{"level=ERROR"}},
} {
var buf bytes.Buffer
log := newLogger(&buf, tc.level)
ctx := context.Background()
logTrace(ctx, log, "t")
log.Debug("d")
log.Info("i")
log.Warn("w")
log.Error("e")
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(lines) != len(tc.want) {
t.Fatalf("level %v: %d lines: %s", tc.level, len(lines), buf.String())
}
for i, w := range tc.want {
if !strings.Contains(lines[i], w) {
t.Errorf("line %q lacks %q", lines[i], w)
}
}
}
}
func TestLoggerUTCAndTrace(t *testing.T) {
var buf bytes.Buffer
log := newLogger(&buf, LevelTrace)
done := traceFunc(context.Background(), log, "fn")
done()
out := buf.String()
if !strings.Contains(out, `msg="function entry" func=fn`) || !strings.Contains(out, `msg="function exit" func=fn`) {
t.Fatalf("trace output %s", out)
}
if !strings.Contains(out, "Z level=TRACE") {
t.Fatalf("time not UTC: %s", out)
}
buf.Reset()
r := replaceLogAttr(nil, slog.Time(slog.TimeKey, time.Date(2026, 1, 2, 3, 4, 5, 0, time.FixedZone("x", 3600))))
if r.Value.Time().Location() != time.UTC || r.Value.Time().Hour() != 2 {
t.Fatalf("got %v", r.Value)
}
}
func TestLoggerRedactsSecrets(t *testing.T) {
var buf bytes.Buffer
log := newLogger(&buf, LevelTrace)
log.Info("x", "password", "hunter2hunter2", "DB_PASSWORD", "dbsecret", "password_hash", "$argon2id$abc",
"token", "tok123", "session_token", "sess456", "cookie", "cookie789", "dsn", "user:pw@tcp(x)/db", "secret", "s1")
log.WithGroup("g").Info("y", "password", "grouped-secret")
out := buf.String()
for _, s := range []string{"hunter2hunter2", "dbsecret", "$argon2id$abc", "tok123", "sess456", "cookie789", "user:pw", "s1 ", "grouped-secret"} {
if strings.Contains(out, s) {
t.Fatalf("secret %q in %s", s, out)
}
}
if strings.Count(out, "[redacted]") != 9 {
t.Fatalf("redactions: %s", out)
}
}
func TestDiscardLogger(t *testing.T) {
discardLogger().Error("nothing")
if discardLogger().Enabled(context.Background(), slog.LevelError) {
t.Fatal("discard logger enabled")
}
}
func TestIsASCII(t *testing.T) {
if !isASCII("abc") || isASCII("é") {
t.Fatal("isASCII")
}
}