1package modes
2
3import (
4 "errors"
5 "strings"
6 "testing"
7
8 "github.com/patriceckhart/zot/packages/agent/extensions"
9 "github.com/patriceckhart/zot/packages/core"
10 "github.com/patriceckhart/zot/packages/tui"
11)
12
13func TestFormatReloadStatusIncludesErrorDetails(t *testing.T) {
14 msg, failed := formatReloadStatus(extensions.ReloadStats{
15 Stopped: 2,
16 Loaded: 1,
17 Ready: 1,
18 Errors: []error{
19 errors.New("broken manifest"),
20 errors.New("spawn worker: executable not found"),
21 },
22 })
23 if !failed {
24 t.Fatal("reload with errors was not marked failed")
25 }
26 for _, want := range []string{
27 "2 stopped, 1 loaded (1 ready)",
28 "2 error(s)",
29 "broken manifest",
30 "spawn worker: executable not found",
31 } {
32 if !strings.Contains(msg, want) {
33 t.Fatalf("reload status missing %q: %s", want, msg)
34 }
35 }
36}
37
38func TestFormatReloadStatusSuccess(t *testing.T) {
39 msg, failed := formatReloadStatus(extensions.ReloadStats{Stopped: 2, Loaded: 3, Ready: 3})
40 if failed {
41 t.Fatal("successful reload was marked failed")
42 }
43 if want := "reloaded: 2 stopped, 3 loaded (3 ready)"; msg != want {
44 t.Fatalf("reload status = %q, want %q", msg, want)
45 }
46}
47
48func TestSetReloadStatusUsesErrorChannelForFailures(t *testing.T) {
49 i := &Interactive{}
50 i.setReloadStatus("broken extension", true)
51 if i.statusErr != "broken extension" || i.statusOK != "" {
52 t.Fatalf("failed reload status used wrong channel: err=%q ok=%q", i.statusErr, i.statusOK)
53 }
54 if len(i.reloadErrors) != 1 || i.reloadErrors[0] != "broken extension" {
55 t.Fatalf("reload errors = %q, want persistent failure", i.reloadErrors)
56 }
57 i.setReloadStatus("reload complete", false)
58 if i.statusOK != "reload complete" || i.statusErr != "" {
59 t.Fatalf("successful reload status used wrong channel: err=%q ok=%q", i.statusErr, i.statusOK)
60 }
61 if len(i.reloadErrors) != 0 {
62 t.Fatalf("reload errors = %q after successful reload, want none", i.reloadErrors)
63 }
64}
65
66func TestStartupExtensionFailureAppearsInChatWithoutEnteringContext(t *testing.T) {
67 agent := core.NewAgent(nil, "", "", nil)
68 i := NewInteractive(InteractiveConfig{
69 Agent: agent,
70 Theme: tui.Theme{Error: 1},
71 StartupExtensionErrors: []string{"startup failed: stderr log: /tmp/startup.log"},
72 })
73
74 chat := strings.Join(i.buildChatLocked(30), "\n")
75 if !strings.Contains(chat, "startup failed") || !strings.Contains(chat, "/tmp/startup.log") {
76 t.Fatalf("chat does not contain startup extension failure: %q", chat)
77 }
78 if got := len(agent.Messages()); got != 0 {
79 t.Fatalf("agent context contains %d startup error message(s), want none", got)
80 }
81}
82
83func TestReloadFailureRemainsInChatWithoutEnteringContext(t *testing.T) {
84 agent := core.NewAgent(nil, "", "", nil)
85 i := NewInteractive(InteractiveConfig{
86 Agent: agent,
87 Theme: tui.Theme{Error: 1},
88 })
89 seq := i.setReloadStatus("broken extension: stderr log: /tmp/ext.log", true)
90 if !i.clearReloadStatus(seq, i.statusErr, true) {
91 t.Fatal("temporary reload status was not cleared")
92 }
93
94 chat := strings.Join(i.buildChatLocked(30), "\n")
95 if !strings.Contains(chat, "broken extension") || !strings.Contains(chat, "/tmp/ext.log") {
96 t.Fatalf("chat does not contain persistent reload failure: %q", chat)
97 }
98 if got := len(agent.Messages()); got != 0 {
99 t.Fatalf("agent context contains %d reload error message(s), want none", got)
100 }
101}
102
103func TestClearReloadStatusDoesNotClearNewerStatus(t *testing.T) {
104 i := &Interactive{statusErr: "newer error", statusOK: "newer success"}
105 if i.clearReloadStatus(0, "old error", true) {
106 t.Fatal("stale error dismissal reported a clear")
107 }
108 if i.clearReloadStatus(0, "old success", false) {
109 t.Fatal("stale success dismissal reported a clear")
110 }
111 if i.statusErr != "newer error" || i.statusOK != "newer success" {
112 t.Fatalf("stale dismissal changed status: err=%q ok=%q", i.statusErr, i.statusOK)
113 }
114
115 if !i.clearReloadStatus(0, "newer error", true) || i.statusErr != "" {
116 t.Fatalf("matching error status was not cleared: %q", i.statusErr)
117 }
118 if !i.clearReloadStatus(0, "newer success", false) || i.statusOK != "" {
119 t.Fatalf("matching success status was not cleared: %q", i.statusOK)
120 }
121
122 first := i.setReloadStatus("same result", false)
123 second := i.setReloadStatus("same result", false)
124 if first == second {
125 t.Fatal("reload status generation did not advance")
126 }
127 if i.clearReloadStatus(first, "same result", false) {
128 t.Fatal("older identical reload status cleared the newer result")
129 }
130}