recallnest 2.3.0 → 2.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +150 -0
- package/README.md +17 -31
- package/README_CN.md +12 -22
- package/assets/ui/app.js +1031 -0
- package/assets/ui/index.html +224 -0
- package/assets/ui/styles.css +781 -0
- package/config.json.example +4 -1
- package/docs/api-reference.md +1291 -0
- package/docs/architecture.md +168 -0
- package/docs/connector-spec.md +126 -0
- package/eval/cases-canary.json +58 -0
- package/eval/cases-scope-robustness.json +278 -0
- package/eval/cases.json +117 -20
- package/package.json +22 -1
- package/src/__tests__/access-tracker-novelty.test.ts +238 -0
- package/src/__tests__/activity-counter.test.ts +125 -0
- package/src/__tests__/adaptive-fidelity.test.ts +102 -0
- package/src/__tests__/admission-control.test.ts +206 -0
- package/src/__tests__/anchor-generator.test.ts +67 -0
- package/src/__tests__/auto-consolidation.test.ts +168 -0
- package/src/__tests__/auto-recall.test.ts +131 -0
- package/src/__tests__/babel-integration.test.ts +144 -0
- package/src/__tests__/batch-dedup.test.ts +65 -0
- package/src/__tests__/canary-eval.test.ts +114 -0
- package/src/__tests__/capture-engine.test.ts +1238 -0
- package/src/__tests__/capture-heuristic.test.ts +171 -0
- package/src/__tests__/cascade-forget.test.ts +210 -0
- package/src/__tests__/category-min-scores.test.ts +33 -0
- package/src/__tests__/cc8-cc1-essential-context.test.ts +298 -0
- package/src/__tests__/circuit-breaker.test.ts +99 -0
- package/src/__tests__/cluster-consolidation.test.ts +782 -0
- package/src/__tests__/confidence-metadata.test.ts +125 -0
- package/src/__tests__/confidence-tracker.test.ts +261 -0
- package/src/__tests__/conflict-advisor.test.ts +206 -0
- package/src/__tests__/conflict-escalation.test.ts +149 -0
- package/src/__tests__/conflict-lifecycle.test.ts +83 -0
- package/src/__tests__/conflict-output.test.ts +85 -0
- package/src/__tests__/conflict-store.test.ts +374 -0
- package/src/__tests__/connector-v1.test.ts +181 -0
- package/src/__tests__/consolidation-engine.test.ts +333 -0
- package/src/__tests__/constructive-retrieval.test.ts +509 -0
- package/src/__tests__/context-collapse-renderer.test.ts +214 -0
- package/src/__tests__/context-composer-pins.test.ts +64 -0
- package/src/__tests__/context-composer-task-selection.test.ts +24 -0
- package/src/__tests__/context-reconstructor.test.ts +65 -0
- package/src/__tests__/context-renderer.test.ts +91 -0
- package/src/__tests__/continuity-eval.test.ts +275 -0
- package/src/__tests__/continuity-schema.test.ts +97 -0
- package/src/__tests__/contracts.test.ts +216 -0
- package/src/__tests__/conversation-importer.test.ts +692 -0
- package/src/__tests__/core-recall-eval.test.ts +235 -0
- package/src/__tests__/core-summary.test.ts +109 -0
- package/src/__tests__/data-checkup.test.ts +276 -0
- package/src/__tests__/decay-exemption.test.ts +84 -0
- package/src/__tests__/dedup-actions.test.ts +58 -0
- package/src/__tests__/dedup-guard.test.ts +39 -0
- package/src/__tests__/deterministic-id.test.ts +39 -0
- package/src/__tests__/distill-lock.test.ts +305 -0
- package/src/__tests__/distill-session-argshape.test.ts +79 -0
- package/src/__tests__/doctor.test.ts +319 -0
- package/src/__tests__/dream-pipeline.test.ts +321 -0
- package/src/__tests__/drill-down.test.ts +108 -0
- package/src/__tests__/embedder.test.ts +122 -0
- package/src/__tests__/emotion-decay.test.ts +46 -0
- package/src/__tests__/emotion-detector.test.ts +56 -0
- package/src/__tests__/emotion-phase1-integration.test.ts +153 -0
- package/src/__tests__/emotion-retrieval.test.ts +56 -0
- package/src/__tests__/entity-resolver.test.ts +58 -0
- package/src/__tests__/env-config.test.ts +151 -0
- package/src/__tests__/error-signature-boost.test.ts +87 -0
- package/src/__tests__/error-signature.test.ts +95 -0
- package/src/__tests__/error-taxonomy.test.ts +129 -0
- package/src/__tests__/ethics-layer.test.ts +476 -0
- package/src/__tests__/event-segmenter.test.ts +153 -0
- package/src/__tests__/feature-flags-integration.test.ts +54 -0
- package/src/__tests__/frequency-tracker.test.ts +113 -0
- package/src/__tests__/graph-export.test.ts +489 -0
- package/src/__tests__/hotness-blend.test.ts +76 -0
- package/src/__tests__/ingest-chunking.test.ts +44 -0
- package/src/__tests__/ingest.test.ts +360 -0
- package/src/__tests__/interference-detector.test.ts +203 -0
- package/src/__tests__/kg-extractor.test.ts +192 -0
- package/src/__tests__/kg-store.test.ts +300 -0
- package/src/__tests__/llm-consolidation.test.ts +226 -0
- package/src/__tests__/maintenance-locks.test.ts +174 -0
- package/src/__tests__/mcp-efficiency.test.ts +288 -0
- package/src/__tests__/mcp-related-scopes.test.ts +151 -0
- package/src/__tests__/memory-boundaries.test.ts +250 -0
- package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +345 -0
- package/src/__tests__/memory-evolution.test.ts +271 -0
- package/src/__tests__/memory-health-rebalance.test.ts +144 -0
- package/src/__tests__/memory-lint.test.ts +421 -0
- package/src/__tests__/memory-output.test.ts +215 -0
- package/src/__tests__/memory-promotion.test.ts +315 -0
- package/src/__tests__/migrate-dedup-ids.test.ts +65 -0
- package/src/__tests__/multi-vector.test.ts +242 -0
- package/src/__tests__/narrative-architecture.test.ts +456 -0
- package/src/__tests__/noise-filter.test.ts +205 -0
- package/src/__tests__/obsidian-connector.test.ts +363 -0
- package/src/__tests__/omitted-reporting.test.ts +98 -0
- package/src/__tests__/pending-review.test.ts +77 -0
- package/src/__tests__/pii-redact.test.ts +314 -0
- package/src/__tests__/post-retrieval-filter.test.ts +178 -0
- package/src/__tests__/ppr-traversal.test.ts +256 -0
- package/src/__tests__/prediction-engine.test.ts +349 -0
- package/src/__tests__/predictive-prospective-memory.test.ts +416 -0
- package/src/__tests__/preference-guard-smoke.test.ts +211 -0
- package/src/__tests__/preference-matcher.test.ts +230 -0
- package/src/__tests__/preference-slots.test.ts +205 -0
- package/src/__tests__/prospective-memory.test.ts +283 -0
- package/src/__tests__/query-entity-detector.test.ts +171 -0
- package/src/__tests__/query-expander-alias.test.ts +146 -0
- package/src/__tests__/query-expander.test.ts +34 -0
- package/src/__tests__/recall-governor.test.ts +227 -0
- package/src/__tests__/recall-mode.test.ts +64 -0
- package/src/__tests__/result-synthesizer.test.ts +198 -0
- package/src/__tests__/resume-context.test.ts +4898 -0
- package/src/__tests__/retrieval-trace.test.ts +213 -0
- package/src/__tests__/retriever-boundary-weight.test.ts +136 -0
- package/src/__tests__/retroactive-boost.test.ts +223 -0
- package/src/__tests__/salience-scoring.test.ts +106 -0
- package/src/__tests__/scope-inventory.test.ts +200 -0
- package/src/__tests__/scope-policy.test.ts +69 -0
- package/src/__tests__/scope-suggester.test.ts +80 -0
- package/src/__tests__/session-checkpoint.test.ts +291 -0
- package/src/__tests__/session-distiller.test.ts +640 -0
- package/src/__tests__/session-gc.test.ts +215 -0
- package/src/__tests__/skill-engine.test.ts +552 -0
- package/src/__tests__/skill-promotion.test.ts +601 -0
- package/src/__tests__/skill-verifier.test.ts +122 -0
- package/src/__tests__/source-heartbeat.test.ts +250 -0
- package/src/__tests__/store-patch-metadata.test.ts +205 -0
- package/src/__tests__/store-scope-audit.test.ts +81 -0
- package/src/__tests__/store-upsert.test.ts +184 -0
- package/src/__tests__/supersede-chain.test.ts +154 -0
- package/src/__tests__/temporal-parser.test.ts +241 -0
- package/src/__tests__/temporal-validity.test.ts +160 -0
- package/src/__tests__/term-registry.test.ts +121 -0
- package/src/__tests__/tool-error-surfacing.test.ts +98 -0
- package/src/__tests__/tool-output-compressor.test.ts +160 -0
- package/src/__tests__/topic-tag.test.ts +113 -0
- package/src/__tests__/trusted-memory.test.ts +839 -0
- package/src/__tests__/usage-tracker.test.ts +287 -0
- package/src/__tests__/version-manager.test.ts +213 -0
- package/src/__tests__/workflow-observation-skill-outcome.test.ts +352 -0
- package/src/__tests__/workflow-observation.test.ts +433 -0
- package/src/__tests__/write-verifier.test.ts +122 -0
- package/src/access-tracker.ts +38 -32
- package/src/activity-counter.ts +46 -18
- package/src/aliases.ts +133 -0
- package/src/api-server.ts +27 -7
- package/src/audit-log.ts +4 -2
- package/src/auto-consolidation.ts +8 -3
- package/src/auto-gc.ts +85 -21
- package/src/capture-engine.ts +54 -21
- package/src/cli.ts +169 -37
- package/src/confidence-tracker.ts +36 -18
- package/src/consolidation-engine.ts +109 -11
- package/src/context-composer.ts +8 -1
- package/src/data-checkup.ts +95 -6
- package/src/distill-lock.ts +194 -15
- package/src/doctor.ts +9 -2
- package/src/dream-pipeline.ts +118 -34
- package/src/env-config.ts +77 -0
- package/src/error-signature.ts +153 -0
- package/src/error-taxonomy.ts +115 -0
- package/src/eval.ts +251 -6
- package/src/idempotency.ts +8 -0
- package/src/ingest.ts +31 -10
- package/src/kg-extractor.ts +6 -2
- package/src/kg-store.ts +236 -44
- package/src/llm-consolidation.ts +2 -1
- package/src/mcp-server.ts +85 -1799
- package/src/mcp-tool-deps.ts +48 -0
- package/src/mcp-tools-advanced.ts +941 -0
- package/src/mcp-tools-core.ts +633 -0
- package/src/mcp-tools-governance.ts +475 -0
- package/src/memory-boundaries.ts +3 -1
- package/src/memory-evolution.ts +73 -1
- package/src/memory-lint.ts +183 -10
- package/src/memory-output.ts +114 -3
- package/src/memory-promotion.ts +246 -0
- package/src/memory-schema.ts +25 -2
- package/src/memory-store-port.ts +43 -0
- package/src/multi-vector.ts +2 -1
- package/src/narrative-schema.ts +2 -1
- package/src/noise-filter.ts +57 -6
- package/src/pii-detector.ts +157 -5
- package/src/query-expander.ts +90 -9
- package/src/result-synthesizer.ts +2 -1
- package/src/retention-policy.ts +2 -1
- package/src/retriever.ts +259 -139
- package/src/runtime-config.ts +8 -1
- package/src/scope-suggester.ts +113 -0
- package/src/session-distiller.ts +23 -2
- package/src/session-engine.ts +2 -1
- package/src/session-schema.ts +1 -0
- package/src/session-store.ts +5 -0
- package/src/skill-engine.ts +78 -0
- package/src/skill-promotion.ts +247 -47
- package/src/skill-schema.ts +16 -3
- package/src/skill-verifier.ts +152 -0
- package/src/store.ts +267 -38
- package/src/tool-output-compressor.ts +3 -1
- package/src/ui-server.ts +2 -1
- package/src/usage-tracker.ts +242 -0
- package/src/version-manager.ts +3 -2
- package/src/workflow-observation-engine.ts +2 -1
- package/src/workflow-observation-schema.ts +2 -1
- package/src/workflow-observation-store.ts +6 -2
- package/.env +0 -4
- package/CLAUDE.md +0 -46
- package/config.json +0 -55
- package/daily-recall.sh +0 -110
- package/logs/daily-recall.error.log +0 -0
- package/logs/daily-recall.log +0 -60
- package/logs/ingest-2026-04-11.log +0 -110
- package/logs/ingest-2026-04-12.log +0 -90
- package/logs/ingest-2026-04-13.log +0 -87
- package/logs/launchagent-stderr.log +0 -0
- package/logs/launchagent-stdout.log +0 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import { FrequencyTracker } from "../frequency-tracker.js";
|
|
3
|
+
import { existsSync, unlinkSync, mkdirSync } from "fs";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
|
|
6
|
+
const TEST_DIR = join(import.meta.dir, "../../.test-data");
|
|
7
|
+
const TEST_FILE = join(TEST_DIR, "freq-test.json");
|
|
8
|
+
|
|
9
|
+
function cleanup() {
|
|
10
|
+
try { if (existsSync(TEST_FILE)) unlinkSync(TEST_FILE); } catch {}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe("FrequencyTracker", () => {
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
if (!existsSync(TEST_DIR)) mkdirSync(TEST_DIR, { recursive: true });
|
|
16
|
+
cleanup();
|
|
17
|
+
});
|
|
18
|
+
afterEach(cleanup);
|
|
19
|
+
|
|
20
|
+
it("records hits and increments count", () => {
|
|
21
|
+
const tracker = new FrequencyTracker({ filePath: TEST_FILE });
|
|
22
|
+
tracker.recordHits(["mem-1", "mem-2"]);
|
|
23
|
+
tracker.recordHits(["mem-1"]);
|
|
24
|
+
|
|
25
|
+
expect(tracker.getStats("mem-1")?.hitCount).toBe(2);
|
|
26
|
+
expect(tracker.getStats("mem-2")?.hitCount).toBe(1);
|
|
27
|
+
expect(tracker.getStats("mem-3")).toBeUndefined();
|
|
28
|
+
tracker.dispose();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("returns 1.0 boost below minHitsForBoost", () => {
|
|
32
|
+
const tracker = new FrequencyTracker({ filePath: TEST_FILE, minHitsForBoost: 3 });
|
|
33
|
+
tracker.recordHits(["mem-1"]);
|
|
34
|
+
tracker.recordHits(["mem-1"]);
|
|
35
|
+
expect(tracker.getBoostMultiplier("mem-1")).toBe(1.0);
|
|
36
|
+
tracker.dispose();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("returns >1.0 boost at or above minHitsForBoost", () => {
|
|
40
|
+
const tracker = new FrequencyTracker({ filePath: TEST_FILE, minHitsForBoost: 2 });
|
|
41
|
+
tracker.recordHits(["mem-1"]);
|
|
42
|
+
tracker.recordHits(["mem-1"]);
|
|
43
|
+
tracker.recordHits(["mem-1"]);
|
|
44
|
+
|
|
45
|
+
const boost = tracker.getBoostMultiplier("mem-1");
|
|
46
|
+
expect(boost).toBeGreaterThan(1.0);
|
|
47
|
+
// log2(3) * 0.15 = ~0.238 → boost ≈ 1.238
|
|
48
|
+
expect(boost).toBeLessThan(1.5);
|
|
49
|
+
tracker.dispose();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("shouldPromoteToCore respects threshold", () => {
|
|
53
|
+
const tracker = new FrequencyTracker({
|
|
54
|
+
filePath: TEST_FILE,
|
|
55
|
+
corePromotionThreshold: 3,
|
|
56
|
+
});
|
|
57
|
+
tracker.recordHits(["mem-1"]);
|
|
58
|
+
tracker.recordHits(["mem-1"]);
|
|
59
|
+
expect(tracker.shouldPromoteToCore("mem-1")).toBe(false);
|
|
60
|
+
|
|
61
|
+
tracker.recordHits(["mem-1"]);
|
|
62
|
+
expect(tracker.shouldPromoteToCore("mem-1")).toBe(true);
|
|
63
|
+
tracker.dispose();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("persists and loads from file", () => {
|
|
67
|
+
const tracker1 = new FrequencyTracker({ filePath: TEST_FILE });
|
|
68
|
+
tracker1.recordHits(["mem-a"]);
|
|
69
|
+
tracker1.recordHits(["mem-a"]);
|
|
70
|
+
tracker1.recordHits(["mem-a"]);
|
|
71
|
+
tracker1.flush();
|
|
72
|
+
tracker1.dispose();
|
|
73
|
+
|
|
74
|
+
const tracker2 = new FrequencyTracker({ filePath: TEST_FILE });
|
|
75
|
+
expect(tracker2.getStats("mem-a")?.hitCount).toBe(3);
|
|
76
|
+
tracker2.dispose();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("tracks size correctly", () => {
|
|
80
|
+
const tracker = new FrequencyTracker({ filePath: TEST_FILE });
|
|
81
|
+
expect(tracker.size).toBe(0);
|
|
82
|
+
tracker.recordHits(["a", "b", "c"]);
|
|
83
|
+
expect(tracker.size).toBe(3);
|
|
84
|
+
tracker.dispose();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("applies time decay to effective hit count", () => {
|
|
88
|
+
const tracker = new FrequencyTracker({
|
|
89
|
+
filePath: TEST_FILE,
|
|
90
|
+
decayHalfLifeDays: 30,
|
|
91
|
+
minHitsForBoost: 1,
|
|
92
|
+
});
|
|
93
|
+
// Simulate old hits by manipulating stats directly
|
|
94
|
+
tracker.recordHits(["old-mem"]);
|
|
95
|
+
tracker.recordHits(["old-mem"]);
|
|
96
|
+
tracker.recordHits(["old-mem"]);
|
|
97
|
+
tracker.recordHits(["old-mem"]);
|
|
98
|
+
tracker.recordHits(["old-mem"]);
|
|
99
|
+
|
|
100
|
+
// Fresh entry should have full boost
|
|
101
|
+
const freshBoost = tracker.getBoostMultiplier("old-mem");
|
|
102
|
+
expect(freshBoost).toBeGreaterThan(1.0);
|
|
103
|
+
|
|
104
|
+
// Manually set lastHitAt to 60 days ago
|
|
105
|
+
const stats = tracker.getStats("old-mem")!;
|
|
106
|
+
stats.lastHitAt = Date.now() - 60 * 86_400_000;
|
|
107
|
+
|
|
108
|
+
// Decayed boost should be lower
|
|
109
|
+
const decayedBoost = tracker.getBoostMultiplier("old-mem");
|
|
110
|
+
expect(decayedBoost).toBeLessThan(freshBoost);
|
|
111
|
+
tracker.dispose();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { buildMemoryGraph, renderGraphHTML, formatGraphExportResult } from "../graph-export.js";
|
|
3
|
+
import type { MemoryEntry, MemoryStore } from "../store.js";
|
|
4
|
+
import type { MemoryGraph } from "../graph-export.js";
|
|
5
|
+
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Helpers
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
function makeEntry(overrides: Partial<MemoryEntry> & { id: string }): MemoryEntry {
|
|
11
|
+
return {
|
|
12
|
+
text: "test memory",
|
|
13
|
+
vector: [1, 0, 0, 0, 0],
|
|
14
|
+
category: "events",
|
|
15
|
+
scope: "project:test",
|
|
16
|
+
importance: 0.7,
|
|
17
|
+
timestamp: Date.now(),
|
|
18
|
+
metadata: JSON.stringify({
|
|
19
|
+
evolution: {
|
|
20
|
+
status: "active",
|
|
21
|
+
version: 1,
|
|
22
|
+
accessCount: 3,
|
|
23
|
+
lastAccessedAt: null,
|
|
24
|
+
supersededBy: null,
|
|
25
|
+
supersedes: null,
|
|
26
|
+
evolutionNote: null,
|
|
27
|
+
consolidatedInto: null,
|
|
28
|
+
contributedToPattern: null,
|
|
29
|
+
sourceMemories: [],
|
|
30
|
+
validFrom: Date.now(),
|
|
31
|
+
validUntil: null,
|
|
32
|
+
},
|
|
33
|
+
}),
|
|
34
|
+
...overrides,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function createMockStore(entries: MemoryEntry[]): Pick<MemoryStore, "list"> & Partial<Pick<MemoryStore, "getVectors">> {
|
|
39
|
+
return {
|
|
40
|
+
async list() { return entries; },
|
|
41
|
+
async getVectors(ids: string[]) {
|
|
42
|
+
const map = new Map<string, number[]>();
|
|
43
|
+
for (const e of entries) {
|
|
44
|
+
if (ids.includes(e.id) && e.vector?.length > 0) {
|
|
45
|
+
map.set(e.id, e.vector);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return map;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// buildMemoryGraph
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
describe("buildMemoryGraph", () => {
|
|
58
|
+
it("returns nodes for active entries", async () => {
|
|
59
|
+
const entries = [
|
|
60
|
+
makeEntry({ id: "a1", text: "User prefers dark mode", category: "preferences" }),
|
|
61
|
+
makeEntry({ id: "a2", text: "Project uses Bun runtime", category: "entities" }),
|
|
62
|
+
];
|
|
63
|
+
const store = createMockStore(entries);
|
|
64
|
+
const graph = await buildMemoryGraph(store);
|
|
65
|
+
|
|
66
|
+
expect(graph.nodes).toHaveLength(2);
|
|
67
|
+
expect(graph.nodes[0].category).toBe("preferences");
|
|
68
|
+
expect(graph.nodes[1].category).toBe("entities");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("limits nodes to maxNodes", async () => {
|
|
72
|
+
const entries = Array.from({ length: 50 }, (_, i) =>
|
|
73
|
+
makeEntry({ id: `n${i}`, importance: 1 - i * 0.01 })
|
|
74
|
+
);
|
|
75
|
+
const store = createMockStore(entries);
|
|
76
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 10 });
|
|
77
|
+
|
|
78
|
+
expect(graph.nodes).toHaveLength(10);
|
|
79
|
+
// Should have the highest-importance nodes
|
|
80
|
+
expect(graph.nodes[0].importance).toBe(1);
|
|
81
|
+
expect(graph.nodes[9].importance).toBe(0.91);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("skips non-active entries", async () => {
|
|
85
|
+
const activeEntry = makeEntry({ id: "active1", text: "I am active" });
|
|
86
|
+
const supersededEntry = makeEntry({
|
|
87
|
+
id: "sup1",
|
|
88
|
+
text: "I am superseded",
|
|
89
|
+
metadata: JSON.stringify({
|
|
90
|
+
evolution: {
|
|
91
|
+
status: "superseded",
|
|
92
|
+
version: 1,
|
|
93
|
+
accessCount: 0,
|
|
94
|
+
lastAccessedAt: null,
|
|
95
|
+
supersededBy: "active1",
|
|
96
|
+
supersedes: null,
|
|
97
|
+
evolutionNote: null,
|
|
98
|
+
consolidatedInto: null,
|
|
99
|
+
contributedToPattern: null,
|
|
100
|
+
sourceMemories: [],
|
|
101
|
+
validFrom: Date.now(),
|
|
102
|
+
validUntil: Date.now(),
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
});
|
|
106
|
+
const store = createMockStore([activeEntry, supersededEntry]);
|
|
107
|
+
const graph = await buildMemoryGraph(store);
|
|
108
|
+
|
|
109
|
+
expect(graph.nodes).toHaveLength(1);
|
|
110
|
+
expect(graph.nodes[0].id).toBe("active1");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("builds supersede edges from evolution metadata", async () => {
|
|
114
|
+
const oldEntry = makeEntry({
|
|
115
|
+
id: "old1",
|
|
116
|
+
text: "Old version",
|
|
117
|
+
metadata: JSON.stringify({
|
|
118
|
+
evolution: {
|
|
119
|
+
status: "active",
|
|
120
|
+
version: 1,
|
|
121
|
+
accessCount: 0,
|
|
122
|
+
lastAccessedAt: null,
|
|
123
|
+
supersededBy: "new1",
|
|
124
|
+
supersedes: null,
|
|
125
|
+
evolutionNote: null,
|
|
126
|
+
consolidatedInto: null,
|
|
127
|
+
contributedToPattern: null,
|
|
128
|
+
sourceMemories: [],
|
|
129
|
+
validFrom: Date.now(),
|
|
130
|
+
validUntil: null,
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
});
|
|
134
|
+
const newEntry = makeEntry({
|
|
135
|
+
id: "new1",
|
|
136
|
+
text: "New version",
|
|
137
|
+
metadata: JSON.stringify({
|
|
138
|
+
evolution: {
|
|
139
|
+
status: "active",
|
|
140
|
+
version: 2,
|
|
141
|
+
accessCount: 0,
|
|
142
|
+
lastAccessedAt: null,
|
|
143
|
+
supersededBy: null,
|
|
144
|
+
supersedes: "old1",
|
|
145
|
+
evolutionNote: "Updated info",
|
|
146
|
+
consolidatedInto: null,
|
|
147
|
+
contributedToPattern: null,
|
|
148
|
+
sourceMemories: [],
|
|
149
|
+
validFrom: Date.now(),
|
|
150
|
+
validUntil: null,
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
});
|
|
154
|
+
const store = createMockStore([oldEntry, newEntry]);
|
|
155
|
+
const graph = await buildMemoryGraph(store);
|
|
156
|
+
|
|
157
|
+
const supersedeEdges = graph.edges.filter(e => e.type === "supersede");
|
|
158
|
+
expect(supersedeEdges.length).toBeGreaterThanOrEqual(1);
|
|
159
|
+
// old1 -> new1 edge
|
|
160
|
+
const edge = supersedeEdges.find(e => e.source === "old1" && e.target === "new1");
|
|
161
|
+
expect(edge).toBeDefined();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("builds cluster edges from metadata", async () => {
|
|
165
|
+
const canonical = makeEntry({
|
|
166
|
+
id: "canon1",
|
|
167
|
+
text: "Canonical entry",
|
|
168
|
+
metadata: JSON.stringify({
|
|
169
|
+
evolution: { status: "active", version: 1, accessCount: 0, lastAccessedAt: null, supersededBy: null, supersedes: null, evolutionNote: null, consolidatedInto: null, contributedToPattern: null, sourceMemories: [], validFrom: Date.now(), validUntil: null },
|
|
170
|
+
cluster_members: ["member1"],
|
|
171
|
+
}),
|
|
172
|
+
});
|
|
173
|
+
const member = makeEntry({
|
|
174
|
+
id: "member1",
|
|
175
|
+
text: "Cluster member",
|
|
176
|
+
metadata: JSON.stringify({
|
|
177
|
+
evolution: { status: "active", version: 1, accessCount: 0, lastAccessedAt: null, supersededBy: null, supersedes: null, evolutionNote: null, consolidatedInto: null, contributedToPattern: null, sourceMemories: [], validFrom: Date.now(), validUntil: null },
|
|
178
|
+
clustered_with: "canon1",
|
|
179
|
+
}),
|
|
180
|
+
});
|
|
181
|
+
const store = createMockStore([canonical, member]);
|
|
182
|
+
const graph = await buildMemoryGraph(store);
|
|
183
|
+
|
|
184
|
+
const clusterEdges = graph.edges.filter(e => e.type === "cluster");
|
|
185
|
+
expect(clusterEdges.length).toBeGreaterThanOrEqual(1);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("filters edges to existing nodes only", async () => {
|
|
189
|
+
// Entry references a supersededBy ID that is not in the node set
|
|
190
|
+
const entry = makeEntry({
|
|
191
|
+
id: "e1",
|
|
192
|
+
text: "Entry with missing reference",
|
|
193
|
+
metadata: JSON.stringify({
|
|
194
|
+
evolution: {
|
|
195
|
+
status: "active",
|
|
196
|
+
version: 1,
|
|
197
|
+
accessCount: 0,
|
|
198
|
+
lastAccessedAt: null,
|
|
199
|
+
supersededBy: "nonexistent-id",
|
|
200
|
+
supersedes: null,
|
|
201
|
+
evolutionNote: null,
|
|
202
|
+
consolidatedInto: null,
|
|
203
|
+
contributedToPattern: null,
|
|
204
|
+
sourceMemories: [],
|
|
205
|
+
validFrom: Date.now(),
|
|
206
|
+
validUntil: null,
|
|
207
|
+
},
|
|
208
|
+
}),
|
|
209
|
+
});
|
|
210
|
+
const store = createMockStore([entry]);
|
|
211
|
+
const graph = await buildMemoryGraph(store);
|
|
212
|
+
|
|
213
|
+
expect(graph.nodes).toHaveLength(1);
|
|
214
|
+
// No edges should reference nonexistent-id
|
|
215
|
+
const badEdges = graph.edges.filter(
|
|
216
|
+
e => e.source === "nonexistent-id" || e.target === "nonexistent-id"
|
|
217
|
+
);
|
|
218
|
+
expect(badEdges).toHaveLength(0);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("builds scope edges for small scope groups", async () => {
|
|
222
|
+
const entries = [
|
|
223
|
+
makeEntry({ id: "s1", scope: "project:alpha" }),
|
|
224
|
+
makeEntry({ id: "s2", scope: "project:alpha" }),
|
|
225
|
+
makeEntry({ id: "s3", scope: "project:alpha" }),
|
|
226
|
+
makeEntry({ id: "s4", scope: "project:beta" }),
|
|
227
|
+
];
|
|
228
|
+
const store = createMockStore(entries);
|
|
229
|
+
const graph = await buildMemoryGraph(store);
|
|
230
|
+
|
|
231
|
+
const scopeEdges = graph.edges.filter(e => e.type === "scope");
|
|
232
|
+
// alpha has 3 entries → chain of 2 scope edges
|
|
233
|
+
expect(scopeEdges.length).toBe(2);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("skips scope edges when scope has more than 20 entries", async () => {
|
|
237
|
+
const entries = Array.from({ length: 25 }, (_, i) =>
|
|
238
|
+
makeEntry({ id: `big${i}`, scope: "project:big" })
|
|
239
|
+
);
|
|
240
|
+
const store = createMockStore(entries);
|
|
241
|
+
const graph = await buildMemoryGraph(store);
|
|
242
|
+
|
|
243
|
+
const scopeEdges = graph.edges.filter(e => e.type === "scope");
|
|
244
|
+
expect(scopeEdges).toHaveLength(0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("truncates long text to 80 chars", async () => {
|
|
248
|
+
const longText = "A".repeat(200);
|
|
249
|
+
const store = createMockStore([makeEntry({ id: "long1", text: longText })]);
|
|
250
|
+
const graph = await buildMemoryGraph(store);
|
|
251
|
+
|
|
252
|
+
expect(graph.nodes[0].label.length).toBe(80);
|
|
253
|
+
expect(graph.nodes[0].label.endsWith("\u2026")).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("extracts accessCount from evolution", async () => {
|
|
257
|
+
const entry = makeEntry({
|
|
258
|
+
id: "ac1",
|
|
259
|
+
metadata: JSON.stringify({
|
|
260
|
+
evolution: {
|
|
261
|
+
status: "active",
|
|
262
|
+
version: 1,
|
|
263
|
+
accessCount: 42,
|
|
264
|
+
lastAccessedAt: null,
|
|
265
|
+
supersededBy: null,
|
|
266
|
+
supersedes: null,
|
|
267
|
+
evolutionNote: null,
|
|
268
|
+
consolidatedInto: null,
|
|
269
|
+
contributedToPattern: null,
|
|
270
|
+
sourceMemories: [],
|
|
271
|
+
validFrom: Date.now(),
|
|
272
|
+
validUntil: null,
|
|
273
|
+
},
|
|
274
|
+
}),
|
|
275
|
+
});
|
|
276
|
+
const store = createMockStore([entry]);
|
|
277
|
+
const graph = await buildMemoryGraph(store);
|
|
278
|
+
|
|
279
|
+
expect(graph.nodes[0].accessCount).toBe(42);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
// renderGraphHTML
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
describe("renderGraphHTML", () => {
|
|
288
|
+
const sampleGraph: MemoryGraph = {
|
|
289
|
+
nodes: [
|
|
290
|
+
{ id: "n1", label: "Test node", category: "profile", scope: "test", importance: 0.8, timestamp: Date.now(), accessCount: 5 },
|
|
291
|
+
],
|
|
292
|
+
edges: [],
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
it("output contains DOCTYPE html", () => {
|
|
296
|
+
const html = renderGraphHTML(sampleGraph);
|
|
297
|
+
expect(html).toContain("<!DOCTYPE html>");
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("output contains d3.js CDN link", () => {
|
|
301
|
+
const html = renderGraphHTML(sampleGraph);
|
|
302
|
+
expect(html).toContain("https://d3js.org/d3.v7.min.js");
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("output contains GRAPH_DATA", () => {
|
|
306
|
+
const html = renderGraphHTML(sampleGraph);
|
|
307
|
+
expect(html).toContain("GRAPH_DATA");
|
|
308
|
+
expect(html).toContain('"id":"n1"');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("output contains category colors", () => {
|
|
312
|
+
const html = renderGraphHTML(sampleGraph);
|
|
313
|
+
expect(html).toContain("#f59e0b"); // profile gold
|
|
314
|
+
expect(html).toContain("#3b82f6"); // preferences blue
|
|
315
|
+
expect(html).toContain("#10b981"); // entities green
|
|
316
|
+
expect(html).toContain("#ef4444"); // cases red
|
|
317
|
+
expect(html).toContain("#8b5cf6"); // patterns purple
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("output contains title and legend", () => {
|
|
321
|
+
const html = renderGraphHTML(sampleGraph);
|
|
322
|
+
expect(html).toContain("RecallNest Knowledge Graph");
|
|
323
|
+
expect(html).toContain("legend");
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("output contains dark background styling", () => {
|
|
327
|
+
const html = renderGraphHTML(sampleGraph);
|
|
328
|
+
expect(html).toContain("#0d1117");
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// ---------------------------------------------------------------------------
|
|
333
|
+
// formatGraphExportResult
|
|
334
|
+
// ---------------------------------------------------------------------------
|
|
335
|
+
|
|
336
|
+
describe("formatGraphExportResult", () => {
|
|
337
|
+
it("includes node and edge counts", () => {
|
|
338
|
+
const graph: MemoryGraph = {
|
|
339
|
+
nodes: [
|
|
340
|
+
{ id: "n1", label: "A", category: "profile", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
341
|
+
{ id: "n2", label: "B", category: "entities", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
342
|
+
],
|
|
343
|
+
edges: [
|
|
344
|
+
{ source: "n1", target: "n2", type: "scope" },
|
|
345
|
+
],
|
|
346
|
+
};
|
|
347
|
+
const result = formatGraphExportResult("/tmp/test.html", graph);
|
|
348
|
+
expect(result).toContain("Nodes: 2");
|
|
349
|
+
expect(result).toContain("Edges: 1");
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("includes file path", () => {
|
|
353
|
+
const graph: MemoryGraph = { nodes: [], edges: [] };
|
|
354
|
+
const result = formatGraphExportResult("/my/path/graph.html", graph);
|
|
355
|
+
expect(result).toContain("/my/path/graph.html");
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("includes category breakdown", () => {
|
|
359
|
+
const graph: MemoryGraph = {
|
|
360
|
+
nodes: [
|
|
361
|
+
{ id: "n1", label: "A", category: "profile", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
362
|
+
{ id: "n2", label: "B", category: "profile", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
363
|
+
{ id: "n3", label: "C", category: "events", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
364
|
+
],
|
|
365
|
+
edges: [],
|
|
366
|
+
};
|
|
367
|
+
const result = formatGraphExportResult("/tmp/test.html", graph);
|
|
368
|
+
expect(result).toContain("profile(2)");
|
|
369
|
+
expect(result).toContain("events(1)");
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("handles empty graph", () => {
|
|
373
|
+
const graph: MemoryGraph = { nodes: [], edges: [] };
|
|
374
|
+
const result = formatGraphExportResult("/tmp/empty.html", graph);
|
|
375
|
+
expect(result).toContain("Nodes: 0");
|
|
376
|
+
expect(result).toContain("Edges: 0");
|
|
377
|
+
expect(result).toContain("none");
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("shows semantic bridge count when present", () => {
|
|
381
|
+
const graph: MemoryGraph = {
|
|
382
|
+
nodes: [],
|
|
383
|
+
edges: [
|
|
384
|
+
{ source: "a", target: "b", type: "semantic" },
|
|
385
|
+
{ source: "c", target: "d", type: "semantic" },
|
|
386
|
+
{ source: "e", target: "f", type: "scope" },
|
|
387
|
+
],
|
|
388
|
+
};
|
|
389
|
+
const result = formatGraphExportResult("/tmp/test.html", graph);
|
|
390
|
+
expect(result).toContain("2 semantic");
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// ---------------------------------------------------------------------------
|
|
395
|
+
// Cross-Scope Semantic Bridges
|
|
396
|
+
// ---------------------------------------------------------------------------
|
|
397
|
+
|
|
398
|
+
describe("cross-scope semantic bridges", () => {
|
|
399
|
+
// Helper: create a normalized vector in a specific direction
|
|
400
|
+
function dirVector(index: number, dim = 8): number[] {
|
|
401
|
+
const v = new Array(dim).fill(0);
|
|
402
|
+
v[index] = 1;
|
|
403
|
+
return v;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Helper: create a vector that's a blend (for high cosine similarity)
|
|
407
|
+
function blendVector(a: number[], b: number[], ratio = 0.5): number[] {
|
|
408
|
+
const v = a.map((_, i) => a[i] * ratio + b[i] * (1 - ratio));
|
|
409
|
+
const norm = Math.sqrt(v.reduce((s, x) => s + x * x, 0));
|
|
410
|
+
return v.map(x => x / (norm || 1));
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
it("creates semantic edges between similar entries in different scopes", async () => {
|
|
414
|
+
const sharedVec = dirVector(0);
|
|
415
|
+
const entries = [
|
|
416
|
+
makeEntry({ id: "s1-a", scope: "scope-writing", vector: sharedVec, text: "good titles need cognitive gap" }),
|
|
417
|
+
makeEntry({ id: "s2-a", scope: "scope-ai-tools", vector: sharedVec, text: "prompts need surprise element" }),
|
|
418
|
+
];
|
|
419
|
+
const store = createMockStore(entries);
|
|
420
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
421
|
+
|
|
422
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
423
|
+
expect(semanticEdges.length).toBe(1);
|
|
424
|
+
expect(semanticEdges[0].source).toBe("s1-a");
|
|
425
|
+
expect(semanticEdges[0].target).toBe("s2-a");
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it("does NOT create semantic edges within the same scope", async () => {
|
|
429
|
+
const sharedVec = dirVector(0);
|
|
430
|
+
const entries = [
|
|
431
|
+
makeEntry({ id: "same-1", scope: "project:alpha", vector: sharedVec }),
|
|
432
|
+
makeEntry({ id: "same-2", scope: "project:alpha", vector: sharedVec }),
|
|
433
|
+
];
|
|
434
|
+
const store = createMockStore(entries);
|
|
435
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
436
|
+
|
|
437
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
438
|
+
expect(semanticEdges.length).toBe(0);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("does NOT create edges below similarity threshold", async () => {
|
|
442
|
+
const entries = [
|
|
443
|
+
makeEntry({ id: "far-1", scope: "scope-a", vector: dirVector(0) }),
|
|
444
|
+
makeEntry({ id: "far-2", scope: "scope-b", vector: dirVector(3) }), // orthogonal
|
|
445
|
+
];
|
|
446
|
+
const store = createMockStore(entries);
|
|
447
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
448
|
+
|
|
449
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
450
|
+
expect(semanticEdges.length).toBe(0);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it("caps semantic edges at MAX_SEMANTIC_EDGES (top-N by similarity)", async () => {
|
|
454
|
+
// Create 40 entries across 2 scopes with identical vectors → would generate
|
|
455
|
+
// 20*20=400 cross-scope pairs, but should be capped at 30
|
|
456
|
+
const vec = dirVector(0);
|
|
457
|
+
const entries: MemoryEntry[] = [];
|
|
458
|
+
for (let i = 0; i < 20; i++) {
|
|
459
|
+
entries.push(makeEntry({ id: `a-${i}`, scope: "scope-alpha", vector: vec }));
|
|
460
|
+
entries.push(makeEntry({ id: `b-${i}`, scope: "scope-beta", vector: vec }));
|
|
461
|
+
}
|
|
462
|
+
const store = createMockStore(entries);
|
|
463
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
464
|
+
|
|
465
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
466
|
+
expect(semanticEdges.length).toBeLessThanOrEqual(30);
|
|
467
|
+
expect(semanticEdges.length).toBeGreaterThan(0);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("handles entries with empty vectors gracefully", async () => {
|
|
471
|
+
const entries = [
|
|
472
|
+
makeEntry({ id: "no-vec-1", scope: "scope-a", vector: [] }),
|
|
473
|
+
makeEntry({ id: "no-vec-2", scope: "scope-b", vector: [] }),
|
|
474
|
+
makeEntry({ id: "has-vec", scope: "scope-c", vector: dirVector(0) }),
|
|
475
|
+
];
|
|
476
|
+
const store = createMockStore(entries);
|
|
477
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
478
|
+
|
|
479
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
480
|
+
expect(semanticEdges.length).toBe(0); // no valid pairs
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it("includes semantic bridge in HTML legend", () => {
|
|
484
|
+
const graph: MemoryGraph = { nodes: [], edges: [] };
|
|
485
|
+
const html = renderGraphHTML(graph);
|
|
486
|
+
expect(html).toContain("semantic bridge");
|
|
487
|
+
expect(html).toContain("#f472b6");
|
|
488
|
+
});
|
|
489
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, it, expect } from "bun:test";
|
|
2
|
+
import { computeHotnessScore, parseAccessMetadata } from "../access-tracker.js";
|
|
3
|
+
|
|
4
|
+
describe("computeHotnessScore", () => {
|
|
5
|
+
it("returns 0 for zero accesses", () => {
|
|
6
|
+
expect(computeHotnessScore(0, Date.now())).toBe(0);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("returns positive score for accessed memories", () => {
|
|
10
|
+
const score = computeHotnessScore(5, Date.now());
|
|
11
|
+
expect(score).toBeGreaterThan(0);
|
|
12
|
+
expect(score).toBeLessThanOrEqual(1);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("higher access count yields higher score", () => {
|
|
16
|
+
const now = Date.now();
|
|
17
|
+
const low = computeHotnessScore(1, now);
|
|
18
|
+
const mid = computeHotnessScore(5, now);
|
|
19
|
+
const high = computeHotnessScore(50, now);
|
|
20
|
+
expect(low).toBeLessThan(mid);
|
|
21
|
+
expect(mid).toBeLessThan(high);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("recent access yields higher score than old access", () => {
|
|
25
|
+
const now = Date.now();
|
|
26
|
+
const recent = computeHotnessScore(5, now);
|
|
27
|
+
const weekAgo = computeHotnessScore(5, now - 7 * 86_400_000);
|
|
28
|
+
const monthAgo = computeHotnessScore(5, now - 30 * 86_400_000);
|
|
29
|
+
expect(recent).toBeGreaterThan(weekAgo);
|
|
30
|
+
expect(weekAgo).toBeGreaterThan(monthAgo);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("decays to near-zero for very old accesses", () => {
|
|
34
|
+
const score = computeHotnessScore(5, Date.now() - 365 * 86_400_000);
|
|
35
|
+
expect(score).toBeLessThan(0.01);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("caps at 1.0 even with extreme access counts", () => {
|
|
39
|
+
const score = computeHotnessScore(10_000, Date.now());
|
|
40
|
+
expect(score).toBeLessThanOrEqual(1.0);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("respects custom decay rate", () => {
|
|
44
|
+
const now = Date.now();
|
|
45
|
+
const fast = computeHotnessScore(5, now - 7 * 86_400_000, 0.5);
|
|
46
|
+
const slow = computeHotnessScore(5, now - 7 * 86_400_000, 0.01);
|
|
47
|
+
expect(slow).toBeGreaterThan(fast);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("parseAccessMetadata", () => {
|
|
52
|
+
it("parses valid metadata", () => {
|
|
53
|
+
const meta = JSON.stringify({ accessCount: 5, lastAccessedAt: 1234567890 });
|
|
54
|
+
const result = parseAccessMetadata(meta);
|
|
55
|
+
expect(result.accessCount).toBe(5);
|
|
56
|
+
expect(result.lastAccessedAt).toBe(1234567890);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("returns defaults for missing fields", () => {
|
|
60
|
+
const result = parseAccessMetadata("{}");
|
|
61
|
+
expect(result.accessCount).toBe(0);
|
|
62
|
+
expect(result.lastAccessedAt).toBe(0);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("handles undefined input", () => {
|
|
66
|
+
const result = parseAccessMetadata(undefined);
|
|
67
|
+
expect(result.accessCount).toBe(0);
|
|
68
|
+
expect(result.lastAccessedAt).toBe(0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("handles malformed JSON", () => {
|
|
72
|
+
const result = parseAccessMetadata("not-json");
|
|
73
|
+
expect(result.accessCount).toBe(0);
|
|
74
|
+
expect(result.lastAccessedAt).toBe(0);
|
|
75
|
+
});
|
|
76
|
+
});
|