Repository navigation
Expand file tree
/
Copy pathsession_index_cleanup_test.go
More file actions
171 lines (163 loc) · 6.12 KB
/
Copy pathsession_index_cleanup_test.go
File metadata and controls
171 lines (163 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func TestKnownSessionIndexCandidateRequiresExactSchema(t *testing.T) {
valid := []byte(`{"id":"thread-1","thread_name":"Task","updated_at":"2026-08-02T00:00:00Z"}`)
candidate, ok := knownSessionIndexCandidate(valid)
if !ok || candidate.ID != "thread-1" || candidate.ThreadName != "Task" {
t.Fatalf("valid candidate = %#v, %v", candidate, ok)
}
invalid := [][]byte{
[]byte(`{"id":"thread-1","thread_name":"Task"}`),
[]byte(`{"id":"thread-1","thread_name":"Task","updated_at":"now","extra":true}`),
[]byte(`{"id":"","thread_name":"Task","updated_at":"now"}`),
[]byte(`{"id":"thread-1","thread_name":"Task","updated_at":1}`),
}
for _, line := range invalid {
if _, ok := knownSessionIndexCandidate(line); ok {
t.Fatalf("unexpected candidate for %s", line)
}
}
}
func TestPreviewSessionIndexCleanupProtectsRolloutAndSQLiteReferences(t *testing.T) {
home := t.TempDir()
filenameID := "018f2f15-b580-7000-8000-000000000001"
metaID := "018f2f15-b580-7000-8000-000000000002"
dbID := "018f2f15-b580-7000-8000-000000000003"
orphanID := "018f2f15-b580-7000-8000-000000000004"
rollout := filepath.Join(home, "sessions", "2026", "rollout-prefix-"+filenameID+".jsonl")
if err := os.MkdirAll(filepath.Dir(rollout), 0o755); err != nil {
t.Fatal(err)
}
meta := `{"type":"session_meta","payload":{"id":"` + metaID + `"}}` + "\n"
if err := os.WriteFile(rollout, []byte(meta), 0o600); err != nil {
t.Fatal(err)
}
dbPath := filepath.Join(home, "sqlite", "references.db")
if err := os.MkdirAll(filepath.Dir(dbPath), 0o755); err != nil {
t.Fatal(err)
}
db, err := openSQLite(dbPath)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE thread_spawn_edges (parent_thread_id TEXT, child_thread_id TEXT); INSERT INTO thread_spawn_edges VALUES (?, '')`, dbID); err != nil {
db.Close()
t.Fatal(err)
}
if err := db.Close(); err != nil {
t.Fatal(err)
}
lines := []string{
sessionIndexLine(filenameID, "Filename live"),
sessionIndexLine(metaID, "Metadata live"),
sessionIndexLine(dbID, "SQLite live"),
sessionIndexLine(orphanID, "Orphan"),
`{"id":"unknown-shape","thread_name":"Do not touch","updated_at":"now","source":"cloud"}`,
}
if err := os.WriteFile(filepath.Join(home, "session_index.jsonl"), []byte(strings.Join(lines, "\r\n")+"\r\n"), 0o600); err != nil {
t.Fatal(err)
}
preview, err := previewSessionIndexCleanup(home)
if err != nil {
t.Fatal(err)
}
if len(preview.Candidates) != 1 || preview.Candidates[0].ID != orphanID {
t.Fatalf("cleanup candidates = %#v", preview.Candidates)
}
}
func TestApplySessionIndexCleanupBacksUpAndPreservesUnknownLines(t *testing.T) {
home := t.TempDir()
orphanID := "018f2f15-b580-7000-8000-000000000010"
unknown := `{"id":"cloud-only","thread_name":"Cloud","updated_at":"now","source":"remote"}`
original := sessionIndexLine(orphanID, "Orphan") + "\r\n" + unknown
path := filepath.Join(home, "session_index.jsonl")
if err := os.WriteFile(path, []byte(original), 0o600); err != nil {
t.Fatal(err)
}
preview, err := previewSessionIndexCleanup(home)
if err != nil {
t.Fatal(err)
}
result, err := applySessionIndexCleanup(home, preview.SnapshotSHA256, []string{orphanID}, false)
if err != nil {
t.Fatal(err)
}
if result.PrunedEntries != 1 || result.BackupDir == "" {
t.Fatalf("cleanup result = %#v", result)
}
next, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(next) != unknown {
t.Fatalf("unexpected filtered index: %q", next)
}
backup, err := os.ReadFile(filepath.Join(result.BackupDir, "session_index.jsonl"))
if err != nil {
t.Fatal(err)
}
if string(backup) != original {
t.Fatalf("backup = %q, want %q", backup, original)
}
if !fileExists(filepath.Join(result.BackupDir, "metadata.json")) {
t.Fatal("cleanup metadata backup is missing")
}
}
func TestApplySessionIndexCleanupRejectsSnapshotAndLastMomentChanges(t *testing.T) {
home := t.TempDir()
id := "018f2f15-b580-7000-8000-000000000020"
path := filepath.Join(home, "session_index.jsonl")
if err := os.WriteFile(path, []byte(sessionIndexLine(id, "Orphan")+"\n"), 0o600); err != nil {
t.Fatal(err)
}
preview, err := previewSessionIndexCleanup(home)
if err != nil {
t.Fatal(err)
}
if _, err := applySessionIndexCleanup(home, "stale", []string{id}, false); err == nil || !strings.Contains(err.Error(), "预览后发生变化") {
t.Fatalf("snapshot mismatch error = %v", err)
}
previous := sessionIndexCleanupBeforeWrite
sessionIndexCleanupBeforeWrite = func() {
_ = os.WriteFile(path, []byte(sessionIndexLine(id, "Changed")+"\n"), 0o600)
}
t.Cleanup(func() { sessionIndexCleanupBeforeWrite = previous })
result, err := applySessionIndexCleanup(home, preview.SnapshotSHA256, []string{id}, false)
if err == nil || !strings.Contains(err.Error(), "写入前再次发生变化") {
t.Fatalf("last moment mutation result = %#v, error = %v", result, err)
}
var applyErr *sessionIndexCleanupApplyError
if !errors.As(err, &applyErr) || applyErr.BackupDir == "" {
t.Fatalf("mutation error should retain backup: %#v", err)
}
}
func TestApplySessionIndexCleanupBlocksWhileCodexIsRunning(t *testing.T) {
home := t.TempDir()
id := "018f2f15-b580-7000-8000-000000000030"
path := filepath.Join(home, "session_index.jsonl")
if err := os.WriteFile(path, []byte(sessionIndexLine(id, "Orphan")+"\n"), 0o600); err != nil {
t.Fatal(err)
}
preview, err := previewSessionIndexCleanup(home)
if err != nil {
t.Fatal(err)
}
previous := detectProviderSyncActiveProcesses
detectProviderSyncActiveProcesses = func() ([]string, error) { return []string{"ChatGPT"}, nil }
t.Cleanup(func() { detectProviderSyncActiveProcesses = previous })
if _, err := applySessionIndexCleanup(home, preview.SnapshotSHA256, []string{id}, true); err == nil || !strings.Contains(err.Error(), "完全退出") {
t.Fatalf("active process should block cleanup: %v", err)
}
if fileExists(filepath.Join(home, "backups_state", "provider-sync")) {
t.Fatal("blocked cleanup should not create a backup")
}
}
func sessionIndexLine(id, title string) string {
return `{"id":"` + id + `","thread_name":"` + title + `","updated_at":"2026-08-02T00:00:00Z"}`
}