@mzzsfy/dsh-context-manager 0.1.0

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.
@@ -0,0 +1,94 @@
1
+ // 镜像逻辑 parity:client.js 与 core.mjs 双实现同源(仓库规约"双实现同源必须 parity 测试")。
2
+ // client.js 为单文件自包含格式无法 import,此处按标记切片提取镜像函数段,
3
+ // 经 new Function 实例化后与 core.mjs 同输入断言同输出。
4
+
5
+ import { readFileSync } from 'node:fs'
6
+ import { dirname, join } from 'node:path'
7
+ import { fileURLToPath } from 'node:url'
8
+ import test from 'node:test'
9
+ import assert from 'node:assert/strict'
10
+
11
+ import {
12
+ filterHistoryInputs as coreFilterHistoryInputs,
13
+ forkFailureText as coreForkFailureText,
14
+ forkRetryText as coreForkRetryText,
15
+ HISTORY_SCOPES,
16
+ } from '../src/core.mjs'
17
+
18
+ const PKG_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..')
19
+ const CLIENT_SRC = readFileSync(join(PKG_ROOT, 'src', 'client.js'), 'utf8')
20
+ const INDEX_SRC = readFileSync(join(PKG_ROOT, 'src', 'index.js'), 'utf8')
21
+
22
+ // 切片:filterHistoryInputs 与 forkFailureText 均为无外部依赖纯函数,
23
+ // 以「历史范围常量声明」为起点、「历史浮层组件声明」为终点
24
+ const MIRROR_START = CLIENT_SRC.indexOf('const HISTORY_SCOPES = ')
25
+ const MIRROR_END = CLIENT_SRC.indexOf('function HistoryDock(')
26
+ assert.ok(MIRROR_START >= 0 && MIRROR_END > MIRROR_START, 'client.js 镜像函数切片定位失败')
27
+
28
+ const mirror = new Function(
29
+ CLIENT_SRC.slice(MIRROR_START, MIRROR_END)
30
+ + '; return { filterHistoryInputs: filterHistoryInputs, forkFailureText: forkFailureText, forkRetryText: forkRetryText, HISTORY_SCOPES: HISTORY_SCOPES }',
31
+ )()
32
+
33
+ test('parity 历史搜索:子串/空白/空查询/非数组同输入同输出', () => {
34
+ const rows = [
35
+ { text: '修复归档面板', at: 1 },
36
+ { text: 'Fix Fork Anchor', at: 2 },
37
+ { text: '无关条目', at: 3 },
38
+ ]
39
+ for (const query of ['归档', 'fork', 'FIX', ' ', '', '不存在']) {
40
+ assert.deepEqual(mirror.filterHistoryInputs(rows, query), coreFilterHistoryInputs(rows, query))
41
+ }
42
+ assert.deepEqual(mirror.filterHistoryInputs(undefined, 'a'), coreFilterHistoryInputs(undefined, 'a'))
43
+ })
44
+
45
+ test('parity fork 错误文案:未知码/挂载失败/未完成轮同输入同输出', () => {
46
+ for (const code of ['session/fork-unavailable', 'session/workspace-attach-failed', 'gateway/internal', undefined, '']) {
47
+ assert.equal(mirror.forkFailureText(code), coreForkFailureText(code))
48
+ }
49
+ })
50
+
51
+ test('parity fork 重试文本:用户/插件注入/空白/纯图/畸形同输入同输出', () => {
52
+ const samples = [
53
+ undefined,
54
+ {},
55
+ { source: { kind: 'user' }, content: [{ type: 'text', text: '第一行' }, { type: 'text', text: '第二行' }] },
56
+ { source: { kind: 'user' }, content: [{ type: 'image', attachment: {} }] },
57
+ { source: { kind: 'user' }, content: [{ type: 'text', text: ' ' }] },
58
+ { source: { kind: 'plugin', plugin: 'x' }, content: [{ type: 'text', text: '注入' }] },
59
+ { source: { kind: 'user' } },
60
+ ]
61
+ for (const data of samples) {
62
+ assert.equal(mirror.forkRetryText(data), coreForkRetryText(data), JSON.stringify(data)?.slice(0, 40))
63
+ }
64
+ })
65
+
66
+ test('parity 历史范围:client 与 core 的 HISTORY_SCOPES 同序同值', () => {
67
+ assert.deepEqual(mirror.HISTORY_SCOPES, HISTORY_SCOPES)
68
+ })
69
+
70
+ test('源码契约:client fetchInputs 必须解包 host 响应信封 { inputs, aligned }', () => {
71
+ // host 与 client 对信封各自测试自洽时,信封形状漂移只会以运行时 TypeError 暴露;
72
+ // 此守卫锁定解包点存在,防消费侧把信封对象当数组使用
73
+ const guard = /Array\.isArray\((\w+)\.inputs\)/
74
+ assert.ok(guard.test('Array.isArray(payload.inputs)'), '守卫正则必须命中合规样本')
75
+ const fetchStart = CLIENT_SRC.indexOf('function fetchInputs(')
76
+ assert.ok(fetchStart >= 0, '未找到 fetchInputs 函数')
77
+ // 切片终点锚必须有效且切片远小于全文件:锚失效退化为全文件检查时契约空转
78
+ const fetchEnd = CLIENT_SRC.indexOf('\n }', fetchStart)
79
+ assert.ok(fetchEnd > fetchStart, 'fetchInputs 切片终点锚失效(实现缩进形态漂移)')
80
+ const fetchBody = CLIENT_SRC.slice(fetchStart, fetchEnd)
81
+ assert.ok(fetchBody.length < 2 * 1000, 'fetchInputs 切片异常膨胀,契约失去定位精度')
82
+ assert.ok(fetchBody.includes('api(INPUTS_URL'), '未找到 fetchInputs 的 host 请求')
83
+ assert.ok(guard.test(fetchBody), 'fetchInputs 缺少 payload.inputs 数组解包')
84
+ assert.ok(fetchBody.includes('payload.aligned'), 'fetchInputs 缺少 aligned 字段解包')
85
+ })
86
+
87
+ test('parity API 路径:client 字面量与 host 路由注册互为镜像', () => {
88
+ // 跨半区契约拼错只会在运行时 404,双向集合相等断言防漂移
89
+ const clientUrls = new Set([...CLIENT_SRC.matchAll("'(/api/context/[^']*)'")].map((m) => m[1])
90
+ .filter((url) => !url.endsWith('/*')))
91
+ assert.ok(clientUrls.size >= 5, 'client.js API 路径字面量异常减少')
92
+ const hostPaths = new Set([...INDEX_SRC.matchAll(/path: '(\/api\/context\/[^']*)'/g)].map((m) => m[1]))
93
+ assert.deepEqual([...hostPaths].sort(), [...clientUrls].sort(), 'client 与 host 的 API 路径集合不一致')
94
+ })