返回 DeepSeek-Reasonix
duplicate_source_test.go
根目录 / desktop / internal / workspacestate / duplicate_source_test.go
1 package workspacestate
2
3 import (
4 "bytes"
5 "crypto/sha256"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12 )
13
14 // reporterState registers one source file under three keys by three spellings
15 // of its path, owned by two sessions.
16 func reporterState(t *testing.T, liveSecond bool) (State, string, func(string) string) {
17 t.Helper()
18 dir := t.TempDir()
19 source := filepath.Join(dir, "sessions", "20260919-053801.jsonl")
20 if err := os.MkdirAll(filepath.Dir(source), 0o700); err != nil {
21 t.Fatal(err)
22 }
23 if err := os.WriteFile(source, []byte("{}\n"), 0o600); err != nil {
24 t.Fatal(err)
25 }
26 physical, err := sourcePathKey(source)
27 if err != nil {
28 t.Fatal(err)
29 }
30 spelled := filepath.Join(dir, "sessions", "..", "sessions", "20260919-053801.jsonl")
31 key := func(head string) string { return fmt.Sprintf("%x", sha256.Sum256([]byte(physical+"\x00"+head))) }
32 state := newState()
33 add := func(sourceKey, path, head, session string) {
34 state.SourceMappings[sourceKey] = SourceMapping{
35 SourceKey: sourceKey, Path: path, HeadID: head, Format: "canonical",
36 Fingerprint: "fp", SessionID: session, WorkspaceID: GlobalWorkspaceID,
37 }
38 }
39 add("legacy-lowercase", spelled, "", "kept")
40 add("legacy-main", source, "main", "duplicate")
41 add("legacy-ulid", source, "01M2W7FH2V02S5E06JPVZYMFNT", "kept")
42 state.SessionStates["kept"] = SessionState{Lifecycle: Active, Generation: 64}
43 state.SessionStates["duplicate"] = SessionState{Lifecycle: Archived, Generation: 91}
44 if liveSecond {
45 state.SessionStates["duplicate"] = SessionState{Lifecycle: Active, Generation: 91}
46 }
47 return state, source, key
48 }
49
50 func TestDuplicateSourceMappingsResolveToTheLiveOwner(t *testing.T) {
51 state, _, key := reporterState(t, false)
52 file := filepath.Join(t.TempDir(), "state.json")
53 body, err := json.Marshal(state)
54 if err != nil {
55 t.Fatal(err)
56 }
57 if err := os.WriteFile(file, body, 0o600); err != nil {
58 t.Fatal(err)
59 }
60 view, err := NewStore(file).loadSnapshot(t.Context(), false)
61 if err != nil {
62 t.Fatal(err)
63 }
64 mapping, found, err := view.ResolveSource(key(""))
65 if err != nil || !found || mapping.SessionID != "kept" {
66 t.Fatalf("resolved %+v found=%v err=%v, want the live session", mapping, found, err)
67 }
68 if after, err := os.ReadFile(file); err != nil || !bytes.Equal(after, body) {
69 t.Fatal("resolving duplicates rewrote the persisted receipts")
70 }
71 if len(view.SourceMappings) != 3 {
72 t.Fatal("a duplicate receipt was dropped")
73 }
74 }
75
76 func TestDuplicateSourceMappingsStayAmbiguousWithoutOneLiveOwner(t *testing.T) {
77 both, _, key := reporterState(t, true)
78 if _, found, err := both.ResolveSource(key("")); found || !errors.Is(err, ErrMutationConflict) {
79 t.Fatalf("two live owners: found=%v err=%v", found, err)
80 }
81 moved, _, key := reporterState(t, false)
82 duplicate := moved.SourceMappings["legacy-main"]
83 duplicate.Fingerprint = "other"
84 moved.SourceMappings["legacy-main"] = duplicate
85 moved.SessionStates["duplicate"] = SessionState{Lifecycle: Active}
86 if _, found, err := moved.ResolveSource(key("")); found || !errors.Is(err, ErrMutationConflict) {
87 t.Fatalf("different content: found=%v err=%v", found, err)
88 }
89 none, _, key := reporterState(t, false)
90 none.SessionStates["kept"] = SessionState{Lifecycle: Archived}
91 if _, found, err := none.ResolveSource(key("")); found || !errors.Is(err, ErrMutationConflict) {
92 t.Fatalf("no live owner: found=%v err=%v", found, err)
93 }
94 }
95
96 func TestDuplicateSourceSettlementLeavesRetiredReceiptsAddressable(t *testing.T) {
97 state, _, key := reporterState(t, false)
98 mapping, _, _ := state.ResolveSource(key(""))
99 if mapping.SessionID == "duplicate" {
100 t.Fatal("lookup by alias resolved to the retired copy")
101 }
102 retired, found, err := state.ResolveSource("legacy-main")
103 if err != nil || !found || retired.SessionID != "duplicate" || retired.HeadID != "main" {
104 t.Fatalf("exact durable key resolved %+v found=%v err=%v", retired, found, err)
105 }
106 if mapping.HeadID == retired.HeadID {
107 t.Fatal("alias lookup returned the retired copy's head, so head-checked callers would act on it")
108 }
109 }
110
110 lines GO