From be3010efc1e66635583f5c23cb96ff3dd39ae540 Mon Sep 17 00:00:00 2001 From: mingmxren Date: Sun, 1 Mar 2026 15:16:25 +0800 Subject: [PATCH] test(commands): cover session list runtime behavior --- pkg/commands/session_handlers_test.go | 53 ++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/pkg/commands/session_handlers_test.go b/pkg/commands/session_handlers_test.go index 5dbbc3989..031f7ee83 100644 --- a/pkg/commands/session_handlers_test.go +++ b/pkg/commands/session_handlers_test.go @@ -3,6 +3,7 @@ package commands import ( "context" "testing" + "time" "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/session" @@ -18,6 +19,10 @@ type sessionHandlerFakeSessionOps struct { pruneValue []string pruneErr error + listScopeKeys []string + listValue []session.SessionMeta + listErr error + resumeScopeKeys []string resumeIndices []int resumeValue string @@ -34,7 +39,8 @@ func (f *sessionHandlerFakeSessionOps) StartNew(scopeKey string) (string, error) } func (f *sessionHandlerFakeSessionOps) List(scopeKey string) ([]session.SessionMeta, error) { - return nil, nil + f.listScopeKeys = append(f.listScopeKeys, scopeKey) + return f.listValue, f.listErr } func (f *sessionHandlerFakeSessionOps) Resume(scopeKey string, index int) (string, error) { @@ -155,3 +161,48 @@ func TestSessionHandlers_SessionResume_UsesRuntimeSessionOps(t *testing.T) { t.Fatalf("reply=%q", reply) } } + +func TestSessionHandlers_SessionList_UsesRuntimeSessionOps(t *testing.T) { + t.Helper() + + ops := &sessionHandlerFakeSessionOps{ + listValue: []session.SessionMeta{ + { + Ordinal: 1, + SessionKey: "scope#3", + UpdatedAt: time.Date(2026, 3, 1, 9, 7, 0, 0, time.UTC), + MessageCnt: 4, + Active: true, + }, + }, + } + runtime := &sessionHandlerFakeRuntime{ + channel: "whatsapp", + scope: "scope", + ops: ops, + cfg: &config.Config{}, + } + + ctx := WithRuntime(context.Background(), runtime) + + var reply string + ex := NewExecutor(NewRegistry(BuiltinDefinitions(nil))) + res := ex.Execute(ctx, Request{ + Channel: "whatsapp", + Text: "/session list", + Reply: func(text string) error { + reply = text + return nil + }, + }) + + if res.Outcome != OutcomeHandled { + t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomeHandled) + } + if len(ops.listScopeKeys) != 1 || ops.listScopeKeys[0] != "scope" { + t.Fatalf("list scope calls=%v, want [scope]", ops.listScopeKeys) + } + if reply != "Sessions for current chat:\n1. [*] scope#3 (4 msgs, updated 2026-03-01 09:07)" { + t.Fatalf("reply=%q", reply) + } +}