refactor(database): simplify session store appends#79
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughCentralizes session entry construction in internal/database/session_store.go by adding appendEntryOptions and appendBuiltEntry; refactors multiple append methods to use the helper; adds a test ensuring input timestamps are preserved and normalized to UTC. ChangesEntry Append Refactoring
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #79 +/- ##
==========================================
+ Coverage 66.69% 66.76% +0.06%
==========================================
Files 207 207
Lines 17884 17869 -15
==========================================
+ Hits 11928 11930 +2
+ Misses 4854 4841 -13
+ Partials 1102 1098 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 02324a32-f155-4e34-973d-2cf4fcfb40ca
⛔ Files ignored due to path filters (1)
go.sumis excluded by!**/*.sum
📒 Files selected for processing (1)
internal/database/session_store.go
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/database/session_repository_test.go (1)
90-117: ⚡ Quick winBroaden this into a table-driven timestamp persistence test.
This only covers a UTC input, so it won’t catch regressions in the non-zero timestamp normalization path. Please add at least one non-UTC case and assert the persisted entry’s
CreatedAtafter a repository read as well, not just the returned struct andsession_messagesrow.Suggested shape
-func TestSessionRepository_AppendMessagePreservesInputTimestamp(t *testing.T) { +func TestSessionRepository_AppendMessagePreservesInputTimestamp(t *testing.T) { t.Parallel() - repository := newTestSessionRepository(t) - ctx := context.Background() - - createdSession, err := repository.CreateSession(ctx, "/work", "timestamp", "") - require.NoError(t, err) - - timestamp := time.Date(2026, time.May, 31, 12, 34, 56, 789000000, time.UTC) - message := database.MessageEntity{ - Timestamp: timestamp, - Role: database.RoleUser, - Content: "hello", - Provider: "", - Model: "", - } - - entry, err := repository.AppendMessage(ctx, createdSession.ID, nil, &message) - require.NoError(t, err) - assert.Equal(t, timestamp, entry.CreatedAt) - assert.Equal(t, timestamp, entry.Message.Timestamp) - - storedMessage, found, err := repository.MessageForEntry(ctx, createdSession.ID, entry.ID) - require.NoError(t, err) - require.True(t, found) - assert.Equal(t, timestamp, storedMessage.CreatedAt) + testCases := []struct { + name string + inputTime time.Time + wantTime time.Time + }{ + { + name: "utc timestamp", + inputTime: time.Date(2026, time.May, 31, 12, 34, 56, 789000000, time.UTC), + wantTime: time.Date(2026, time.May, 31, 12, 34, 56, 789000000, time.UTC), + }, + { + name: "offset timestamp is normalized to utc", + inputTime: time.Date(2026, time.May, 31, 5, 34, 56, 789000000, time.FixedZone("UTC-7", -7*60*60)), + wantTime: time.Date(2026, time.May, 31, 12, 34, 56, 789000000, time.UTC), + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + repository := newTestSessionRepository(t) + ctx := context.Background() + + createdSession, err := repository.CreateSession(ctx, "/work", "timestamp", "") + require.NoError(t, err) + + message := database.MessageEntity{ + Timestamp: tc.inputTime, + Role: database.RoleUser, + Content: "hello", + } + + entry, err := repository.AppendMessage(ctx, createdSession.ID, nil, &message) + require.NoError(t, err) + assert.Equal(t, tc.wantTime, entry.CreatedAt) + assert.Equal(t, tc.wantTime, entry.Message.Timestamp) + + storedEntry, found, err := repository.Entry(ctx, createdSession.ID, entry.ID) + require.NoError(t, err) + require.True(t, found) + assert.Equal(t, tc.wantTime, storedEntry.CreatedAt) + + storedMessage, found, err := repository.MessageForEntry(ctx, createdSession.ID, entry.ID) + require.NoError(t, err) + require.True(t, found) + assert.Equal(t, tc.wantTime, storedMessage.CreatedAt) + }) + } }As per coding guidelines,
**/*_test.go: Prefer table-driven tests for core behavior and regression tests for terminal rendering bugs.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/database/session_repository_test.go` around lines 90 - 117, Convert TestSessionRepository_AppendMessagePreservesInputTimestamp into a table-driven test that iterates over at least two cases (UTC and a non-UTC timezone timestamp); for each case call repository.AppendMessage (using the same MessageEntity.Timestamp values) and assert both the returned entry.CreatedAt and entry.Message.Timestamp, then read back the persisted row via repository.MessageForEntry and assert the persisted storedMessage.CreatedAt equals the original timestamp (normalized as expected) to cover the non-zero timezone normalization path; keep the test name and use repository.CreateSession, repository.AppendMessage, and repository.MessageForEntry to locate the code under test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/database/session_repository_test.go`:
- Around line 90-117: Convert
TestSessionRepository_AppendMessagePreservesInputTimestamp into a table-driven
test that iterates over at least two cases (UTC and a non-UTC timezone
timestamp); for each case call repository.AppendMessage (using the same
MessageEntity.Timestamp values) and assert both the returned entry.CreatedAt and
entry.Message.Timestamp, then read back the persisted row via
repository.MessageForEntry and assert the persisted storedMessage.CreatedAt
equals the original timestamp (normalized as expected) to cover the non-zero
timezone normalization path; keep the test name and use
repository.CreateSession, repository.AppendMessage, and
repository.MessageForEntry to locate the code under test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 04473572-6399-4d21-817e-7c9e29ca244e
📒 Files selected for processing (2)
internal/database/session_repository_test.gointernal/database/session_store.go
🚧 Files skipped from review as they are similar to previous changes (1)
- internal/database/session_store.go
|



Summary
Validation