dsh-knit 0.5.0 → 0.5.1
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/package.json +1 -1
- package/test/fixture.mjs +48 -0
- package/test/host-http.test.mjs +5 -4
- package/test/host.test.mjs +5 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-knit",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Knit — recent Markdown, images and video in the DSH sidebar, sorted by relevance to the current conversation, with inline previews and HTTP Range streaming. Zero model calls.",
|
|
6
6
|
"main": "./src/host/index.js",
|
package/test/fixture.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试用样本工作区:把「至少 3 篇 Markdown + 一张图 + 一个非媒体文件」
|
|
3
|
+
* 造成一个临时目录,测试因此不再依赖仓库/包的目录布局。
|
|
4
|
+
*
|
|
5
|
+
* 为什么需要它:测试原先用 `new URL('../../')` 当样本工作区 ——
|
|
6
|
+
* 作者本机那是 08_Knit/(恰好有样本),但别人 clone 公开仓库时那是 clone 的父目录,
|
|
7
|
+
* 装进 npm 包时那是 node_modules/,两处都没有样本,测试就会误报红。
|
|
8
|
+
*/
|
|
9
|
+
import { mkdtempSync, mkdirSync, writeFileSync, utimesSync, rmSync } from 'node:fs'
|
|
10
|
+
import { tmpdir } from 'node:os'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
|
|
13
|
+
/** 1×1 透明 PNG 的真实字节,尾部补 0 到 2048 —— 够测 Range 后缀,且仍是可解码的 PNG。 */
|
|
14
|
+
const PNG_1X1 = Buffer.from(
|
|
15
|
+
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==',
|
|
16
|
+
'base64',
|
|
17
|
+
)
|
|
18
|
+
const PADDED_PNG = Buffer.concat([PNG_1X1, Buffer.alloc(2048 - PNG_1X1.length)])
|
|
19
|
+
|
|
20
|
+
/** 假视频:宿主不解析容器,只按扩展名放行并支持 Range。 */
|
|
21
|
+
const FAKE_MP4 = Buffer.alloc(4096, 0x21)
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 建样本工作区。文件名刻意与旧断言一致(README.md / package.json / docs/screenshot.png),
|
|
25
|
+
* 断言语义不用改;mtime 用 utimes 定死,排序断言不再看运气。
|
|
26
|
+
* @returns {string} 工作区绝对路径;进程退出时自动清理
|
|
27
|
+
*/
|
|
28
|
+
export function makeWorkspace() {
|
|
29
|
+
const dir = mkdtempSync(join(tmpdir(), 'knit-fixture-'))
|
|
30
|
+
const base = Date.now() - 120_000
|
|
31
|
+
const files = [
|
|
32
|
+
['README.md', '# 样本 README\n\nKnit 样本工作区主文档,含关键词:相关性排序、sidebar。\n'],
|
|
33
|
+
['docs/notes.md', '# 样本笔记\n\n第二篇,用来凑够三篇。\n'],
|
|
34
|
+
['CHANGELOG.md', '# 样本变更\n\n第三篇。\n'],
|
|
35
|
+
['package.json', '{\n "name": "sample"\n}\n'],
|
|
36
|
+
['docs/screenshot.png', PADDED_PNG],
|
|
37
|
+
['docs/clip.mp4', FAKE_MP4],
|
|
38
|
+
]
|
|
39
|
+
files.forEach(([rel, content], i) => {
|
|
40
|
+
const abs = join(dir, rel)
|
|
41
|
+
mkdirSync(join(abs, '..'), { recursive: true })
|
|
42
|
+
writeFileSync(abs, content)
|
|
43
|
+
const t = (base + i * 1000) / 1000
|
|
44
|
+
utimesSync(abs, t, t)
|
|
45
|
+
})
|
|
46
|
+
process.on('exit', () => rmSync(dir, { recursive: true, force: true }))
|
|
47
|
+
return dir
|
|
48
|
+
}
|
package/test/host-http.test.mjs
CHANGED
|
@@ -13,9 +13,10 @@ import { fileURLToPath } from 'node:url'
|
|
|
13
13
|
import { stat } from 'node:fs/promises'
|
|
14
14
|
|
|
15
15
|
import { apply } from '../src/host/index.js'
|
|
16
|
+
import { makeWorkspace } from './fixture.mjs'
|
|
16
17
|
|
|
17
|
-
/**
|
|
18
|
-
const PROJECT_ROOT =
|
|
18
|
+
/** 测试自带样本工作区(临时目录),里面有 docs/screenshot.png 当媒体样本。 */
|
|
19
|
+
const PROJECT_ROOT = makeWorkspace()
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* 起一个挂着 Knit 路由的回环 server(伪造 cordis 的 webServer / sessions / effect)。
|
|
@@ -69,7 +70,7 @@ test('HTTP 端到端:媒体列表、/raw 整文件、Range 206、安全拒绝'
|
|
|
69
70
|
assert.ok(body.docs.every((d) => d.kind === 'md'))
|
|
70
71
|
|
|
71
72
|
// /raw 整文件:200、字节一致、安全头齐全
|
|
72
|
-
const rel = '
|
|
73
|
+
const rel = 'docs/screenshot.png'
|
|
73
74
|
const realSize = (await stat(`${PROJECT_ROOT}/${rel}`)).size
|
|
74
75
|
r = await fetch(`${base}/api/raw?sessionId=s&rel=${encodeURIComponent(rel)}`)
|
|
75
76
|
assert.equal(r.status, 200)
|
|
@@ -104,7 +105,7 @@ test('HTTP 端到端:媒体列表、/raw 整文件、Range 206、安全拒绝'
|
|
|
104
105
|
assert.equal(body.code, 'knit/outside-workspace')
|
|
105
106
|
|
|
106
107
|
// 非白名单扩展名:404 + media-only
|
|
107
|
-
r = await fetch(`${base}/api/raw?sessionId=s&rel=${encodeURIComponent('
|
|
108
|
+
r = await fetch(`${base}/api/raw?sessionId=s&rel=${encodeURIComponent('package.json')}`)
|
|
108
109
|
body = await r.json()
|
|
109
110
|
assert.equal(r.status, 404)
|
|
110
111
|
assert.equal(body.code, 'knit/media-only')
|
package/test/host.test.mjs
CHANGED
|
@@ -20,9 +20,10 @@ import {
|
|
|
20
20
|
ERROR_CODES,
|
|
21
21
|
} from '../src/host/index.js'
|
|
22
22
|
import { extractKeywords, rankByRelevance, topicLabel } from '../src/host/relevance.js'
|
|
23
|
+
import { makeWorkspace } from './fixture.mjs'
|
|
23
24
|
|
|
24
|
-
/**
|
|
25
|
-
const PROJECT_ROOT =
|
|
25
|
+
/** 测试自带样本工作区(临时目录),不依赖仓库/包的目录布局。 */
|
|
26
|
+
const PROJECT_ROOT = makeWorkspace()
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* 造一个假的宿主会话对象。
|
|
@@ -135,7 +136,7 @@ test('readDocument: 拒绝绝对路径逃逸', async () => {
|
|
|
135
136
|
})
|
|
136
137
|
|
|
137
138
|
test('readDocument: 拒绝非 Markdown', async () => {
|
|
138
|
-
const r = await readDocument(PROJECT_ROOT, '
|
|
139
|
+
const r = await readDocument(PROJECT_ROOT, 'package.json')
|
|
139
140
|
assert.equal(r.ok, false)
|
|
140
141
|
assert.equal(r.code, ERROR_CODES.markdownOnly)
|
|
141
142
|
})
|
|
@@ -275,7 +276,7 @@ test('collectDocs: 同时扫出 Markdown 与媒体,媒体带 kind/size 且不
|
|
|
275
276
|
assert.ok(docs.every((d) => typeof d.size === 'number'))
|
|
276
277
|
|
|
277
278
|
const shot = media.find((m) => m.rel.split('/').pop() === 'screenshot.png')
|
|
278
|
-
assert.ok(shot, '
|
|
279
|
+
assert.ok(shot, '应扫到样本里的 screenshot.png')
|
|
279
280
|
assert.equal(shot.kind, 'image')
|
|
280
281
|
assert.ok(shot.size > 0)
|
|
281
282
|
assert.equal(shot.summary, '', '媒体没有正文摘要')
|