Files
apes-sidebar/tests/pty-deps.spec.ts
T
figmar 5a7e2139a0 chore(sync): re-base the fork onto upstream dsh-better-sidebar v0.19.1
The fork was pinned at upstream v0.18.0. That is the entire reason the window
ran TWO sidebars and TWO near-identical panel toggles: DSH 2.0.10 ships its own
right sidebar (@deepseek-ai/dsh-client-ui-sidebar-right), and upstream only
adopted it in v0.19.0-alpha.1 — which arrived after our fork point. v0.19.1 is
also the one upstream line whose support range actually matches our host
(engines.dsh >=0.1.5-rc.1, 14 DSH peers ^0.1.5-rc.1, host = 0.1.5-rc.2), so it
is the only tag the host's startup precheck accepts.

What upstream v0.18.0 → v0.19.1 brings
- src/client/native/{index,surface,tab-adapter}.tsx: every tab the plugin can
  draw becomes an OFFICIAL native tab type + body, and an external plugin's
  descriptor registered later gets its native type too. This is what retires
  the fork's own right panel — and with it the duplicate toggle cluster
  (`toggleCluster` is gone from v0.19.1).
- native tab-chip icons via the `sidebar.right.pane.tab.title` seat, coloured
  built-in tab icons, file/folder glyphs from DSH's own `FileTypeIcon`.
- guide descriptions (DSH renders them while the guide lists <= 4 entries),
  tab bodies that fill the panel, and the 0.1.5-rc.2 adaptation work we were
  carrying by hand (peer/devDep pinning, primitives' transitive devDeps).

How the sync was done
Rebuilt the tree from the v0.19.1 tag rather than resolving 53 conflicted
files: our v0.18.0 delta was measured first and is only (a) one mechanical
rename `dsh-better-sidebar` -> `dsh-apes-sidebar` and (b) ten real edits. So:
tag tree + rename (scoped: code/config/scripts/tests/README, while the 46
historical docs/plans records and switch.sh's upstream package name stay
untouched) + re-applied deltas + our apes module.

Fork deltas re-applied on the new base
- src/client/apes/ (icons / locales / tabs): the three apes:* information-layer
  tabs (agents / apps / profile), registered through the same BetterSidebarService
  as the builtins — which is exactly why they now ride upstream's native adapter
  into the official sidebar for free.
- identity: package.json (name/version 1.2.0/description/forkedFrom), dsh.plugin.json
  (id/version), SIDEBAR_SERVICE_VERSION 1.2.0, README fork notice,
  docs/external-plugin-guide.md.
- scripts/switch.sh + the pnpm switch* scripts (apes <-> upstream two-line switch).
- tests/apes-tabs.spec.ts (unescaped: it starts from the v0.19.1 base).

Dropped
- the Phase A native-registration spike (`apes/native-tabs.tsx` + its spec): it
  re-derived by hand exactly what upstream's native layer already does — the
  hand-written bridge is dead weight now, and v0.19.1's own e2e/jsdom lanes
  cover the same seat contract far better.
- the unused apesProfileGuideDesc key and the two package.json.bak.* backups.

Verified on the new base: typecheck clean; build clean; 1351 passed / 9 skipped
(128 files), up from 1255 on the old base.
2026-10-03 22:51:09 +08:00

210 lines
8.8 KiB
TypeScript

/**
* pty-deps unit tests (issue #140, plugin side): lazy node-pty loading,
* profile/plugin-root discovery, and the pasteable repair command — the
* degraded mode the terminal tab enters when node-pty cannot load, instead
* of the whole plugin (and `dsh web`) failing to boot.
*/
import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, dirname } from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import {
buildRepairCommand,
depsStatus,
DSH_NODE_PTY_RANGE,
findPluginRoot,
findProfileDir,
loadNodePty,
loadRequiredNodePty,
nodePtyLoadCause,
resetNodePtyCache,
} from '../src/pty-deps.ts'
import { SidebarError } from '../src/wire.ts'
/** A throw-only require: node-pty missing or its native binding broken. */
const failingRequire = (): never => { throw new Error('Cannot find package node-pty') }
describe('loadNodePty', () => {
beforeEach(() => { resetNodePtyCache() })
it('loads the real node-pty module in this repository', () => {
const mod = loadNodePty()
expect(mod).not.toBeNull()
expect(typeof mod?.spawn).toBe('function')
})
it('returns null and records the cause when the require throws', () => {
expect(loadNodePty(failingRequire)).toBeNull()
expect((nodePtyLoadCause() as Error).message).toBe('Cannot find package node-pty')
})
it('caches the first outcome (a later working require does not override)', () => {
expect(loadNodePty(failingRequire)).toBeNull()
expect(loadNodePty(() => ({ spawn: () => undefined }))).toBeNull()
resetNodePtyCache()
expect(loadNodePty(() => ({ spawn: () => undefined }))).not.toBeNull()
})
it('loadRequiredNodePty throws the canonical degraded-mode error', () => {
loadNodePty(failingRequire)
let thrown: unknown
try {
loadRequiredNodePty()
} catch (error) {
thrown = error
}
expect(thrown).toBeInstanceOf(SidebarError)
expect((thrown as SidebarError).code).toBe('pty-deps-missing')
expect((thrown as SidebarError).status).toBe(503)
})
})
describe('buildRepairCommand', () => {
let root: string
let pluginRoot: string
let profileDir: string
beforeEach(() => {
root = mkdtempSync(join(tmpdir(), 'pty-deps-'))
pluginRoot = join(root, 'plugin')
profileDir = join(root, 'profiles', 'web')
mkdirSync(join(pluginRoot, 'scripts'), { recursive: true })
mkdirSync(profileDir, { recursive: true })
writeFileSync(join(pluginRoot, 'scripts', 'install.sh'), '#!/usr/bin/env bash\n')
writeFileSync(join(pluginRoot, 'scripts', 'install.ps1'), '# ps1\n')
})
afterEach(() => { rmSync(root, { recursive: true, force: true }) })
it('posix: bash installer with --repair and the detected profile', () => {
const { command, note } = buildRepairCommand({ pluginRoot, profileDir, platform: 'darwin' })
expect(command).toBe(`bash "${join(pluginRoot, 'scripts', 'install.sh')}" --repair --profile "web"`)
expect(note).toBeUndefined()
})
it('posix: omits --profile when the profile could not be detected', () => {
const { command } = buildRepairCommand({ pluginRoot, profileDir: null, platform: 'linux' })
expect(command).toBe(`bash "${join(pluginRoot, 'scripts', 'install.sh')}" --repair`)
})
it('win32: powershell -ExecutionPolicy Bypass with -Repair and -Profile', () => {
const { command } = buildRepairCommand({ pluginRoot, profileDir, platform: 'win32' })
const expected = join(pluginRoot, 'scripts', 'install.ps1')
expect(command).toBe(`powershell -ExecutionPolicy Bypass -File "${expected}" -Repair -Profile "web"`)
})
it('falls back to the dsh plugin command when the plugin scripts are unavailable', () => {
const bareRoot = join(root, 'bare')
mkdirSync(bareRoot)
const { command, note } = buildRepairCommand({ pluginRoot: bareRoot, profileDir, platform: 'darwin' })
expect(command).toBe('dsh plugin --profile "web" install')
expect(note).toContain('allowBuilds')
})
it('falls back to the dsh plugin command (default web) when nothing is detectable', () => {
const { command } = buildRepairCommand({ pluginRoot: null, profileDir: null, platform: 'darwin' })
expect(command).toBe('dsh plugin --profile "web" install')
})
})
describe('findProfileDir / findPluginRoot', () => {
let root: string
let moduleFile: string
beforeEach(() => {
root = mkdtempSync(join(tmpdir(), 'pty-deps-'))
// A fake plugin module deep inside the profile's pnpm node_modules.
const pkgRoot = join(root, 'profiles', 'web', 'node_modules', '.pnpm', 'dsh-apes-sidebar@0.0.0', 'node_modules', 'dsh-apes-sidebar')
const libDir = join(pkgRoot, 'lib')
mkdirSync(libDir, { recursive: true })
writeFileSync(join(root, 'profiles', 'web', 'package.json'), JSON.stringify({ name: 'dsh-profile-web' }))
writeFileSync(join(root, 'profiles', 'web', 'pnpm-workspace.yaml'), '')
writeFileSync(join(pkgRoot, 'package.json'), JSON.stringify({ name: 'dsh-apes-sidebar' }))
moduleFile = join(libDir, 'index.js')
writeFileSync(moduleFile, '')
})
afterEach(() => { rmSync(root, { recursive: true, force: true }) })
it('walks up from the plugin module to the profile root (pnpm-workspace.yaml)', () => {
expect(findProfileDir(moduleFile)).toBe(realpathSync(join(root, 'profiles', 'web')))
})
it('walks up to the plugin package root by package.json name', () => {
expect(findPluginRoot(moduleFile)).toBe(realpathSync(join(root, 'profiles', 'web', 'node_modules', '.pnpm', 'dsh-apes-sidebar@0.0.0', 'node_modules', 'dsh-apes-sidebar')))
})
it('falls back to $DSH_HOME/profiles/web when no ancestor looks like a profile', () => {
const home = join(root, 'home')
const web = join(home, 'profiles', 'web')
mkdirSync(web, { recursive: true })
writeFileSync(join(web, 'package.json'), JSON.stringify({ name: 'dsh-profile-web' }))
writeFileSync(join(web, 'pnpm-workspace.yaml'), '')
// A bare module path with no profile-like ancestor (the walk-up must miss).
const bareModule = join(root, 'elsewhere', 'lib', 'index.js')
mkdirSync(dirname(bareModule), { recursive: true })
writeFileSync(bareModule, '')
const previous = process.env.DSH_HOME
process.env.DSH_HOME = home
try {
expect(findProfileDir(bareModule)).toBe(realpathSync(web))
} finally {
if (previous === undefined) delete process.env.DSH_HOME
else process.env.DSH_HOME = previous
}
})
})
describe('depsStatus', () => {
let root: string
let moduleFile: string
beforeEach(() => {
root = mkdtempSync(join(tmpdir(), 'pty-deps-'))
const pkgRoot = join(root, 'profiles', 'web', 'node_modules', '.pnpm', 'dsh-apes-sidebar@0.0.0', 'node_modules', 'dsh-apes-sidebar')
mkdirSync(join(pkgRoot, 'scripts'), { recursive: true })
mkdirSync(join(pkgRoot, 'lib'), { recursive: true })
writeFileSync(join(root, 'profiles', 'web', 'package.json'), JSON.stringify({ name: 'dsh-profile-web' }))
writeFileSync(join(root, 'profiles', 'web', 'pnpm-workspace.yaml'), '')
writeFileSync(join(pkgRoot, 'package.json'), JSON.stringify({ name: 'dsh-apes-sidebar' }))
writeFileSync(join(pkgRoot, 'scripts', 'install.sh'), '#!/usr/bin/env bash\n')
writeFileSync(join(pkgRoot, 'scripts', 'install.ps1'), '# repair fixture\n')
moduleFile = join(pkgRoot, 'lib', 'index.js')
writeFileSync(moduleFile, '')
resetNodePtyCache()
})
afterEach(() => {
rmSync(root, { recursive: true, force: true })
resetNodePtyCache()
})
it('reports ok:true when node-pty loads', () => {
expect(depsStatus({ fromFile: moduleFile })).toEqual({ ok: true })
})
it('reports the cause, the repair command and the profile when the load fails', () => {
loadNodePty(failingRequire)
const status = depsStatus({ fromFile: moduleFile })
expect(status.ok).toBe(false)
if (status.ok) throw new Error('unreachable')
expect(status.cause).toBe('Cannot find package node-pty')
const scriptName = process.platform === 'win32' ? 'install.ps1' : 'install.sh'
const script = realpathSync(join(root, 'profiles', 'web', 'node_modules', '.pnpm', 'dsh-apes-sidebar@0.0.0', 'node_modules', 'dsh-apes-sidebar', 'scripts', scriptName))
expect(status.command).toBe(process.platform === 'win32'
? `powershell -ExecutionPolicy Bypass -File "${script}" -Repair -Profile "web"`
: `bash "${script}" --repair --profile "web"`)
expect(status.profile).toBe('web')
})
})
describe('DSH sync contract', () => {
it('the declared node-pty range stays identical to DSH core (dsh-subprocess-local ^1.1.0)', () => {
expect(DSH_NODE_PTY_RANGE).toBe('^1.1.0')
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')) as {
dependencies: Record<string, string>
}
expect(pkg.dependencies['node-pty']).toBe(DSH_NODE_PTY_RANGE)
})
})