| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | "reasonix/internal/store" |
| 10 | ) |
| 11 | |
| 12 | func TestClearSessionForTabReturnsReplacementIdentity(t *testing.T) { |
| 13 | app := historySliceTestApp(t) |
| 14 | dir := t.TempDir() |
| 15 | sess, oldPath := saveHistorySliceSession(t, dir, "old.jsonl", []provider.Message{ |
| 16 | {Role: provider.RoleUser, Content: "old content that must not reappear"}, |
| 17 | {Role: provider.RoleAssistant, Content: "old assistant reply"}, |
| 18 | }) |
| 19 | tab := newLiveHistoryTab(t, app, dir, oldPath, sess) |
| 20 | if err := savePinnedContextState(oldPath, []string{"README.md"}); err != nil { |
| 21 | t.Fatalf("save pinned sidecar: %v", err) |
| 22 | } |
| 23 | tab.setPinnedFiles([]string{"README.md"}) |
| 24 | beforeGen := tab.SessionGeneration |
| 25 | |
| 26 | result, err := app.ClearSessionForTab(tab.ID) |
| 27 | if err != nil { |
| 28 | t.Fatalf("ClearSessionForTab: %v", err) |
| 29 | } |
| 30 | if result.SessionPath == "" { |
| 31 | t.Fatal("sessionPath empty after clear") |
| 32 | } |
| 33 | if sameDesktopPath(result.SessionPath, oldPath) { |
| 34 | t.Fatalf("sessionPath stayed %q; want a rotated path", result.SessionPath) |
| 35 | } |
| 36 | if result.SessionGeneration <= beforeGen { |
| 37 | t.Fatalf("sessionGeneration = %d, want > %d", result.SessionGeneration, beforeGen) |
| 38 | } |
| 39 | if tab.SessionGeneration != result.SessionGeneration { |
| 40 | t.Fatalf("tab generation = %d, result = %d", tab.SessionGeneration, result.SessionGeneration) |
| 41 | } |
| 42 | if !sameDesktopPath(tab.currentSessionPath(), result.SessionPath) { |
| 43 | t.Fatalf("tab path = %q, result = %q", tab.currentSessionPath(), result.SessionPath) |
| 44 | } |
| 45 | meta := app.tabMeta(tab, true) |
| 46 | if meta.SessionGeneration != result.SessionGeneration || !sameDesktopPath(meta.SessionPath, result.SessionPath) { |
| 47 | t.Fatalf("tabMeta identity = path:%q gen:%d, want path:%q gen:%d", |
| 48 | meta.SessionPath, meta.SessionGeneration, result.SessionPath, result.SessionGeneration) |
| 49 | } |
| 50 | // Replacement path must not share the old file identity. |
| 51 | if filepath.Base(result.SessionPath) == filepath.Base(oldPath) && sameDesktopPath(result.SessionPath, oldPath) { |
| 52 | t.Fatal("replacement path collided with destroyed session") |
| 53 | } |
| 54 | if got := tab.GetPinnedFiles(); len(got) != 0 { |
| 55 | t.Fatalf("cleared session inherited pins: %v", got) |
| 56 | } |
| 57 | if state, err := loadPinnedContextState(result.SessionPath); err != nil || len(state.Files) != 0 { |
| 58 | t.Fatalf("replacement pinned state = %+v, err=%v", state, err) |
| 59 | } |
| 60 | if _, err := os.Stat(store.SessionPinnedContext(result.SessionPath)); err != nil { |
| 61 | t.Fatalf("replacement pinned sidecar was not written: %v", err) |
| 62 | } |
| 63 | } |
| 64 |