返回 DeepSeek-Reasonix
activity_time_test.go
根目录 / internal / sessioncatalog / activity_time_test.go
1 package sessioncatalog
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "testing"
8 "time"
9
10 "reasonix/internal/agent"
11 )
12
13 func TestReconcileKeepsSidecarActivityWhenFileMtimeIsNewer(t *testing.T) {
14 t.Parallel()
15 ctx := context.Background()
16 dir := t.TempDir()
17 path := filepath.Join(dir, "old.jsonl")
18 if err := os.WriteFile(path, []byte(`{"role":"user","content":"hi"}`+"\n"), 0o600); err != nil {
19 t.Fatal(err)
20 }
21 activity := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
22 if err := agent.SaveBranchMetaPreserveUpdated(path, agent.BranchMeta{
23 CreatedAt: activity,
24 UpdatedAt: activity,
25 Scope: "global",
26 TopicID: "topic-old",
27 TopicTitle: "old",
28 SchemaVersion: agent.BranchMetaCountsVersion,
29 Turns: 1,
30 Preview: "hi",
31 }); err != nil {
32 t.Fatal(err)
33 }
34 later := activity.Add(48 * time.Hour)
35 if err := os.Chtimes(path, later, later); err != nil {
36 t.Fatal(err)
37 }
38
39 catalog, err := Open(ctx, Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), DisableRepair: true})
40 if err != nil {
41 t.Fatal(err)
42 }
43 t.Cleanup(func() { _ = catalog.Close(context.Background()) })
44 if err := catalog.ReconcileDirectory(ctx, DirectoryTarget{Path: dir, Scope: "global"}); err != nil {
45 t.Fatal(err)
46 }
47 record, ok, err := catalog.GetSession(ctx, path)
48 if err != nil || !ok {
49 t.Fatalf("GetSession: ok=%v err=%v", ok, err)
50 }
51 if record.LastActivityAt != activity.UnixMilli() {
52 t.Fatalf("lastActivityAt = %d, want sidecar %d; file mtime must not raise known activity", record.LastActivityAt, activity.UnixMilli())
53 }
54 }
55
56 func TestReconcileFillsZeroActivityFromFileMtime(t *testing.T) {
57 t.Parallel()
58 ctx := context.Background()
59 dir := t.TempDir()
60 path := filepath.Join(dir, "bare.jsonl")
61 if err := os.WriteFile(path, []byte(`{"role":"user","content":"hi"}`+"\n"), 0o600); err != nil {
62 t.Fatal(err)
63 }
64 when := time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC)
65 if err := os.Chtimes(path, when, when); err != nil {
66 t.Fatal(err)
67 }
68
69 catalog, err := Open(ctx, Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), DisableRepair: true})
70 if err != nil {
71 t.Fatal(err)
72 }
73 t.Cleanup(func() { _ = catalog.Close(context.Background()) })
74 if err := catalog.ReconcileDirectory(ctx, DirectoryTarget{Path: dir, Scope: "global"}); err != nil {
75 t.Fatal(err)
76 }
77 record, ok, err := catalog.GetSession(ctx, path)
78 if err != nil || !ok {
79 t.Fatalf("GetSession: ok=%v err=%v", ok, err)
80 }
81 if record.LastActivityAt != when.UnixMilli() {
82 t.Fatalf("lastActivityAt = %d, want file mtime %d", record.LastActivityAt, when.UnixMilli())
83 }
84 }
85
86 func TestReconcileExternalTouchDoesNotReorderTopics(t *testing.T) {
87 t.Parallel()
88 ctx := context.Background()
89 dir := t.TempDir()
90 write := func(name, topicID string, activity time.Time) string {
91 path := filepath.Join(dir, name+".jsonl")
92 if err := os.WriteFile(path, []byte(`{"role":"user","content":"hi"}`+"\n"), 0o600); err != nil {
93 t.Fatal(err)
94 }
95 if err := agent.SaveBranchMetaPreserveUpdated(path, agent.BranchMeta{
96 CreatedAt: activity, UpdatedAt: activity, Scope: "global", TopicID: topicID,
97 TopicTitle: topicID, SchemaVersion: agent.BranchMetaCountsVersion, Turns: 1,
98 }); err != nil {
99 t.Fatal(err)
100 }
101 return path
102 }
103 oldActivity := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
104 oldPath := write("old", "topic-old", oldActivity)
105 newActivity := oldActivity.Add(24 * time.Hour)
106 write("new", "topic-new", newActivity)
107 if err := os.Chtimes(oldPath, newActivity.Add(24*time.Hour), newActivity.Add(24*time.Hour)); err != nil {
108 t.Fatal(err)
109 }
110
111 catalog, err := Open(ctx, Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), DisableRepair: true})
112 if err != nil {
113 t.Fatal(err)
114 }
115 t.Cleanup(func() { _ = catalog.Close(context.Background()) })
116 if err := catalog.ReconcileDirectory(ctx, DirectoryTarget{Path: dir, Scope: "global"}); err != nil {
117 t.Fatal(err)
118 }
119 page, err := catalog.ListTopics(ctx, TopicPageRequest{Scope: "global", Limit: 10})
120 if err != nil {
121 t.Fatal(err)
122 }
123 if len(page.Items) != 2 || page.Items[0].TopicID != "topic-new" {
124 t.Fatalf("topic order = %#v, want authoritative newer topic first", page.Items)
125 }
126 if page.Items[1].LastActivityAt != oldActivity.UnixMilli() {
127 t.Fatalf("old activity = %d, want %d", page.Items[1].LastActivityAt, oldActivity.UnixMilli())
128 }
129 }
130
130 lines GO