import { Context } from '@deepseek-ai/cordis' import AgentRegistry from '@deepseek-ai/dsh-agent' import SessionStore from '@deepseek-ai/dsh-session' import { describe, expect, it, vi } from 'vitest' import { createSessionTestController, createSessionTestRemote, } from './test-remote.ts' async function context(): Promise { const ctx = new Context() await ctx.plugin(SessionStore) await ctx.plugin(AgentRegistry) return ctx } describe('session/openWorkspacePath', () => { it('hands a Client-resolved workspace path to the Host opener unchanged', async () => { const ctx = await context() const openPath = vi.fn((_path: string, _signal: AbortSignal) => Promise.resolve()) const remote = createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/default', openPath, }) const signal = new AbortController().signal await expect(remote.openWorkspacePath({ path: '/workspace/project/src/a.ts' }, signal)) .resolves.toEqual({ ok: true, value: { opened: true } }) expect(openPath).toHaveBeenCalledWith('/workspace/project/src/a.ts', signal) expect(ctx.agents.list()).toEqual([]) }) it('preserves relative and absolute Host-resolvable paths', async () => { const ctx = await context() const openPath = vi.fn((_path: string, _signal: AbortSignal) => Promise.resolve()) const remote = createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/default', openPath, }) await remote.openWorkspacePath({ path: '/tmp/result.html' }) await remote.openWorkspacePath({ path: 'result.html' }) expect(openPath.mock.calls.map(call => call[0])).toEqual(['/tmp/result.html', 'result.html']) }) it('rejects empty paths before opening anything', async () => { const ctx = await context() const openPath = vi.fn((_path: string, _signal: AbortSignal) => Promise.resolve()) const remote = createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/default', openPath, }) await expect(remote.openWorkspacePath({ path: '' })) .resolves.toMatchObject({ ok: false, error: { code: 'bad-request' } }) expect(openPath).not.toHaveBeenCalled() }) it('preserves native opener failure and cancellation results', async () => { const ctx = await context() const openPath = vi.fn((_path: string, _signal: AbortSignal) => Promise.reject(new Error('desktop unavailable'))) const remote = createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/default', openPath, }) await expect(remote.openWorkspacePath({ path: 'result.html' })) .resolves.toMatchObject({ ok: false, error: { code: 'internal', message: 'path open failed: desktop unavailable' }, }) const aborted = new AbortController() aborted.abort(new Error('cancelled')) await expect(remote.openWorkspacePath({ path: 'result.html' }, aborted.signal)) .resolves.toMatchObject({ ok: false, error: { code: 'cancelled' } }) }) it('classifies opener cancellation and non-Error failures', async () => { const ctx = await context() const aborted = new AbortController() const openPath = vi.fn() .mockImplementationOnce(async () => { aborted.abort(new Error('cancelled')) throw new Error('opening stopped') }) .mockRejectedValueOnce('desktop unavailable') const controller = createSessionTestController(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/default', openPath, }) await expect(controller.openWorkspacePath({ path: 'first.html' }, aborted.signal)) .rejects.toMatchObject({ failure: { code: 'cancelled' } }) await expect(controller.openWorkspacePath({ path: 'second.html', }, new AbortController().signal)).rejects.toMatchObject({ failure: { code: 'internal', message: 'path open failed: desktop unavailable' }, }) }) })