From 38e318e71d50e4340ddbf1ae63b3d85e958b4644 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 30 Apr 2026 11:48:05 +0300 Subject: fa: wire GCWorker startup and shutdown in cmd/mediaplayer/main.go - Refactor main into run(args) for testability and clean error propagation. - Create slog.Logger from cfg.LogLevel using TextHandler. - Instantiate service.NewGCWorker with store, clock, cfg.MediaRoot, and time.Duration(cfg.GCIntervalMinutes)*time.Minute. - Start GCWorker after construction and defer Stop for graceful shutdown. - Add cmd/mediaplayer/main_test.go as an integration smoke test wiring the GCWorker against a real SQLite store. --- cmd/mediaplayer/main_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 cmd/mediaplayer/main_test.go (limited to 'cmd/mediaplayer/main_test.go') diff --git a/cmd/mediaplayer/main_test.go b/cmd/mediaplayer/main_test.go new file mode 100644 index 0000000..08b9d92 --- /dev/null +++ b/cmd/mediaplayer/main_test.go @@ -0,0 +1,47 @@ +package main + +import ( + "log/slog" + "os" + "path/filepath" + "testing" + "time" + + "codeberg.org/snonux/play/internal" + "codeberg.org/snonux/play/internal/clock" + "codeberg.org/snonux/play/internal/repository" + "codeberg.org/snonux/play/internal/service" +) + +// TestGCWorkerWiring verifies that the GC worker can be constructed with the +// same dependencies used in main, started, and stopped cleanly against a real +// SQLite store. This is an integration-friendly smoke test for the wiring. +func TestGCWorkerWiring(t *testing.T) { + tmpDir := t.TempDir() + dbPath := filepath.Join(tmpDir, "test.db") + mediaRoot := filepath.Join(tmpDir, "media") + if err := os.MkdirAll(mediaRoot, 0o755); err != nil { + t.Fatalf("mkdir media root: %v", err) + } + + store, err := repository.Open(dbPath) + if err != nil { + t.Fatalf("open db: %v", err) + } + defer func() { + if err := store.Close(); err != nil { + t.Logf("close db: %v", err) + } + }() + + cfg := &internal.Config{ + GCIntervalMinutes: 1, + } + + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) + clk := clock.RealClock{} + + w := service.NewGCWorker(store, clk, mediaRoot, time.Duration(cfg.GCIntervalMinutes)*time.Minute, logger) + w.Start() + w.Stop() // must not panic even after interacting with real store +} -- cgit v1.2.3