dyzur-bot/live_test.go

51 lines
1.5 KiB
Go

package dyzurbot
import (
"context"
"os"
"path/filepath"
"testing"
)
// TestLiveFetch exercises the full production path — LoadConfig reads the
// environment/.env, NewService wires the Store + Syncer, and Syncer.Sync hits
// the live Google Sheets API, parses the result, and persists it to the cache
// file via Store.Replace. The resulting JSON is left on disk (CachePath,
// default rota.json) for inspection.
//
// It is skipped unless RUN_LIVE=1, so the normal `go test ./...` run stays
// offline and fast.
//
// RUN_LIVE=1 go test -run TestLiveFetch -v .
//
// Requires GOOGLE_API_KEY and GOOGLE_SHEETS_ID in the environment/.env.
func TestLiveFetch(t *testing.T) {
if os.Getenv("RUN_LIVE") != "1" {
t.Skip("set RUN_LIVE=1 to run the live Google Sheets fetch")
}
cfg, err := LoadConfig()
if err != nil {
t.Fatalf("LoadConfig: %v", err)
}
t.Logf("config: spreadsheet=%s range=%q cache=%s",
cfg.SpreadsheetID, cfg.SheetRange, cfg.CachePath)
svc := NewService(cfg)
if err := svc.Syncer.Sync(context.Background()); err != nil {
t.Fatalf("sync: %v", err)
}
rota, lastSynced := svc.Store.Snapshot()
t.Logf("synced %d weeks, %d people at %s",
len(rota.Weeks), len(rota.People), lastSynced.Format("2006-01-02 15:04:05"))
// Confirm the JSON actually landed on disk and show where.
written, err := os.ReadFile(cfg.CachePath)
if err != nil {
t.Fatalf("read cache %q: %v", cfg.CachePath, err)
}
abs, _ := filepath.Abs(cfg.CachePath)
t.Logf("wrote cache JSON (%d bytes) to %s", len(written), abs)
}