|
1 | 1 | import type { DevframeHubContext } from '../context' |
2 | 2 | import { describe, expect, it, vi } from 'vitest' |
3 | | -import { hubDocksActivate, hubTerminalsResize, hubTerminalsWrite } from '../rpc-builtins' |
| 3 | +import { |
| 4 | + hubDocksActivate, |
| 5 | + hubTerminalsRemove, |
| 6 | + hubTerminalsResize, |
| 7 | + hubTerminalsRestart, |
| 8 | + hubTerminalsTerminate, |
| 9 | + hubTerminalsWrite, |
| 10 | +} from '../rpc-builtins' |
4 | 11 |
|
5 | 12 | function contextWithSessions(sessions: Map<string, any>): DevframeHubContext { |
6 | 13 | return { terminals: { sessions } } as unknown as DevframeHubContext |
@@ -38,6 +45,80 @@ describe('hub terminal write/resize RPC', () => { |
38 | 45 | }) |
39 | 46 | }) |
40 | 47 |
|
| 48 | +describe('hub terminal terminate/restart/remove RPC', () => { |
| 49 | + it('terminates a controllable (child-process or pty) session', async () => { |
| 50 | + const terminate = vi.fn() |
| 51 | + const ctx = contextWithSessions(new Map([ |
| 52 | + ['log', { id: 'log', type: 'child-process', terminate }], |
| 53 | + ])) |
| 54 | + const fn = await hubTerminalsTerminate.setup!(ctx) |
| 55 | + await fn.handler!('log') |
| 56 | + expect(terminate).toHaveBeenCalledOnce() |
| 57 | + }) |
| 58 | + |
| 59 | + it('restarts a restartable session', async () => { |
| 60 | + const restart = vi.fn() |
| 61 | + const ctx = contextWithSessions(new Map([ |
| 62 | + ['log', { id: 'log', type: 'child-process', terminate: vi.fn(), restart }], |
| 63 | + ])) |
| 64 | + const fn = await hubTerminalsRestart.setup!(ctx) |
| 65 | + await fn.handler!('log') |
| 66 | + expect(restart).toHaveBeenCalledOnce() |
| 67 | + }) |
| 68 | + |
| 69 | + it('rejects restarting a session marked restartable: false', async () => { |
| 70 | + const restart = vi.fn() |
| 71 | + const ctx = contextWithSessions(new Map([ |
| 72 | + ['svc', { id: 'svc', type: 'child-process', terminate: vi.fn(), restart, restartable: false }], |
| 73 | + ])) |
| 74 | + const fn = await hubTerminalsRestart.setup!(ctx) |
| 75 | + await expect(fn.handler!('svc')).rejects.toThrow(/not restartable/i) |
| 76 | + expect(restart).not.toHaveBeenCalled() |
| 77 | + }) |
| 78 | + |
| 79 | + it('rejects controlling a session with no lifecycle handle', async () => { |
| 80 | + const ctx = contextWithSessions(new Map([ |
| 81 | + ['bare', { id: 'bare' }], |
| 82 | + ])) |
| 83 | + const fn = await hubTerminalsTerminate.setup!(ctx) |
| 84 | + await expect(fn.handler!('bare')).rejects.toThrow(/cannot be controlled/i) |
| 85 | + }) |
| 86 | + |
| 87 | + it('kills then drops a session on remove', async () => { |
| 88 | + const terminate = vi.fn() |
| 89 | + const remove = vi.fn() |
| 90 | + const session = { id: 'log', type: 'child-process', terminate } |
| 91 | + const ctx = { |
| 92 | + terminals: { sessions: new Map([['log', session]]), remove }, |
| 93 | + } as unknown as DevframeHubContext |
| 94 | + const fn = await hubTerminalsRemove.setup!(ctx) |
| 95 | + await fn.handler!('log') |
| 96 | + expect(terminate).toHaveBeenCalledOnce() |
| 97 | + expect(remove).toHaveBeenCalledWith(session) |
| 98 | + }) |
| 99 | + |
| 100 | + it('removes a bare registered session without a terminate handle', async () => { |
| 101 | + const remove = vi.fn() |
| 102 | + const session = { id: 'mirror' } |
| 103 | + const ctx = { |
| 104 | + terminals: { sessions: new Map([['mirror', session]]), remove }, |
| 105 | + } as unknown as DevframeHubContext |
| 106 | + const fn = await hubTerminalsRemove.setup!(ctx) |
| 107 | + await fn.handler!('mirror') |
| 108 | + expect(remove).toHaveBeenCalledWith(session) |
| 109 | + }) |
| 110 | + |
| 111 | + it('rejects removing an unknown session', async () => { |
| 112 | + const remove = vi.fn() |
| 113 | + const ctx = { |
| 114 | + terminals: { sessions: new Map(), remove }, |
| 115 | + } as unknown as DevframeHubContext |
| 116 | + const fn = await hubTerminalsRemove.setup!(ctx) |
| 117 | + await expect(fn.handler!('nope')).rejects.toThrow(/not registered/i) |
| 118 | + expect(remove).not.toHaveBeenCalled() |
| 119 | + }) |
| 120 | +}) |
| 121 | + |
41 | 122 | describe('hub docks activate RPC', () => { |
42 | 123 | it('forwards dockId and params to docks.activate', async () => { |
43 | 124 | const activate = vi.fn() |
|
0 commit comments