@modusensus/dsh-mneme 0.6.6 → 0.6.8

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.
Files changed (117) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +463 -457
  3. package/cordis.patch.yml +15 -15
  4. package/lib/api.js +783 -693
  5. package/lib/client.js +1757 -1656
  6. package/lib/commands.js +64 -64
  7. package/lib/config.js +288 -288
  8. package/lib/dream/clustering.js +118 -118
  9. package/lib/dream/decisions.js +439 -439
  10. package/lib/dream/sleep.js +561 -554
  11. package/lib/dream/tag-extractor.js +156 -156
  12. package/lib/dream.js +935 -929
  13. package/lib/embedding.js +154 -154
  14. package/lib/entities/extractor.js +242 -242
  15. package/lib/hot-memory.js +53 -53
  16. package/lib/index.js +361 -361
  17. package/lib/inject.js +208 -208
  18. package/lib/local-embedder.js +282 -282
  19. package/lib/mirror.js +170 -170
  20. package/lib/parser/tag.js +59 -59
  21. package/lib/parser/wiki-link.js +38 -38
  22. package/lib/quality-filter.js +123 -123
  23. package/lib/reranker.js +218 -218
  24. package/lib/search/adaptive.js +22 -22
  25. package/lib/search/bm25.js +96 -96
  26. package/lib/search/tag-boost.js +61 -61
  27. package/lib/service.js +1726 -1726
  28. package/lib/settings.js +172 -142
  29. package/lib/store.js +2238 -2238
  30. package/lib/summarize.js +236 -236
  31. package/lib/tools.js +290 -290
  32. package/lib/vector-index.js +116 -116
  33. package/package.json +80 -75
  34. package/scripts/benchmark-embed.js +201 -201
  35. package/scripts/benchmark-recall.js +133 -133
  36. package/scripts/benchmark-rerank.js +166 -166
  37. package/scripts/e2e-dsh.js +218 -218
  38. package/scripts/stress-dsh.js +255 -255
  39. package/scripts/sync-lib.js +52 -52
  40. package/src/api.js +783 -693
  41. package/src/commands.js +64 -64
  42. package/src/config.js +288 -288
  43. package/src/dream/clustering.js +118 -118
  44. package/src/dream/decisions.js +439 -439
  45. package/src/dream/sleep.js +561 -554
  46. package/src/dream/tag-extractor.js +156 -156
  47. package/src/dream.js +935 -929
  48. package/src/embedding.js +154 -154
  49. package/src/entities/extractor.js +242 -242
  50. package/src/hot-memory.js +53 -53
  51. package/src/index.js +361 -361
  52. package/src/inject.js +208 -208
  53. package/src/local-embedder.js +282 -282
  54. package/src/mirror.js +170 -170
  55. package/src/parser/tag.js +59 -59
  56. package/src/parser/wiki-link.js +38 -38
  57. package/src/quality-filter.js +123 -123
  58. package/src/reranker.js +218 -218
  59. package/src/search/adaptive.js +22 -22
  60. package/src/search/bm25.js +96 -96
  61. package/src/search/tag-boost.js +61 -61
  62. package/src/service.js +1726 -1726
  63. package/src/settings.js +172 -142
  64. package/src/store.js +2238 -2238
  65. package/src/summarize.js +236 -236
  66. package/src/tools.js +290 -290
  67. package/src/vector-index.js +116 -116
  68. package/test/api.test.js +594 -549
  69. package/test/audit.test.js +448 -448
  70. package/test/benchmark.test.js +35 -35
  71. package/test/boundary-v0625.test.js +82 -82
  72. package/test/client.test.js +368 -368
  73. package/test/clustering.test.js +100 -100
  74. package/test/commands.test.js +69 -69
  75. package/test/config.test.js +50 -50
  76. package/test/conflict-freeze.test.js +290 -290
  77. package/test/directory.test.js +134 -134
  78. package/test/dream.test.js +903 -901
  79. package/test/entities.test.js +522 -522
  80. package/test/epistemic.test.js +298 -298
  81. package/test/fnew-0112.test.js +311 -311
  82. package/test/fnew-03.test.js +422 -422
  83. package/test/graph-api.test.js +175 -175
  84. package/test/helpers/dream-mock.js +82 -82
  85. package/test/hot-memory.test.js +174 -174
  86. package/test/inject.test.js +103 -103
  87. package/test/llm-audit.test.js +279 -279
  88. package/test/local-embedder.test.js +227 -227
  89. package/test/mirror-dirty.test.js +424 -424
  90. package/test/mirror-edit-digest.test.js +187 -187
  91. package/test/mirror-generation.test.js +499 -499
  92. package/test/mirror.test.js +249 -249
  93. package/test/normalize-decisions.test.js +120 -120
  94. package/test/peer-blockers.test.js +190 -190
  95. package/test/policy-epoch.test.js +259 -259
  96. package/test/provenance.test.js +103 -103
  97. package/test/quality-filter.test.js +118 -118
  98. package/test/reasoning-effort.test.js +199 -199
  99. package/test/recall-evals.test.js +235 -235
  100. package/test/recall-layer.test.js +315 -315
  101. package/test/receipt-chain.test.js +451 -451
  102. package/test/reflection.test.js +226 -226
  103. package/test/reranker.test.js +240 -240
  104. package/test/search-fusion.test.js +90 -90
  105. package/test/semantic.test.js +124 -124
  106. package/test/service-search.test.js +199 -199
  107. package/test/service.test.js +435 -435
  108. package/test/settings.test.js +118 -101
  109. package/test/sleep.test.js +365 -365
  110. package/test/store.test.js +436 -436
  111. package/test/stress.test.js +209 -209
  112. package/test/summarize.test.js +191 -191
  113. package/test/tag-boost.test.js +125 -125
  114. package/test/tag.test.js +312 -312
  115. package/test/tools.test.js +285 -285
  116. package/test/vector-index.test.js +221 -221
  117. package/test/wiki-link.test.js +332 -332
@@ -1,332 +1,332 @@
1
- // v0.6.1 Wiki-Link(显式跨记忆链接)测试。
2
- // 覆盖:解析器 parseWikiLinks(正常/显示文本/未闭合/空/多链接/非法)/
3
- // resolveWikiLink 大小写匹配 / 存储幂等(同对只写一条、目标不存在跳过)/
4
- // links_to 的 partial 唯一索引(仅 links_to 去重,其余 relation 保持 append-only)/
5
- // saveWikiLinks 幂等 / service 三 API(getBacklinks/getForwardLinks/
6
- // resolveWikiLink)与 saveWithDedupe 的 wikiLinkEnabled hook / 三个只读 HTTP 端点。
7
- import test from "node:test";
8
- import assert from "node:assert/strict";
9
- import { EventEmitter } from "node:events";
10
- import { createStore } from "../src/store.js";
11
- import { createService } from "../src/service.js";
12
- import { createApi } from "../src/api.js";
13
- import { createSettings } from "../src/settings.js";
14
- import { parseWikiLinks } from "../src/parser/wiki-link.js";
15
-
16
- function openStore() {
17
- return createStore(":memory:");
18
- }
19
-
20
- function makeService(config = {}) {
21
- const store = createStore(":memory:");
22
- const service = createService({ store, mirror: null, config });
23
- return { store, service };
24
- }
25
-
26
- /** Let fire-and-forget enqueue tasks (wiki-link resolve) settle. */
27
- const flush = () => new Promise((resolve) => setTimeout(resolve, 5));
28
-
29
- function seed(service, title, content, type = "preference") {
30
- return service.saveWithDedupe({ type, title, content, importance: 3 }).memory;
31
- }
32
-
33
- // ============================================================ parser
34
-
35
- test("parseWikiLinks: plain [[target]] returns display=target", () => {
36
- assert.deepEqual(parseWikiLinks("看看 [[Alpha]] 这篇"), [
37
- { display: "Alpha", target: "Alpha" }
38
- ]);
39
- });
40
-
41
- test("parseWikiLinks: [[显示|target]] keeps display text separate from target", () => {
42
- assert.deepEqual(parseWikiLinks("参考 [[精彩解读|Alpha]] 一文"), [
43
- { display: "精彩解读", target: "Alpha" }
44
- ]);
45
- });
46
-
47
- test("parseWikiLinks: unclosed [[ marker is ignored", () => {
48
- assert.deepEqual(parseWikiLinks("这里有个没闭合的 [[Alpha"), []);
49
- assert.deepEqual(parseWikiLinks("[[Alpha] 只有一个括号"), []);
50
- });
51
-
52
- test("parseWikiLinks: empty targets are ignored", () => {
53
- assert.deepEqual(parseWikiLinks("[[]]"), []);
54
- assert.deepEqual(parseWikiLinks("[[ ]]"), []);
55
- assert.deepEqual(parseWikiLinks("[[显示|]]"), []);
56
- assert.deepEqual(parseWikiLinks("[[|]]"), []);
57
- });
58
-
59
- test("parseWikiLinks: multiple links are all returned in order", () => {
60
- const links = parseWikiLinks("[[A]] 与 [[B]] 以及 [[C|D]]");
61
- assert.deepEqual(links, [
62
- { display: "A", target: "A" },
63
- { display: "B", target: "B" },
64
- { display: "C", target: "D" }
65
- ]);
66
- });
67
-
68
- test("parseWikiLinks: multi-pipe and bracket-nested markers are illegal", () => {
69
- assert.deepEqual(parseWikiLinks("[[a|b|c]]"), [], "multi-pipe skipped");
70
- assert.deepEqual(parseWikiLinks("[[a [[b]] c]]"), [
71
- { display: "b", target: "b" }
72
- ], "only the well-formed inner [[b]] is kept");
73
- });
74
-
75
- test("parseWikiLinks: non-string / empty content returns []", () => {
76
- assert.deepEqual(parseWikiLinks(""), []);
77
- assert.deepEqual(parseWikiLinks(undefined), []);
78
- assert.deepEqual(parseWikiLinks(null), []);
79
- assert.deepEqual(parseWikiLinks("没有链接的普通文本"), []);
80
- });
81
-
82
- test("parseWikiLinks: display falls back to target when the display side is empty", () => {
83
- assert.deepEqual(parseWikiLinks("[[|Beta]]"), [
84
- { display: "Beta", target: "Beta" }
85
- ]);
86
- });
87
-
88
- // ============================================================ storage
89
-
90
- test("partial unique index on links_to (from_entity,to_entity) is created", () => {
91
- const store = openStore();
92
- const idx = store.db.prepare(
93
- "SELECT name, sql FROM sqlite_master WHERE type='index' AND name='idx_relations_wikilink'"
94
- ).get();
95
- assert.ok(idx, "idx_relations_wikilink exists");
96
- assert.match(idx.sql, /WHERE relation_type = 'links_to'/i,
97
- "index is partial — scoped to links_to only");
98
- const fullUnique = store.db.prepare(
99
- "SELECT name FROM sqlite_master WHERE type='index' AND name='idx_relations_unique'"
100
- ).get();
101
- assert.ok(!fullUnique, "no full-table unique index (keeps other types append-only)");
102
- store.close();
103
- });
104
-
105
- test("saveWikiLinks: same target pair writes exactly one row (idempotent)", () => {
106
- const store = openStore();
107
- const a = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
108
- store.save({ type: "project", title: "Beta", content: "c", tags: [] });
109
- const r1 = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
110
- const r2 = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
111
- assert.equal(r1.saved.length, 1);
112
- assert.equal(r2.saved.length, 0, "second call is a no-op");
113
- assert.deepEqual(r2.skipped, []);
114
- const rels = store.getRelations(a.title).filter((r) => r.relation_type === "links_to");
115
- assert.equal(rels.length, 1, "one links_to row total");
116
- assert.equal(rels[0].from_entity, "Alpha");
117
- assert.equal(rels[0].to_entity, "Beta");
118
- assert.equal(rels[0].memory_id, a.id);
119
- store.close();
120
- });
121
-
122
- test("saveWikiLinks: missing target is skipped, not an error (fail-safe)", () => {
123
- const store = openStore();
124
- const a = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
125
- store.save({ type: "project", title: "Beta", content: "c", tags: [] });
126
- const r = store.saveWikiLinks({
127
- memoryId: a.id,
128
- title: a.title,
129
- targets: ["不存在", "Beta"]
130
- });
131
- assert.equal(r.saved.length, 1);
132
- assert.deepEqual(r.skipped, ["不存在"]);
133
- store.close();
134
- });
135
-
136
- test("saveWikiLinks: target title matches case-insensitively", () => {
137
- const store = openStore();
138
- const a = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
139
- const b = store.save({ type: "project", title: "Beta", content: "c", tags: [] });
140
- const r = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["BETA"] });
141
- assert.equal(r.saved.length, 1, "lowercase target resolves the mixed-case title");
142
- assert.equal(r.skipped.length, 0);
143
- const rel = store.getRelations(a.title).find((x) => x.relation_type === "links_to");
144
- assert.ok(rel, "relation written");
145
- // resolved target memory carried in metadata
146
- assert.equal(rel.metadata?.target_memory_id, b.id);
147
- store.close();
148
- });
149
-
150
- test("saveRelation stays append-only for non-links_to (preserves extractor/dream semantics)", () => {
151
- const store = openStore();
152
- store.saveRelation({ from_entity: "X", to_entity: "Y", relation_type: "uses" });
153
- store.saveRelation({ from_entity: "X", to_entity: "Y", relation_type: "uses" });
154
- // two rows — not deduped. (saveRelation's return is a SELECT LIMIT 1, so it
155
- // always yields the first matching row; the row count is the real signal.)
156
- assert.equal(store.getRelations("X").filter((r) => r.relation_type === "uses").length, 2);
157
- store.close();
158
- });
159
-
160
- test("partial unique index dedups links_to at the DB level", () => {
161
- const store = openStore();
162
- store.saveRelation({ from_entity: "A", to_entity: "B", relation_type: "links_to" });
163
- assert.throws(
164
- () => store.saveRelation({ from_entity: "A", to_entity: "B", relation_type: "links_to" }),
165
- /UNIQUE constraint failed/,
166
- "second links_to for the same pair is rejected by idx_relations_wikilink"
167
- );
168
- store.close();
169
- });
170
-
171
- // ============================================================ service read APIs
172
-
173
- test("service.getForwardLinks returns target memories the memory links to", () => {
174
- const { store, service } = makeService({ wikiLinkEnabled: true });
175
- const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
176
- const b = seed(service, "Beta", "正文", "project");
177
- const rel = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
178
- assert.equal(rel.saved.length, 1);
179
- const links = service.getForwardLinks(a.id);
180
- assert.equal(links.length, 1);
181
- assert.equal(links[0].target.id, b.id);
182
- assert.equal(links[0].target.title, "Beta");
183
- assert.equal(links[0].relation.relation_type, "links_to");
184
- store.close();
185
- });
186
-
187
- test("service.getBacklinks returns source memories that link to a memory", () => {
188
- const { store, service } = makeService({ wikiLinkEnabled: true });
189
- const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
190
- const b = seed(service, "Beta", "正文", "project");
191
- store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["beta"] }); // case-insensitive resolve
192
- const backlinks = service.getBacklinks(b.id);
193
- assert.equal(backlinks.length, 1);
194
- assert.equal(backlinks[0].source.id, a.id);
195
- assert.equal(backlinks[0].source.title, "Alpha");
196
- store.close();
197
- });
198
-
199
- test("service.resolveWikiLink matches titles case-insensitively", () => {
200
- const { store, service } = makeService();
201
- store.save({ type: "project", title: "Beta", content: "c", tags: [] });
202
- const mem = service.resolveWikiLink("BETA");
203
- assert.ok(mem, "resolved");
204
- assert.equal(mem.title, "Beta");
205
- assert.equal(service.resolveWikiLink("不存在"), undefined);
206
- store.close();
207
- });
208
-
209
- // ============================================================ service hook
210
-
211
- test("saveWithDedupe resolves [[wiki-links]] when wikiLinkEnabled=true", async () => {
212
- const { store, service } = makeService({ wikiLinkEnabled: true });
213
- seed(service, "Beta", "正文", "project");
214
- seed(service, "Alpha", "参见 [[Beta]] 和 [[不存在]]", "preference");
215
- await flush(); // fire-and-forget enqueue task settles
216
- const rels = store.getRelations("Alpha").filter((r) => r.relation_type === "links_to");
217
- assert.equal(rels.length, 1, "Beta resolved; 不存在 skipped");
218
- assert.equal(rels[0].to_entity, "Beta");
219
- store.close();
220
- });
221
-
222
- test("saveWithDedupe writes no links_to when wikiLinkEnabled=false (default)", async () => {
223
- const { store, service } = makeService();
224
- seed(service, "Beta", "正文", "project");
225
- seed(service, "Alpha", "参见 [[Beta]]", "preference");
226
- await flush();
227
- assert.equal(store.getRelations("Alpha").filter((r) => r.relation_type === "links_to").length, 0);
228
- store.close();
229
- });
230
-
231
- test("service.update re-resolves [[wiki-links]] for edited content", async () => {
232
- const { store, service } = makeService({ wikiLinkEnabled: true });
233
- seed(service, "Beta", "正文", "project");
234
- const a = seed(service, "Alpha", "暂无链接", "preference");
235
- service.update(a.id, { content: "现在指向 [[Beta]]" });
236
- await flush();
237
- const rels = store.getRelations("Alpha").filter((r) => r.relation_type === "links_to");
238
- assert.equal(rels.length, 1);
239
- assert.equal(rels[0].to_entity, "Beta");
240
- store.close();
241
- });
242
-
243
- // ============================================================ HTTP endpoints
244
-
245
- class FakeRes extends EventEmitter {
246
- constructor() { super(); this.statusCode = 200; this.body = ""; }
247
- writeHead(code, headers) { this.statusCode = code; this.headers = headers; return this; }
248
- end(text) { this.body = text ?? ""; this.emit("end"); return this; }
249
- }
250
-
251
- function req(path) {
252
- const r = new EventEmitter();
253
- r.url = path;
254
- r.method = "GET";
255
- r.headers = {};
256
- return r;
257
- }
258
-
259
- function setup(apiToken = "") {
260
- const store = createStore(":memory:");
261
- const service = createService({ store, mirror: null, config: {} });
262
- const settings = createSettings(store.db);
263
- const commands = { add: () => {}, remove: () => {}, list: () => [] };
264
- const routes = [];
265
- const ctx = { webServer: { register(route) { routes.push(route); return () => {}; } } };
266
- createApi(ctx, service, settings, commands, null, undefined, apiToken);
267
- return { store, service, routes };
268
- }
269
-
270
- function wikiRoute(routes, path) {
271
- return routes.find((r) => r.path === path);
272
- }
273
-
274
- test("three wiki-link endpoints are registered and open without apiToken", async () => {
275
- const { routes } = setup("secret-token");
276
- for (const p of [
277
- "/api/dsh-mneme/wikilinks/backlinks",
278
- "/api/dsh-mneme/wikilinks/forward",
279
- "/api/dsh-mneme/wikilinks/resolve"
280
- ]) {
281
- assert.ok(wikiRoute(routes, p), `${p} registered`);
282
- }
283
- // read-only: missing param is a 400, never a 401
284
- const back = wikiRoute(routes, "/api/dsh-mneme/wikilinks/backlinks");
285
- const res = new FakeRes();
286
- await back.handler(req("/api/dsh-mneme/wikilinks/backlinks"), res);
287
- assert.equal(res.statusCode, 400);
288
- });
289
-
290
- test("GET backlinks returns linking source memories", async () => {
291
- const { routes, store, service } = setup();
292
- const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
293
- const b = seed(service, "Beta", "正文", "project");
294
- store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
295
- const route = wikiRoute(routes, "/api/dsh-mneme/wikilinks/backlinks");
296
- const res = new FakeRes();
297
- await route.handler(req(`/api/dsh-mneme/wikilinks/backlinks?id=${b.id}`), res);
298
- assert.equal(res.statusCode, 200);
299
- const data = JSON.parse(res.body);
300
- assert.equal(data.memoryId, b.id);
301
- assert.equal(data.backlinks.length, 1);
302
- assert.equal(data.backlinks[0].title, "Alpha");
303
- assert.equal(data.backlinks[0].id, a.id);
304
- });
305
-
306
- test("GET forward links returns target memories", async () => {
307
- const { routes, store, service } = setup();
308
- const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
309
- const b = seed(service, "Beta", "正文", "project");
310
- store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
311
- const route = wikiRoute(routes, "/api/dsh-mneme/wikilinks/forward");
312
- const res = new FakeRes();
313
- await route.handler(req(`/api/dsh-mneme/wikilinks/forward?id=${a.id}`), res);
314
- assert.equal(res.statusCode, 200);
315
- const data = JSON.parse(res.body);
316
- assert.equal(data.links.length, 1);
317
- assert.equal(data.links[0].title, "Beta");
318
- assert.equal(data.links[0].id, b.id);
319
- });
320
-
321
- test("GET resolve matches title case-insensitively and 404s when absent", async () => {
322
- const { routes, store, service } = setup();
323
- store.save({ type: "project", title: "Beta", content: "c", tags: [] });
324
- const route = wikiRoute(routes, "/api/dsh-mneme/wikilinks/resolve");
325
- const ok = new FakeRes();
326
- await route.handler(req("/api/dsh-mneme/wikilinks/resolve?title=BETA"), ok);
327
- assert.equal(ok.statusCode, 200);
328
- assert.equal(JSON.parse(ok.body).memory.title, "Beta");
329
- const missing = new FakeRes();
330
- await route.handler(req("/api/dsh-mneme/wikilinks/resolve?title=nope"), missing);
331
- assert.equal(missing.statusCode, 404);
332
- });
1
+ // v0.6.1 Wiki-Link(显式跨记忆链接)测试。
2
+ // 覆盖:解析器 parseWikiLinks(正常/显示文本/未闭合/空/多链接/非法)/
3
+ // resolveWikiLink 大小写匹配 / 存储幂等(同对只写一条、目标不存在跳过)/
4
+ // links_to 的 partial 唯一索引(仅 links_to 去重,其余 relation 保持 append-only)/
5
+ // saveWikiLinks 幂等 / service 三 API(getBacklinks/getForwardLinks/
6
+ // resolveWikiLink)与 saveWithDedupe 的 wikiLinkEnabled hook / 三个只读 HTTP 端点。
7
+ import test from "node:test";
8
+ import assert from "node:assert/strict";
9
+ import { EventEmitter } from "node:events";
10
+ import { createStore } from "../src/store.js";
11
+ import { createService } from "../src/service.js";
12
+ import { createApi } from "../src/api.js";
13
+ import { createSettings } from "../src/settings.js";
14
+ import { parseWikiLinks } from "../src/parser/wiki-link.js";
15
+
16
+ function openStore() {
17
+ return createStore(":memory:");
18
+ }
19
+
20
+ function makeService(config = {}) {
21
+ const store = createStore(":memory:");
22
+ const service = createService({ store, mirror: null, config });
23
+ return { store, service };
24
+ }
25
+
26
+ /** Let fire-and-forget enqueue tasks (wiki-link resolve) settle. */
27
+ const flush = () => new Promise((resolve) => setTimeout(resolve, 5));
28
+
29
+ function seed(service, title, content, type = "preference") {
30
+ return service.saveWithDedupe({ type, title, content, importance: 3 }).memory;
31
+ }
32
+
33
+ // ============================================================ parser
34
+
35
+ test("parseWikiLinks: plain [[target]] returns display=target", () => {
36
+ assert.deepEqual(parseWikiLinks("看看 [[Alpha]] 这篇"), [
37
+ { display: "Alpha", target: "Alpha" }
38
+ ]);
39
+ });
40
+
41
+ test("parseWikiLinks: [[显示|target]] keeps display text separate from target", () => {
42
+ assert.deepEqual(parseWikiLinks("参考 [[精彩解读|Alpha]] 一文"), [
43
+ { display: "精彩解读", target: "Alpha" }
44
+ ]);
45
+ });
46
+
47
+ test("parseWikiLinks: unclosed [[ marker is ignored", () => {
48
+ assert.deepEqual(parseWikiLinks("这里有个没闭合的 [[Alpha"), []);
49
+ assert.deepEqual(parseWikiLinks("[[Alpha] 只有一个括号"), []);
50
+ });
51
+
52
+ test("parseWikiLinks: empty targets are ignored", () => {
53
+ assert.deepEqual(parseWikiLinks("[[]]"), []);
54
+ assert.deepEqual(parseWikiLinks("[[ ]]"), []);
55
+ assert.deepEqual(parseWikiLinks("[[显示|]]"), []);
56
+ assert.deepEqual(parseWikiLinks("[[|]]"), []);
57
+ });
58
+
59
+ test("parseWikiLinks: multiple links are all returned in order", () => {
60
+ const links = parseWikiLinks("[[A]] 与 [[B]] 以及 [[C|D]]");
61
+ assert.deepEqual(links, [
62
+ { display: "A", target: "A" },
63
+ { display: "B", target: "B" },
64
+ { display: "C", target: "D" }
65
+ ]);
66
+ });
67
+
68
+ test("parseWikiLinks: multi-pipe and bracket-nested markers are illegal", () => {
69
+ assert.deepEqual(parseWikiLinks("[[a|b|c]]"), [], "multi-pipe skipped");
70
+ assert.deepEqual(parseWikiLinks("[[a [[b]] c]]"), [
71
+ { display: "b", target: "b" }
72
+ ], "only the well-formed inner [[b]] is kept");
73
+ });
74
+
75
+ test("parseWikiLinks: non-string / empty content returns []", () => {
76
+ assert.deepEqual(parseWikiLinks(""), []);
77
+ assert.deepEqual(parseWikiLinks(undefined), []);
78
+ assert.deepEqual(parseWikiLinks(null), []);
79
+ assert.deepEqual(parseWikiLinks("没有链接的普通文本"), []);
80
+ });
81
+
82
+ test("parseWikiLinks: display falls back to target when the display side is empty", () => {
83
+ assert.deepEqual(parseWikiLinks("[[|Beta]]"), [
84
+ { display: "Beta", target: "Beta" }
85
+ ]);
86
+ });
87
+
88
+ // ============================================================ storage
89
+
90
+ test("partial unique index on links_to (from_entity,to_entity) is created", () => {
91
+ const store = openStore();
92
+ const idx = store.db.prepare(
93
+ "SELECT name, sql FROM sqlite_master WHERE type='index' AND name='idx_relations_wikilink'"
94
+ ).get();
95
+ assert.ok(idx, "idx_relations_wikilink exists");
96
+ assert.match(idx.sql, /WHERE relation_type = 'links_to'/i,
97
+ "index is partial — scoped to links_to only");
98
+ const fullUnique = store.db.prepare(
99
+ "SELECT name FROM sqlite_master WHERE type='index' AND name='idx_relations_unique'"
100
+ ).get();
101
+ assert.ok(!fullUnique, "no full-table unique index (keeps other types append-only)");
102
+ store.close();
103
+ });
104
+
105
+ test("saveWikiLinks: same target pair writes exactly one row (idempotent)", () => {
106
+ const store = openStore();
107
+ const a = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
108
+ store.save({ type: "project", title: "Beta", content: "c", tags: [] });
109
+ const r1 = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
110
+ const r2 = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
111
+ assert.equal(r1.saved.length, 1);
112
+ assert.equal(r2.saved.length, 0, "second call is a no-op");
113
+ assert.deepEqual(r2.skipped, []);
114
+ const rels = store.getRelations(a.title).filter((r) => r.relation_type === "links_to");
115
+ assert.equal(rels.length, 1, "one links_to row total");
116
+ assert.equal(rels[0].from_entity, "Alpha");
117
+ assert.equal(rels[0].to_entity, "Beta");
118
+ assert.equal(rels[0].memory_id, a.id);
119
+ store.close();
120
+ });
121
+
122
+ test("saveWikiLinks: missing target is skipped, not an error (fail-safe)", () => {
123
+ const store = openStore();
124
+ const a = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
125
+ store.save({ type: "project", title: "Beta", content: "c", tags: [] });
126
+ const r = store.saveWikiLinks({
127
+ memoryId: a.id,
128
+ title: a.title,
129
+ targets: ["不存在", "Beta"]
130
+ });
131
+ assert.equal(r.saved.length, 1);
132
+ assert.deepEqual(r.skipped, ["不存在"]);
133
+ store.close();
134
+ });
135
+
136
+ test("saveWikiLinks: target title matches case-insensitively", () => {
137
+ const store = openStore();
138
+ const a = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
139
+ const b = store.save({ type: "project", title: "Beta", content: "c", tags: [] });
140
+ const r = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["BETA"] });
141
+ assert.equal(r.saved.length, 1, "lowercase target resolves the mixed-case title");
142
+ assert.equal(r.skipped.length, 0);
143
+ const rel = store.getRelations(a.title).find((x) => x.relation_type === "links_to");
144
+ assert.ok(rel, "relation written");
145
+ // resolved target memory carried in metadata
146
+ assert.equal(rel.metadata?.target_memory_id, b.id);
147
+ store.close();
148
+ });
149
+
150
+ test("saveRelation stays append-only for non-links_to (preserves extractor/dream semantics)", () => {
151
+ const store = openStore();
152
+ store.saveRelation({ from_entity: "X", to_entity: "Y", relation_type: "uses" });
153
+ store.saveRelation({ from_entity: "X", to_entity: "Y", relation_type: "uses" });
154
+ // two rows — not deduped. (saveRelation's return is a SELECT LIMIT 1, so it
155
+ // always yields the first matching row; the row count is the real signal.)
156
+ assert.equal(store.getRelations("X").filter((r) => r.relation_type === "uses").length, 2);
157
+ store.close();
158
+ });
159
+
160
+ test("partial unique index dedups links_to at the DB level", () => {
161
+ const store = openStore();
162
+ store.saveRelation({ from_entity: "A", to_entity: "B", relation_type: "links_to" });
163
+ assert.throws(
164
+ () => store.saveRelation({ from_entity: "A", to_entity: "B", relation_type: "links_to" }),
165
+ /UNIQUE constraint failed/,
166
+ "second links_to for the same pair is rejected by idx_relations_wikilink"
167
+ );
168
+ store.close();
169
+ });
170
+
171
+ // ============================================================ service read APIs
172
+
173
+ test("service.getForwardLinks returns target memories the memory links to", () => {
174
+ const { store, service } = makeService({ wikiLinkEnabled: true });
175
+ const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
176
+ const b = seed(service, "Beta", "正文", "project");
177
+ const rel = store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
178
+ assert.equal(rel.saved.length, 1);
179
+ const links = service.getForwardLinks(a.id);
180
+ assert.equal(links.length, 1);
181
+ assert.equal(links[0].target.id, b.id);
182
+ assert.equal(links[0].target.title, "Beta");
183
+ assert.equal(links[0].relation.relation_type, "links_to");
184
+ store.close();
185
+ });
186
+
187
+ test("service.getBacklinks returns source memories that link to a memory", () => {
188
+ const { store, service } = makeService({ wikiLinkEnabled: true });
189
+ const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
190
+ const b = seed(service, "Beta", "正文", "project");
191
+ store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["beta"] }); // case-insensitive resolve
192
+ const backlinks = service.getBacklinks(b.id);
193
+ assert.equal(backlinks.length, 1);
194
+ assert.equal(backlinks[0].source.id, a.id);
195
+ assert.equal(backlinks[0].source.title, "Alpha");
196
+ store.close();
197
+ });
198
+
199
+ test("service.resolveWikiLink matches titles case-insensitively", () => {
200
+ const { store, service } = makeService();
201
+ store.save({ type: "project", title: "Beta", content: "c", tags: [] });
202
+ const mem = service.resolveWikiLink("BETA");
203
+ assert.ok(mem, "resolved");
204
+ assert.equal(mem.title, "Beta");
205
+ assert.equal(service.resolveWikiLink("不存在"), undefined);
206
+ store.close();
207
+ });
208
+
209
+ // ============================================================ service hook
210
+
211
+ test("saveWithDedupe resolves [[wiki-links]] when wikiLinkEnabled=true", async () => {
212
+ const { store, service } = makeService({ wikiLinkEnabled: true });
213
+ seed(service, "Beta", "正文", "project");
214
+ seed(service, "Alpha", "参见 [[Beta]] 和 [[不存在]]", "preference");
215
+ await flush(); // fire-and-forget enqueue task settles
216
+ const rels = store.getRelations("Alpha").filter((r) => r.relation_type === "links_to");
217
+ assert.equal(rels.length, 1, "Beta resolved; 不存在 skipped");
218
+ assert.equal(rels[0].to_entity, "Beta");
219
+ store.close();
220
+ });
221
+
222
+ test("saveWithDedupe writes no links_to when wikiLinkEnabled=false (default)", async () => {
223
+ const { store, service } = makeService();
224
+ seed(service, "Beta", "正文", "project");
225
+ seed(service, "Alpha", "参见 [[Beta]]", "preference");
226
+ await flush();
227
+ assert.equal(store.getRelations("Alpha").filter((r) => r.relation_type === "links_to").length, 0);
228
+ store.close();
229
+ });
230
+
231
+ test("service.update re-resolves [[wiki-links]] for edited content", async () => {
232
+ const { store, service } = makeService({ wikiLinkEnabled: true });
233
+ seed(service, "Beta", "正文", "project");
234
+ const a = seed(service, "Alpha", "暂无链接", "preference");
235
+ service.update(a.id, { content: "现在指向 [[Beta]]" });
236
+ await flush();
237
+ const rels = store.getRelations("Alpha").filter((r) => r.relation_type === "links_to");
238
+ assert.equal(rels.length, 1);
239
+ assert.equal(rels[0].to_entity, "Beta");
240
+ store.close();
241
+ });
242
+
243
+ // ============================================================ HTTP endpoints
244
+
245
+ class FakeRes extends EventEmitter {
246
+ constructor() { super(); this.statusCode = 200; this.body = ""; }
247
+ writeHead(code, headers) { this.statusCode = code; this.headers = headers; return this; }
248
+ end(text) { this.body = text ?? ""; this.emit("end"); return this; }
249
+ }
250
+
251
+ function req(path) {
252
+ const r = new EventEmitter();
253
+ r.url = path;
254
+ r.method = "GET";
255
+ r.headers = {};
256
+ return r;
257
+ }
258
+
259
+ function setup(apiToken = "") {
260
+ const store = createStore(":memory:");
261
+ const service = createService({ store, mirror: null, config: {} });
262
+ const settings = createSettings(store.db);
263
+ const commands = { add: () => {}, remove: () => {}, list: () => [] };
264
+ const routes = [];
265
+ const ctx = { webServer: { register(route) { routes.push(route); return () => {}; } } };
266
+ createApi(ctx, service, settings, commands, null, undefined, apiToken);
267
+ return { store, service, routes };
268
+ }
269
+
270
+ function wikiRoute(routes, path) {
271
+ return routes.find((r) => r.path === path);
272
+ }
273
+
274
+ test("three wiki-link endpoints are registered and open without apiToken", async () => {
275
+ const { routes } = setup("secret-token");
276
+ for (const p of [
277
+ "/api/dsh-mneme/wikilinks/backlinks",
278
+ "/api/dsh-mneme/wikilinks/forward",
279
+ "/api/dsh-mneme/wikilinks/resolve"
280
+ ]) {
281
+ assert.ok(wikiRoute(routes, p), `${p} registered`);
282
+ }
283
+ // read-only: missing param is a 400, never a 401
284
+ const back = wikiRoute(routes, "/api/dsh-mneme/wikilinks/backlinks");
285
+ const res = new FakeRes();
286
+ await back.handler(req("/api/dsh-mneme/wikilinks/backlinks"), res);
287
+ assert.equal(res.statusCode, 400);
288
+ });
289
+
290
+ test("GET backlinks returns linking source memories", async () => {
291
+ const { routes, store, service } = setup();
292
+ const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
293
+ const b = seed(service, "Beta", "正文", "project");
294
+ store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
295
+ const route = wikiRoute(routes, "/api/dsh-mneme/wikilinks/backlinks");
296
+ const res = new FakeRes();
297
+ await route.handler(req(`/api/dsh-mneme/wikilinks/backlinks?id=${b.id}`), res);
298
+ assert.equal(res.statusCode, 200);
299
+ const data = JSON.parse(res.body);
300
+ assert.equal(data.memoryId, b.id);
301
+ assert.equal(data.backlinks.length, 1);
302
+ assert.equal(data.backlinks[0].title, "Alpha");
303
+ assert.equal(data.backlinks[0].id, a.id);
304
+ });
305
+
306
+ test("GET forward links returns target memories", async () => {
307
+ const { routes, store, service } = setup();
308
+ const a = seed(service, "Alpha", "参见 [[Beta]]", "preference");
309
+ const b = seed(service, "Beta", "正文", "project");
310
+ store.saveWikiLinks({ memoryId: a.id, title: a.title, targets: ["Beta"] });
311
+ const route = wikiRoute(routes, "/api/dsh-mneme/wikilinks/forward");
312
+ const res = new FakeRes();
313
+ await route.handler(req(`/api/dsh-mneme/wikilinks/forward?id=${a.id}`), res);
314
+ assert.equal(res.statusCode, 200);
315
+ const data = JSON.parse(res.body);
316
+ assert.equal(data.links.length, 1);
317
+ assert.equal(data.links[0].title, "Beta");
318
+ assert.equal(data.links[0].id, b.id);
319
+ });
320
+
321
+ test("GET resolve matches title case-insensitively and 404s when absent", async () => {
322
+ const { routes, store, service } = setup();
323
+ store.save({ type: "project", title: "Beta", content: "c", tags: [] });
324
+ const route = wikiRoute(routes, "/api/dsh-mneme/wikilinks/resolve");
325
+ const ok = new FakeRes();
326
+ await route.handler(req("/api/dsh-mneme/wikilinks/resolve?title=BETA"), ok);
327
+ assert.equal(ok.statusCode, 200);
328
+ assert.equal(JSON.parse(ok.body).memory.title, "Beta");
329
+ const missing = new FakeRes();
330
+ await route.handler(req("/api/dsh-mneme/wikilinks/resolve?title=nope"), missing);
331
+ assert.equal(missing.statusCode, 404);
332
+ });