Files
apes-sidebar/tests/fs-read-session-root.spec.ts
T
Menghuan1918 795beabe22 chore(dsh): adapt to DSH 0.2.3-alpha.2 and drop pre-0.2.3 compatibility
Raise the whole baseline to the alpha line and stop carrying code that only
exists for older hosts (no runtime compatibility layer, per AGENTS §4):

- peers/engines: 14 DSH peers and engines.dsh -> 0.2.3-alpha.2, Cordis ->
  4.2.0-alpha.1, group/include 1.2.0-alpha.1, schemastery 3.20.0-alpha.1;
  dev pins and CI/e2e/install scripts move with them; plugin version
  0.34.0-alpha.0.
- admission: DSH 0.2.3 deleted `requestRejection`, so only the injected
  `connection.authentication` provider can call `admit`; the legacy branch
  and the direct-admit fallback are gone and the fence fails loudly.
- directory events: the projection authority is `workspace-root/change`;
  the side-chat cwd stamp emits it and the worktree scan no longer reads
  the retired `working-directory/change`.
- llm types: `@deepseek-ai/dsh-llm` no longer exports `./brand`, so
  MessageId comes from `./types`.
- client anchors: only `[data-slot="main.conversation"]`; the alpha.1
  `conversation` key is retired. `webServer.getOrigin` is called directly.

Gates: peers check, typecheck, lint, test (366 files / 4615 passed),
build, check:consumer-types.
2026-11-25 01:03:57 +08:00

79 lines
3.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { execFileSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { apply } from '../src/index.ts'
import type { SidebarWebRoute } from '../src/context-types.ts'
function mountApi(cwd: string): SidebarWebRoute {
const routes: SidebarWebRoute[] = []
const ctx = {
connection: { admit: () => ({ peer: null }) },
webRuntime: { trustedHosts: [] },
webServer: {
register: (route: SidebarWebRoute) => { routes.push(route); return () => {} },
registerUpgrade: () => () => {},
},
sessions: { get: () => ({ header: { cwd }, snapshotEvents: () => [] }) },
tools: { register: () => () => {} },
effect: (fn: () => unknown) => { fn() },
inject: (deps: readonly string[], callback: (sctx: never) => void) => {
if (deps.includes('connection.authentication')) callback({ connection: { admit: () => ({ peer: null }) } } as never)
return () => {}
},
on: () => () => {},
get: () => undefined,
}
apply(ctx as never, {})
return routes.find(route => route.path === '/sidebar/api')!
}
async function read(route: SidebarWebRoute, path: string): Promise<{
ok: boolean; value?: { content?: string }; error?: { message?: string }
}> {
const body = Buffer.from(JSON.stringify({ sessionId: 's', path }))
const req = {
method: 'POST', url: '/sidebar/api/fs.read', headers: { host: '127.0.0.1:3080' },
[Symbol.asyncIterator]: async function* () { yield body },
} as never
let result = ''
const res = { writeHead: () => {}, end: (chunk: unknown) => { result += String(chunk ?? '') } } as never
await route.handler(req, res)
return JSON.parse(result)
}
for (const subdirectory of ['sub-en', '社媒运营']) {
describe(`fs.read session root: ${subdirectory}`, () => {
it('reads relative paths from the session and refuses a repository-root decoy', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-session-read-'))
try {
const repo = join(root, 'repo')
const cwd = join(repo, subdirectory)
mkdirSync(join(cwd, 'tools'), { recursive: true })
execFileSync('git', ['init', '-q'], { cwd: repo })
writeFileSync(join(cwd, 'tools', 'a.md'), 'session file\n')
writeFileSync(join(repo, 'root-only.md'), 'wrong repository-root file\n')
const outside = join(root, 'outside.md')
writeFileSync(outside, 'outside file\n')
const route = mountApi(cwd)
expect(await read(route, 'tools/a.md')).toMatchObject({ ok: true, value: { content: 'session file\n' } })
const missing = await read(route, 'root-only.md')
expect(missing.ok).toBe(false)
// Windows spells the same temp directory as either RUNNER~1 or
// runneradmin depending on who resolved it; the assertion is about the
// session-relative tail, not the user-profile spelling.
const message = (missing.error?.message ?? '').replace(/\\/g, '/')
expect(message).toContain(['repo', subdirectory, 'root-only.md'].join('/'))
expect(readFileSync(join(repo, 'root-only.md'), 'utf8')).toBe('wrong repository-root file\n')
expect(await read(route, join(cwd, 'tools', 'a.md'))).toMatchObject({ ok: true, value: { content: 'session file\n' } })
expect(await read(route, outside)).toMatchObject({ ok: true, value: { content: 'outside file\n' } })
expect(await read(route, '../root-only.md')).toMatchObject({ ok: true, value: { content: 'wrong repository-root file\n' } })
} finally {
rmSync(root, { recursive: true, force: true })
}
})
})
}