|
| 1 | +package luaplugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestMessageAPIDeliversTextAndDuration(t *testing.T) { |
| 9 | + m := newTestManager() |
| 10 | + var gotText string |
| 11 | + var gotDur time.Duration |
| 12 | + m.SetUIProvider(UIProvider{ |
| 13 | + ShowMessage: func(text string, duration time.Duration) { |
| 14 | + gotText = text |
| 15 | + gotDur = duration |
| 16 | + }, |
| 17 | + }) |
| 18 | + |
| 19 | + loadTestPlugin(t, m, "msg-test", ` |
| 20 | + plugin.register({name = "msg-test", type = "hook"}) |
| 21 | + cliamp.message("Scrobble Sent", 2) |
| 22 | + `) |
| 23 | + |
| 24 | + if gotText != "Scrobble Sent" { |
| 25 | + t.Fatalf("text = %q, want %q", gotText, "Scrobble Sent") |
| 26 | + } |
| 27 | + if gotDur != 2*time.Second { |
| 28 | + t.Fatalf("duration = %v, want 2s", gotDur) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestMessageAPIDefaultsDurationToZero(t *testing.T) { |
| 33 | + m := newTestManager() |
| 34 | + var gotDur time.Duration |
| 35 | + seen := false |
| 36 | + m.SetUIProvider(UIProvider{ |
| 37 | + ShowMessage: func(_ string, duration time.Duration) { |
| 38 | + gotDur = duration |
| 39 | + seen = true |
| 40 | + }, |
| 41 | + }) |
| 42 | + |
| 43 | + loadTestPlugin(t, m, "msg-default", ` |
| 44 | + plugin.register({name = "msg-default", type = "hook"}) |
| 45 | + cliamp.message("hello") |
| 46 | + `) |
| 47 | + |
| 48 | + if !seen { |
| 49 | + t.Fatal("ShowMessage was not called") |
| 50 | + } |
| 51 | + if gotDur != 0 { |
| 52 | + t.Fatalf("duration = %v, want 0 (UI decides default)", gotDur) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestMessageAPIClampsMaxDuration(t *testing.T) { |
| 57 | + m := newTestManager() |
| 58 | + var gotDur time.Duration |
| 59 | + m.SetUIProvider(UIProvider{ |
| 60 | + ShowMessage: func(_ string, duration time.Duration) { gotDur = duration }, |
| 61 | + }) |
| 62 | + |
| 63 | + loadTestPlugin(t, m, "msg-clamp", ` |
| 64 | + plugin.register({name = "msg-clamp", type = "hook"}) |
| 65 | + cliamp.message("long", 9999) |
| 66 | + `) |
| 67 | + |
| 68 | + if gotDur != messageMaxDuration { |
| 69 | + t.Fatalf("duration = %v, want %v (clamped)", gotDur, messageMaxDuration) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestMessageAPIWithoutProviderIsNoop(t *testing.T) { |
| 74 | + m := newTestManager() |
| 75 | + // No SetUIProvider call — ShowMessage is nil. |
| 76 | + loadTestPlugin(t, m, "msg-noop", ` |
| 77 | + plugin.register({name = "msg-noop", type = "hook"}) |
| 78 | + cliamp.message("nobody listening") |
| 79 | + `) |
| 80 | + // Success = no panic / no error from loadPlugin above. |
| 81 | +} |
0 commit comments