|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import Notifications from "@/cli/cmd/tui/feature-plugins/system/notifications" |
| 3 | +import type { Event, PermissionRequest, QuestionRequest } from "@opencode-ai/sdk/v2" |
| 4 | +import type { TuiAttentionNotifyInput, TuiPluginApi } from "@opencode-ai/plugin/tui" |
| 5 | + |
| 6 | +class Harness { |
| 7 | + notifications: TuiAttentionNotifyInput[] = [] |
| 8 | + private handlers = new Map<Event["type"], ((event: Event) => void)[]>() |
| 9 | + |
| 10 | + api() { |
| 11 | + return { |
| 12 | + attention: { |
| 13 | + notify: async (input: TuiAttentionNotifyInput) => { |
| 14 | + this.notifications.push(input) |
| 15 | + return { ok: true, notification: true, sound: true } |
| 16 | + }, |
| 17 | + soundboard: { |
| 18 | + registerPack: () => () => {}, |
| 19 | + activate: () => false, |
| 20 | + current: () => "opencode.default", |
| 21 | + list: () => [], |
| 22 | + }, |
| 23 | + }, |
| 24 | + event: { |
| 25 | + on: <Type extends Event["type"]>(type: Type, handler: (event: Extract<Event, { type: Type }>) => void) => { |
| 26 | + const list = this.handlers.get(type) ?? [] |
| 27 | + const wrapped = handler as (event: Event) => void |
| 28 | + list.push(wrapped) |
| 29 | + this.handlers.set(type, list) |
| 30 | + return () => { |
| 31 | + this.handlers.set( |
| 32 | + type, |
| 33 | + (this.handlers.get(type) ?? []).filter((item) => item !== wrapped), |
| 34 | + ) |
| 35 | + } |
| 36 | + }, |
| 37 | + }, |
| 38 | + } as unknown as TuiPluginApi |
| 39 | + } |
| 40 | + |
| 41 | + emit(event: Event) { |
| 42 | + for (const handler of this.handlers.get(event.type) ?? []) handler(event) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function question(id: string): QuestionRequest { |
| 47 | + return { |
| 48 | + id, |
| 49 | + sessionID: "session", |
| 50 | + questions: [], |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function permission(id: string): PermissionRequest { |
| 55 | + return { |
| 56 | + id, |
| 57 | + sessionID: "session", |
| 58 | + permission: "edit", |
| 59 | + patterns: [], |
| 60 | + metadata: {}, |
| 61 | + always: [], |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +async function setup() { |
| 66 | + const harness = new Harness() |
| 67 | + await Notifications.tui(harness.api(), undefined, {} as never) |
| 68 | + return harness |
| 69 | +} |
| 70 | + |
| 71 | +const questionNotification: TuiAttentionNotifyInput = { |
| 72 | + message: "Question needs input", |
| 73 | + notification: { when: "blurred" }, |
| 74 | + sound: { name: "question", when: "always" }, |
| 75 | +} |
| 76 | + |
| 77 | +const permissionNotification: TuiAttentionNotifyInput = { |
| 78 | + message: "Permission needs input", |
| 79 | + notification: { when: "blurred" }, |
| 80 | + sound: { name: "permission", when: "always" }, |
| 81 | +} |
| 82 | + |
| 83 | +describe("internal notifications TUI plugin", () => { |
| 84 | + test("notifies for question and permission requests with blurred notifications and always-on sounds", async () => { |
| 85 | + const harness = await setup() |
| 86 | + |
| 87 | + harness.emit({ id: "event-1", type: "question.asked", properties: question("question-1") }) |
| 88 | + harness.emit({ id: "event-2", type: "permission.asked", properties: permission("permission-1") }) |
| 89 | + |
| 90 | + expect(harness.notifications).toEqual([questionNotification, permissionNotification]) |
| 91 | + }) |
| 92 | + |
| 93 | + test("dedupes pending questions and permissions until they are resolved", async () => { |
| 94 | + const harness = await setup() |
| 95 | + |
| 96 | + harness.emit({ id: "event-1", type: "question.asked", properties: question("question-1") }) |
| 97 | + harness.emit({ id: "event-2", type: "question.asked", properties: question("question-1") }) |
| 98 | + harness.emit({ id: "event-3", type: "question.replied", properties: { sessionID: "session", requestID: "question-1", answers: [] } }) |
| 99 | + harness.emit({ id: "event-4", type: "question.asked", properties: question("question-1") }) |
| 100 | + |
| 101 | + harness.emit({ id: "event-5", type: "permission.asked", properties: permission("permission-1") }) |
| 102 | + harness.emit({ id: "event-6", type: "permission.asked", properties: permission("permission-1") }) |
| 103 | + harness.emit({ |
| 104 | + id: "event-7", |
| 105 | + type: "permission.replied", |
| 106 | + properties: { sessionID: "session", requestID: "permission-1", reply: "once" }, |
| 107 | + }) |
| 108 | + harness.emit({ id: "event-8", type: "permission.asked", properties: permission("permission-1") }) |
| 109 | + |
| 110 | + expect(harness.notifications).toEqual([ |
| 111 | + questionNotification, |
| 112 | + questionNotification, |
| 113 | + permissionNotification, |
| 114 | + permissionNotification, |
| 115 | + ]) |
| 116 | + }) |
| 117 | + |
| 118 | + test("notifies when an active session becomes idle and suppresses no-op idle", async () => { |
| 119 | + const harness = await setup() |
| 120 | + |
| 121 | + harness.emit({ id: "event-1", type: "session.status", properties: { sessionID: "session", status: { type: "idle" } } }) |
| 122 | + harness.emit({ id: "event-2", type: "session.status", properties: { sessionID: "session", status: { type: "busy" } } }) |
| 123 | + harness.emit({ id: "event-3", type: "session.status", properties: { sessionID: "session", status: { type: "idle" } } }) |
| 124 | + |
| 125 | + expect(harness.notifications).toEqual([ |
| 126 | + { |
| 127 | + message: "Session done", |
| 128 | + notification: { when: "blurred" }, |
| 129 | + sound: { name: "done", when: "always" }, |
| 130 | + }, |
| 131 | + ]) |
| 132 | + }) |
| 133 | + |
| 134 | + test("notifies session errors once and suppresses the following idle done notification", async () => { |
| 135 | + const harness = await setup() |
| 136 | + |
| 137 | + harness.emit({ id: "event-1", type: "session.status", properties: { sessionID: "session", status: { type: "busy" } } }) |
| 138 | + harness.emit({ |
| 139 | + id: "event-2", |
| 140 | + type: "session.error", |
| 141 | + properties: { sessionID: "session", error: { name: "UnknownError", data: { message: "boom" } } }, |
| 142 | + }) |
| 143 | + harness.emit({ id: "event-3", type: "session.status", properties: { sessionID: "session", status: { type: "idle" } } }) |
| 144 | + |
| 145 | + expect(harness.notifications).toEqual([ |
| 146 | + { |
| 147 | + message: "Session error", |
| 148 | + notification: { when: "blurred" }, |
| 149 | + sound: { name: "error", when: "always" }, |
| 150 | + }, |
| 151 | + ]) |
| 152 | + }) |
| 153 | + |
| 154 | + test("special-cases aborts and model response timeouts", async () => { |
| 155 | + const harness = await setup() |
| 156 | + |
| 157 | + harness.emit({ id: "event-1", type: "session.status", properties: { sessionID: "abort", status: { type: "busy" } } }) |
| 158 | + harness.emit({ |
| 159 | + id: "event-2", |
| 160 | + type: "session.error", |
| 161 | + properties: { sessionID: "abort", error: { name: "MessageAbortedError", data: { message: "Aborted" } } }, |
| 162 | + }) |
| 163 | + harness.emit({ id: "event-3", type: "session.status", properties: { sessionID: "timeout", status: { type: "busy" } } }) |
| 164 | + harness.emit({ |
| 165 | + id: "event-4", |
| 166 | + type: "session.error", |
| 167 | + properties: { sessionID: "timeout", error: { name: "UnknownError", data: { message: "SSE read timed out" } } }, |
| 168 | + }) |
| 169 | + |
| 170 | + expect(harness.notifications).toEqual([ |
| 171 | + { |
| 172 | + message: "Session aborted", |
| 173 | + notification: { when: "blurred" }, |
| 174 | + sound: { name: "error", when: "always" }, |
| 175 | + }, |
| 176 | + { |
| 177 | + message: "Model stopped responding", |
| 178 | + notification: { when: "blurred" }, |
| 179 | + sound: { name: "error", when: "always" }, |
| 180 | + }, |
| 181 | + ]) |
| 182 | + }) |
| 183 | +}) |
0 commit comments