forked from manaflow-ai/cmux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_opencode_plugin_runtime.js
More file actions
216 lines (194 loc) · 6.4 KB
/
test_opencode_plugin_runtime.js
File metadata and controls
216 lines (194 loc) · 6.4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env node
const fs = require("fs")
const os = require("os")
const path = require("path")
const { pathToFileURL } = require("url")
function fail(message) {
console.error(`FAIL: ${message}`)
process.exit(1)
}
function expect(condition, message) {
if (!condition) {
fail(message)
}
}
function render(strings, values) {
let output = ""
for (let index = 0; index < strings.length; index += 1) {
output += strings[index]
if (index < values.length) {
output += String(values[index])
}
}
return output.replace(/\s+/g, " ").trim()
}
function includes(commands, expected) {
return commands.some((command) => command === expected)
}
function starts(commands, expected) {
return commands.some((command) => command.startsWith(expected))
}
async function main() {
const source = path.join(__dirname, "..", "Resources", "bin", "opencode-cmux-plugin.js")
const copy = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "cmux-opencode-plugin-")), "plugin.mjs")
fs.copyFileSync(source, copy)
const mod = await import(pathToFileURL(copy).href)
const plugin = mod.CmuxIntegrationPlugin || mod.default
expect(typeof plugin === "function", "expected an importable OpenCode cmux plugin function")
const commands = []
const $ = (strings, ...values) => {
commands.push(render(strings, values))
const promise = Promise.resolve()
promise.quiet = () => promise
promise.nothrow = () => promise
promise.text = () => Promise.resolve("")
return promise
}
const hooks = await plugin({ $ })
expect(hooks && typeof hooks.event === "function", "expected an event hook from the OpenCode cmux plugin")
async function emit(event) {
await hooks.event({ event })
const snapshot = [...commands]
commands.length = 0
return snapshot
}
let output = await emit({
type: "session.created",
properties: { info: { id: "s1" } },
})
expect(
starts(output, "cmux set-status opencode Idle --icon pause.circle.fill --color #8E8E93 --pid "),
`expected Idle status after session.created, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "session.status",
properties: { sessionID: "s1", status: { type: "busy" } },
})
expect(
starts(output, "cmux set-status opencode Running --icon bolt.fill --color #4C8DFF --pid "),
`expected Running status after busy event, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "permission.asked",
properties: {
id: "p1",
sessionID: "s1",
permission: "bash",
patterns: ["git status"],
metadata: {},
always: [],
},
})
expect(
includes(output, "cmux notify --title OpenCode --subtitle Permission --body Permission required: bash git status"),
`expected permission notification, got ${JSON.stringify(output)}`,
)
expect(
starts(output, "cmux set-status opencode Needs input --icon bell.fill --color #4C8DFF --pid "),
`expected Needs input status for permission prompt, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "session.status",
properties: { sessionID: "s1", status: { type: "busy" } },
})
expect(
includes(output, "cmux clear-notifications"),
`expected clear-notifications when work resumes, got ${JSON.stringify(output)}`,
)
expect(
starts(output, "cmux set-status opencode Running --icon bolt.fill --color #4C8DFF --pid "),
`expected Running status after resume, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "session.status",
properties: {
sessionID: "s1",
status: { type: "retry", attempt: 2, message: "Rate limited", next: Date.now() + 1000 },
},
})
expect(
starts(output, "cmux set-status opencode Retrying --icon arrow.triangle.2.circlepath --color #FF9500 --pid "),
`expected Retrying status, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "session.idle",
properties: { sessionID: "s1" },
})
expect(
starts(output, "cmux set-status opencode Idle --icon pause.circle.fill --color #8E8E93 --pid "),
`expected Idle status after session.idle, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "question.asked",
properties: {
id: "q1",
sessionID: "s1",
questions: [
{
question: "Continue with deploy?",
header: "Deploy",
options: [
{ label: "Yes", description: "Continue" },
{ label: "No", description: "Stop" },
],
},
],
},
})
expect(
includes(output, "cmux notify --title OpenCode --subtitle Question --body Continue with deploy?"),
`expected question notification, got ${JSON.stringify(output)}`,
)
expect(
starts(output, "cmux set-status opencode Needs input --icon bell.fill --color #4C8DFF --pid "),
`expected Needs input status for question, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "permission.updated",
properties: {
id: "legacy-permission",
type: "edit",
pattern: ["src/app.ts"],
sessionID: "s1",
messageID: "m1",
message: "Allow editing src/app.ts",
metadata: {},
time: { created: Date.now() },
},
})
expect(
includes(output, "cmux notify --title OpenCode --subtitle Permission --body Allow editing src/app.ts"),
`expected legacy permission notification, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "session.error",
properties: {
sessionID: "s1",
error: { message: "Boom" },
},
})
expect(
includes(output, "cmux notify --title OpenCode --subtitle Error --body Boom"),
`expected error notification, got ${JSON.stringify(output)}`,
)
expect(
starts(output, "cmux set-status opencode Error --icon exclamationmark.triangle.fill --color #FF3B30 --pid "),
`expected Error status, got ${JSON.stringify(output)}`,
)
output = await emit({
type: "session.deleted",
properties: { info: { id: "s1" } },
})
expect(
includes(output, "cmux clear-notifications"),
`expected notifications to clear when session disappears, got ${JSON.stringify(output)}`,
)
expect(
includes(output, "cmux clear-status opencode"),
`expected status to clear when session disappears, got ${JSON.stringify(output)}`,
)
console.log("PASS: OpenCode cmux plugin maps session events to sidebar status + notifications")
}
main().catch((error) => {
fail(error instanceof Error ? error.message : String(error))
})