| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/control" |
| 11 | "reasonix/internal/event" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | func newSchemaTwoChatTUI(t *testing.T) (chatTUI, *control.Controller, *agent.Session, string) { |
| 16 | t.Helper() |
| 17 | dir := t.TempDir() |
| 18 | sess := agent.NewSession("sys") |
| 19 | sess.Add(provider.Message{Role: provider.RoleUser, Content: "root prompt"}) |
| 20 | sess.Add(provider.Message{Role: provider.RoleAssistant, Content: "root answer"}) |
| 21 | exec := agent.New(nil, nil, sess, agent.Options{}, event.Discard) |
| 22 | ctrl := newOwnedTestController(t, control.Options{Executor: exec, SessionDir: dir, Label: "test", Sink: event.Discard}) |
| 23 | path := filepath.Join(dir, "root.jsonl") |
| 24 | ctrl.SetSessionPath(path) |
| 25 | if err := ctrl.Snapshot(); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | t.Cleanup(ctrl.Close) |
| 29 | m := newChatTUI(ctrl, "", make(chan event.Event, 1), 80) |
| 30 | return m, ctrl, sess, path |
| 31 | } |
| 32 | |
| 33 | func TestBranchAndSwitchCommandsUseIndependentSessions(t *testing.T) { |
| 34 | m, ctrl, _, parentPath := newSchemaTwoChatTUI(t) |
| 35 | m.runBranchCommand("/branch experiment") |
| 36 | childPath := ctrl.SessionPath() |
| 37 | if childPath == parentPath { |
| 38 | t.Fatalf("/branch kept the parent log %q writable", parentPath) |
| 39 | } |
| 40 | tree := strings.Join(*m.pendingCommit, "\n") |
| 41 | if !strings.Contains(tree, "experiment") || !strings.Contains(tree, "current") { |
| 42 | t.Fatalf("/branch tree must list the child session as current:\n%s", tree) |
| 43 | } |
| 44 | m.runSwitchCommand("/switch " + agent.BranchID(parentPath)) |
| 45 | if got := len(ctrl.History()); got != 3 || !m.sessionSwitch || ctrl.SessionPath() != parentPath { |
| 46 | t.Fatalf("/switch to main: history %d switch %v path %q", got, m.sessionSwitch, ctrl.SessionPath()) |
| 47 | } |
| 48 | m.sessionSwitch = false |
| 49 | m.runSwitchCommand("/switch experiment") |
| 50 | if got := ctrl.History(); len(got) != 3 || !m.sessionSwitch || ctrl.SessionPath() != childPath { |
| 51 | t.Fatalf("/switch by name: history %d switch %v", len(got), m.sessionSwitch) |
| 52 | } |
| 53 | if heads, err := agent.ListSessionHeads(parentPath); err != nil || len(heads) != 1 { |
| 54 | t.Fatalf("parent heads = %+v err=%v, want one read-only legacy head", heads, err) |
| 55 | } |
| 56 | if heads, err := agent.ListSessionHeads(childPath); err != nil || len(heads) != 1 { |
| 57 | t.Fatalf("child heads = %+v err=%v, want one independent head", heads, err) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestSessionsDiagnoseCountsSessionLogHeadsAndCleanupLeavesThem(t *testing.T) { |
| 62 | t.Setenv("REASONIX_HOME", t.TempDir()) |
| 63 | dir := t.TempDir() |
| 64 | path := filepath.Join(dir, "log.jsonl") |
| 65 | s := agent.NewSession("system") |
| 66 | s.Add(provider.Message{Role: provider.RoleUser, Content: "shared question"}) |
| 67 | s.Add(provider.Message{Role: provider.RoleAssistant, Content: "shared answer"}) |
| 68 | if err := s.Save(path); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if _, err := s.ForkHead(path, s.Snapshot()[2].ID, agent.HeadKindFork, "alt"); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | s.Add(provider.Message{Role: provider.RoleUser, Content: "alt question"}) |
| 75 | if err := s.Save(path); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | run := func(args ...string) sessionRecoveryReport { |
| 79 | t.Helper() |
| 80 | var report sessionRecoveryReport |
| 81 | out := captureStdout(t, func() { |
| 82 | if rc := sessionsRecoveryCommand(append([]string{"--dir", dir, "--json"}, args...), true); rc != 0 { |
| 83 | t.Fatalf("sessions cleanup rc = %d", rc) |
| 84 | } |
| 85 | }) |
| 86 | if err := json.Unmarshal([]byte(out), &report); err != nil { |
| 87 | t.Fatalf("report %q: %v", out, err) |
| 88 | } |
| 89 | return report |
| 90 | } |
| 91 | dry := run() |
| 92 | if dry.SourceSessions != 1 || dry.SessionLogs != 1 || dry.Heads != 2 || dry.CoveredHeads != 1 || dry.RetiredHeads != 0 { |
| 93 | t.Fatalf("dry-run report = %+v", dry) |
| 94 | } |
| 95 | if dry.Groups != 0 || dry.CleanupEligible != 0 || !dry.DryRun { |
| 96 | t.Fatalf("a session log must not form a recovery group: %+v", dry) |
| 97 | } |
| 98 | applied := run("--apply") |
| 99 | if applied.MovedToTrash != 0 || applied.Busy != 0 || applied.DryRun { |
| 100 | t.Fatalf("cleanup --apply on a session log = %+v, want nothing moved", applied) |
| 101 | } |
| 102 | heads, err := agent.ListSessionHeads(path) |
| 103 | if err != nil || len(heads) != 2 || heads[0].Retired || heads[1].Retired { |
| 104 | t.Fatalf("heads after cleanup = %+v err=%v, want the log untouched", heads, err) |
| 105 | } |
| 106 | } |
| 107 |