Files
Cyrene_v2/internal/runtime/memory/timefmt_test.go
T
AskaEth 31086ed1ab fix(memory): 定宽时间戳存储 + rowid 确定性排序 + 遗留数据归一化
- formatTime 改用固定 30 字节布局(9 位小数秒),让 TEXT 列的字典序与时间序一致(RFC3339Nano 会去掉尾随零产生变宽字符串,秒内会反序)
- memory_items / project / project_chapter 的 ORDER BY 全部补 rowid 兜底,消除同 tick 打平时的顺序不确定
- 开库时幂等归一化变宽的历史值;无法解析的行原样保留,不阻塞开库
- 回归测试:变宽排序反例、同一时间戳插入序、归一化幂等与跳过
2026-10-03 16:08:50 +08:00

149 lines
4.9 KiB
Go

package memory
import (
"context"
"database/sql"
"testing"
"time"
"git.yeij.top/AskaEth/Cyrene_v2/internal/db"
)
// newTestDB opens an isolated in-memory SQLite DB for tests that must inspect
// the raw stored timestamp text.
func newTestDB(t *testing.T) *sql.DB {
t.Helper()
ctx := context.Background()
d, err := db.Open(ctx, ":memory:", testLogger())
if err != nil {
t.Fatalf("open in-memory db: %v", err)
}
t.Cleanup(func() { _ = d.Close() })
return d
}
// insertLegacyItemSQL writes a memory_items row with caller-supplied raw
// timestamps, standing in for a row persisted before the fixed-width layout.
const insertLegacyItemSQL = `INSERT INTO memory_items
(id, persona_id, role_id, project_id, scope, kind, content, tags, confidence,
source_conversation, created_at, updated_at, decay_weight)
VALUES (?, 'persona.a', '', '', 'persona', 'relationship', '旧数据', '[]', 1.0, '', ?, ?, 1.0)`
func TestFormatTime_定宽_正常(t *testing.T) {
t.Parallel()
tests := []struct {
name string
at time.Time
want string
}{
{"零小数秒", time.Date(2026, 9, 7, 3, 20, 5, 0, time.UTC), "2026-09-07T03:20:05.000000000Z"},
{"一位小数秒", time.Date(2026, 9, 7, 3, 20, 5, 830000000, time.UTC), "2026-09-07T03:20:05.830000000Z"},
{"纳秒", time.Date(2026, 9, 7, 3, 20, 5, 830978900, time.UTC), "2026-09-07T03:20:05.830978900Z"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := formatTime(tt.at)
if got != tt.want {
t.Errorf("formatTime = %q, want %q", got, tt.want)
}
if len(got) != storedTimeLen {
t.Errorf("formatTime length = %d, want %d", len(got), storedTimeLen)
}
})
}
}
func TestFormatTime_非UTC_正常(t *testing.T) {
t.Parallel()
zone := time.FixedZone("UTC+8", 8*60*60)
// 11:20:05.830+08:00 is 03:20:05.830Z, so the stored text is UTC.
at := time.Date(2026, 9, 7, 11, 20, 5, 830000000, zone)
if got, want := formatTime(at), "2026-09-07T03:20:05.830000000Z"; got != want {
t.Errorf("formatTime = %q, want %q", got, want)
}
}
// TestNewStore_归一化历史时间戳_正常 writes a legacy variable-width row directly,
// re-opens the store to trigger normalization, and checks both the repaired text
// and its idempotence on a further open.
func TestNewStore_归一化历史时间戳_正常(t *testing.T) {
t.Parallel()
ctx := context.Background()
d := newTestDB(t)
s, err := NewStore(ctx, d, testLogger())
if err != nil {
t.Fatalf("NewStore: %v", err)
}
// RFC3339Nano drops the trailing fractional zeros, so this legacy value is
// only 23 bytes wide and loses the text-ordering guarantee.
const legacy = "2026-09-07T03:20:05.83Z"
if _, err := d.ExecContext(ctx, insertLegacyItemSQL, "legacy", legacy, legacy); err != nil {
t.Fatalf("insert legacy row: %v", err)
}
if _, err := NewStore(ctx, d, testLogger()); err != nil {
t.Fatalf("NewStore (normalize): %v", err)
}
var raw string
if err := d.QueryRowContext(ctx,
`SELECT created_at FROM memory_items WHERE id = ?`, "legacy").Scan(&raw); err != nil {
t.Fatalf("read normalized created_at: %v", err)
}
const want = "2026-09-07T03:20:05.830000000Z"
if raw != want {
t.Errorf("normalized created_at = %q, want %q", raw, want)
}
got, err := s.Get(ctx, "legacy")
if err != nil {
t.Fatalf("Get: %v", err)
}
wantTime := time.Date(2026, 9, 7, 3, 20, 5, 830000000, time.UTC)
if !got.CreatedAt.Equal(wantTime) || !got.UpdatedAt.Equal(wantTime) {
t.Errorf("timestamps = %v/%v, want %v", got.CreatedAt, got.UpdatedAt, wantTime)
}
// A further open must be a no-op: the length condition selects nothing once
// the row is fixed width.
if _, err := NewStore(ctx, d, testLogger()); err != nil {
t.Fatalf("NewStore (second pass): %v", err)
}
var again string
if err := d.QueryRowContext(ctx,
`SELECT created_at FROM memory_items WHERE id = ?`, "legacy").Scan(&again); err != nil {
t.Fatalf("read created_at after second open: %v", err)
}
if again != raw {
t.Errorf("second open changed created_at: %q -> %q", raw, again)
}
}
// TestNewStore_无法解析的时间戳_跳过 pins the "leave it alone" rule: one bad legacy
// value must not keep the store from opening, and must not be rewritten.
func TestNewStore_无法解析的时间戳_跳过(t *testing.T) {
t.Parallel()
ctx := context.Background()
d := newTestDB(t)
if _, err := NewStore(ctx, d, testLogger()); err != nil {
t.Fatalf("NewStore: %v", err)
}
if _, err := d.ExecContext(ctx, insertLegacyItemSQL, "broken", "not-a-time", "not-a-time"); err != nil {
t.Fatalf("insert unparseable row: %v", err)
}
if _, err := NewStore(ctx, d, testLogger()); err != nil {
t.Fatalf("NewStore with unparseable timestamp: %v", err)
}
var raw string
if err := d.QueryRowContext(ctx,
`SELECT created_at FROM memory_items WHERE id = ?`, "broken").Scan(&raw); err != nil {
t.Fatalf("read created_at: %v", err)
}
if raw != "not-a-time" {
t.Errorf("created_at = %q, want the unparseable value left as is", raw)
}
}