|
| 1 | +/** |
| 2 | + * Client/host routing for the file tree's external-open action. Remote |
| 3 | + * VSCode-family SSH URLs must launch on the browser machine, while local |
| 4 | + * editor URLs and reveal actions keep using the DSH host opener. |
| 5 | + */ |
| 6 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { api } from '../src/client/api.ts' |
| 8 | + |
| 9 | +afterEach(() => { |
| 10 | + vi.unstubAllGlobals() |
| 11 | +}) |
| 12 | + |
| 13 | +function hostOk(): ReturnType<typeof vi.fn> { |
| 14 | + return vi.fn(async () => ({ |
| 15 | + ok: true, |
| 16 | + status: 200, |
| 17 | + json: async () => ({ ok: true, value: { started: true } }), |
| 18 | + })) |
| 19 | +} |
| 20 | + |
| 21 | +describe('api.openExternal', () => { |
| 22 | + it('launches an SSH remote-editor URL on the client without calling the host', async () => { |
| 23 | + const assign = vi.fn() |
| 24 | + const fetchMock = hostOk() |
| 25 | + vi.stubGlobal('window', { location: { assign } }) |
| 26 | + vi.stubGlobal('fetch', fetchMock) |
| 27 | + const url = 'vscode://vscode-remote/ssh-remote+dev/home/u/f.ts' |
| 28 | + |
| 29 | + await expect(api.openExternal({ action: 'url', url })).resolves.toEqual({ started: true }) |
| 30 | + expect(assign).toHaveBeenCalledOnce() |
| 31 | + expect(assign).toHaveBeenCalledWith(url) |
| 32 | + expect(fetchMock).not.toHaveBeenCalled() |
| 33 | + }) |
| 34 | + |
| 35 | + it('launches Cursor/custom VSCode-family SSH URLs on the client too', async () => { |
| 36 | + const assign = vi.fn() |
| 37 | + const fetchMock = hostOk() |
| 38 | + vi.stubGlobal('window', { location: { assign } }) |
| 39 | + vi.stubGlobal('fetch', fetchMock) |
| 40 | + |
| 41 | + await api.openExternal({ |
| 42 | + action: 'url', |
| 43 | + url: 'cursor://vscode-remote/ssh-remote+dev/home/u/f.ts', |
| 44 | + }) |
| 45 | + await api.openExternal({ |
| 46 | + action: 'url', |
| 47 | + url: 'myfork://vscode-remote/ssh-remote+dev/home/u/f.ts', |
| 48 | + }) |
| 49 | + |
| 50 | + expect(assign).toHaveBeenCalledTimes(2) |
| 51 | + expect(fetchMock).not.toHaveBeenCalled() |
| 52 | + }) |
| 53 | + |
| 54 | + it('keeps local editor URLs on the host opener', async () => { |
| 55 | + const assign = vi.fn() |
| 56 | + const fetchMock = hostOk() |
| 57 | + vi.stubGlobal('window', { location: { assign } }) |
| 58 | + vi.stubGlobal('fetch', fetchMock) |
| 59 | + |
| 60 | + await expect(api.openExternal({ |
| 61 | + action: 'url', |
| 62 | + url: 'vscode://file//tmp/a.ts', |
| 63 | + })).resolves.toEqual({ started: true }) |
| 64 | + |
| 65 | + expect(assign).not.toHaveBeenCalled() |
| 66 | + expect(fetchMock).toHaveBeenCalledOnce() |
| 67 | + expect(fetchMock.mock.calls[0]?.[0]).toBe('/sidebar/api/open.external') |
| 68 | + }) |
| 69 | + |
| 70 | + it('keeps reveal actions and http(s) lookalikes on the host opener', async () => { |
| 71 | + const assign = vi.fn() |
| 72 | + const fetchMock = hostOk() |
| 73 | + vi.stubGlobal('window', { location: { assign } }) |
| 74 | + vi.stubGlobal('fetch', fetchMock) |
| 75 | + |
| 76 | + await api.openExternal({ action: 'reveal', path: '/tmp/a.ts' }) |
| 77 | + await api.openExternal({ |
| 78 | + action: 'url', |
| 79 | + url: 'https://vscode-remote/ssh-remote+dev/home/u/f.ts', |
| 80 | + }) |
| 81 | + |
| 82 | + expect(assign).not.toHaveBeenCalled() |
| 83 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 84 | + }) |
| 85 | + |
| 86 | + it('returns a rejected promise when client navigation throws', async () => { |
| 87 | + const error = new Error('navigation failed') |
| 88 | + const fetchMock = hostOk() |
| 89 | + vi.stubGlobal('window', { location: { assign: vi.fn(() => { throw error }) } }) |
| 90 | + vi.stubGlobal('fetch', fetchMock) |
| 91 | + |
| 92 | + await expect(api.openExternal({ |
| 93 | + action: 'url', |
| 94 | + url: 'vscode://vscode-remote/ssh-remote+dev/home/u/f.ts', |
| 95 | + })).rejects.toBe(error) |
| 96 | + expect(fetchMock).not.toHaveBeenCalled() |
| 97 | + }) |
| 98 | +}) |
0 commit comments