Files
apes-sidebar/tests/paths.spec.ts
T
NeedsTree b4e9924982 fix(editor): open a directory target as the folder window (#1029)
A path that resolves to a directory (a chat folder link, a sidebar_open
folder, or a directory pasted into the editor address bar) was read as a
file: the host answered 400 fs-error '"<path>" is a directory' and
EditorHost painted that raw message into the tab instead of the plugin's
folder window.

- fs.read / fs.readPage now answer a directory with the dedicated
  fs-directory wire code (src/wire.ts, readText/readPage).
- EditorHost promotes the tab to the folder window it already draws.
- The promoted root is a PLATFORM-NATIVE ABSOLUTE path, resolved against
  the session workspace root by the new resolveNativeSidebarPath helper
  (paths.ts): chat file addresses carry session-relative, forward-slash
  spellings, while the host keys every listing by its path.resolve'd
  absolute path and FileTree caches levels by the requested root. A
  relative root or a mixed-separator root lands in another cache key and
  the tree sits on its loading placeholder forever. The helper also drops
  a trailing separator (except a drive root) like path.resolve does.
- The fix covers all three entry points at once: conversation folder
  links (no meta.dir on the tab), sidebar_open folders (service.openTab
  drops the seed meta on the native resource route), and #933's
  pasted/stepped directory path.

Tests: isDirectoryReadError unit cases, the EditorHost relative-path and
Windows-root fallback render cases, resolveNativeSidebarPath win32/POSIX
cases, and the fs.readPage directory code. typecheck + build + lint pass;
targeted suites 78/78; the full suite's remaining failures reproduce on a
clean dev checkout (Windows symlink / pnpm pack / git smokes) and are
unrelated to this change. Verified in the live GUI: a folder opened after
the patch expands its tree immediately.
2026-10-22 17:58:10 +08:00

155 lines
7.8 KiB
TypeScript

import './browser-globals.ts'
import { describe, expect, it } from 'vitest'
import { decodeHtmlUrl } from '../src/html-route.ts'
import { isAbsolutePath, relativeTo, workspaceRelativePath } from '../src/client/paths.ts'
import { resolveNativeSidebarPath, resolveSidebarPath } from '../src/client/paths.ts'
import { downloadUrl, htmlUrl, mediaUrl } from '../src/client/api.ts'
describe('path helpers', () => {
it('derives relative paths under the cwd (and "." for the cwd itself)', () => {
expect(relativeTo('/Users/me/code', '/Users/me/code/src/main.ts')).toBe('src/main.ts')
expect(relativeTo('/Users/me/code', '/Users/me/code')).toBe('.')
expect(relativeTo('/Users/me/code/', '/Users/me/code/src/a/b.ts')).toBe('src/a/b.ts')
})
it('falls back to the path unchanged when it lies outside the cwd', () => {
expect(relativeTo('/Users/me/code', '/Users/other/x.ts')).toBe('/Users/other/x.ts')
expect(relativeTo('/Users/me/code', '/Users/me/codex/y.ts')).toBe('/Users/me/codex/y.ts')
})
it('handles windows roots and mixed separators', () => {
expect(relativeTo('C:\\Users\\me', 'C:\\Users\\me\\src\\a.ts')).toBe('src/a.ts')
expect(relativeTo('C:\\Users\\me', 'C:/Users/me/src/a.ts')).toBe('src/a.ts')
expect(relativeTo('C:\\Users\\me\\', 'C:\\Users\\me')).toBe('.')
})
it('containment is case-insensitive (windows/macOS case-insensitive volumes)', () => {
expect(relativeTo('C:\\Users\\Me', 'c:/users/me/src/a.ts')).toBe('src/a.ts')
expect(relativeTo('/Users/Me/code', '/users/me/code/src/main.ts')).toBe('src/main.ts')
// The returned relative text keeps the caller's own casing.
expect(relativeTo('C:\\Users\\me', 'C:\\Users\\Me\\SRC\\a.ts')).toBe('SRC/a.ts')
})
it('resolves produced paths against windows cwds', () => {
expect(resolveSidebarPath('C:\\work\\proj', 'src/a.ts')).toBe('C:\\work\\proj\\src/a.ts')
expect(resolveSidebarPath('C:\\work\\proj', 'C:\\abs\\x.ts')).toBe('C:\\abs\\x.ts')
expect(resolveSidebarPath('C:\\work\\proj\\', 'C:\\abs\\x.ts')).toBe('C:\\abs\\x.ts')
})
it('normalizes a folder root to the cwd separator dialect (#1029)', () => {
// A chat file address is `/`-separated and session-relative; the host keys
// listings by path.resolve'd absolute paths, so the folder root the tree
// requests must speak the same dialect or it never matches its level.
expect(resolveNativeSidebarPath('C:\\work\\proj', 'src/a.ts')).toBe('C:\\work\\proj\\src\\a.ts')
// path.resolve drops a trailing separator; the helper mirrors that (a
// drive root keeps it).
expect(resolveNativeSidebarPath('C:\\work\\proj\\', 'src/a.ts/')).toBe('C:\\work\\proj\\src\\a.ts')
expect(resolveNativeSidebarPath('C:\\work\\proj', 'C:\\')).toBe('C:\\')
// POSIX keeps the plain join: a backslash is a legal filename character.
expect(resolveNativeSidebarPath('/home/me/proj', 'src/a.ts')).toBe('/home/me/proj/src/a.ts')
expect(resolveNativeSidebarPath(undefined, 'src/a.ts')).toBe('src/a.ts')
})
it('keeps UNC produced paths absolute instead of joining them onto the cwd', () => {
// Pure client function: UNC detection is platform-independent, so these
// assertions run on every host without a platform guard.
expect(resolveSidebarPath('C:\\work\\proj', '\\\\server\\share\\abs\\x.ts'))
.toBe('\\\\server\\share\\abs\\x.ts')
expect(resolveSidebarPath('C:\\work\\proj', '//server/share/abs/x.ts'))
.toBe('//server/share/abs/x.ts')
// A relative path under a UNC cwd joins with backslashes.
expect(resolveSidebarPath('\\\\server\\share\\proj', 'src/a.ts'))
.toBe('\\\\server\\share\\proj\\src/a.ts')
})
it('mirrors the host absolute-path notion without node:path', () => {
expect(isAbsolutePath('/abs/x.ts')).toBe(true)
expect(isAbsolutePath('C:\\abs\\x.ts')).toBe(true)
expect(isAbsolutePath('C:/abs/x.ts')).toBe(true)
expect(isAbsolutePath('\\\\server\\share\\x.ts')).toBe(true)
expect(isAbsolutePath('//server/share/x.ts')).toBe(true)
expect(isAbsolutePath('C:relative.ts')).toBe(false)
expect(isAbsolutePath('rel/x.ts')).toBe(false)
})
it('treats ~ as home-absolute, never a session-relative path (#713)', () => {
expect(isAbsolutePath('~')).toBe(true)
expect(isAbsolutePath('~/notes/x.md')).toBe(true)
expect(isAbsolutePath('~\\notes\\x.md')).toBe(true)
// `~other-user` is NOT the home shorthand; it stays relative-shaped.
expect(isAbsolutePath('~other/x.ts')).toBe(false)
})
it('htmlUrl always marks UNC paths (platform-neutral marker)', () => {
// The marker is platform-neutral now: the host resolves the decoded
// '//server/share/...' form per-platform, so no cwd/OS signal is needed.
expect(htmlUrl({ sessionId: 's' }, '\\\\server\\share\\proj\\a.html'))
.toBe('http://localhost/sidebar/html/s//server/share/proj/a.html')
expect(htmlUrl({ sessionId: 's', cwd: '/home/me' }, '//server/share/a.html'))
.toBe('http://localhost/sidebar/html/s//server/share/a.html')
expect(htmlUrl({ sessionId: 's', cwd: '/home/me' }, '/home/me/index.html'))
.toBe('http://localhost/sidebar/html/s/home/me/index.html')
})
})
describe('preview URL path resolution', () => {
it.each([
['/home/project', 'pages/report.html', '/home/project/pages/report.html'],
['/home/project/', '报告 #1.html', '/home/project/报告 #1.html'],
['C:\\work\\project', 'pages/report.html', 'C:/work/project/pages/report.html'],
['\\\\server\\share\\project', 'report.html', '//server/share/project/report.html'],
['/home/project', '/tmp/report.html', '/tmp/report.html'],
])('resolves %s + %s before encoding', (cwd, path, expected) => {
const scope = { sessionId: 'session #1', cwd }
const decoded = decodeHtmlUrl(new URL(htmlUrl(scope, path), 'http://localhost').pathname)
expect(decoded).toEqual({ ok: true, ref: { sessionId: scope.sessionId, path: expected } })
for (const build of [mediaUrl, downloadUrl]) {
const url = new URL(build(scope, path), 'http://localhost')
expect(url.searchParams.get('path')?.replace(/\\/g, '/')).toBe(expected)
expect(url.searchParams.get('cwd')).toBe(cwd)
expect(url.searchParams.get('download')).toBe(build === downloadUrl ? '1' : null)
}
})
it('keeps nested relative assets in the same session and document directory', () => {
const page = new URL(htmlUrl({ sessionId: 's', cwd: '/work/project' }, 'pages/report.html'), 'http://localhost')
for (const [asset, expected] of [['./style.css', '/work/project/pages/style.css'], ['../img/pic.png', '/work/project/img/pic.png']] as const) {
expect(decodeHtmlUrl(new URL(asset, page).pathname)).toEqual({ ok: true, ref: { sessionId: 's', path: expected } })
}
})
it('leaves relative file queries for server resolution when cwd is unavailable', () => {
expect(new URL(mediaUrl({ sessionId: 's' }, 'pic.png'), 'http://localhost').searchParams.get('path')).toBe('pic.png')
})
})
describe('workspaceRelativePath', () => {
it.each([
['/work', '/work/src/a.ts', 'src/a.ts'],
['/work/', '/work', '.'],
['/', '/src/a.ts', 'src/a.ts'],
['/', '/', '.'],
['C:\\Work', 'c:/work/SRC/a.ts', 'SRC/a.ts'],
['C:\\Work\\', 'c:/WORK', '.'],
['C:\\', 'c:/SRC/a.ts', 'SRC/a.ts'],
['C:\\Work', 'c:/work/my dir\\a.ts', 'my dir/a.ts'],
['\\\\server\\share\\Work', '//SERVER/share/work/src/a.ts', 'src/a.ts'],
])('projects %s / %s', (root, path, expected) => {
expect(workspaceRelativePath(root, path)).toBe(expected)
})
it.each([
[undefined, '/work/a.ts'],
['', '/work/a.ts'],
['work', '/work/a.ts'],
['/work', '/outside/a.ts'],
['/work', '/work-other/a.ts'],
['/work', '/WORK/a.ts'],
['/work', '/work/../outside/a.ts'],
['/work', 'src/a.ts'],
['C:/Work', 'D:/Work/a.ts'],
])('refuses an unavailable root or unsafe projection %j / %s', (root, path) => {
expect(workspaceRelativePath(root, path)).toBeUndefined()
})
})