-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-host-controller.test.ts
More file actions
157 lines (130 loc) · 4.85 KB
/
opencode-host-controller.test.ts
File metadata and controls
157 lines (130 loc) · 4.85 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
147
148
149
150
151
152
153
154
155
156
157
import type { ServiceDefinition, ServiceStatus } from '@xnetjs/plugins/node'
import { describe, expect, it, vi } from 'vitest'
import { createOpenCodeHostController } from '../main/opencode-host-controller'
import {
OPENCODE_SERVICE_ID,
createOpenCodeHostConfig,
type OpenCodeBinaryResolution,
type OpenCodeHostConfig,
type OpenCodeHostStatus
} from '../shared/opencode-host'
const createServiceStatus = (
config: OpenCodeHostConfig,
overrides: Partial<ServiceStatus> = {}
): ServiceStatus => ({
id: OPENCODE_SERVICE_ID,
state: 'running',
port: config.port,
pid: 4242,
startedAt: 1,
restartCount: 0,
...overrides
})
const createBinaryResolution = (path: string): OpenCodeBinaryResolution => ({
found: true,
path,
source: 'path'
})
describe('createOpenCodeHostController', () => {
it('should return a missing-binary status without starting a service', async () => {
const config = createOpenCodeHostConfig({
XNET_OPENCODE_PORT: '4100'
})
const startService = vi.fn<(_: ServiceDefinition) => Promise<ServiceStatus>>()
const publishStatus = vi.fn<(status: OpenCodeHostStatus) => void>()
const controller = createOpenCodeHostController({
getConfig: () => config,
getServiceStatus: () => undefined,
startService,
restartService: vi.fn(),
stopService: vi.fn(),
resolveBinary: vi.fn(async () => ({
found: false,
checkedPaths: [],
error: 'OpenCode CLI was not found on PATH',
recovery: 'Install OpenCode'
})),
probeHealth: vi.fn(async () => null),
publishStatus
})
const status = await controller.ensure()
expect(status.state).toBe('missing-binary')
expect(startService).not.toHaveBeenCalled()
expect(publishStatus).toHaveBeenCalledWith(status)
})
it('should dedupe concurrent ensure calls', async () => {
const config = createOpenCodeHostConfig({
XNET_OPENCODE_PORT: '4101'
})
let serviceStatus: ServiceStatus | undefined
let resolveStart: ((status: ServiceStatus) => void) | null = null
const startService = vi.fn(async (_definition: ServiceDefinition) => {
const nextStatus = await new Promise<ServiceStatus>((resolve) => {
resolveStart = resolve
})
serviceStatus = nextStatus
return nextStatus
})
const controller = createOpenCodeHostController({
getConfig: () => config,
getServiceStatus: () => serviceStatus,
startService,
restartService: vi.fn(),
stopService: vi.fn(),
resolveBinary: vi.fn(async () => createBinaryResolution('/usr/local/bin/opencode')),
probeHealth: vi.fn(async () => ({ healthy: true, version: '1.0.0' })),
publishStatus: vi.fn()
})
const first = controller.ensure()
const second = controller.ensure()
await new Promise((resolve) => setTimeout(resolve, 0))
expect(startService).toHaveBeenCalledTimes(1)
resolveStart?.(createServiceStatus(config))
const [firstStatus, secondStatus] = await Promise.all([first, second])
expect(firstStatus.state).toBe('ready')
expect(secondStatus).toEqual(firstStatus)
expect(startService).toHaveBeenCalledTimes(1)
})
it('should reuse an existing running service', async () => {
const config = createOpenCodeHostConfig({
XNET_OPENCODE_PORT: '4102'
})
const serviceStatus = createServiceStatus(config)
const startService = vi.fn()
const restartService = vi.fn()
const controller = createOpenCodeHostController({
getConfig: () => config,
getServiceStatus: () => serviceStatus,
startService,
restartService,
stopService: vi.fn(),
resolveBinary: vi.fn(async () => createBinaryResolution('/usr/local/bin/opencode')),
probeHealth: vi.fn(async () => ({ healthy: true, version: '1.2.3' })),
publishStatus: vi.fn()
})
const status = await controller.ensure()
expect(status.state).toBe('ready')
expect(status.version).toBe('1.2.3')
expect(startService).not.toHaveBeenCalled()
expect(restartService).not.toHaveBeenCalled()
})
it('should restart a stopped service instead of returning an idle status', async () => {
const config = createOpenCodeHostConfig({
XNET_OPENCODE_PORT: '4103'
})
const restartService = vi.fn(async () => createServiceStatus(config))
const controller = createOpenCodeHostController({
getConfig: () => config,
getServiceStatus: () => createServiceStatus(config, { state: 'stopped' }),
startService: vi.fn(),
restartService,
stopService: vi.fn(),
resolveBinary: vi.fn(async () => createBinaryResolution('/usr/local/bin/opencode')),
probeHealth: vi.fn(async () => ({ healthy: true, version: '1.2.4' })),
publishStatus: vi.fn()
})
const status = await controller.ensure()
expect(status.state).toBe('ready')
expect(restartService).toHaveBeenCalledTimes(1)
})
})