- html viewer:.html/.htm 沙箱 iframe 预览(相对资源经 /sidebar/html 路径编码路由加载,CSP sandbox + 信任围栏 + cwd 边界),预览/编辑切换 - browser tab:+ 菜单多开(browser:<n>),地址栏后退/前进/刷新/在浏览器中打开(15px 图标),URL 与标题按会话持久化(patchTab + nextBrowser) - 安全:iframe 一律不透明源沙箱(无 allow-same-origin/top-navigation、no-referrer、权限策略全禁、禁 srcdoc);地址栏仅 http(s)、拒绝 localhost 等本机地址;界面实时沙箱状态(关闭红色警示)+ 临时解锁/恢复 - 设置:htmlViewerNoSandbox / htmlViewerDefaultUnsafe / browserNoSandbox / browserInterceptLinks 四个声明式二级设置(默认安全) - 外链拦截:聊天/界面 http(s) 外链默认在侧边栏打开(openTab seed.url),Ctrl/Cmd 点击放行 - 嵌入拒绝面板:browser.probe 路由探测 X-Frame-Options/frame-ancestors,被拒站点显示原因 + 在浏览器中打开/仍然加载 - 设置页可滚动;内置 7 tab + 9 viewer;新增设计文档;268 个测试全绿
172 lines
7.1 KiB
TypeScript
172 lines
7.1 KiB
TypeScript
/**
|
|
* Built-in registration tests: the plugin registers 7 tabs and 9 file
|
|
* viewers through the same service external plugins use (dogfooding);
|
|
* the catch-all `code` viewer, the NUL-sniffing `binary-download` viewer,
|
|
* and the html/browser sandbox settings pin the registry's behavior.
|
|
*/
|
|
import { describe, expect, it } from 'vitest'
|
|
// First import: browser globals before the xterm-carrying builtin graph loads.
|
|
import './browser-globals.ts'
|
|
|
|
import type { Context } from '../src/context-types.ts'
|
|
import { createBetterSidebarService } from '../src/client/service.ts'
|
|
import { createSidebarStore } from '../src/client/state.ts'
|
|
import { allLeaves } from '../src/client/state.ts'
|
|
import { registerBuiltins } from '../src/client/builtins/index.ts'
|
|
|
|
function setup(): { service: ReturnType<typeof createBetterSidebarService>; store: ReturnType<typeof createSidebarStore>; dispose: () => void } {
|
|
const store = createSidebarStore()
|
|
const service = createBetterSidebarService(store)
|
|
const dispose = registerBuiltins({} as Context, service)
|
|
return { service, store, dispose }
|
|
}
|
|
|
|
describe('built-in tab registrations', () => {
|
|
it('registers the 7 built-in tabs', () => {
|
|
const { service } = setup()
|
|
expect(service.getTabs().map(t => t.id).sort()).toEqual(
|
|
['browser', 'diff', 'editor', 'explorer', 'git', 'subagent', 'terminal'],
|
|
)
|
|
})
|
|
|
|
it('editor and diff are hidden from the + menu (opened by file-open / git view)', () => {
|
|
const { service } = setup()
|
|
expect(service.getTabs().filter(t => t.hidden).map(t => t.id).sort()).toEqual(['diff', 'editor'])
|
|
})
|
|
|
|
it('single-instance tabs use the single sugar', () => {
|
|
const { service } = setup()
|
|
for (const id of ['explorer', 'git', 'subagent']) {
|
|
expect(service.getTab(id)?.single).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('the subagent tab declares its auto-open related setting', () => {
|
|
const { service } = setup()
|
|
const toggles = service.getTab('subagent')?.settings?.toggles ?? []
|
|
expect(toggles.map(t => t.key)).toEqual(['autoOpenSubagent'])
|
|
})
|
|
|
|
it('the terminal tab declares the model terminal-tools related setting', () => {
|
|
const { service } = setup()
|
|
const toggles = service.getTab('terminal')?.settings?.toggles ?? []
|
|
expect(toggles.map(t => t.key)).toEqual(['agentTerminalTools'])
|
|
})
|
|
|
|
it('the browser tab declares its sandbox and link-takeover related settings', () => {
|
|
const { service } = setup()
|
|
const toggles = service.getTab('browser')?.settings?.toggles ?? []
|
|
expect(toggles.map(t => t.key)).toEqual(['browserNoSandbox', 'browserInterceptLinks'])
|
|
expect(toggles[0]?.title).toBeDefined()
|
|
expect(toggles[0]?.desc).toBeDefined()
|
|
expect(toggles[1]?.title).toBeDefined()
|
|
expect(toggles[1]?.desc).toBeDefined()
|
|
})
|
|
|
|
it('the browser createTab mints browser:<n> ids and bumps nextBrowser', () => {
|
|
const { service, store } = setup()
|
|
store.setSession('s1')
|
|
service.openTab({ type: 'browser' })
|
|
service.openTab({ type: 'browser' })
|
|
const state = store.getSnapshot().state!
|
|
const tabs = allLeaves(state.splits).flatMap(leaf => leaf.tabs).filter(t => t.type === 'browser')
|
|
expect(tabs).toHaveLength(2)
|
|
expect(tabs[0]!.id).toBe('browser:1')
|
|
expect(tabs[1]!.id).toBe('browser:2')
|
|
expect(state.nextBrowser).toBe(3)
|
|
})
|
|
|
|
it('every built-in tab carries the settings-surface icon', () => {
|
|
const { service } = setup()
|
|
for (const tab of service.getTabs()) {
|
|
expect(tab.icon, tab.id).toBeDefined()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('built-in file viewer registrations', () => {
|
|
it('registers the 9 built-in file viewers', () => {
|
|
const { service } = setup()
|
|
expect(service.getFileViewers().map(v => v.id).sort()).toEqual(
|
|
['binary-download', 'code', 'docx', 'html', 'image', 'markdown', 'pdf', 'pptx', 'xlsx'],
|
|
)
|
|
})
|
|
|
|
it('code is the catch-all at the lowest priority', () => {
|
|
const { service } = setup()
|
|
const code = service.getFileViewers().find(v => v.id === 'code')
|
|
expect(code?.exts).toEqual([])
|
|
expect(code?.priority).toBe(-100)
|
|
expect(code?.fetchStrategy).toBe('fsRead')
|
|
expect(service.matchFileViewer('anything.zzz')?.id).toBe('code')
|
|
})
|
|
|
|
it('markdown claims md/markdown before the catch-all', () => {
|
|
const { service } = setup()
|
|
expect(service.matchFileViewer('readme.md')?.id).toBe('markdown')
|
|
expect(service.matchFileViewer('readme.markdown')?.id).toBe('markdown')
|
|
expect(service.matchFileViewer('readme.md', new Uint8Array([0x61]))?.id).toBe('markdown')
|
|
})
|
|
|
|
it('html claims html/htm before the catch-all', () => {
|
|
const { service } = setup()
|
|
expect(service.matchFileViewer('index.html')?.id).toBe('html')
|
|
expect(service.matchFileViewer('page.htm')?.id).toBe('html')
|
|
expect(service.matchFileViewer('index.html', new Uint8Array([0x3c, 0x21]))?.id).toBe('html')
|
|
expect(service.matchFileViewer('index.HTML')?.id).toBe('html')
|
|
})
|
|
|
|
it('the html viewer declares its sandbox and default-unsafe related settings', () => {
|
|
const { service } = setup()
|
|
const toggles = service.getFileViewers().find(v => v.id === 'html')?.settings?.toggles ?? []
|
|
expect(toggles.map(t => t.key)).toEqual(['htmlViewerNoSandbox', 'htmlViewerDefaultUnsafe'])
|
|
expect(toggles[0]?.title).toBeDefined()
|
|
expect(toggles[0]?.desc).toBeDefined()
|
|
expect(toggles[1]?.title).toBeDefined()
|
|
expect(toggles[1]?.desc).toBeDefined()
|
|
})
|
|
|
|
it('binary-download claims legacy office by extension', () => {
|
|
const { service } = setup()
|
|
expect(service.matchFileViewer('old.doc')?.id).toBe('binary-download')
|
|
expect(service.matchFileViewer('old.xls')?.id).toBe('binary-download')
|
|
expect(service.matchFileViewer('old.ppt')?.id).toBe('binary-download')
|
|
})
|
|
|
|
it('binary-download NUL detect claims unknown-extension binaries over code', () => {
|
|
const { service } = setup()
|
|
// First match (no head) falls to the catch-all code viewer...
|
|
expect(service.matchFileViewer('blob.zzz')?.id).toBe('code')
|
|
// ...but the head re-match (NUL probe) routes it to binary-download.
|
|
expect(service.matchFileViewer('blob.zzz', new Uint8Array([0x01, 0x00, 0x02]))?.id).toBe('binary-download')
|
|
// A NUL-free blob stays with code.
|
|
expect(service.matchFileViewer('blob.zzz', new Uint8Array([0x61, 0x62]))?.id).toBe('code')
|
|
})
|
|
|
|
it('office mediaUrl viewers beat the binary sniffers for their own extensions', () => {
|
|
const { service } = setup()
|
|
// A .docx is a zip (no NUL in its head): the docx viewer (priority 0)
|
|
// claims it before binary-download (-50) is consulted.
|
|
expect(service.matchFileViewer('book.docx', new Uint8Array([0x50, 0x4b, 0x03, 0x04]))?.id).toBe('docx')
|
|
})
|
|
|
|
it('every built-in viewer carries the declarative settings surface (title + icon)', () => {
|
|
const { service } = setup()
|
|
for (const viewer of service.getFileViewers()) {
|
|
expect(viewer.title, viewer.id).toBeDefined()
|
|
expect(viewer.icon, viewer.id).toBeDefined()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('built-in disposer', () => {
|
|
it('unregisters everything (HMR-safe)', () => {
|
|
const { service, dispose } = setup()
|
|
dispose()
|
|
expect(service.getTabs()).toHaveLength(0)
|
|
expect(service.getFileViewers()).toHaveLength(0)
|
|
// The disposer is idempotent.
|
|
dispose()
|
|
})
|
|
})
|