Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

83 lines
2.0 KiB
Go

package persistence
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNotesPersistence_RoundTrip(t *testing.T) {
cache := t.TempDir()
dir := NotesDir(cache, "/tmp/repo-a")
store := &NoteStore{
Version: "test",
RepoPath: "/tmp/repo-a",
Entries: []NoteEntry{
{
ID: "nt-1",
Timestamp: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
SessionID: "sess-1",
Body: "decision: switch to fastpath",
SymbolID: "pkg/foo.go::Bar",
Tags: []string{"decision"},
AutoLinks: []string{"pkg/foo.go::Bar"},
Pinned: true,
},
},
}
require.NoError(t, SaveNotes(dir, store))
got, err := LoadNotes(dir)
require.NoError(t, err)
require.NotNil(t, got)
require.Len(t, got.Entries, 1)
assert.Equal(t, "nt-1", got.Entries[0].ID)
assert.Equal(t, "pkg/foo.go::Bar", got.Entries[0].SymbolID)
assert.True(t, got.Entries[0].Pinned)
}
func TestNotesPersistence_EmptyOnMissingFile(t *testing.T) {
dir := filepath.Join(t.TempDir(), "does-not-exist")
got, err := LoadNotes(dir)
require.NoError(t, err)
require.NotNil(t, got)
assert.Empty(t, got.Entries)
}
func TestNotesPersistence_TrimDropsOldestUnpinned(t *testing.T) {
in := make([]NoteEntry, 0, 10)
for i := range 10 {
in = append(in, NoteEntry{ID: noteID(i), Pinned: i%5 == 0})
}
out := trimNotes(in, 6)
require.Len(t, out, 6)
// Both pinned entries (i=0, i=5) must survive.
pinnedIDs := map[string]bool{}
for _, e := range out {
if e.Pinned {
pinnedIDs[e.ID] = true
}
}
assert.True(t, pinnedIDs[noteID(0)], "pinned[0] must survive")
assert.True(t, pinnedIDs[noteID(5)], "pinned[5] must survive")
// The newest entries should be present (LIFO preserves the tail).
assert.Equal(t, noteID(9), out[len(out)-1].ID)
}
func TestNotesPersistence_TrimNoopUnderCap(t *testing.T) {
in := []NoteEntry{{ID: "a"}, {ID: "b"}, {ID: "c"}}
out := trimNotes(in, 10)
assert.Equal(t, in, out)
}
func noteID(i int) string {
return "nt-" + string(rune('a'+i))
}