Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions internal/memo/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (s *FileStore) ListTopics(ctx context.Context, scope Scope) ([]string, erro
s.mu.RLock()
defer s.mu.RUnlock()

seen := make(map[string]struct{})
for _, dir := range s.topicsDirs(scope) {
entries, err := os.ReadDir(dir)
if err != nil {
Expand All @@ -192,14 +193,22 @@ func (s *FileStore) ListTopics(ctx context.Context, scope Scope) ([]string, erro
}
return nil, fmt.Errorf("memo: list topics: %w", err)
}
if names := collectTopicNames(entries); len(names) > 0 {
return names, nil
for _, name := range collectTopicNames(entries) {
seen[name] = struct{}{}
}
}
return nil, nil
if len(seen) == 0 {
return nil, nil
}
names := make([]string, 0, len(seen))
for name := range seen {
names = append(names, name)
}
sort.Strings(names)
return names, nil
}

// collectTopicNames 将目录项过滤为 topic 文件名列表,并按字典序排序保证稳定输出
// collectTopicNames 将目录项过滤为 topic 文件名列表。
func collectTopicNames(entries []os.DirEntry) []string {
names := make([]string, 0, len(entries))
for _, entry := range entries {
Expand All @@ -208,7 +217,6 @@ func collectTopicNames(entries []os.DirEntry) []string {
}
names = append(names, entry.Name())
}
sort.Strings(names)
return names
}

Expand Down
35 changes: 35 additions & 0 deletions internal/memo/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,41 @@ func TestFileStoreLoadTopicAndListTopicsFallbackToLegacyProjectPath(t *testing.T
}
}

func TestFileStoreListTopicsMergesScopedAndLegacyProjectTopics(t *testing.T) {
store, legacyDir := newLegacyProjectStore(t)
scopedTopicsDir := store.topicsDir(ScopeProject)
legacyTopicsDir := filepath.Join(legacyDir, topicsDirName)
if err := os.MkdirAll(scopedTopicsDir, 0o755); err != nil {
t.Fatalf("MkdirAll(scoped topics) error = %v", err)
}
if err := os.MkdirAll(legacyTopicsDir, 0o755); err != nil {
t.Fatalf("MkdirAll(legacy topics) error = %v", err)
}
if err := os.WriteFile(filepath.Join(scopedTopicsDir, "scoped.md"), []byte("scoped"), 0o644); err != nil {
t.Fatalf("WriteFile(scoped topic) error = %v", err)
}
if err := os.WriteFile(filepath.Join(legacyTopicsDir, "legacy.md"), []byte("legacy"), 0o644); err != nil {
t.Fatalf("WriteFile(legacy topic) error = %v", err)
}
if err := os.WriteFile(filepath.Join(legacyTopicsDir, "scoped.md"), []byte("legacy dup"), 0o644); err != nil {
t.Fatalf("WriteFile(legacy duplicate topic) error = %v", err)
}

topics, err := store.ListTopics(context.Background(), ScopeProject)
if err != nil {
t.Fatalf("ListTopics() error = %v", err)
}
want := []string{"legacy.md", "scoped.md"}
if len(topics) != len(want) {
t.Fatalf("len(topics) = %d, want %d, topics = %#v", len(topics), len(want), topics)
}
for i := range want {
if topics[i] != want[i] {
t.Fatalf("topics[%d] = %q, want %q (topics=%#v)", i, topics[i], want[i], topics)
}
}
}

func TestFileStoreSaveIndexMigratesLegacyProjectData(t *testing.T) {
store, legacyDir := newLegacyProjectStore(t)
legacyTopicsDir := filepath.Join(legacyDir, topicsDirName)
Expand Down
Loading