@modusensus/dsh-mneme 0.8.0 → 0.8.2
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/README.md +15 -5
- package/lib/api-standalone.js +12 -0
- package/lib/api.js +52 -14
- package/lib/client.js +130 -8
- package/lib/config.js +8 -0
- package/lib/dream/decisions.js +55 -8
- package/lib/dream/sleep.js +79 -17
- package/lib/dream.js +61 -9
- package/lib/entities/extractor.js +2 -2
- package/lib/index.js +6 -0
- package/lib/lang.js +13 -2
- package/lib/local-embedder.js +9 -1
- package/lib/reranker.js +38 -8
- package/lib/scope.js +39 -4
- package/lib/service.js +121 -18
- package/lib/store.js +243 -40
- package/lib/summarize.js +2 -2
- package/lib/tools.js +64 -30
- package/lib/vector-index.js +2 -8
- package/lib/version-check.js +122 -0
- package/package.json +2 -1
- package/scripts/sync-test-badge.mjs +61 -0
- package/src/api-standalone.js +12 -0
- package/src/api.js +52 -14
- package/src/config.js +8 -0
- package/src/dream/decisions.js +55 -8
- package/src/dream/sleep.js +79 -17
- package/src/dream.js +61 -9
- package/src/entities/extractor.js +2 -2
- package/src/index.js +6 -0
- package/src/lang.js +13 -2
- package/src/local-embedder.js +9 -1
- package/src/reranker.js +38 -8
- package/src/scope.js +39 -4
- package/src/service.js +121 -18
- package/src/store.js +243 -40
- package/src/summarize.js +2 -2
- package/src/tools.js +64 -30
- package/src/vector-index.js +2 -8
- package/src/version-check.js +122 -0
- package/test/api.test.js +5 -0
- package/test/conflict-freeze.test.js +15 -5
- package/test/dream-archive-guardrail.test.js +116 -0
- package/test/dream-coverage-degrade.test.js +86 -0
- package/test/dream.test.js +15 -11
- package/test/inject-first-round-bm25.test.js +89 -0
- package/test/llm-message-source.test.js +177 -0
- package/test/reranker-single-logit.test.js +73 -0
- package/test/scope-candidates.test.js +239 -0
- package/test/scope-explicit.test.js +452 -0
- package/test/scope-storage.test.js +13 -5
- package/test/scope-strict.test.js +107 -41
- package/test/skip-invalid-restore.test.js +7 -5
- package/test/sleep-scheduler-rearm.test.js +89 -0
- package/test/sleep.test.js +2 -2
- package/test/standalone-api.test.js +34 -1
- package/test/store-vector-cache.test.js +98 -0
- package/test/tools-memory-get-schema.test.js +65 -0
- package/test/version-check.test.js +152 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
validateRegistryUrl,
|
|
6
|
+
compareVersions,
|
|
7
|
+
classify,
|
|
8
|
+
fetchLatestVersion,
|
|
9
|
+
resetCacheForTest,
|
|
10
|
+
REGISTRY_URL,
|
|
11
|
+
PACKAGE_VERSION,
|
|
12
|
+
} from '../src/version-check.js';
|
|
13
|
+
|
|
14
|
+
describe('version-check.js', () => {
|
|
15
|
+
// ---- validateRegistryUrl:https + 精确 host 白名单(构造性排除本机/内网)----
|
|
16
|
+
describe('validateRegistryUrl', () => {
|
|
17
|
+
test('白名单内 https 地址通过', () => {
|
|
18
|
+
assert.equal(validateRegistryUrl('https://registry.npmjs.org/@modusensus%2Fdsh-mneme/latest'), true);
|
|
19
|
+
assert.equal(validateRegistryUrl('https://registry.npmjs.org/anything'), true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('非 https 一律拒绝', () => {
|
|
23
|
+
assert.equal(validateRegistryUrl('http://registry.npmjs.org/latest'), false);
|
|
24
|
+
assert.equal(validateRegistryUrl('ftp://registry.npmjs.org/latest'), false);
|
|
25
|
+
assert.equal(validateRegistryUrl('file:///etc/passwd'), false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('host 不在白名单一律拒绝(含本机/内网/相似域名)', () => {
|
|
29
|
+
assert.equal(validateRegistryUrl('https://localhost/latest'), false);
|
|
30
|
+
assert.equal(validateRegistryUrl('https://127.0.0.1/latest'), false);
|
|
31
|
+
assert.equal(validateRegistryUrl('https://[::1]/latest'), false);
|
|
32
|
+
assert.equal(validateRegistryUrl('https://10.0.0.5/latest'), false);
|
|
33
|
+
assert.equal(validateRegistryUrl('https://192.168.1.10/latest'), false);
|
|
34
|
+
assert.equal(validateRegistryUrl('https://evil-registry.npmjs.org.example.com/latest'), false);
|
|
35
|
+
assert.equal(validateRegistryUrl('https://registry.npmjs.org.evil.com/latest'), false);
|
|
36
|
+
assert.equal(validateRegistryUrl('https://npmjs.org/latest'), false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('非法 URL 拒绝而不抛出', () => {
|
|
40
|
+
assert.equal(validateRegistryUrl('not a url'), false);
|
|
41
|
+
assert.equal(validateRegistryUrl(''), false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// ---- compareVersions / classify:纯函数比对 ----
|
|
46
|
+
describe('compareVersions', () => {
|
|
47
|
+
test('三元组数值比较', () => {
|
|
48
|
+
assert.equal(compareVersions('0.8.0', '0.8.1'), -1);
|
|
49
|
+
assert.equal(compareVersions('0.8.1', '0.8.0'), 1);
|
|
50
|
+
assert.equal(compareVersions('0.8.0', '0.8.0'), 0);
|
|
51
|
+
assert.equal(compareVersions('0.10.0', '0.9.0'), 1);
|
|
52
|
+
assert.equal(compareVersions('1.0.0', '0.9.9'), 1);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('同号带 prerelease 的一方更旧;两侧都带视为同版', () => {
|
|
56
|
+
assert.equal(compareVersions('0.9.0-rc.1', '0.9.0'), -1);
|
|
57
|
+
assert.equal(compareVersions('0.9.0', '0.9.0-rc.1'), 1);
|
|
58
|
+
assert.equal(compareVersions('0.9.0-rc.1', '0.9.0-rc.1'), 0);
|
|
59
|
+
// 数字三元组相等、两侧都带后缀 → 同版(横幅只关心是否落后 latest,
|
|
60
|
+
// 不追求完整 semver prerelease 序)
|
|
61
|
+
assert.equal(compareVersions('0.9.0-rc.2', '0.9.0-rc.1'), 0);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('解析失败返回 null', () => {
|
|
65
|
+
assert.equal(compareVersions('unknown', '0.8.0'), null);
|
|
66
|
+
assert.equal(compareVersions('0.8.0', ''), null);
|
|
67
|
+
assert.equal(compareVersions(null, null), null);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('classify', () => {
|
|
72
|
+
test('四种形态', () => {
|
|
73
|
+
assert.equal(classify('0.8.0', '0.8.1'), 'outdated');
|
|
74
|
+
assert.equal(classify('0.8.0', '0.8.0'), 'up-to-date');
|
|
75
|
+
// workspace/link 直载开发态:运行版本高于已发布 → ahead(前端不打扰)
|
|
76
|
+
assert.equal(classify('0.9.0', '0.8.1'), 'ahead');
|
|
77
|
+
// 查询失败/离线 → unknown(前端完全静默)
|
|
78
|
+
assert.equal(classify('0.8.0', null), 'unknown');
|
|
79
|
+
assert.equal(classify('unknown', '0.8.0'), 'unknown');
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// ---- fetchLatestVersion:TTL 缓存 + 失败静默 ----
|
|
84
|
+
describe('fetchLatestVersion', () => {
|
|
85
|
+
test('正常响应取 latest;TTL 内复用缓存不重复请求', async () => {
|
|
86
|
+
resetCacheForTest();
|
|
87
|
+
let calls = 0;
|
|
88
|
+
let clock = 1000;
|
|
89
|
+
const fetchImpl = async () => { calls += 1; return { ok: true, json: async () => ({ version: '0.8.1' }) }; };
|
|
90
|
+
const now = () => clock;
|
|
91
|
+
|
|
92
|
+
assert.equal(await fetchLatestVersion({ fetchImpl, now }), '0.8.1');
|
|
93
|
+
assert.equal(await fetchLatestVersion({ fetchImpl, now }), '0.8.1');
|
|
94
|
+
assert.equal(calls, 1);
|
|
95
|
+
|
|
96
|
+
clock += 60 * 60 * 1000 - 1;
|
|
97
|
+
assert.equal(await fetchLatestVersion({ fetchImpl, now }), '0.8.1');
|
|
98
|
+
assert.equal(calls, 1);
|
|
99
|
+
|
|
100
|
+
// 过 TTL 后重新请求
|
|
101
|
+
clock += 1;
|
|
102
|
+
assert.equal(await fetchLatestVersion({ fetchImpl, now }), '0.8.1');
|
|
103
|
+
assert.equal(calls, 2);
|
|
104
|
+
resetCacheForTest();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('失败静默返回 null:网络异常 / 非 2xx / 形状不对', async () => {
|
|
108
|
+
resetCacheForTest();
|
|
109
|
+
assert.equal(await fetchLatestVersion({ fetchImpl: async () => { throw new Error('offline'); }, now: () => 1000 }), null);
|
|
110
|
+
assert.equal(await fetchLatestVersion({ fetchImpl: async () => ({ ok: false, status: 503 }), now: () => 1000 }), null);
|
|
111
|
+
assert.equal(await fetchLatestVersion({ fetchImpl: async () => ({ ok: true, json: async () => ({ nope: true }) }), now: () => 1000 }), null);
|
|
112
|
+
resetCacheForTest();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('失败不写缓存:下次调用立刻重试', async () => {
|
|
116
|
+
resetCacheForTest();
|
|
117
|
+
let calls = 0;
|
|
118
|
+
let fail = true;
|
|
119
|
+
const fetchImpl = async () => {
|
|
120
|
+
calls += 1;
|
|
121
|
+
if (fail) throw new Error('boom');
|
|
122
|
+
return { ok: true, json: async () => ({ version: '0.8.2' }) };
|
|
123
|
+
};
|
|
124
|
+
assert.equal(await fetchLatestVersion({ fetchImpl, now: () => 1000 }), null);
|
|
125
|
+
assert.equal(await fetchLatestVersion({ fetchImpl, now: () => 1000 }), null);
|
|
126
|
+
assert.equal(calls, 2);
|
|
127
|
+
fail = false;
|
|
128
|
+
assert.equal(await fetchLatestVersion({ fetchImpl, now: () => 1000 }), '0.8.2');
|
|
129
|
+
resetCacheForTest();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('运行环境无全局 fetch 时走默认参兜底:resolve null 而非同步抛', async () => {
|
|
133
|
+
resetCacheForTest();
|
|
134
|
+
const original = globalThis.fetch;
|
|
135
|
+
globalThis.fetch = undefined;
|
|
136
|
+
try {
|
|
137
|
+
// 修复前:默认参 fetchImpl = fetch 在参数求值期同步抛 ReferenceError,
|
|
138
|
+
// 路由的 .catch 接不住;修复后应归入常规失败路径静默返 null。
|
|
139
|
+
assert.equal(await fetchLatestVersion({ now: () => 1000 }), null);
|
|
140
|
+
} finally {
|
|
141
|
+
globalThis.fetch = original;
|
|
142
|
+
}
|
|
143
|
+
resetCacheForTest();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// ---- 模块常量:URL 必须过白名单,运行版本可读 ----
|
|
148
|
+
test('REGISTRY_URL 自身通过白名单校验;PACKAGE_VERSION 可读', () => {
|
|
149
|
+
assert.equal(validateRegistryUrl(REGISTRY_URL), true);
|
|
150
|
+
assert.match(PACKAGE_VERSION, /^\d+\.\d+\.\d+/);
|
|
151
|
+
});
|
|
152
|
+
});
|