Repository navigation
Expand file tree
/
Copy pathexport_test.go
More file actions
212 lines (186 loc) · 7.68 KB
/
Copy pathexport_test.go
File metadata and controls
212 lines (186 loc) · 7.68 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package lumen
import (
"encoding/json"
"strings"
"testing"
"time"
)
func TestExportJSON(t *testing.T) {
s, now := setupSearchStore(t)
data, err := s.ExportJSON(now)
if err != nil { t.Fatalf("ExportJSON: %v", err) }
var parsed map[string]interface{}
if err := json.Unmarshal(data, &parsed); err != nil {
t.Fatalf("Invalid JSON output: %v", err)
}
if _, ok := parsed["beliefs"]; !ok { t.Error("JSON should contain 'beliefs'") }
if _, ok := parsed["records"]; !ok { t.Error("JSON should contain 'records'") }
t.Logf("JSON export: %d bytes, %d beliefs", len(data), int(parsed["beliefs"].([]interface{})[0].(map[string]interface{})["confidence"].(float64)*100))
}
func TestExportMarkdown(t *testing.T) {
s, now := setupSearchStore(t)
md := s.ExportMarkdown("Test Knowledge Base", now)
if !strings.Contains(md, "# Test Knowledge Base") { t.Error("title missing") }
if !strings.Contains(md, "**") { t.Error("bold confidence values missing") }
if !strings.Contains(md, "Records") { t.Error("records section missing") }
t.Logf("Markdown:\n%s", md)
}
func TestExportLM(t *testing.T) {
s, now := setupSearchStore(t)
lm := s.ExportLM(now)
if !strings.Contains(lm, "record ") { t.Error("records missing") }
if !strings.Contains(lm, "believe ") { t.Error("beliefs missing") }
if !strings.Contains(lm, "confidence:") { t.Error("confidence missing") }
t.Logf("LM export: %d bytes", len(lm))
}
func TestExportVersionHistory(t *testing.T) {
s, now := setupSearchStore(t)
// Create some version history
s.Revise("r1", "Updated Cogitate finding", 0, "new analysis", now.Add(3600))
s.ReAssert("iit-weakened", "IIT is strongly weakened by updated Cogitate findings.", 0.75, now.Add(7200))
h, err := s.BeliefHistory("iit-weakened")
if err != nil { t.Fatalf("BeliefHistory: %v", err) }
t.Logf("Version history for iit-weakened (%d versions):", len(h))
t.Log(RenderHistory("iit-weakened", h))
// Export and verify versions appear in JSON
data, _ := s.ExportJSON(now.Add(7200))
if !strings.Contains(string(data), "iit-weakened") {
t.Error("exported belief should include iit-weakened")
}
}
func TestExportLMRoundtrip(t *testing.T) {
// Build a store with frames, records (including foundational), and beliefs.
// Export to .lm, re-parse, verify the same beliefs are present.
s := NewStore()
s.RegisterFrame(Frame{
Name: "philo", Decay: DecayPolicy{Kind: DecayNone},
})
s.RegisterFrame(Frame{
Name: "empirical",
Decay: DecayPolicy{Kind: DecayExponential, Halflife: 365 * 24 * time.Hour},
OnStaleDerivation: StaleMarkSuspect,
})
now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
_ = s.Assert(&Record{ID: "r-axiom", Content: "First principles.", Frame: "philo", Timestamp: now, Foundational: true})
_ = s.Assert(&Record{ID: "r-data", Content: "Empirical observation.", Frame: "empirical", Timestamp: now})
_ = s.Believe(&Belief{ID: "b1", Content: "Core belief.", Confidence: 0.80, Frame: "philo", AssertedAt: now, Derivation: []string{"r-axiom"}})
_ = s.Believe(&Belief{ID: "b2", Content: "Empirical belief.", Confidence: 0.65, Frame: "empirical", AssertedAt: now, Derivation: []string{"r-data", "b1"}})
exported := s.ExportLM(now)
t.Logf("exported .lm:\n%s", exported)
// Parse the exported .lm.
s2 := NewStore()
if err := LoadFile(exported, s2, now); err != nil {
t.Fatalf("LoadFile on exported .lm: %v", err)
}
// Verify frames.
s2.mu.RLock()
_, hasPhilo := s2.frames["philo"]
_, hasEmpirical := s2.frames["empirical"]
s2.mu.RUnlock()
if !hasPhilo || !hasEmpirical {
t.Errorf("frames missing after round-trip: philo=%v empirical=%v", hasPhilo, hasEmpirical)
}
// Verify records.
s2.mu.RLock()
axiom, hasAxiom := s2.records["r-axiom"]
_, hasData := s2.records["r-data"]
s2.mu.RUnlock()
if !hasAxiom || !hasData {
t.Errorf("records missing after round-trip")
}
if axiom != nil && !axiom.Foundational {
t.Error("r-axiom should be foundational after round-trip")
}
// Verify beliefs.
r1, err := s2.Query("b1", now)
if err != nil {
t.Fatalf("b1 not found after round-trip: %v", err)
}
if r1.CurrentConfidence < 0.78 || r1.CurrentConfidence > 0.82 {
t.Errorf("b1 confidence %.3f not in [0.78, 0.82]", r1.CurrentConfidence)
}
r2, err := s2.Query("b2", now)
if err != nil {
t.Fatalf("b2 not found after round-trip: %v", err)
}
if r2.CurrentConfidence < 0.63 || r2.CurrentConfidence > 0.67 {
t.Errorf("b2 confidence %.3f not in [0.63, 0.67]", r2.CurrentConfidence)
}
// Verify on_stale_derivation survived.
s2.mu.RLock()
ef := s2.frames["empirical"]
s2.mu.RUnlock()
if ef.OnStaleDerivation != StaleMarkSuspect {
t.Errorf("OnStaleDerivation: got %q, want mark_suspect", ef.OnStaleDerivation)
}
// Verify named query survived export/import.
// First register a query in the original store and re-export.
s.AddQuery(ParsedQuery{ID: "test-query", Target: "b1", Select: "confidence-changes", Since: "2026-01-01T00:00:00Z"})
exported2 := s.ExportLM(now)
s3 := NewStore()
if err := LoadFile(exported2, s3, now); err != nil {
t.Fatalf("LoadFile with queries: %v", err)
}
q, ok := s3.GetQuery("test-query")
if !ok {
t.Error("named query should survive ExportLM/LoadFile round-trip")
} else if q.Target != "b1" || q.Select != "confidence-changes" {
t.Errorf("query round-trip: target=%q select=%q", q.Target, q.Select)
}
t.Log("ExportLM round-trip: all checks passed")
}
// TestExportLMComposedRoundtrip verifies that composition metadata
// (prior + evidence) and non-exponential decay kinds survive a full
// export → parse → load cycle. Before this, a composed belief silently
// round-tripped as a plain belief and FragilityScan lost its exact path.
func TestExportLMComposedRoundtrip(t *testing.T) {
s := NewStore()
now := time.Now()
s.RegisterFrame(Frame{Name: "reasoning", Decay: DecayPolicy{Kind: DecayNone}})
s.RegisterFrame(Frame{Name: "lin", Decay: DecayPolicy{Kind: DecayLinear, Rate: 0.01}})
s.RegisterFrame(Frame{Name: "st", Decay: DecayPolicy{Kind: DecayStep, StepAt: 30 * 24 * time.Hour, StepTo: 0.2}})
r1 := &Record{ID: "r-x", Frame: "reasoning", Content: "Evidence X", Timestamp: now}
if err := s.Assert(r1); err != nil { t.Fatal(err) }
b := &Belief{
ID: "b-composed", Content: "A composed claim", Confidence: 0.8,
Frame: "reasoning", AssertedAt: now, Derivation: []string{"r-x"},
}
evidence := []Evidence{{SourceID: "r-x", Confidence: 0.8, LikelihoodRatio: 3.0}}
if _, err := s.BelieveComposed(b, 0.5, evidence); err != nil { t.Fatal(err) }
exported := s.ExportLM(now)
// Exported text must carry the composition metadata and all decay kinds.
for _, want := range []string{"prior: [0.5000, 0.5000]", "evidence r-x", "lr: 3", "decay: linear rate: 0.01", "decay: step at: 30d to: 0.2"} {
if !strings.Contains(exported, want) {
t.Errorf("export missing %q\n---\n%s", want, exported)
}
}
// Round-trip: load into a fresh store.
s2 := NewStore()
if err := LoadFile(exported, s2, now); err != nil {
t.Fatalf("re-import failed: %v\n---\n%s", err, exported)
}
s2.mu.RLock()
loaded := s2.beliefs["b-composed"]
s2.mu.RUnlock()
if loaded == nil { t.Fatal("belief lost in round-trip") }
if len(loaded.CompositionEvidence) != 1 {
t.Fatalf("composition evidence lost: got %d blocks", len(loaded.CompositionEvidence))
}
if loaded.CompositionEvidence[0].SourceID != "r-x" {
t.Errorf("evidence source: want r-x, got %s", loaded.CompositionEvidence[0].SourceID)
}
if loaded.CompositionPrior != 0.5 {
t.Errorf("prior: want 0.5, got %g", loaded.CompositionPrior)
}
// Decay kinds must survive.
s2.mu.RLock()
lin := s2.frames["lin"]
st := s2.frames["st"]
s2.mu.RUnlock()
if lin.Decay.Kind != DecayLinear || lin.Decay.Rate != 0.01 {
t.Errorf("linear decay lost: %+v", lin.Decay)
}
if st.Decay.Kind != DecayStep || st.Decay.StepTo != 0.2 {
t.Errorf("step decay lost: %+v", st.Decay)
}
}