|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/TeoSlayer/pilotprotocol/internal/motd" |
| 12 | +) |
| 13 | + |
| 14 | +// withMOTD saves and restores the package-global banner/json state so these |
| 15 | +// tests don't leak into the rest of the (non-parallel) package suite. |
| 16 | +func withMOTD(t *testing.T, msg string, asJSON bool, fn func()) { |
| 17 | + t.Helper() |
| 18 | + origMsg, origJSON := importantUpdate, jsonOutput |
| 19 | + importantUpdate, jsonOutput = msg, asJSON |
| 20 | + defer func() { importantUpdate, jsonOutput = origMsg, origJSON }() |
| 21 | + fn() |
| 22 | +} |
| 23 | + |
| 24 | +func TestLoadMOTDFromMirror(t *testing.T) { |
| 25 | + home := t.TempDir() |
| 26 | + t.Setenv("HOME", home) |
| 27 | + |
| 28 | + // configDir() resolves to $HOME/.pilot — write the mirror the daemon |
| 29 | + // would have produced for today, then confirm loadMOTD picks it up. |
| 30 | + now := time.Now() |
| 31 | + if err := motd.WriteMirror(motdMirrorPath(), motd.Message{Date: motd.DayKey(now), Text: "overlay maintenance 22:00 UTC"}, now); err != nil { |
| 32 | + t.Fatalf("seed mirror: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + importantUpdate = "" |
| 36 | + loadMOTD() |
| 37 | + if importantUpdate != "overlay maintenance 22:00 UTC" { |
| 38 | + t.Fatalf("importantUpdate = %q, want the seeded message", importantUpdate) |
| 39 | + } |
| 40 | + |
| 41 | + // Clear it (empty motd) — loadMOTD must yield no banner. |
| 42 | + if err := motd.WriteMirror(motdMirrorPath(), motd.Message{}, now); err != nil { |
| 43 | + t.Fatalf("clear mirror: %v", err) |
| 44 | + } |
| 45 | + importantUpdate = "" |
| 46 | + loadMOTD() |
| 47 | + if importantUpdate != "" { |
| 48 | + t.Fatalf("importantUpdate = %q, want empty after clear", importantUpdate) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestPrintMOTDBannerTextMode(t *testing.T) { |
| 53 | + withMOTD(t, "scheduled maintenance", false, func() { |
| 54 | + out := captureStdout(t, printMOTDBanner) |
| 55 | + if !strings.Contains(out, "Message of the day: scheduled maintenance") { |
| 56 | + t.Fatalf("banner missing from output: %q", out) |
| 57 | + } |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func TestPrintMOTDBannerSuppressedInJSON(t *testing.T) { |
| 62 | + withMOTD(t, "scheduled maintenance", true, func() { |
| 63 | + out := captureStdout(t, printMOTDBanner) |
| 64 | + if out != "" { |
| 65 | + t.Fatalf("expected no text banner in JSON mode, got %q", out) |
| 66 | + } |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestPrintMOTDBannerEmpty(t *testing.T) { |
| 71 | + withMOTD(t, "", false, func() { |
| 72 | + out := captureStdout(t, printMOTDBanner) |
| 73 | + if out != "" { |
| 74 | + t.Fatalf("expected no banner with no message, got %q", out) |
| 75 | + } |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func TestOutputJSONCarriesImportantUpdate(t *testing.T) { |
| 80 | + withMOTD(t, "read this first", true, func() { |
| 81 | + out := captureStdout(t, func() { output(map[string]interface{}{"ok": true}) }) |
| 82 | + var env map[string]interface{} |
| 83 | + if err := json.Unmarshal([]byte(out), &env); err != nil { |
| 84 | + t.Fatalf("unmarshal %q: %v", out, err) |
| 85 | + } |
| 86 | + if env["important_update"] != "read this first" { |
| 87 | + t.Fatalf("important_update = %v, want the message", env["important_update"]) |
| 88 | + } |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +func TestOutputJSONOmitsImportantUpdateWhenEmpty(t *testing.T) { |
| 93 | + withMOTD(t, "", true, func() { |
| 94 | + out := captureStdout(t, func() { output(map[string]interface{}{"ok": true}) }) |
| 95 | + var env map[string]interface{} |
| 96 | + if err := json.Unmarshal([]byte(out), &env); err != nil { |
| 97 | + t.Fatalf("unmarshal %q: %v", out, err) |
| 98 | + } |
| 99 | + if _, present := env["important_update"]; present { |
| 100 | + t.Fatalf("important_update should be absent when there is no message: %q", out) |
| 101 | + } |
| 102 | + }) |
| 103 | +} |
0 commit comments