@modusensus/dsh-mneme 0.8.2 → 0.8.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/LICENSE +21 -21
- package/README.md +718 -633
- package/bin/cli.mjs +0 -0
- package/bin/dsh-mneme-mcp.mjs +502 -0
- package/cordis.patch.yml +21 -21
- package/lib/api-standalone.js +495 -316
- package/lib/api.js +1382 -1316
- package/lib/bootstrap.js +211 -0
- package/lib/client.js +4475 -4362
- package/lib/commands.js +95 -95
- package/lib/config.js +533 -500
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +658 -658
- package/lib/dream/narratives.js +44 -0
- package/lib/dream/sleep.js +700 -700
- package/lib/dream.js +1319 -1216
- package/lib/embedding.js +177 -177
- package/lib/entities/extractor.js +249 -249
- package/lib/heat.js +142 -136
- package/lib/hot-memory.js +46 -46
- package/lib/index.js +515 -513
- package/lib/inject.js +299 -253
- package/lib/lang.js +202 -179
- package/lib/local-embedder.js +320 -308
- package/lib/mirror.js +198 -198
- package/lib/quality-filter.js +126 -126
- package/lib/recall-stats.js +103 -0
- package/lib/reranker.js +269 -260
- package/lib/runtime/adopt.js +176 -176
- package/lib/runtime/closure.js +172 -172
- package/lib/runtime/download.js +247 -247
- package/lib/runtime/layout.js +235 -235
- package/lib/runtime/loader.js +249 -249
- package/lib/runtime/model-fetch.js +361 -0
- package/lib/runtime/provision.js +237 -237
- package/lib/runtime/tarball.js +130 -130
- package/lib/runtime/verify.js +191 -191
- package/lib/scope.js +118 -118
- package/lib/search/adaptive.js +22 -22
- package/lib/search/bm25.js +96 -96
- package/lib/service.js +2171 -2071
- package/lib/settings.js +430 -419
- package/lib/store.js +2448 -2367
- package/lib/summarize.js +575 -449
- package/lib/tools.js +574 -574
- package/lib/vector-index.js +110 -110
- package/lib/version-check.js +122 -122
- package/package.json +109 -108
- package/runtime-manifest.json +596 -596
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +199 -199
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/build-runtime-manifest.mjs +217 -217
- package/scripts/check-sync.js +42 -42
- package/scripts/e2e-dsh.js +220 -220
- package/scripts/mneme-runtime.mjs +277 -277
- package/scripts/stress-dsh.js +255 -255
- package/scripts/sync-lib.js +47 -47
- package/scripts/sync-test-badge.mjs +61 -61
- package/src/api-standalone.js +495 -316
- package/src/api.js +1382 -1316
- package/src/bootstrap.js +211 -0
- package/src/commands.js +95 -95
- package/src/config.js +533 -500
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +658 -658
- package/src/dream/narratives.js +44 -0
- package/src/dream/sleep.js +700 -700
- package/src/dream.js +1319 -1216
- package/src/embedding.js +177 -177
- package/src/entities/extractor.js +249 -249
- package/src/heat.js +142 -136
- package/src/hot-memory.js +46 -46
- package/src/index.js +515 -513
- package/src/inject.js +299 -253
- package/src/lang.js +202 -179
- package/src/local-embedder.js +320 -308
- package/src/mirror.js +198 -198
- package/src/quality-filter.js +126 -126
- package/src/recall-stats.js +103 -0
- package/src/reranker.js +269 -260
- package/src/runtime/adopt.js +176 -176
- package/src/runtime/closure.js +172 -172
- package/src/runtime/download.js +247 -247
- package/src/runtime/layout.js +235 -235
- package/src/runtime/loader.js +249 -249
- package/src/runtime/model-fetch.js +361 -0
- package/src/runtime/provision.js +237 -237
- package/src/runtime/tarball.js +130 -130
- package/src/runtime/verify.js +191 -191
- package/src/scope.js +118 -118
- package/src/search/adaptive.js +22 -22
- package/src/search/bm25.js +96 -96
- package/src/service.js +2171 -2071
- package/src/settings.js +430 -419
- package/src/store.js +2448 -2367
- package/src/summarize.js +575 -449
- package/src/tools.js +574 -574
- package/src/vector-index.js +110 -110
- package/src/version-check.js +122 -122
- package/test/api-routes-gaps.test.js +218 -218
- package/test/api.test.js +1578 -1481
- package/test/audit.test.js +448 -448
- package/test/benchmark.test.js +94 -94
- package/test/bootstrap.test.js +220 -0
- package/test/client.test.js +799 -736
- package/test/clustering.test.js +100 -100
- package/test/commands.test.js +106 -106
- package/test/config.test.js +66 -66
- package/test/conflict-freeze.test.js +300 -300
- package/test/dream-archive-guardrail.test.js +116 -116
- package/test/dream-candidate-hybrid.test.js +281 -281
- package/test/dream-coverage-degrade.test.js +86 -86
- package/test/dream.test.js +1257 -1254
- package/test/embedding-ready.test.js +196 -196
- package/test/entities.test.js +547 -547
- package/test/entity-recall.test.js +179 -0
- package/test/epistemic.test.js +298 -298
- package/test/fnew-0112.test.js +311 -311
- package/test/fnew-03.test.js +422 -422
- package/test/graph-api.test.js +175 -175
- package/test/heat.test.js +175 -148
- package/test/helpers/dream-mock.js +82 -82
- package/test/helpers/peer-worker.mjs +25 -25
- package/test/helpers/runtime-source.js +60 -60
- package/test/helpers/tar-builder.js +80 -80
- package/test/hot-memory.test.js +145 -145
- package/test/inject-content-cap.test.js +89 -0
- package/test/inject-first-round-bm25.test.js +89 -89
- package/test/inject-heat.test.js +61 -0
- package/test/inject-rotation.test.js +161 -0
- package/test/inject.test.js +249 -249
- package/test/lang.test.js +87 -87
- package/test/lib-smoke.test.js +96 -96
- package/test/llm-audit.test.js +309 -309
- package/test/llm-message-source.test.js +177 -177
- package/test/local-embedder.test.js +249 -229
- package/test/mcp-stdio.test.js +385 -0
- package/test/mirror-dirty.test.js +424 -424
- package/test/mirror-edit-digest.test.js +187 -187
- package/test/mirror-generation.test.js +499 -499
- package/test/mirror.test.js +249 -249
- package/test/narrative-bars.test.js +162 -0
- package/test/peer-blockers.test.js +193 -193
- package/test/policy-epoch.test.js +259 -259
- package/test/quality-filter.test.js +186 -186
- package/test/reasoning-effort.test.js +746 -746
- package/test/recall-evals.test.js +235 -235
- package/test/recall-layer.test.js +326 -326
- package/test/recall-stats.test.js +117 -0
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reindex-backfill.test.js +62 -62
- package/test/reranker-single-logit.test.js +73 -73
- package/test/reranker.test.js +197 -197
- package/test/resident-status.test.js +83 -0
- package/test/runtime-adopt.test.js +206 -206
- package/test/runtime-cli.test.js +89 -89
- package/test/runtime-closure.test.js +193 -193
- package/test/runtime-download.test.js +302 -302
- package/test/runtime-layout.test.js +163 -163
- package/test/runtime-loader.test.js +237 -237
- package/test/runtime-manifest.test.js +158 -158
- package/test/runtime-model-fetch.test.js +348 -0
- package/test/runtime-provision.test.js +101 -101
- package/test/runtime-tarball.test.js +81 -81
- package/test/runtime-verify.test.js +214 -214
- package/test/scope-candidates.test.js +239 -239
- package/test/scope-explicit.test.js +452 -452
- package/test/scope-retrieval.test.js +197 -197
- package/test/scope-storage.test.js +255 -255
- package/test/scope-strict.test.js +231 -231
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/service-search.test.js +174 -174
- package/test/service.test.js +405 -405
- package/test/settings.test.js +261 -261
- package/test/skip-invalid-restore.test.js +262 -262
- package/test/sleep-action-set.test.js +422 -422
- package/test/sleep-heat.test.js +126 -125
- package/test/sleep-scheduler-rearm.test.js +89 -89
- package/test/sleep.test.js +373 -373
- package/test/standalone-api.test.js +544 -359
- package/test/store-vector-cache.test.js +98 -98
- package/test/store.test.js +383 -383
- package/test/stress.test.js +209 -209
- package/test/summarize.test.js +884 -575
- package/test/tools-memory-get-schema.test.js +65 -65
- package/test/tools.test.js +341 -341
- package/test/updated-at-semantics.test.js +113 -113
- package/test/vector-index.test.js +221 -221
- package/test/version-check.test.js +152 -152
- package/test/webServer-optional.test.js +82 -82
|
@@ -1,359 +1,544 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { createStore } from "../src/store.js";
|
|
4
|
-
import { createService } from "../src/service.js";
|
|
5
|
-
import { createSettings } from "../src/settings.js";
|
|
6
|
-
import { createStandaloneApi } from "../src/api-standalone.js";
|
|
7
|
-
import { Config, applyLightModePreset } from "../src/config.js";
|
|
8
|
-
|
|
9
|
-
// Real HTTP server on an OS-assigned port (port: 0), driven with fetch.
|
|
10
|
-
async function setup() {
|
|
11
|
-
const store = createStore(":memory:");
|
|
12
|
-
const service = createService({ store, mirror: null, config: {} });
|
|
13
|
-
const settings = createSettings(store.db);
|
|
14
|
-
const api = createStandaloneApi({ service, store, config: {}, settings, logger: null, port: 0 });
|
|
15
|
-
await api.ready;
|
|
16
|
-
const base = `http://127.0.0.1:${api.port}`;
|
|
17
|
-
const auth = { authorization: `Bearer ${api.token}` };
|
|
18
|
-
return {
|
|
19
|
-
store,
|
|
20
|
-
service,
|
|
21
|
-
settings,
|
|
22
|
-
api,
|
|
23
|
-
base,
|
|
24
|
-
auth,
|
|
25
|
-
close: () => {
|
|
26
|
-
// Drop pooled keep-alive sockets so node --test can drain the loop.
|
|
27
|
-
api.server.closeIdleConnections?.();
|
|
28
|
-
api.server.close();
|
|
29
|
-
store.close();
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// --- auth ---------------------------------------------------------------------
|
|
35
|
-
|
|
36
|
-
test("GET /health is open without a token; every other route 401s", async () => {
|
|
37
|
-
const { base, close } = await setup();
|
|
38
|
-
try {
|
|
39
|
-
const health = await fetch(`${base}/health`);
|
|
40
|
-
assert.equal(health.status, 200);
|
|
41
|
-
assert.deepEqual(await health.json(), { ok: true });
|
|
42
|
-
|
|
43
|
-
for (const path of ["/status", "/profile", "/rules", "/memories", "/memories/x", "/search?q=x"]) {
|
|
44
|
-
const res = await fetch(`${base}${path}`);
|
|
45
|
-
assert.equal(res.status, 401, `${path} requires a token`);
|
|
46
|
-
assert.deepEqual(await res.json(), { error: "unauthorized" });
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const bad = await fetch(`${base}/status`, { headers: { authorization: "Bearer wrong-token" } });
|
|
50
|
-
assert.equal(bad.status, 401, "wrong token rejected");
|
|
51
|
-
} finally {
|
|
52
|
-
close();
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("GET /profile and /rules return saved settings", async () => {
|
|
57
|
-
const { base, auth, settings, close } = await setup();
|
|
58
|
-
try {
|
|
59
|
-
settings.setProfile("我是后端工程师");
|
|
60
|
-
settings.setRules(["先验证再修改", "默认使用中文回复"]);
|
|
61
|
-
|
|
62
|
-
const profile = await fetch(`${base}/profile`, { headers: auth });
|
|
63
|
-
assert.equal(profile.status, 200);
|
|
64
|
-
assert.deepEqual(await profile.json(), { profile: "我是后端工程师" });
|
|
65
|
-
|
|
66
|
-
const rules = await fetch(`${base}/rules`, { headers: auth });
|
|
67
|
-
assert.equal(rules.status, 200);
|
|
68
|
-
assert.deepEqual(await rules.json(), { rules: ["先验证再修改", "默认使用中文回复"] });
|
|
69
|
-
} finally {
|
|
70
|
-
close();
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("GET /profile and /rules return empty values when unset", async () => {
|
|
75
|
-
const { base, auth, close } = await setup();
|
|
76
|
-
try {
|
|
77
|
-
const profile = await fetch(`${base}/profile`, { headers: auth });
|
|
78
|
-
assert.equal(profile.status, 200);
|
|
79
|
-
assert.deepEqual(await profile.json(), { profile: "" });
|
|
80
|
-
|
|
81
|
-
const rules = await fetch(`${base}/rules`, { headers: auth });
|
|
82
|
-
assert.equal(rules.status, 200);
|
|
83
|
-
assert.deepEqual(await rules.json(), { rules: [] });
|
|
84
|
-
} finally {
|
|
85
|
-
close();
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test("token is persisted into settings kv and reused across restarts", async () => {
|
|
90
|
-
const store = createStore(":memory:");
|
|
91
|
-
const service = createService({ store, mirror: null, config: {} });
|
|
92
|
-
const settings = createSettings(store.db);
|
|
93
|
-
const first = createStandaloneApi({ service, store, config: {}, settings, port: 0 });
|
|
94
|
-
await first.ready;
|
|
95
|
-
try {
|
|
96
|
-
assert.equal(settings.getExternalApi().token, first.token, "generated token persisted");
|
|
97
|
-
assert.ok(first.token.length >= 20, "token is a real random value");
|
|
98
|
-
const second = createStandaloneApi({ service, store, config: {}, settings, port: 0 });
|
|
99
|
-
await second.ready;
|
|
100
|
-
assert.equal(second.token, first.token, "restart reuses the persisted token");
|
|
101
|
-
second.server.closeIdleConnections?.();
|
|
102
|
-
second.server.close();
|
|
103
|
-
} finally {
|
|
104
|
-
first.server.closeIdleConnections?.();
|
|
105
|
-
first.server.close();
|
|
106
|
-
store.close();
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// --- CRUD loop ------------------------------------------------------------------
|
|
111
|
-
|
|
112
|
-
test("POST/GET/DELETE /memories round trip", async () => {
|
|
113
|
-
const { base, auth, close } = await setup();
|
|
114
|
-
try {
|
|
115
|
-
const post = await fetch(`${base}/memories`, {
|
|
116
|
-
method: "POST",
|
|
117
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
118
|
-
body: JSON.stringify({ type: "preference", title: "语言", content: "始终用中文回复", importance: 4, tags: ["交流"] })
|
|
119
|
-
});
|
|
120
|
-
assert.equal(post.status, 201);
|
|
121
|
-
const created = await post.json();
|
|
122
|
-
assert.ok(created.id, "row id returned");
|
|
123
|
-
assert.equal(created.title, "语言");
|
|
124
|
-
assert.equal(created.importance, 4);
|
|
125
|
-
|
|
126
|
-
const list = await fetch(`${base}/memories?limit=10`, { headers: auth });
|
|
127
|
-
assert.equal(list.status, 200);
|
|
128
|
-
const listBody = await list.json();
|
|
129
|
-
assert.equal(listBody.total, 1);
|
|
130
|
-
assert.equal(listBody.items.length, 1);
|
|
131
|
-
|
|
132
|
-
const got = await fetch(`${base}/memories/${created.id}`, { headers: auth });
|
|
133
|
-
assert.equal(got.status, 200);
|
|
134
|
-
assert.equal((await got.json()).content, "始终用中文回复");
|
|
135
|
-
|
|
136
|
-
const del = await fetch(`${base}/memories/${created.id}`, { method: "DELETE", headers: auth });
|
|
137
|
-
assert.equal(del.status, 200);
|
|
138
|
-
assert.deepEqual(await del.json(), { ok: true });
|
|
139
|
-
|
|
140
|
-
const gone = await fetch(`${base}/memories/${created.id}`, { headers: auth });
|
|
141
|
-
assert.equal(gone.status, 404, "deleted row reads back as 404");
|
|
142
|
-
const delAgain = await fetch(`${base}/memories/${created.id}`, { method: "DELETE", headers: auth });
|
|
143
|
-
assert.equal(delAgain.status, 404, "deleting a missing row is 404, not a fake ok");
|
|
144
|
-
} finally {
|
|
145
|
-
close();
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
test("POST /memories dedupes same title within a type (200 + merged content)", async () => {
|
|
150
|
-
const { base, auth, close } = await setup();
|
|
151
|
-
try {
|
|
152
|
-
const first = await fetch(`${base}/memories`, {
|
|
153
|
-
method: "POST",
|
|
154
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
155
|
-
body: JSON.stringify({ type: "project", title: "mneme", content: "v1 内容" })
|
|
156
|
-
});
|
|
157
|
-
assert.equal(first.status, 201);
|
|
158
|
-
const row1 = await first.json();
|
|
159
|
-
|
|
160
|
-
const second = await fetch(`${base}/memories`, {
|
|
161
|
-
method: "POST",
|
|
162
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
163
|
-
body: JSON.stringify({ type: "project", title: "mneme", content: "v2 追加" })
|
|
164
|
-
});
|
|
165
|
-
assert.equal(second.status, 200, "merge, not a second creation");
|
|
166
|
-
const row2 = await second.json();
|
|
167
|
-
assert.equal(row2.id, row1.id, "same row reused");
|
|
168
|
-
assert.ok(row2.content.includes("v1 内容") && row2.content.includes("v2 追加"), "content appended");
|
|
169
|
-
|
|
170
|
-
const list = await fetch(`${base}/memories`, { headers: auth });
|
|
171
|
-
assert.equal((await list.json()).total, 1, "no duplicate row created");
|
|
172
|
-
} finally {
|
|
173
|
-
close();
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test("POST /memories validation: invalid type, bad JSON, non-array tags → 400", async () => {
|
|
178
|
-
const { base, auth, close } = await setup();
|
|
179
|
-
try {
|
|
180
|
-
const badType = await fetch(`${base}/memories`, {
|
|
181
|
-
method: "POST",
|
|
182
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
183
|
-
body: JSON.stringify({ type: "diary", title: "t", content: "c" })
|
|
184
|
-
});
|
|
185
|
-
assert.equal(badType.status, 400);
|
|
186
|
-
assert.deepEqual(await badType.json(), { error: "invalid-type" });
|
|
187
|
-
|
|
188
|
-
const badJson = await fetch(`${base}/memories`, {
|
|
189
|
-
method: "POST",
|
|
190
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
191
|
-
body: "{not json"
|
|
192
|
-
});
|
|
193
|
-
assert.equal(badJson.status, 400);
|
|
194
|
-
assert.deepEqual(await badJson.json(), { error: "invalid-json" });
|
|
195
|
-
|
|
196
|
-
const badTags = await fetch(`${base}/memories`, {
|
|
197
|
-
method: "POST",
|
|
198
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
199
|
-
body: JSON.stringify({ type: "project", title: "t", content: "c", tags: "x" })
|
|
200
|
-
});
|
|
201
|
-
assert.equal(badTags.status, 400);
|
|
202
|
-
assert.deepEqual(await badTags.json(), { error: "tags-must-be-an-array" });
|
|
203
|
-
|
|
204
|
-
const noTitle = await fetch(`${base}/memories`, {
|
|
205
|
-
method: "POST",
|
|
206
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
207
|
-
body: JSON.stringify({ type: "project", content: "c" })
|
|
208
|
-
});
|
|
209
|
-
assert.equal(noTitle.status, 400);
|
|
210
|
-
assert.deepEqual(await noTitle.json(), { error: "missing-title" });
|
|
211
|
-
} finally {
|
|
212
|
-
close();
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
// --- list filters / status / search ---------------------------------------------
|
|
217
|
-
|
|
218
|
-
test("GET /memories honors type + minImportance filters and paging", async () => {
|
|
219
|
-
const { base, auth, service, close } = await setup();
|
|
220
|
-
try {
|
|
221
|
-
service.saveWithDedupe({ type: "preference", title: "低", content: "i2", importance: 2 });
|
|
222
|
-
service.saveWithDedupe({ type: "preference", title: "高", content: "i5", importance: 5 });
|
|
223
|
-
service.saveWithDedupe({ type: "project", title: "项目", content: "i4", importance: 4 });
|
|
224
|
-
|
|
225
|
-
const byType = await fetch(`${base}/memories?type=preference`, { headers: auth });
|
|
226
|
-
assert.equal((await byType.json()).total, 2);
|
|
227
|
-
|
|
228
|
-
const byImportance = await fetch(`${base}/memories?minImportance=4`, { headers: auth });
|
|
229
|
-
const filtered = await byImportance.json();
|
|
230
|
-
assert.equal(filtered.total, 2);
|
|
231
|
-
assert.deepEqual(filtered.items.map((m) => m.title).sort(), ["项目", "高"]);
|
|
232
|
-
|
|
233
|
-
const paged = await fetch(`${base}/memories?limit=2&offset=0&order=chrono`, { headers: auth });
|
|
234
|
-
const page = await paged.json();
|
|
235
|
-
assert.equal(page.items.length, 2);
|
|
236
|
-
assert.equal(page.total, 3);
|
|
237
|
-
} finally {
|
|
238
|
-
close();
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
test("GET /status reports version, per-type totals, entities and uptime", async () => {
|
|
243
|
-
const { base, auth, service, close } = await setup();
|
|
244
|
-
try {
|
|
245
|
-
service.saveWithDedupe({ type: "preference", title: "p", content: "c" });
|
|
246
|
-
service.saveWithDedupe({ type: "project", title: "j", content: "c" });
|
|
247
|
-
service.saveWithDedupe({ type: "project", title: "j2", content: "c" });
|
|
248
|
-
|
|
249
|
-
const res = await fetch(`${base}/status`, { headers: auth });
|
|
250
|
-
assert.equal(res.status, 200);
|
|
251
|
-
const body = await res.json();
|
|
252
|
-
assert.equal(body.version, "0.7.12");
|
|
253
|
-
assert.equal(body.memories.total, 3);
|
|
254
|
-
assert.equal(body.memories.byType.preference, 1);
|
|
255
|
-
assert.equal(body.memories.byType.project, 2);
|
|
256
|
-
assert.equal(body.entities, 0, "no extraction wired → empty entity table");
|
|
257
|
-
assert.ok(Number.isInteger(body.uptime_s));
|
|
258
|
-
} finally {
|
|
259
|
-
close();
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
test("GET /search finds saved memories; empty q returns empty keyword result", async () => {
|
|
264
|
-
const { base, auth, close } = await setup();
|
|
265
|
-
try {
|
|
266
|
-
await fetch(`${base}/memories`, {
|
|
267
|
-
method: "POST",
|
|
268
|
-
headers: { ...auth, "content-type": "application/json" },
|
|
269
|
-
body: JSON.stringify({ type: "project", title: "记忆插件", content: "SQLite 全文检索" })
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
const res = await fetch(`${base}/search?q=${encodeURIComponent("全文检索")}&mode=keyword`, { headers: auth });
|
|
273
|
-
assert.equal(res.status, 200);
|
|
274
|
-
const body = await res.json();
|
|
275
|
-
assert.equal(body.mode, "keyword");
|
|
276
|
-
assert.equal(body.items.length, 1);
|
|
277
|
-
assert.equal(body.items[0].title, "记忆插件");
|
|
278
|
-
|
|
279
|
-
const empty = await fetch(`${base}/search?q=`, { headers: auth });
|
|
280
|
-
assert.deepEqual(await empty.json(), { items: [], mode: "keyword" });
|
|
281
|
-
} finally {
|
|
282
|
-
close();
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
test("unknown path returns 404 json", async () => {
|
|
287
|
-
const { base, auth, close } = await setup();
|
|
288
|
-
try {
|
|
289
|
-
const res = await fetch(`${base}/nope`, { headers: auth });
|
|
290
|
-
assert.equal(res.status, 404);
|
|
291
|
-
assert.deepEqual(await res.json(), { error: "not-found" });
|
|
292
|
-
} finally {
|
|
293
|
-
close();
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
// --- light-mode preset (applyLightModePreset) ------------------------------------
|
|
298
|
-
|
|
299
|
-
const LIGHT_OFF_FIELDS = [
|
|
300
|
-
"entityExtractionEnabled",
|
|
301
|
-
"autoDream",
|
|
302
|
-
"sleepModeEnabled",
|
|
303
|
-
"rerankEnabled",
|
|
304
|
-
"autoReindexOnBoot",
|
|
305
|
-
"hybridInject",
|
|
306
|
-
"searchSemanticDedup",
|
|
307
|
-
"selectiveInjectEnabled",
|
|
308
|
-
"bm25SearchEnabled"
|
|
309
|
-
];
|
|
310
|
-
|
|
311
|
-
test("applyLightModePreset turns heavy features off and keeps the core loop", () => {
|
|
312
|
-
const cfg = applyLightModePreset(Config({ lightMode: true }));
|
|
313
|
-
for (const field of LIGHT_OFF_FIELDS) {
|
|
314
|
-
assert.equal(cfg[field], false, `${field} forced off in light mode`);
|
|
315
|
-
}
|
|
316
|
-
// Core loop preserved.
|
|
317
|
-
assert.equal(cfg.autoInject, true);
|
|
318
|
-
assert.equal(cfg.autoSummarize, true);
|
|
319
|
-
assert.equal(cfg.hotMemoryEnabled, true);
|
|
320
|
-
assert.equal(cfg.memoryQualityFilter.enabled, true);
|
|
321
|
-
// Unrelated knobs untouched.
|
|
322
|
-
assert.equal(cfg.dreamThresholdCount, 10);
|
|
323
|
-
assert.equal(cfg.dreamDelayMs, 2000);
|
|
324
|
-
assert.equal(cfg.maxInjectedItems, 5);
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
test("applyLightModePreset keeps explicit non-preset values and is a no-op without lightMode", () => {
|
|
328
|
-
const tuned = applyLightModePreset(Config({ lightMode: true, dreamThresholdCount: 30, maxInjectedItems: 8 }));
|
|
329
|
-
assert.equal(tuned.dreamThresholdCount, 30, "operator values survive the preset");
|
|
330
|
-
assert.equal(tuned.maxInjectedItems, 8);
|
|
331
|
-
|
|
332
|
-
const plain = Config({});
|
|
333
|
-
assert.equal(applyLightModePreset(plain), plain, "identity when lightMode is unset");
|
|
334
|
-
const off = Config({ lightMode: false });
|
|
335
|
-
assert.equal(applyLightModePreset(off), off, "identity when lightMode is false");
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
test("persisted panel_mode=light wins over a bundle config that did not ask for it", () => {
|
|
339
|
-
const store = createStore(":memory:");
|
|
340
|
-
const settings = createSettings(store.db);
|
|
341
|
-
try {
|
|
342
|
-
// Panel switched to light in a previous session; bundle config says nothing.
|
|
343
|
-
settings.setPanelMode("light");
|
|
344
|
-
const rawCfg = Config({});
|
|
345
|
-
const lightMode = rawCfg.lightMode === true || settings.getPanelMode() === "light";
|
|
346
|
-
const cfg = applyLightModePreset({ ...rawCfg, lightMode });
|
|
347
|
-
assert.equal(lightMode, true, "persisted mode forces light");
|
|
348
|
-
assert.equal(cfg.autoDream, false);
|
|
349
|
-
assert.equal(cfg.entityExtractionEnabled, false);
|
|
350
|
-
assert.equal(cfg.autoInject, true, "core injection stays on");
|
|
351
|
-
|
|
352
|
-
// standard (default) never forces the preset even with a light bundle flag off.
|
|
353
|
-
settings.setPanelMode("standard");
|
|
354
|
-
const raw2 = Config({});
|
|
355
|
-
assert.equal(raw2.lightMode === true || settings.getPanelMode() === "light", false);
|
|
356
|
-
} finally {
|
|
357
|
-
store.close();
|
|
358
|
-
}
|
|
359
|
-
});
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createStore } from "../src/store.js";
|
|
4
|
+
import { createService } from "../src/service.js";
|
|
5
|
+
import { createSettings } from "../src/settings.js";
|
|
6
|
+
import { createStandaloneApi } from "../src/api-standalone.js";
|
|
7
|
+
import { Config, applyLightModePreset } from "../src/config.js";
|
|
8
|
+
|
|
9
|
+
// Real HTTP server on an OS-assigned port (port: 0), driven with fetch.
|
|
10
|
+
async function setup() {
|
|
11
|
+
const store = createStore(":memory:");
|
|
12
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
13
|
+
const settings = createSettings(store.db);
|
|
14
|
+
const api = createStandaloneApi({ service, store, config: {}, settings, logger: null, port: 0 });
|
|
15
|
+
await api.ready;
|
|
16
|
+
const base = `http://127.0.0.1:${api.port}`;
|
|
17
|
+
const auth = { authorization: `Bearer ${api.token}` };
|
|
18
|
+
return {
|
|
19
|
+
store,
|
|
20
|
+
service,
|
|
21
|
+
settings,
|
|
22
|
+
api,
|
|
23
|
+
base,
|
|
24
|
+
auth,
|
|
25
|
+
close: () => {
|
|
26
|
+
// Drop pooled keep-alive sockets so node --test can drain the loop.
|
|
27
|
+
api.server.closeIdleConnections?.();
|
|
28
|
+
api.server.close();
|
|
29
|
+
store.close();
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// --- auth ---------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
test("GET /health is open without a token; every other route 401s", async () => {
|
|
37
|
+
const { base, close } = await setup();
|
|
38
|
+
try {
|
|
39
|
+
const health = await fetch(`${base}/health`);
|
|
40
|
+
assert.equal(health.status, 200);
|
|
41
|
+
assert.deepEqual(await health.json(), { ok: true });
|
|
42
|
+
|
|
43
|
+
for (const path of ["/status", "/profile", "/rules", "/memories", "/memories/x", "/search?q=x"]) {
|
|
44
|
+
const res = await fetch(`${base}${path}`);
|
|
45
|
+
assert.equal(res.status, 401, `${path} requires a token`);
|
|
46
|
+
assert.deepEqual(await res.json(), { error: "unauthorized" });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const bad = await fetch(`${base}/status`, { headers: { authorization: "Bearer wrong-token" } });
|
|
50
|
+
assert.equal(bad.status, 401, "wrong token rejected");
|
|
51
|
+
} finally {
|
|
52
|
+
close();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("GET /profile and /rules return saved settings", async () => {
|
|
57
|
+
const { base, auth, settings, close } = await setup();
|
|
58
|
+
try {
|
|
59
|
+
settings.setProfile("我是后端工程师");
|
|
60
|
+
settings.setRules(["先验证再修改", "默认使用中文回复"]);
|
|
61
|
+
|
|
62
|
+
const profile = await fetch(`${base}/profile`, { headers: auth });
|
|
63
|
+
assert.equal(profile.status, 200);
|
|
64
|
+
assert.deepEqual(await profile.json(), { profile: "我是后端工程师" });
|
|
65
|
+
|
|
66
|
+
const rules = await fetch(`${base}/rules`, { headers: auth });
|
|
67
|
+
assert.equal(rules.status, 200);
|
|
68
|
+
assert.deepEqual(await rules.json(), { rules: ["先验证再修改", "默认使用中文回复"] });
|
|
69
|
+
} finally {
|
|
70
|
+
close();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("GET /profile and /rules return empty values when unset", async () => {
|
|
75
|
+
const { base, auth, close } = await setup();
|
|
76
|
+
try {
|
|
77
|
+
const profile = await fetch(`${base}/profile`, { headers: auth });
|
|
78
|
+
assert.equal(profile.status, 200);
|
|
79
|
+
assert.deepEqual(await profile.json(), { profile: "" });
|
|
80
|
+
|
|
81
|
+
const rules = await fetch(`${base}/rules`, { headers: auth });
|
|
82
|
+
assert.equal(rules.status, 200);
|
|
83
|
+
assert.deepEqual(await rules.json(), { rules: [] });
|
|
84
|
+
} finally {
|
|
85
|
+
close();
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("token is persisted into settings kv and reused across restarts", async () => {
|
|
90
|
+
const store = createStore(":memory:");
|
|
91
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
92
|
+
const settings = createSettings(store.db);
|
|
93
|
+
const first = createStandaloneApi({ service, store, config: {}, settings, port: 0 });
|
|
94
|
+
await first.ready;
|
|
95
|
+
try {
|
|
96
|
+
assert.equal(settings.getExternalApi().token, first.token, "generated token persisted");
|
|
97
|
+
assert.ok(first.token.length >= 20, "token is a real random value");
|
|
98
|
+
const second = createStandaloneApi({ service, store, config: {}, settings, port: 0 });
|
|
99
|
+
await second.ready;
|
|
100
|
+
assert.equal(second.token, first.token, "restart reuses the persisted token");
|
|
101
|
+
second.server.closeIdleConnections?.();
|
|
102
|
+
second.server.close();
|
|
103
|
+
} finally {
|
|
104
|
+
first.server.closeIdleConnections?.();
|
|
105
|
+
first.server.close();
|
|
106
|
+
store.close();
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// --- CRUD loop ------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
test("POST/GET/DELETE /memories round trip", async () => {
|
|
113
|
+
const { base, auth, close } = await setup();
|
|
114
|
+
try {
|
|
115
|
+
const post = await fetch(`${base}/memories`, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
118
|
+
body: JSON.stringify({ type: "preference", title: "语言", content: "始终用中文回复", importance: 4, tags: ["交流"] })
|
|
119
|
+
});
|
|
120
|
+
assert.equal(post.status, 201);
|
|
121
|
+
const created = await post.json();
|
|
122
|
+
assert.ok(created.id, "row id returned");
|
|
123
|
+
assert.equal(created.title, "语言");
|
|
124
|
+
assert.equal(created.importance, 4);
|
|
125
|
+
|
|
126
|
+
const list = await fetch(`${base}/memories?limit=10`, { headers: auth });
|
|
127
|
+
assert.equal(list.status, 200);
|
|
128
|
+
const listBody = await list.json();
|
|
129
|
+
assert.equal(listBody.total, 1);
|
|
130
|
+
assert.equal(listBody.items.length, 1);
|
|
131
|
+
|
|
132
|
+
const got = await fetch(`${base}/memories/${created.id}`, { headers: auth });
|
|
133
|
+
assert.equal(got.status, 200);
|
|
134
|
+
assert.equal((await got.json()).content, "始终用中文回复");
|
|
135
|
+
|
|
136
|
+
const del = await fetch(`${base}/memories/${created.id}`, { method: "DELETE", headers: auth });
|
|
137
|
+
assert.equal(del.status, 200);
|
|
138
|
+
assert.deepEqual(await del.json(), { ok: true });
|
|
139
|
+
|
|
140
|
+
const gone = await fetch(`${base}/memories/${created.id}`, { headers: auth });
|
|
141
|
+
assert.equal(gone.status, 404, "deleted row reads back as 404");
|
|
142
|
+
const delAgain = await fetch(`${base}/memories/${created.id}`, { method: "DELETE", headers: auth });
|
|
143
|
+
assert.equal(delAgain.status, 404, "deleting a missing row is 404, not a fake ok");
|
|
144
|
+
} finally {
|
|
145
|
+
close();
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("POST /memories dedupes same title within a type (200 + merged content)", async () => {
|
|
150
|
+
const { base, auth, close } = await setup();
|
|
151
|
+
try {
|
|
152
|
+
const first = await fetch(`${base}/memories`, {
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
155
|
+
body: JSON.stringify({ type: "project", title: "mneme", content: "v1 内容" })
|
|
156
|
+
});
|
|
157
|
+
assert.equal(first.status, 201);
|
|
158
|
+
const row1 = await first.json();
|
|
159
|
+
|
|
160
|
+
const second = await fetch(`${base}/memories`, {
|
|
161
|
+
method: "POST",
|
|
162
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
163
|
+
body: JSON.stringify({ type: "project", title: "mneme", content: "v2 追加" })
|
|
164
|
+
});
|
|
165
|
+
assert.equal(second.status, 200, "merge, not a second creation");
|
|
166
|
+
const row2 = await second.json();
|
|
167
|
+
assert.equal(row2.id, row1.id, "same row reused");
|
|
168
|
+
assert.ok(row2.content.includes("v1 内容") && row2.content.includes("v2 追加"), "content appended");
|
|
169
|
+
|
|
170
|
+
const list = await fetch(`${base}/memories`, { headers: auth });
|
|
171
|
+
assert.equal((await list.json()).total, 1, "no duplicate row created");
|
|
172
|
+
} finally {
|
|
173
|
+
close();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("POST /memories validation: invalid type, bad JSON, non-array tags → 400", async () => {
|
|
178
|
+
const { base, auth, close } = await setup();
|
|
179
|
+
try {
|
|
180
|
+
const badType = await fetch(`${base}/memories`, {
|
|
181
|
+
method: "POST",
|
|
182
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
183
|
+
body: JSON.stringify({ type: "diary", title: "t", content: "c" })
|
|
184
|
+
});
|
|
185
|
+
assert.equal(badType.status, 400);
|
|
186
|
+
assert.deepEqual(await badType.json(), { error: "invalid-type" });
|
|
187
|
+
|
|
188
|
+
const badJson = await fetch(`${base}/memories`, {
|
|
189
|
+
method: "POST",
|
|
190
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
191
|
+
body: "{not json"
|
|
192
|
+
});
|
|
193
|
+
assert.equal(badJson.status, 400);
|
|
194
|
+
assert.deepEqual(await badJson.json(), { error: "invalid-json" });
|
|
195
|
+
|
|
196
|
+
const badTags = await fetch(`${base}/memories`, {
|
|
197
|
+
method: "POST",
|
|
198
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
199
|
+
body: JSON.stringify({ type: "project", title: "t", content: "c", tags: "x" })
|
|
200
|
+
});
|
|
201
|
+
assert.equal(badTags.status, 400);
|
|
202
|
+
assert.deepEqual(await badTags.json(), { error: "tags-must-be-an-array" });
|
|
203
|
+
|
|
204
|
+
const noTitle = await fetch(`${base}/memories`, {
|
|
205
|
+
method: "POST",
|
|
206
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
207
|
+
body: JSON.stringify({ type: "project", content: "c" })
|
|
208
|
+
});
|
|
209
|
+
assert.equal(noTitle.status, 400);
|
|
210
|
+
assert.deepEqual(await noTitle.json(), { error: "missing-title" });
|
|
211
|
+
} finally {
|
|
212
|
+
close();
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// --- list filters / status / search ---------------------------------------------
|
|
217
|
+
|
|
218
|
+
test("GET /memories honors type + minImportance filters and paging", async () => {
|
|
219
|
+
const { base, auth, service, close } = await setup();
|
|
220
|
+
try {
|
|
221
|
+
service.saveWithDedupe({ type: "preference", title: "低", content: "i2", importance: 2 });
|
|
222
|
+
service.saveWithDedupe({ type: "preference", title: "高", content: "i5", importance: 5 });
|
|
223
|
+
service.saveWithDedupe({ type: "project", title: "项目", content: "i4", importance: 4 });
|
|
224
|
+
|
|
225
|
+
const byType = await fetch(`${base}/memories?type=preference`, { headers: auth });
|
|
226
|
+
assert.equal((await byType.json()).total, 2);
|
|
227
|
+
|
|
228
|
+
const byImportance = await fetch(`${base}/memories?minImportance=4`, { headers: auth });
|
|
229
|
+
const filtered = await byImportance.json();
|
|
230
|
+
assert.equal(filtered.total, 2);
|
|
231
|
+
assert.deepEqual(filtered.items.map((m) => m.title).sort(), ["项目", "高"]);
|
|
232
|
+
|
|
233
|
+
const paged = await fetch(`${base}/memories?limit=2&offset=0&order=chrono`, { headers: auth });
|
|
234
|
+
const page = await paged.json();
|
|
235
|
+
assert.equal(page.items.length, 2);
|
|
236
|
+
assert.equal(page.total, 3);
|
|
237
|
+
} finally {
|
|
238
|
+
close();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("GET /status reports version, per-type totals, entities and uptime", async () => {
|
|
243
|
+
const { base, auth, service, close } = await setup();
|
|
244
|
+
try {
|
|
245
|
+
service.saveWithDedupe({ type: "preference", title: "p", content: "c" });
|
|
246
|
+
service.saveWithDedupe({ type: "project", title: "j", content: "c" });
|
|
247
|
+
service.saveWithDedupe({ type: "project", title: "j2", content: "c" });
|
|
248
|
+
|
|
249
|
+
const res = await fetch(`${base}/status`, { headers: auth });
|
|
250
|
+
assert.equal(res.status, 200);
|
|
251
|
+
const body = await res.json();
|
|
252
|
+
assert.equal(body.version, "0.7.12");
|
|
253
|
+
assert.equal(body.memories.total, 3);
|
|
254
|
+
assert.equal(body.memories.byType.preference, 1);
|
|
255
|
+
assert.equal(body.memories.byType.project, 2);
|
|
256
|
+
assert.equal(body.entities, 0, "no extraction wired → empty entity table");
|
|
257
|
+
assert.ok(Number.isInteger(body.uptime_s));
|
|
258
|
+
} finally {
|
|
259
|
+
close();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("GET /search finds saved memories; empty q returns empty keyword result", async () => {
|
|
264
|
+
const { base, auth, close } = await setup();
|
|
265
|
+
try {
|
|
266
|
+
await fetch(`${base}/memories`, {
|
|
267
|
+
method: "POST",
|
|
268
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
269
|
+
body: JSON.stringify({ type: "project", title: "记忆插件", content: "SQLite 全文检索" })
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const res = await fetch(`${base}/search?q=${encodeURIComponent("全文检索")}&mode=keyword`, { headers: auth });
|
|
273
|
+
assert.equal(res.status, 200);
|
|
274
|
+
const body = await res.json();
|
|
275
|
+
assert.equal(body.mode, "keyword");
|
|
276
|
+
assert.equal(body.items.length, 1);
|
|
277
|
+
assert.equal(body.items[0].title, "记忆插件");
|
|
278
|
+
|
|
279
|
+
const empty = await fetch(`${base}/search?q=`, { headers: auth });
|
|
280
|
+
assert.deepEqual(await empty.json(), { items: [], mode: "keyword" });
|
|
281
|
+
} finally {
|
|
282
|
+
close();
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test("unknown path returns 404 json", async () => {
|
|
287
|
+
const { base, auth, close } = await setup();
|
|
288
|
+
try {
|
|
289
|
+
const res = await fetch(`${base}/nope`, { headers: auth });
|
|
290
|
+
assert.equal(res.status, 404);
|
|
291
|
+
assert.deepEqual(await res.json(), { error: "not-found" });
|
|
292
|
+
} finally {
|
|
293
|
+
close();
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
// --- light-mode preset (applyLightModePreset) ------------------------------------
|
|
298
|
+
|
|
299
|
+
const LIGHT_OFF_FIELDS = [
|
|
300
|
+
"entityExtractionEnabled",
|
|
301
|
+
"autoDream",
|
|
302
|
+
"sleepModeEnabled",
|
|
303
|
+
"rerankEnabled",
|
|
304
|
+
"autoReindexOnBoot",
|
|
305
|
+
"hybridInject",
|
|
306
|
+
"searchSemanticDedup",
|
|
307
|
+
"selectiveInjectEnabled",
|
|
308
|
+
"bm25SearchEnabled"
|
|
309
|
+
];
|
|
310
|
+
|
|
311
|
+
test("applyLightModePreset turns heavy features off and keeps the core loop", () => {
|
|
312
|
+
const cfg = applyLightModePreset(Config({ lightMode: true }));
|
|
313
|
+
for (const field of LIGHT_OFF_FIELDS) {
|
|
314
|
+
assert.equal(cfg[field], false, `${field} forced off in light mode`);
|
|
315
|
+
}
|
|
316
|
+
// Core loop preserved.
|
|
317
|
+
assert.equal(cfg.autoInject, true);
|
|
318
|
+
assert.equal(cfg.autoSummarize, true);
|
|
319
|
+
assert.equal(cfg.hotMemoryEnabled, true);
|
|
320
|
+
assert.equal(cfg.memoryQualityFilter.enabled, true);
|
|
321
|
+
// Unrelated knobs untouched.
|
|
322
|
+
assert.equal(cfg.dreamThresholdCount, 10);
|
|
323
|
+
assert.equal(cfg.dreamDelayMs, 2000);
|
|
324
|
+
assert.equal(cfg.maxInjectedItems, 5);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("applyLightModePreset keeps explicit non-preset values and is a no-op without lightMode", () => {
|
|
328
|
+
const tuned = applyLightModePreset(Config({ lightMode: true, dreamThresholdCount: 30, maxInjectedItems: 8 }));
|
|
329
|
+
assert.equal(tuned.dreamThresholdCount, 30, "operator values survive the preset");
|
|
330
|
+
assert.equal(tuned.maxInjectedItems, 8);
|
|
331
|
+
|
|
332
|
+
const plain = Config({});
|
|
333
|
+
assert.equal(applyLightModePreset(plain), plain, "identity when lightMode is unset");
|
|
334
|
+
const off = Config({ lightMode: false });
|
|
335
|
+
assert.equal(applyLightModePreset(off), off, "identity when lightMode is false");
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test("persisted panel_mode=light wins over a bundle config that did not ask for it", () => {
|
|
339
|
+
const store = createStore(":memory:");
|
|
340
|
+
const settings = createSettings(store.db);
|
|
341
|
+
try {
|
|
342
|
+
// Panel switched to light in a previous session; bundle config says nothing.
|
|
343
|
+
settings.setPanelMode("light");
|
|
344
|
+
const rawCfg = Config({});
|
|
345
|
+
const lightMode = rawCfg.lightMode === true || settings.getPanelMode() === "light";
|
|
346
|
+
const cfg = applyLightModePreset({ ...rawCfg, lightMode });
|
|
347
|
+
assert.equal(lightMode, true, "persisted mode forces light");
|
|
348
|
+
assert.equal(cfg.autoDream, false);
|
|
349
|
+
assert.equal(cfg.entityExtractionEnabled, false);
|
|
350
|
+
assert.equal(cfg.autoInject, true, "core injection stays on");
|
|
351
|
+
|
|
352
|
+
// standard (default) never forces the preset even with a light bundle flag off.
|
|
353
|
+
settings.setPanelMode("standard");
|
|
354
|
+
const raw2 = Config({});
|
|
355
|
+
assert.equal(raw2.lightMode === true || settings.getPanelMode() === "light", false);
|
|
356
|
+
} finally {
|
|
357
|
+
store.close();
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
// --- #181:MCP 六件套的 API 面(PUT / save 透传 / archived 与 occurred 窗口)---
|
|
362
|
+
|
|
363
|
+
test("PUT /memories/:id patches fields and returns the DTO (unauthorized without token)", async () => {
|
|
364
|
+
const { base, auth, service, close } = await setup();
|
|
365
|
+
try {
|
|
366
|
+
const created = await (await fetch(`${base}/memories`, {
|
|
367
|
+
method: "POST",
|
|
368
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
369
|
+
body: JSON.stringify({ type: "preference", title: "编辑器", content: "用 vim", importance: 3 })
|
|
370
|
+
})).json();
|
|
371
|
+
|
|
372
|
+
const noAuth = await fetch(`${base}/memories/${created.id}`, {
|
|
373
|
+
method: "PUT",
|
|
374
|
+
headers: { "content-type": "application/json" },
|
|
375
|
+
body: JSON.stringify({ content: "用 nvim" })
|
|
376
|
+
});
|
|
377
|
+
assert.equal(noAuth.status, 401, "PUT requires the Bearer token");
|
|
378
|
+
|
|
379
|
+
const put = await fetch(`${base}/memories/${created.id}`, {
|
|
380
|
+
method: "PUT",
|
|
381
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
382
|
+
body: JSON.stringify({ content: "用 nvim", importance: 4, tags: ["工具"], reason: "用户改用 nvim 了" })
|
|
383
|
+
});
|
|
384
|
+
assert.equal(put.status, 200);
|
|
385
|
+
const { memory } = await put.json();
|
|
386
|
+
assert.equal(memory.content, "用 nvim");
|
|
387
|
+
assert.equal(memory.importance, 4);
|
|
388
|
+
assert.deepEqual(memory.tags, ["工具"]);
|
|
389
|
+
|
|
390
|
+
// content 改写按 human_override 入档(与内部面板写路径一致)。
|
|
391
|
+
const raw = service.getById(created.id);
|
|
392
|
+
assert.equal(raw.content_history?.[0]?.source, "human_override");
|
|
393
|
+
assert.equal(raw.content_history?.[0]?.content, "用 vim");
|
|
394
|
+
} finally {
|
|
395
|
+
close();
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test("PUT /memories/:id validation: bad fields → 400, empty patch → 400, missing id → 404", async () => {
|
|
400
|
+
const { base, auth, close } = await setup();
|
|
401
|
+
try {
|
|
402
|
+
const put = (body, id = "whatever") => fetch(`${base}/memories/${id}`, {
|
|
403
|
+
method: "PUT",
|
|
404
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
405
|
+
body: JSON.stringify(body)
|
|
406
|
+
});
|
|
407
|
+
assert.equal((await put({ title: "" })).status, 400);
|
|
408
|
+
assert.equal((await put({ importance: 9 })).status, 400);
|
|
409
|
+
assert.equal((await put({ tags: "x" })).status, 400);
|
|
410
|
+
assert.equal((await put({ type: "nope" })).status, 400);
|
|
411
|
+
assert.equal((await put({})).status, 400, "empty patch is no-fields");
|
|
412
|
+
assert.equal((await put({ content: "x" }, "missing-id")).status, 404);
|
|
413
|
+
assert.equal((await put({ agent_scope: 5 })).status, 400);
|
|
414
|
+
} finally {
|
|
415
|
+
close();
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
test("PUT /memories/:id surfaces service failures as 500 instead of a hung request", async () => {
|
|
420
|
+
const { base, auth, service, close } = await setup();
|
|
421
|
+
try {
|
|
422
|
+
const created = await (await fetch(`${base}/memories`, {
|
|
423
|
+
method: "POST",
|
|
424
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
425
|
+
body: JSON.stringify({ type: "preference", title: "会抛错", content: "v1" })
|
|
426
|
+
})).json();
|
|
427
|
+
const original = service.update;
|
|
428
|
+
service.update = () => { throw new Error("boom"); };
|
|
429
|
+
try {
|
|
430
|
+
const res = await fetch(`${base}/memories/${created.id}`, {
|
|
431
|
+
method: "PUT",
|
|
432
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
433
|
+
body: JSON.stringify({ content: "v2" })
|
|
434
|
+
});
|
|
435
|
+
assert.equal(res.status, 500, "service throw must not strand the request");
|
|
436
|
+
assert.deepEqual(await res.json(), { error: "internal" });
|
|
437
|
+
} finally {
|
|
438
|
+
service.update = original;
|
|
439
|
+
}
|
|
440
|
+
} finally {
|
|
441
|
+
close();
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
test("POST /memories passes through sensitivity/occurred_at/scope and returns action", async () => {
|
|
446
|
+
const { base, auth, close } = await setup();
|
|
447
|
+
try {
|
|
448
|
+
const post = await fetch(`${base}/memories`, {
|
|
449
|
+
method: "POST",
|
|
450
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
451
|
+
body: JSON.stringify({
|
|
452
|
+
type: "decision",
|
|
453
|
+
title: "迁移",
|
|
454
|
+
content: "迁到 sqlite 分支",
|
|
455
|
+
sensitivity: "personal",
|
|
456
|
+
occurred_at: "2026-01-15T10:00:00Z",
|
|
457
|
+
agent_scope: "global"
|
|
458
|
+
})
|
|
459
|
+
});
|
|
460
|
+
assert.equal(post.status, 201);
|
|
461
|
+
const created = await post.json();
|
|
462
|
+
assert.equal(created.action, "created", "action rides along for tool parity");
|
|
463
|
+
assert.equal(created.sensitivity, "personal");
|
|
464
|
+
// store 把 ISO 瞬时归一化成完整毫秒形式(2026-01-15T10:00:00Z → …T10:00:00.000Z)。
|
|
465
|
+
assert.match(created.occurred_at, /^2026-01-15T10:00:00/);
|
|
466
|
+
assert.equal(created.agent_scope, "global");
|
|
467
|
+
assert.equal(created.agent_scope_source, "explicit");
|
|
468
|
+
|
|
469
|
+
// 合并判据=同 type + 同标题 + 同 sensitivity + 同作用域三元(0.8.0 起去重键
|
|
470
|
+
// 含 scope,跨作用域同标题本就分开存):补齐 agent_scope 才 merged。
|
|
471
|
+
const merged = await (await fetch(`${base}/memories`, {
|
|
472
|
+
method: "POST",
|
|
473
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
474
|
+
body: JSON.stringify({ type: "decision", title: "迁移", content: "迁到 sqlite 分支(更新)", sensitivity: "personal", agent_scope: "global" })
|
|
475
|
+
})).json();
|
|
476
|
+
assert.equal(merged.action, "merged", "same dedupe key reports merged");
|
|
477
|
+
|
|
478
|
+
const separate = await (await fetch(`${base}/memories`, {
|
|
479
|
+
method: "POST",
|
|
480
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
481
|
+
body: JSON.stringify({ type: "decision", title: "迁移", content: "无标注版本" })
|
|
482
|
+
})).json();
|
|
483
|
+
assert.equal(separate.action, "created", "no sensitivity/scope → different dedupe key");
|
|
484
|
+
|
|
485
|
+
const bad = await fetch(`${base}/memories`, {
|
|
486
|
+
method: "POST",
|
|
487
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
488
|
+
body: JSON.stringify({ type: "decision", title: "x", content: "y", sensitivity: 5 })
|
|
489
|
+
});
|
|
490
|
+
assert.equal(bad.status, 400);
|
|
491
|
+
assert.equal((await bad.json()).error, "invalid-sensitivity");
|
|
492
|
+
} finally {
|
|
493
|
+
close();
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
test("GET /memories supports include_archived and the occurred_at window", async () => {
|
|
498
|
+
const { base, auth, service, close } = await setup();
|
|
499
|
+
try {
|
|
500
|
+
const save = (title, occurredAt) => fetch(`${base}/memories`, {
|
|
501
|
+
method: "POST",
|
|
502
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
503
|
+
body: JSON.stringify({ type: "history", title, content: `body of ${title}`, ...(occurredAt ? { occurred_at: occurredAt } : {}) })
|
|
504
|
+
});
|
|
505
|
+
const january = await (await save("一月的事", "2026-01-15T10:00:00Z")).json();
|
|
506
|
+
await save("二月的事", "2026-02-15T10:00:00Z");
|
|
507
|
+
await save("无时间戳的事", null);
|
|
508
|
+
|
|
509
|
+
const february = await (await fetch(`${base}/memories?occurred_from=2026-02-01`, { headers: auth })).json();
|
|
510
|
+
// 「无时间戳的事」按 occurred_at→created_at 回退(写入日 2026-09),落在窗口内。
|
|
511
|
+
assert.deepEqual(february.items.map((m) => m.title).sort(), ["二月的事", "无时间戳的事"]);
|
|
512
|
+
assert.equal(february.total, 2, "total honors the same window");
|
|
513
|
+
|
|
514
|
+
const upToJanuary = await (await fetch(`${base}/memories?occurred_to=2026-01-31`, { headers: auth })).json();
|
|
515
|
+
assert.deepEqual(upToJanuary.items.map((m) => m.title), ["一月的事"]);
|
|
516
|
+
|
|
517
|
+
service.setArchived(january.id, true);
|
|
518
|
+
const active = await (await fetch(`${base}/memories`, { headers: auth })).json();
|
|
519
|
+
assert.equal(active.total, 2, "archived rows stay hidden by default");
|
|
520
|
+
const withArchived = await (await fetch(`${base}/memories?include_archived=true`, { headers: auth })).json();
|
|
521
|
+
assert.equal(withArchived.total, 3, "include_archived=true surfaces archived rows");
|
|
522
|
+
} finally {
|
|
523
|
+
close();
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
test("GET /search honors the occurred_at window", async () => {
|
|
528
|
+
const { base, auth, close } = await setup();
|
|
529
|
+
try {
|
|
530
|
+
const save = (title, occurredAt) => fetch(`${base}/memories`, {
|
|
531
|
+
method: "POST",
|
|
532
|
+
headers: { ...auth, "content-type": "application/json" },
|
|
533
|
+
body: JSON.stringify({ type: "history", title, content: `记忆星球 ${title}`, ...(occurredAt ? { occurred_at: occurredAt } : {}) })
|
|
534
|
+
});
|
|
535
|
+
await save("旧事", "2026-01-15T10:00:00Z");
|
|
536
|
+
await save("新事", "2026-03-15T10:00:00Z");
|
|
537
|
+
|
|
538
|
+
const hit = await (await fetch(`${base}/search?q=${encodeURIComponent("记忆星球")}&occurred_from=2026-03-01`, { headers: auth })).json();
|
|
539
|
+
assert.equal(hit.items.length, 1);
|
|
540
|
+
assert.equal(hit.items[0].title, "新事");
|
|
541
|
+
} finally {
|
|
542
|
+
close();
|
|
543
|
+
}
|
|
544
|
+
});
|