Files
apes-sidebar/tests/chunk-loader.spec.ts
T
Menghuan1918andidoall 78274c7b78 fix(sidebar): /sidebar/* 传输按页面前缀解析,反代子路径下不再甩掉前缀(#584 移植)
贡献者 PR #584(fix: preserve reverse-proxy base paths,head 1ca99d8)的改动面在
dev 上仍然需要——JSON API / upload / file / html / bundle 与两条 WebSocket 都还是
源站绝对路径(带前导斜杠 → 反代子路径下 404 / 1006),目标 issue #753 仍 OPEN,
docs/plans/2026-10-04-issue-triage.md 也把 #584 记为优先合入项。但它不能机械解冲突,
故按 dev 现有结构重写。

复用(PR 的可复用部分):
- 新增 src/client/host-route-url.ts:hostRouteUrl() / hostWebSocketUrl() 先去前导
  斜杠,再按注入基底解析;基底去 search/hash 并强制目录尾斜杠——launch token 查询串
  与「缺尾斜杠的前缀」都不会吃掉路径最后一段。

改写(dev 侧已经变掉的东西):
- 基底来源并进 dev 的 2718725:desktop-env.ts 的 sidebarWebSocketBase() 泛化为
  hostTransportBase()(__DSH_TRANSPORT__.streamBaseUrl 优先,回落 document.baseURI),
  HTTP 与 WS 从此共用一个基底权威,而不是另起一套。丢弃 PR 的 hostDocumentBase()
  「取 document.baseURI 与 location.href 中更长者」启发式:它会顶掉刚落地的桌面壳修复
  (dsh-app://app/ 下 location.href 会重新拼出不可达的 ws://app/…),而基线
  0.2.0-rc.1 的宿主产物里没有 <base href>(PR 自己的 e2e 也只采集、从不断言它)。
  最终解析式与宿主 dsh-api-gateway 逐字同形(去前导斜杠 + 壳注入基底优先)。
- 丢弃 src/client/TerminalView.tsx 的改动:该文件在 dev 已删除(终端已交还宿主),
  其覆盖的 WS 语义改由 dev 现存的两条调用点承担,并补齐 PR 文件清单里漏掉的
  src/client/use-dir-watch.ts(fs-watch socket)。

调用点(一律相对路径 + 前缀解析):api.ts 的 JSON API / upload / file / html /
archive、chunk-loader.ts 的 CHUNK_URL、markdown-images.ts 与 MarkdownHtml /
TextEditor / DiffPane 的媒体改写(原 window.location.origin 在桌面壳下是
dsh-app://app,媒体同样不可达)、use-host-feeds.ts 与 use-dir-watch.ts 的两条
WebSocket。archiveDownloadUrl 是 PR 漏掉的同类调用点(FileTree 用它 fetch zip 字节),
一并纳入;open-in-app.ts 打的是宿主路由 /open-in-app/*,与 PR 明确不碰的宿主 /api
同类,不纳入。

消费插件契约(指南「接入 API 即文档」):
- BetterSidebarService 新增 hostRouteUrl(path)(features 'hostRouteUrl'),
  docs/external-plugin-guide.md §6 / §7 / §15 的 fetch('/sidebar/api/…') 示例改为
  经它解析,并写明「绝不写前导斜杠」。

验证:pnpm typecheck / pnpm exec vitest run(161 files / 1812 passed / 13 skipped)/
eslint 全绿。tests/base-path.spec.ts 钉前缀保留与基底优先级,并在真实调用点上断言
(api fetch 的 URL、chunk 注入的 script src、jsdom 下 fs-watch 的 socket URL);
PR 自带的 base-path e2e 在 page.evaluate 里复制实现后自证,回归守不住,故不引入该 lane。

Refs #584

Co-authored-by: idoall <6040958+idoall@users.noreply.github.com>
2026-10-05 02:22:03 +08:00

438 lines
18 KiB
TypeScript

/**
* Chunk loader tests: the lazy chunk machinery (src/client/chunk-loader.ts).
* Pins the caching contract that makes the lazy mechanism correct:
* - one in-flight promise per chunk (concurrent opens share it),
* - a failed load clears the cache so the next call retries (the script
* re-executes and overwrites its global registry slot — assignments are
* idempotent, no duplicate-registration class of errors),
* - externals resolve through the module system's seed branch (the stable,
* version-independent part), once per page,
* - resetChunks drops the cache and the externals memo (HMR).
* The production path runs against a fake module system injected via
* {@link setChunkModuleSystem} (mirroring the client half's `ctx.modules`
* injection) and a stub script loader that simulates the executed chunk
* script by assigning the plugin-owned global factory registry.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import './browser-globals.ts'
import {
CHUNK_EXTERNALS,
loadChunk,
registerChunkForTests,
revalidateChunksOnReactivate,
resetChunks,
setChunkModuleSystem,
setChunkScriptLoaderForTests,
} from '../src/client/chunk-loader.ts'
import type { ChunkExports } from '../src/client/chunk-loader.ts'
interface FakeModuleSystem {
import: (specifier: string) => Promise<unknown>
}
function installModuleSystem(): FakeModuleSystem {
const fake: FakeModuleSystem = {
import: vi.fn(async (specifier: string) => ({ seed: specifier })),
}
setChunkModuleSystem(fake)
return fake
}
/** Simulate a chunk script executing: it assigns its factory to the registry. */
function simulateScript(name: string, factory: (require: (spec: string) => unknown) => ChunkExports): void {
const g = globalThis as { __dshChunks__?: Record<string, unknown> }
g.__dshChunks__ = g.__dshChunks__ ?? {}
g.__dshChunks__[name] = factory
}
beforeEach(() => {
setChunkModuleSystem(undefined)
delete (globalThis as Record<string, unknown>).__dshChunks__
resetChunks()
setChunkScriptLoaderForTests(null)
})
describe('test-registry path (vitest / jsdom-less environments)', () => {
it('returns the registered chunk exports, loading once even for concurrent callers', async () => {
let calls = 0
registerChunkForTests('editor', async () => {
calls += 1
return { TextEditor: 'text-editor' }
})
const [a, b, c] = await Promise.all([loadChunk('editor'), loadChunk('editor'), loadChunk('editor')])
expect(a).toEqual({ TextEditor: 'text-editor' })
expect(b).toBe(a)
expect(c).toBe(a)
expect(calls).toBe(1)
// Memoized after resolution too.
expect(await loadChunk('editor')).toBe(a)
expect(calls).toBe(1)
})
it('a failed load clears the cache so the next call retries', async () => {
let calls = 0
registerChunkForTests('locale', async () => {
calls += 1
if (calls === 1) throw new Error('boom')
return { localeDicts: 'locale-dicts' }
})
await expect(loadChunk('locale')).rejects.toThrow('boom')
await expect(loadChunk('locale')).resolves.toEqual({ localeDicts: 'locale-dicts' })
expect(calls).toBe(2)
})
})
describe('production path (script injection + global registry + externals require)', () => {
it('injects the chunk script under the page prefix (reverse proxy, issue #753)', async () => {
installModuleSystem()
// The loader's fire-and-forget ETag HEAD rides along; stub it so no real
// request to the fake prefix host outlives this case.
vi.stubGlobal('fetch', vi.fn(async () => ({ headers: { get: () => null } }) as unknown as Response))
const loaded: string[] = []
setChunkScriptLoaderForTests(async (src) => {
loaded.push(src)
simulateScript('editor', () => ({ TextEditor: 'editor-view' }))
})
// The GUI served at `/dataops/proxy/3080/`: a leading-slash bundle URL
// would ask the origin root, where the proxy serves nothing → 404 and a
// chunk that never materializes.
const document = globalThis.document as { baseURI?: string }
const previous = document.baseURI
Object.defineProperty(document, 'baseURI', {
value: 'https://host.test/dataops/proxy/3080/',
configurable: true,
writable: true,
})
try {
await loadChunk('editor')
} finally {
Object.defineProperty(document, 'baseURI', { value: previous, configurable: true, writable: true })
}
expect(loaded).toEqual(['https://host.test/dataops/proxy/3080/sidebar/bundle/editor.js'])
})
it('resolves externals through an injected ctx.modules system (rc.8 — no page global)', async () => {
const modules = installModuleSystem()
const loaded: string[] = []
setChunkScriptLoaderForTests(async (src) => {
loaded.push(src)
simulateScript('editor', (require) => ({ TextEditor: `view:${String(require('react'))}` }))
})
const exports = await loadChunk('editor')
expect(loaded).toEqual(['http://localhost/sidebar/bundle/editor.js'])
expect(exports).toEqual({ TextEditor: 'view:[object Object]' })
expect(modules.import).toHaveBeenCalledTimes(CHUNK_EXTERNALS.length)
// The injection also lands on a plugin-owned global so chunk-bundle
// copies of this loader (which inline their own module instance and
// never run apply()) can resolve externals too — rc.8 has no shell
// page global anymore.
expect((globalThis as Record<string, unknown>).__dshSidebarModuleSystem__).toBe(modules)
// The injection survives resetChunks (shell state, not chunk state).
resetChunks()
setChunkScriptLoaderForTests(async () => {
simulateScript('editor', () => ({ TextEditor: 'editor-view' }))
})
await loadChunk('editor')
expect(modules.import).toHaveBeenCalledTimes(CHUNK_EXTERNALS.length * 2)
})
it('injects the chunk script, then materializes the factory with externals from the module table', async () => {
const modules = installModuleSystem()
const loaded: string[] = []
setChunkScriptLoaderForTests(async (src) => {
loaded.push(src)
simulateScript('editor', (require) => ({ TextEditor: `view:${String(require('react'))}` }))
})
const exports = await loadChunk('editor')
expect(loaded).toEqual(['http://localhost/sidebar/bundle/editor.js'])
expect(exports).toEqual({ TextEditor: 'view:[object Object]' })
// Externals resolved through the module system's seed branch, once.
expect(modules.import).toHaveBeenCalledTimes(CHUNK_EXTERNALS.length)
// Memoized: no second script injection, no second externals resolution.
await loadChunk('editor')
expect(loaded).toHaveLength(1)
expect(modules.import).toHaveBeenCalledTimes(CHUNK_EXTERNALS.length)
})
it('resolves the externals table once across chunks', async () => {
const modules = installModuleSystem()
const seen: string[] = []
setChunkScriptLoaderForTests(async (src) => {
seen.push(src)
simulateScript(src.endsWith('editor.js') ? 'editor' : 'locale', () => ({}))
})
await loadChunk('locale')
await loadChunk('editor')
expect(seen).toEqual(['http://localhost/sidebar/bundle/locale.js', 'http://localhost/sidebar/bundle/editor.js'])
expect(modules.import).toHaveBeenCalledTimes(CHUNK_EXTERNALS.length)
})
it('a chunk requiring an unresolvable externals spec fails loudly', async () => {
installModuleSystem()
setChunkScriptLoaderForTests(async () => {
simulateScript('editor', (require) => { require('not-in-the-table'); return {} })
})
await expect(loadChunk('editor')).rejects.toThrow('missed the module table')
})
it('a script load failure rejects without materializing', async () => {
const modules = installModuleSystem()
setChunkScriptLoaderForTests(async () => { throw new Error('script 404') })
await expect(loadChunk('locale')).rejects.toThrow('script 404')
expect(modules.import).not.toHaveBeenCalled()
// Cache cleared: the retry re-attempts the script load.
setChunkScriptLoaderForTests(async () => { simulateScript('locale', () => ({ localeDicts: 'ok' })) })
await expect(loadChunk('locale')).resolves.toEqual({ localeDicts: 'ok' })
})
it('a script that ran but registered no factory fails with a clear error', async () => {
installModuleSystem()
setChunkScriptLoaderForTests(async () => { /* executes but assigns nothing */ })
await expect(loadChunk('editor')).rejects.toThrow('did not register its factory')
})
it('a materialization failure clears the cache; the retry re-executes cleanly (idempotent slot assignment)', async () => {
const modules = installModuleSystem()
let calls = 0
setChunkScriptLoaderForTests(async () => {
calls += 1
simulateScript('editor', () => {
if (calls === 1) throw new Error('materialize boom')
return { TextEditor: 'text-editor' }
})
})
await expect(loadChunk('editor')).rejects.toThrow('materialize boom')
// Retry: script re-injected, slot overwritten, materialization succeeds.
await expect(loadChunk('editor')).resolves.toEqual({ TextEditor: 'text-editor' })
expect(calls).toBe(2)
expect(modules.import).toHaveBeenCalled()
})
it('fails loudly when no module system is installed (before touching the network)', async () => {
const loaded: string[] = []
setChunkScriptLoaderForTests(async (src) => { loaded.push(src) })
await expect(loadChunk('editor')).rejects.toThrow('client module system unavailable')
expect(loaded).toEqual([])
})
it('resetChunks drops the cache and the externals memo (HMR re-activation)', async () => {
const modules = installModuleSystem()
let calls = 0
setChunkScriptLoaderForTests(async () => {
calls += 1
simulateScript('editor', () => ({ TextEditor: 'editor-view' }))
})
await loadChunk('editor')
expect(calls).toBe(1)
expect(modules.import).toHaveBeenCalledTimes(CHUNK_EXTERNALS.length)
resetChunks()
// Cache dropped: the next open re-fetches and re-executes; the externals
// memo is rebuilt (fresh module-table resolution).
await loadChunk('editor')
expect(calls).toBe(2)
expect(modules.import).toHaveBeenCalledTimes(CHUNK_EXTERNALS.length * 2)
})
it('resetChunks is a safe no-op without a module system', () => {
resetChunks()
expect(() => resetChunks()).not.toThrow()
})
})
describe('revalidateChunksOnReactivate (HMR re-activation keeps unchanged chunks)', () => {
/** Stub the bundle route's HEAD endpoint with per-name ETags (Error = unreachable). */
function stubBundleHead(etagFor: (name: string) => string | null | Error): void {
vi.stubGlobal('fetch', vi.fn(async (input: unknown) => {
const url = String(input)
const name = url.endsWith('locale.js') ? 'locale' : 'editor'
const etag = etagFor(name)
if (etag instanceof Error) throw etag
return {
headers: { get: (key: string) => (key.toLowerCase() === 'etag' ? etag : null) },
} as unknown as Response
}))
}
/** Let the fire-and-forget ETag recorder (recordEtag) settle. */
const settleEtag = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 5))
it('keeps the resolved exports of an unchanged chunk — no re-inject / re-execute', async () => {
installModuleSystem()
let scriptCalls = 0
setChunkScriptLoaderForTests(async () => {
scriptCalls += 1
simulateScript('editor', () => ({ TextEditor: 'editor-view' }))
})
stubBundleHead(() => '"v1"')
const first = await loadChunk('editor')
expect(scriptCalls).toBe(1)
await settleEtag()
await revalidateChunksOnReactivate()
const second = await loadChunk('editor')
expect(second).toBe(first)
expect(scriptCalls).toBe(1)
})
it('drops a chunk whose ETag changed on disk — the next open re-injects and re-executes', async () => {
installModuleSystem()
let scriptCalls = 0
setChunkScriptLoaderForTests(async () => {
scriptCalls += 1
simulateScript('editor', () => ({ TextEditor: `editor-view:${scriptCalls}` }))
})
let etag = '"v1"'
stubBundleHead(() => etag)
const first = await loadChunk('editor')
await settleEtag()
etag = '"v2"'
await revalidateChunksOnReactivate()
const second = await loadChunk('editor')
expect(second).not.toBe(first)
expect(scriptCalls).toBe(2)
})
it('fails open when revalidation cannot reach the bundle route', async () => {
installModuleSystem()
let scriptCalls = 0
setChunkScriptLoaderForTests(async () => {
scriptCalls += 1
simulateScript('locale', () => ({ localeDicts: 'ld' }))
})
stubBundleHead(() => new Error('HEAD unreachable'))
await loadChunk('locale')
await settleEtag()
await revalidateChunksOnReactivate()
await loadChunk('locale')
expect(scriptCalls).toBe(2)
})
it('loadChunk is a barrier: a pending revalidation never serves stale exports (CR P1)', async () => {
installModuleSystem()
let scriptCalls = 0
setChunkScriptLoaderForTests(async () => {
scriptCalls += 1
simulateScript('editor', () => ({ TextEditor: `editor-view:${scriptCalls}` }))
})
// First load: etag "v1", recorded after settlement.
let etag = '"v1"'
stubBundleHead(() => etag)
const first = await loadChunk('editor')
await settleEtag()
expect(scriptCalls).toBe(1)
// The chunk changed on disk; the revalidation HEAD is gated (pending).
etag = '"v2"'
let release: (() => void) | undefined
const gate = new Promise<void>((resolve) => { release = resolve })
vi.stubGlobal('fetch', vi.fn(async () => {
await gate
return { headers: { get: () => etag } } as unknown as Response
}))
const revalidating = revalidateChunksOnReactivate()
// A lazy open DURING the pending revalidation must NOT get the old cache.
let resolved = false
const pendingLoad = loadChunk('editor').then((exports) => { resolved = true; return exports })
await new Promise((resolve) => setTimeout(resolve, 5))
expect(resolved, 'loadChunk must await the pending revalidation').toBe(false)
// Release the HEAD; the barrier lifts and the load re-injects fresh exports.
release?.()
await revalidating
const after = await pendingLoad
expect(after).not.toBe(first)
expect(scriptCalls).toBe(2)
})
it('a HEAD that never answers is bounded by a timeout signal and fails open (barrier cannot wedge loads forever)', async () => {
installModuleSystem()
let scriptCalls = 0
setChunkScriptLoaderForTests(async () => {
scriptCalls += 1
simulateScript('editor', () => ({ TextEditor: `editor-view:${scriptCalls}` }))
})
// First load settles an ETag under a healthy HEAD route.
stubBundleHead(() => '"v1"')
const first = await loadChunk('editor')
await settleEtag()
expect(scriptCalls).toBe(1)
// The revalidation HEAD now hangs. The loader must hand the fetch a
// timeout signal and, when it fires, fail open (drop + re-fetch) — the
// barrier must never block lazy loads indefinitely.
const controller = new AbortController()
const timeoutSpy = vi.spyOn(AbortSignal, 'timeout').mockReturnValue(controller.signal)
vi.stubGlobal('fetch', vi.fn(async (_input, init?: RequestInit) => {
await new Promise<void>((_resolve, reject) => {
init?.signal?.addEventListener('abort', () => reject(new DOMException('timed out', 'AbortError')))
})
}))
const revalidating = revalidateChunksOnReactivate()
let resolved = false
const pendingLoad = loadChunk('editor').then((exports) => { resolved = true; return exports })
await new Promise((resolve) => setTimeout(resolve, 5))
expect(resolved, 'loadChunk must await the pending revalidation').toBe(false)
controller.abort() // the timeout fires
await revalidating
const after = await pendingLoad
expect(after).not.toBe(first)
expect(scriptCalls).toBe(2)
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Number))
timeoutSpy.mockRestore()
})
it('resetChunks clears a pending revalidation barrier (the next load does not wait on the orphaned task)', async () => {
installModuleSystem()
let scriptCalls = 0
setChunkScriptLoaderForTests(async () => {
scriptCalls += 1
simulateScript('editor', () => ({ TextEditor: `editor-view:${scriptCalls}` }))
})
const etag = '"v1"'
stubBundleHead(() => etag)
await loadChunk('editor')
await settleEtag()
// Gate the revalidation HEAD so the barrier stays pending.
let release: (() => void) | undefined
const gate = new Promise<void>((resolve) => { release = resolve })
vi.stubGlobal('fetch', vi.fn(async () => {
await gate
return { headers: { get: () => etag } } as unknown as Response
}))
const revalidating = revalidateChunksOnReactivate()
// resetChunks mid-revalidation: the barrier guarded only the state being
// dropped, so the next load must proceed without waiting for the HEAD.
resetChunks()
const after = await loadChunk('editor')
expect(after).toEqual({ TextEditor: 'editor-view:2' })
expect(scriptCalls).toBe(2)
// The orphaned task still settles; its identity-guarded finally no-ops.
// (Its sweep may later drop the freshly loaded entry — chunkEtags was
// cleared by resetChunks, so the old ETag no longer matches. That is the
// fail-safe direction: never serve stale, at worst one redundant fetch.)
release?.()
await revalidating
})
it('clears test-registry fixtures on re-activation (per-test stubs never leak)', async () => {
let testCalls = 0
registerChunkForTests('editor', async () => { testCalls += 1; return { TextEditor: 'test-view' } })
await loadChunk('editor')
expect(testCalls).toBe(1)
await revalidateChunksOnReactivate()
// Test fixture gone: the production path runs and fails fast (no module system).
await expect(loadChunk('editor')).rejects.toThrow('client module system unavailable')
expect(testCalls).toBe(1)
})
})
describe('externals contract', () => {
it('the loader resolves exactly the platform externals the chunk builds keep external', () => {
expect(CHUNK_EXTERNALS).toEqual([
'react',
'react/jsx-runtime',
'react-dom',
'react-dom/client',
'cordis',
'@deepseek-ai/dsh-client-ui-slots',
'@deepseek-ai/dsh-client-ui-primitives',
])
})
})