-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.test.js
More file actions
123 lines (102 loc) · 3.22 KB
/
plugin.test.js
File metadata and controls
123 lines (102 loc) · 3.22 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
import test from "node:test"
import assert from "node:assert/strict"
import { OpencodeBellPlugin } from "../index.js"
const withStdoutStub = async ({ isTTY = true }, run) => {
const originalWrite = process.stdout.write
const originalDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "isTTY")
const writes = []
process.stdout.write = (chunk) => {
writes.push(chunk)
return true
}
Object.defineProperty(process.stdout, "isTTY", {
configurable: true,
value: isTTY
})
try {
await run(writes)
} finally {
process.stdout.write = originalWrite
if (originalDescriptor) {
Object.defineProperty(process.stdout, "isTTY", originalDescriptor)
}
}
}
const withMockedNow = async (times, run) => {
const originalNow = Date.now
let index = 0
Date.now = () => {
const value = times[index]
index += 1
return value
}
try {
await run()
} finally {
Date.now = originalNow
}
}
test("does not throw when event.properties is missing", async () => {
const plugin = await OpencodeBellPlugin()
await assert.doesNotReject(
plugin.event({
event: { type: "permission.asked" }
})
)
})
test("rings once for each supported event type", async () => {
const plugin = await OpencodeBellPlugin()
await withStdoutStub({ isTTY: true }, async (writes) => {
await plugin.event({ event: { type: "permission.asked" } })
await plugin.event({ event: { type: "question.asked" } })
await plugin.event({ event: { type: "session.idle" } })
await plugin.event({ event: { type: "session.error" } })
assert.equal(writes.length, 4)
assert.deepEqual(writes, ["\x07", "\x07", "\x07", "\x07"])
})
})
test("does not ring for unsupported event types", async () => {
const plugin = await OpencodeBellPlugin()
await withStdoutStub({ isTTY: true }, async (writes) => {
await plugin.event({ event: { type: "session.started" } })
assert.equal(writes.length, 0)
})
})
test("does not ring when stdout is not a TTY", async () => {
const plugin = await OpencodeBellPlugin()
await withStdoutStub({ isTTY: false }, async (writes) => {
await plugin.event({ event: { type: "session.idle" } })
assert.equal(writes.length, 0)
})
})
test("debounces repeated events within 1200ms for the same key", async () => {
const plugin = await OpencodeBellPlugin()
await withStdoutStub({ isTTY: true }, async (writes) => {
await withMockedNow([2000, 2500, 3301], async () => {
await plugin.event({ event: { type: "permission.asked" } })
await plugin.event({ event: { type: "permission.asked" } })
await plugin.event({ event: { type: "permission.asked" } })
})
assert.equal(writes.length, 2)
})
})
test("uses session-specific debounce keys", async () => {
const plugin = await OpencodeBellPlugin()
await withStdoutStub({ isTTY: true }, async (writes) => {
await withMockedNow([2000, 2100], async () => {
await plugin.event({
event: {
type: "permission.asked",
properties: { sessionID: "A" }
}
})
await plugin.event({
event: {
type: "permission.asked",
properties: { sessionID: "B" }
}
})
})
assert.equal(writes.length, 2)
})
})