修复: 重命名时缓存缺条目补建防标题丢失

- 缓存缺 entry(缓存损坏或新会话未入缓存)时补建,标题才能落盘
- 补缓存补建测试,顺带补齐测试文件缺失的换行
This commit is contained in:
lxy
2026-10-03 10:13:40 +08:00
parent 558b676eea
commit cc58de4661
3 changed files with 29 additions and 4 deletions
+8 -3
View File
@@ -191,10 +191,15 @@ func (m *Model) setUserTitle(sessionID, title string) tea.Cmd {
}
}
if m.history.Cache != nil {
if entry, ok := m.history.Cache[sessionID]; ok {
entry.UserTitle = title
m.cacheDirty = true
entry, ok := m.history.Cache[sessionID]
if !ok {
// 缓存缺 entry(如缓存文件损坏/新会话未入缓存)时补建,
// 否则标题只改内存不落盘,重启后丢失
entry = &cacheEntry{}
m.history.Cache[sessionID] = entry
}
entry.UserTitle = title
m.cacheDirty = true
}
// 调度落盘,避免改名只存在内存、被磁盘旧缓存覆盖
return cacheFlushTick()
+20
View File
@@ -53,3 +53,23 @@ func TestRenameSurvivesStaleScan(t *testing.T) {
t.Fatalf("扫描后 session UserTitle 丢失: = %q, 期望 \"我的新名\"", s.UserTitle)
}
}
// TestSetUserTitleBackfillCacheEntry 验证缓存缺 entry 时(缓存文件损坏/
// 新会话未入缓存)重命名会补建 entry,保证标题落盘而非只改内存
func TestSetUserTitleBackfillCacheEntry(t *testing.T) {
m := NewModel()
m.history.Cache = map[string]*cacheEntry{}
m.setUserTitle("missing-session", "新标题")
e, ok := m.history.Cache["missing-session"]
if !ok {
t.Fatal("缓存缺 entry 时应补建")
}
if e.UserTitle != "新标题" {
t.Fatalf("补建 entry 应写入标题, got %q", e.UserTitle)
}
if !m.cacheDirty {
t.Fatal("应置 cacheDirty 调度落盘")
}
}