|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/BackendStack21/odek/internal/config" |
| 10 | + "github.com/BackendStack21/odek/internal/llm" |
| 11 | + "github.com/BackendStack21/odek/internal/schedule" |
| 12 | + "github.com/BackendStack21/odek/internal/session" |
| 13 | + "github.com/BackendStack21/odek/internal/telegram" |
| 14 | +) |
| 15 | + |
| 16 | +func newTestDeliverer(t *testing.T) (telegramDeliverer, *telegram.SessionManager, <-chan string) { |
| 17 | + t.Helper() |
| 18 | + t.Setenv("HOME", t.TempDir()) |
| 19 | + store, err := session.NewStore() |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("NewStore: %v", err) |
| 22 | + } |
| 23 | + sm := telegram.NewSessionManager(store, time.Hour) |
| 24 | + bot, recv := newRecordingTestBot(t) |
| 25 | + d := telegramDeliverer{ |
| 26 | + bot: bot, |
| 27 | + fallback: cliDeliverer{resolved: config.ResolvedConfig{}}, |
| 28 | + sessions: sm, |
| 29 | + log: schedule.NopLogger{}, |
| 30 | + } |
| 31 | + return d, sm, recv |
| 32 | +} |
| 33 | + |
| 34 | +// TestScheduleDeliver_RecordsIntoExistingSession verifies Option B: a delivered |
| 35 | +// scheduled result is appended to the target chat's existing conversation as a |
| 36 | +// labeled user turn + the assistant result, so a follow-up message sees it. |
| 37 | +func TestScheduleDeliver_RecordsIntoExistingSession(t *testing.T) { |
| 38 | + d, sm, recv := newTestDeliverer(t) |
| 39 | + chatID := int64(5551) |
| 40 | + if err := sm.Save(chatID, []llm.Message{ |
| 41 | + {Role: "system", Content: "sys"}, |
| 42 | + {Role: "user", Content: "hi"}, |
| 43 | + {Role: "assistant", Content: "hello"}, |
| 44 | + }); err != nil { |
| 45 | + t.Fatalf("seed session: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + job := schedule.Job{ |
| 49 | + ID: "jb-1", Name: "daily digest", Task: "summarize my day", |
| 50 | + Deliver: schedule.Delivery{Kind: schedule.DeliverTelegram, ChatID: chatID}, |
| 51 | + } |
| 52 | + if err := d.Deliver(context.Background(), job, "the digest"); err != nil { |
| 53 | + t.Fatalf("Deliver: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + // The message was actually sent. |
| 57 | + select { |
| 58 | + case got := <-recv: |
| 59 | + if !strings.Contains(got, "digest") { |
| 60 | + t.Errorf("sent text = %q, want it to contain 'digest'", got) |
| 61 | + } |
| 62 | + case <-time.After(2 * time.Second): |
| 63 | + t.Fatal("no message was sent to Telegram") |
| 64 | + } |
| 65 | + |
| 66 | + // The conversation now ends with the scheduled exchange. |
| 67 | + cs, err := sm.Load(chatID) |
| 68 | + if err != nil || cs == nil { |
| 69 | + t.Fatalf("Load: %v, cs=%v", err, cs) |
| 70 | + } |
| 71 | + if len(cs.Messages) != 5 { |
| 72 | + t.Fatalf("expected 5 messages (3 seed + 2 scheduled), got %d", len(cs.Messages)) |
| 73 | + } |
| 74 | + userTurn := cs.Messages[3] |
| 75 | + if userTurn.Role != "user" || !strings.Contains(userTurn.Content, "scheduled task") || !strings.Contains(userTurn.Content, "summarize my day") { |
| 76 | + t.Errorf("scheduled user turn wrong: %+v", userTurn) |
| 77 | + } |
| 78 | + asstTurn := cs.Messages[4] |
| 79 | + if asstTurn.Role != "assistant" || asstTurn.Content != "the digest" { |
| 80 | + t.Errorf("assistant result turn wrong: %+v", asstTurn) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TestScheduleDeliver_PreservesAlternationAfterUserEndingSession guards the |
| 85 | +// edge where the session already ends on a bare user message (a turn cancelled |
| 86 | +// before the agent replied, or a context-injection command). Appending another |
| 87 | +// user turn would produce two consecutive user messages, which Anthropic |
| 88 | +// rejects on the next call. The write-back must fold into a single assistant |
| 89 | +// turn instead — and never produce two same-role messages in a row. |
| 90 | +func TestScheduleDeliver_PreservesAlternationAfterUserEndingSession(t *testing.T) { |
| 91 | + d, sm, recv := newTestDeliverer(t) |
| 92 | + chatID := int64(5560) |
| 93 | + if err := sm.Save(chatID, []llm.Message{ |
| 94 | + {Role: "system", Content: "sys"}, |
| 95 | + {Role: "user", Content: "an interrupted turn"}, // session ends on user |
| 96 | + }); err != nil { |
| 97 | + t.Fatalf("seed: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + job := schedule.Job{ |
| 101 | + ID: "jb-9", Name: "digest", Task: "summarize", |
| 102 | + Deliver: schedule.Delivery{Kind: schedule.DeliverTelegram, ChatID: chatID}, |
| 103 | + } |
| 104 | + if err := d.Deliver(context.Background(), job, "the result"); err != nil { |
| 105 | + t.Fatalf("Deliver: %v", err) |
| 106 | + } |
| 107 | + <-recv |
| 108 | + |
| 109 | + cs, err := sm.Load(chatID) |
| 110 | + if err != nil || cs == nil { |
| 111 | + t.Fatalf("Load: %v cs=%v", err, cs) |
| 112 | + } |
| 113 | + // No two consecutive same-role messages anywhere. |
| 114 | + for i := 1; i < len(cs.Messages); i++ { |
| 115 | + if cs.Messages[i].Role == cs.Messages[i-1].Role { |
| 116 | + t.Fatalf("consecutive %q messages at %d: %+v", cs.Messages[i].Role, i, cs.Messages) |
| 117 | + } |
| 118 | + } |
| 119 | + // The result was recorded, and the session ends on an assistant turn. |
| 120 | + last := cs.Messages[len(cs.Messages)-1] |
| 121 | + if last.Role != "assistant" || !strings.Contains(last.Content, "the result") { |
| 122 | + t.Errorf("last message should be the assistant result, got %+v", last) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TestScheduleDeliver_NoSessionNotCreated verifies a notification-only chat |
| 127 | +// (never used interactively) is NOT given a session just because a schedule |
| 128 | +// posted to it — avoiding an ever-growing transcript of scheduled posts. |
| 129 | +func TestScheduleDeliver_NoSessionNotCreated(t *testing.T) { |
| 130 | + d, sm, recv := newTestDeliverer(t) |
| 131 | + chatID := int64(5552) |
| 132 | + |
| 133 | + job := schedule.Job{ |
| 134 | + ID: "jb-2", Name: "ping", Task: "ping", |
| 135 | + Deliver: schedule.Delivery{Kind: schedule.DeliverTelegram, ChatID: chatID}, |
| 136 | + } |
| 137 | + if err := d.Deliver(context.Background(), job, "pong"); err != nil { |
| 138 | + t.Fatalf("Deliver: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + select { |
| 142 | + case <-recv: |
| 143 | + case <-time.After(2 * time.Second): |
| 144 | + t.Fatal("no message was sent to Telegram") |
| 145 | + } |
| 146 | + |
| 147 | + cs, err := sm.Load(chatID) |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("Load: %v", err) |
| 150 | + } |
| 151 | + if cs != nil { |
| 152 | + t.Errorf("a notification-only chat should not get a session, got %d messages", len(cs.Messages)) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// TestScheduleDeliver_EmptyResultNotRecorded verifies an empty result is not |
| 157 | +// appended (nothing meaningful to record). |
| 158 | +func TestScheduleDeliver_EmptyResultNotRecorded(t *testing.T) { |
| 159 | + d, sm, _ := newTestDeliverer(t) |
| 160 | + chatID := int64(5553) |
| 161 | + if err := sm.Save(chatID, []llm.Message{{Role: "user", Content: "hi"}}); err != nil { |
| 162 | + t.Fatalf("seed: %v", err) |
| 163 | + } |
| 164 | + |
| 165 | + job := schedule.Job{ |
| 166 | + ID: "jb-3", Name: "noop", Task: "t", |
| 167 | + Deliver: schedule.Delivery{Kind: schedule.DeliverTelegram, ChatID: chatID}, |
| 168 | + } |
| 169 | + if err := d.Deliver(context.Background(), job, ""); err != nil { |
| 170 | + t.Fatalf("Deliver: %v", err) |
| 171 | + } |
| 172 | + |
| 173 | + cs, err := sm.Load(chatID) |
| 174 | + if err != nil || cs == nil { |
| 175 | + t.Fatalf("Load: %v cs=%v", err, cs) |
| 176 | + } |
| 177 | + if len(cs.Messages) != 1 { |
| 178 | + t.Errorf("empty result must not append, got %d messages", len(cs.Messages)) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// TestScheduleDeliver_NilSessionManagerNoPanic verifies the write-back is a |
| 183 | +// safe no-op when no SessionManager is wired (e.g. the CLI daemon path). |
| 184 | +func TestScheduleDeliver_NilSessionManagerNoPanic(t *testing.T) { |
| 185 | + bot, recv := newRecordingTestBot(t) |
| 186 | + d := telegramDeliverer{bot: bot, fallback: cliDeliverer{resolved: config.ResolvedConfig{}}} |
| 187 | + job := schedule.Job{ |
| 188 | + Deliver: schedule.Delivery{Kind: schedule.DeliverTelegram, ChatID: 5554}, |
| 189 | + } |
| 190 | + if err := d.Deliver(context.Background(), job, "result"); err != nil { |
| 191 | + t.Fatalf("Deliver: %v", err) |
| 192 | + } |
| 193 | + select { |
| 194 | + case <-recv: |
| 195 | + case <-time.After(2 * time.Second): |
| 196 | + t.Fatal("no message was sent") |
| 197 | + } |
| 198 | +} |
0 commit comments