-
-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathapiMachine.test.ts
More file actions
146 lines (125 loc) · 5.37 KB
/
Copy pathapiMachine.test.ts
File metadata and controls
146 lines (125 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { mkdtempSync, rmSync, mkdirSync, realpathSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const ioMock = vi.hoisted(() => vi.fn())
const listOpencodeModelsForCwdMock = vi.hoisted(() => vi.fn())
vi.mock('socket.io-client', () => ({
io: ioMock
}))
vi.mock('@/api/auth', () => ({
getAuthToken: () => 'cli-token'
}))
vi.mock('../modules/common/opencodeModels', () => ({
listOpencodeModelsForCwd: listOpencodeModelsForCwdMock
}))
import { ApiMachineClient } from './apiMachine'
import type { Machine } from './types'
function makeMachine(id: string): Machine {
return {
id,
namespace: 'default',
seq: 1,
createdAt: 0,
updatedAt: 0,
active: true,
activeAt: 0,
metadata: null,
metadataVersion: 0,
runnerState: null,
runnerStateVersion: 0
}
}
async function callListOpencodeModels(client: ApiMachineClient, machineId: string, cwd: string): Promise<unknown> {
// Reach into the private rpc handler manager to dispatch a request.
// Mirrors how the on-socket 'rpc-request' listener invokes handleRequest.
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
const raw = await manager.handleRequest({
method: `${machineId}:listOpencodeModelsForCwd`,
params: JSON.stringify({ cwd })
})
return JSON.parse(raw) as unknown
}
describe('ApiMachineClient listOpencodeModelsForCwd handler', () => {
let workspaceRoot: string
beforeEach(() => {
ioMock.mockReset()
listOpencodeModelsForCwdMock.mockReset()
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-machine-ws-'))
})
afterEach(() => {
rmSync(workspaceRoot, { recursive: true, force: true })
})
it('rejects cwd outside the workspace root with the standard error shape', async () => {
const machine = makeMachine('machine-1')
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
const outsideCwd = mkdtempSync(join(tmpdir(), 'hapi-outside-'))
try {
const result = await callListOpencodeModels(client, machine.id, outsideCwd)
expect(result).toEqual({ success: false, error: 'Path is outside workspace roots' })
expect(listOpencodeModelsForCwdMock).not.toHaveBeenCalled()
} finally {
rmSync(outsideCwd, { recursive: true, force: true })
client.shutdown()
}
})
it('rejects empty cwd with cwd-required error', async () => {
const machine = makeMachine('machine-2')
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
try {
const result = await callListOpencodeModels(client, machine.id, '')
expect(result).toEqual({ success: false, error: 'cwd is required' })
expect(listOpencodeModelsForCwdMock).not.toHaveBeenCalled()
} finally {
client.shutdown()
}
})
it('forwards a workspace-internal cwd to listOpencodeModelsForCwd', async () => {
const machine = makeMachine('machine-3')
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
const innerDir = join(workspaceRoot, 'inner-project')
mkdirSync(innerDir)
listOpencodeModelsForCwdMock.mockResolvedValueOnce({
success: true,
availableModels: [{ modelId: 'a/b' }],
currentModelId: 'a/b'
})
try {
const result = await callListOpencodeModels(client, machine.id, innerDir)
expect(result).toEqual({
success: true,
availableModels: [{ modelId: 'a/b' }],
currentModelId: 'a/b'
})
expect(listOpencodeModelsForCwdMock).toHaveBeenCalledTimes(1)
// The handler should pass the resolved (realpath'd) cwd to the lower layer.
expect(listOpencodeModelsForCwdMock).toHaveBeenCalledWith(expect.stringContaining('inner-project'))
} finally {
client.shutdown()
}
})
it('accepts cwd inside any configured workspace root', async () => {
const machine = makeMachine('machine-4')
const secondWorkspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-machine-ws-2-'))
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot, secondWorkspaceRoot])
listOpencodeModelsForCwdMock.mockResolvedValueOnce({
success: true,
availableModels: [{ modelId: 'x/y' }],
currentModelId: 'x/y'
})
try {
const result = await callListOpencodeModels(client, machine.id, secondWorkspaceRoot)
expect(result).toEqual({
success: true,
availableModels: [{ modelId: 'x/y' }],
currentModelId: 'x/y'
})
// The handler realpaths the cwd (security: prevents symlink escape),
// so on macOS /var/folders/... resolves to /private/var/folders/...
expect(listOpencodeModelsForCwdMock).toHaveBeenCalledWith(realpathSync(secondWorkspaceRoot))
} finally {
rmSync(secondWorkspaceRoot, { recursive: true, force: true })
client.shutdown()
}
})
})