|
| 1 | +package chat |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/urfave/cli/v2" |
| 10 | + "go-micro.dev/v6/cmd" |
| 11 | + "go-micro.dev/v6/registry" |
| 12 | + "go-micro.dev/v6/store" |
| 13 | +) |
| 14 | + |
| 15 | +func registeredSupport(t *testing.T) registry.Registry { |
| 16 | + t.Helper() |
| 17 | + reg := registry.NewMemoryRegistry() |
| 18 | + if err := reg.Register(®istry.Service{Name: "support", Metadata: map[string]string{"agent": "v1"}, Nodes: []*registry.Node{{Id: "support-1", Address: "127.0.0.1:1234", Metadata: map[string]string{"sessions": "v1"}}}}); err != nil { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + return reg |
| 22 | +} |
| 23 | + |
| 24 | +func TestDefaultChatDoesNotSelectRegisteredAgent(t *testing.T) { |
| 25 | + oldDir := store.DefaultDir |
| 26 | + store.DefaultDir = t.TempDir() |
| 27 | + defer func() { store.DefaultDir = oldDir }() |
| 28 | + options := cmd.DefaultOptions() |
| 29 | + oldReg, oldClient := *options.Registry, *options.Client |
| 30 | + remote := &remoteClient{Client: oldClient} |
| 31 | + *options.Registry, *options.Client = registeredSupport(t), remote |
| 32 | + defer func() { *options.Registry, *options.Client = oldReg, oldClient }() |
| 33 | + flags := flag.NewFlagSet("chat", flag.ContinueOnError) |
| 34 | + flags.String("provider", "unconfigured", "") |
| 35 | + flags.String("prompt", "hello", "") |
| 36 | + // The local configuration error must not be bypassed by a registered agent. |
| 37 | + err := run(cli.NewContext(cli.NewApp(), flags, nil)) |
| 38 | + if err == nil || !strings.Contains(err.Error(), "unknown provider") { |
| 39 | + t.Fatalf("expected local setup, got %v", err) |
| 40 | + } |
| 41 | + if remote.calls != 0 { |
| 42 | + t.Fatalf("silently called remote agent %d times", remote.calls) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestAgentSwitchKeepsConversationsSeparate(t *testing.T) { |
| 47 | + var out bytes.Buffer |
| 48 | + s := &session{reg: registeredSupport(t), state: store.NewMemoryStore(), project: t.TempDir(), output: &out} |
| 49 | + if err := s.selectSession("local-thread"); err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + if err := s.remember("local question"); err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + s.listAgents() |
| 56 | + if s.agentName() != "micro" || !strings.Contains(out.String(), "support") { |
| 57 | + t.Fatal("listing changed routing or omitted support") |
| 58 | + } |
| 59 | + if err := s.switchAgent("support"); err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + if s.id == "local-thread" { |
| 63 | + t.Fatal("remote reused local conversation") |
| 64 | + } |
| 65 | + remoteID := s.id |
| 66 | + if err := s.remember("support question"); err != nil { |
| 67 | + t.Fatal(err) |
| 68 | + } |
| 69 | + if err := s.switchAgent("missing"); err == nil { |
| 70 | + t.Fatal("unknown agent accepted") |
| 71 | + } |
| 72 | + if s.agentName() != "support" || s.id != remoteID { |
| 73 | + t.Fatal("failed switch changed conversation") |
| 74 | + } |
| 75 | + if err := s.switchAgent("micro"); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + if s.id != "local-thread" { |
| 79 | + t.Fatalf("local conversation lost: %s", s.id) |
| 80 | + } |
| 81 | + records, err := s.conversations().Read("session/local-thread") |
| 82 | + if err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + var entry conversation |
| 86 | + if err := records[0].Decode(&entry); err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + if entry.Title != "local question" { |
| 90 | + t.Fatal("remote title leaked into local history") |
| 91 | + } |
| 92 | + if err := s.switchAgent("support"); err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + if s.id != remoteID { |
| 96 | + t.Fatal("remote conversation not restored") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestRemoteConversationIsScopedToProject(t *testing.T) { |
| 101 | + state := store.NewMemoryStore() |
| 102 | + first := &session{state: state, project: t.TempDir(), agents: map[string]agentInfo{"support": {}}} |
| 103 | + second := &session{state: state, project: t.TempDir(), agents: map[string]agentInfo{"support": {}}} |
| 104 | + if err := first.selectSession("first-project"); err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + if records, err := second.conversations().Read("current"); err == nil && len(records) != 0 { |
| 108 | + t.Fatal("remote conversation leaked between projects") |
| 109 | + } |
| 110 | + if records, err := store.Scope(state, "agent", "support").Read("current"); err == nil && len(records) != 0 { |
| 111 | + t.Fatal("CLI state leaked into agent's store") |
| 112 | + } |
| 113 | +} |
0 commit comments