Files
dsh-worktable/04_test/preview-files.test.mjs
T
wangheng b567701fa1 feat: 二级页面、孤儿路由防护、iframe 全屏与 DS Bot 侧栏隐藏 (v0.5.3-local.5)
借鉴同线分支 dsh-worktable 0.5.2-local.9 的四项改造,逐项评估后移植:

- 二级页面:LayoutSpec.parentId + layoutTree 树形侧栏 + 折叠/展开。
  仅一级嵌套;删父项目不级联删子页面(提升为顶级,避免误删用户内容)。
  管理面板加「+新增二级页面」,继承父项目文件夹。
- 孤儿路由防护:register 遇 duplicate 路由只 warn 不抛,避免热重载时
  旧 fiber 未释放导致新 fiber 激活失败、侧边栏抽屉整体消失。
- iframe allowFullScreen:项目窗格里的 HTML 看板可全屏。
- DS Bot 侧栏隐藏:由本插件注入 CSS,替代改 ds-bot/client.js 的做法
  (ds-bot 随应用升级,改源码必丢)。nav.bt-home-nav 带标签选择器避免误伤。

未移植对方的状态防清空(stateStore.serverKeys):本仓 hostState.ts 已有
hydrated 守卫(读完宿主前禁写、读不到整场不写)与更细的 6 段退避重试,
覆盖面更强。

顺带修三个 Windows 兼容缺陷,否则 pack 门禁过不去:
- release-prep.mjs:tar 加 --force-local(GNU tar 把 C: 当 rsh 主机)
- preview-files.test.mjs:rmSync 删 junction 失败,加 unlinkSync 兜底
- widget-mount.test.mjs:提取段缺 normalizeProjects 依赖,起点前移

验证:npm run pack 全绿(139 项兼容回归 + 工厂门禁 10/10 + 结构 7 文件 +
独立安装 import + 锚点 14 场景 + Chromium 分栏 5 场景 + 服务端 3 场景)。
双资产同源 sha256 cf633ad7…。
2026-12-02 16:37:29 +08:00

108 lines
5.7 KiB
JavaScript

import assert from 'node:assert/strict'
import { test, after, afterEach } from 'node:test'
import { createRequire } from 'node:module'
import { mkdtempSync, writeFileSync, mkdirSync, rmSync, symlinkSync, renameSync, truncateSync, unlinkSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { pathToFileURL, fileURLToPath } from 'node:url'
const require=createRequire(new URL('../01_content/package.json',import.meta.url))
const {build}=require('esbuild')
const temp=mkdtempSync(join(tmpdir(),'wt-preview-test-'))
after(()=>rmSync(temp,{recursive:true,force:true}))
// Instrument filesystem scheduling only in the test bundle; production has no hooks.
afterEach(()=>{ delete globalThis.__wtPreviewHooks })
await build({
entryPoints:[fileURLToPath(new URL('../01_content/src/previewFiles.ts',import.meta.url))],
bundle:true,platform:'node',format:'esm',outfile:join(temp,'preview.mjs'),
plugins:[{name:'filesystem-race-fixture',setup(build){
build.onResolve({filter:/^node:fs\/promises$/},args=>args.namespace==='race-fixture'?undefined:{path:args.path,namespace:'race-fixture'})
build.onLoad({filter:/.*/,namespace:'race-fixture'},()=>({loader:'js',contents:`
import * as fs from 'node:fs/promises';
export const realpath=fs.realpath, stat=fs.stat;
export async function open(...args) {
await globalThis.__wtPreviewHooks?.beforeOpen?.();
const h=await fs.open(...args);
await globalThis.__wtPreviewHooks?.afterOpen?.();
const read=h.read.bind(h);
h.read=async(...args)=>{await globalThis.__wtPreviewHooks?.beforeRead?.();return read(...args)};
return h;
}`}))
}}],
})
const {createPreviewFiles,PREVIEW_MAX_BYTES}=await import(pathToFileURL(join(temp,'preview.mjs')).href)
const root=join(temp,'root'),outside=join(temp,'outside');mkdirSync(root);mkdirSync(outside)
writeFileSync(join(root,'index.html'),'fixture');writeFileSync(join(outside,'outside.txt'),'outside')
test('Preview reads only its canonical root, including directory indexes',async()=>{
const store=createPreviewFiles(),token=await store.mount(root)
assert.equal((await store.file(token,[])).path,join(root,'index.html'))
assert.equal(await store.file('invented-token',['index.html']),null)
for(const parts of [['..','outside','outside.txt'],['..\\outside\\outside.txt'],['/outside'],['bad\0']]) assert.equal(await store.file(token,parts),null)
symlinkSync(outside,join(root,'escape'),process.platform==='win32'?'junction':'dir')
assert.equal(await store.file(token,['escape','outside.txt']),null)
try { rmSync(join(root,'escape')) } catch { try { unlinkSync(join(root,'escape')) } catch {} }
})
test('Capabilities expire after idle time; revocation and disposal remove them',async()=>{
let now=0;const store=createPreviewFiles(()=>now),a=await store.mount(root)
now=29*60*1000;assert.ok(await store.file(a,['index.html']))
now+=30*60*1000;assert.equal(await store.file(a,['index.html']),null)
const b=await store.mount(root);store.revoke(b);assert.equal(await store.file(b,[]),null)
const c=await store.mount(root);store.clear();assert.equal(await store.file(c,[]),null)
})
test('Reject relative roots and bound the live capability table',async()=>{
const store=createPreviewFiles();await assert.rejects(store.mount('.'),/absolute/)
for(let i=0;i<128;i++)await store.mount(root)
await assert.rejects(store.mount(root),/Too many/)
})
test('Read content through the checked handle; HEAD omits the body and enforces the limit',async()=>{
const store=createPreviewFiles(),token=await store.mount(root)
assert.equal((await store.file(token,['index.html'])).data.toString(),'fixture')
const head=await store.file(token,['index.html'],true)
assert.equal(head.size,7);assert.equal(head.data,undefined)
const large=join(root,'large.bin');writeFileSync(large,'x');truncateSync(large,PREVIEW_MAX_BYTES+1)
await assert.rejects(store.file(token,['large.bin'],true),e=>e.status===413)
})
test('A directory replaced by a junction after path checking cannot return outside content',async()=>{
const slot=join(root,'slot'),moved=join(root,'slot-original')
mkdirSync(slot);writeFileSync(join(slot,'outside.txt'),'inside')
const store=createPreviewFiles(),token=await store.mount(root)
globalThis.__wtPreviewHooks={beforeOpen(){
delete globalThis.__wtPreviewHooks
renameSync(slot,moved);symlinkSync(outside,slot,process.platform==='win32'?'junction':'dir')
}}
try {assert.equal(await store.file(token,['slot','outside.txt']),null)}
finally {unlinkSync(slot);renameSync(moved,slot)}
})
test('Replacing the named file after open rejects the response, even with an open old handle',async()=>{
const name=join(root,'replace.txt'),old=join(root,'replace-original.txt')
writeFileSync(name,'original')
const store=createPreviewFiles(),token=await store.mount(root)
globalThis.__wtPreviewHooks={afterOpen(){
delete globalThis.__wtPreviewHooks
renameSync(name,old);writeFileSync(name,'replacement')
}}
assert.equal(await store.file(token,['replace.txt']),null)
})
test('Growth after stat cannot produce a response over 40 MiB',async()=>{
const name=join(root,'growing.bin');writeFileSync(name,'small')
const store=createPreviewFiles(),token=await store.mount(root)
globalThis.__wtPreviewHooks={beforeRead(){
delete globalThis.__wtPreviewHooks
truncateSync(name,PREVIEW_MAX_BYTES+1)
}}
await assert.rejects(store.file(token,['growing.bin']),e=>e.status===413)
})
test('Revocation during a pending read prevents the response',async()=>{
const store=createPreviewFiles(),token=await store.mount(root)
globalThis.__wtPreviewHooks={beforeRead(){
delete globalThis.__wtPreviewHooks
store.revoke(token)
}}
assert.equal(await store.file(token,['index.html']),null)
})