Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions desktop/src/features/notifications/lib/desktop.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import assert from "node:assert/strict";
import test from "node:test";

const notifications = [];

class WorkingNotification {
static permission = "granted";

constructor(title, options) {
notifications.push({ title, options });
}

close() {}
}

class ThrowingNotification {
static permission = "granted";

constructor() {
throw new Error("notification backend unavailable");
}
}

globalThis.window = { Notification: ThrowingNotification };

const { sendDesktopNotification } = await import("./desktop.ts");

test("constructor failure is a delivery miss and does not prevent a later notification", async (t) => {
const warnings = [];
t.mock.method(console, "warn", (...args) => warnings.push(args));

const failed = await sendDesktopNotification({ title: "First" });

assert.equal(failed, false);
assert.equal(warnings.length, 1);
assert.match(String(warnings[0][1]), /notification backend unavailable/);

window.Notification = WorkingNotification;

const delivered = await sendDesktopNotification({
title: "Second",
body: "Recovered",
});

assert.equal(delivered, true);
assert.deepEqual(notifications, [
{
title: "Second",
options: { body: "Recovered", silent: true, extra: undefined },
},
]);
});
41 changes: 27 additions & 14 deletions desktop/src/features/notifications/lib/desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,32 @@ export async function sendDesktopNotification(
}
}

const notification = new window.Notification(payload.title, {
body: payload.body,
silent: true,
extra: notificationExtra(payload.target),
} as DesktopNotificationOptions);

const target = payload.target;
if (!isTauri() && target) {
notification.onclick = () => {
dispatchDesktopNotificationTarget(target);
notification.close();
};
}
// block/buzz#5081 — WebKit throws `NotificationError` from the constructor
// when the notification backend becomes temporarily unavailable. Callers
// discard the returned promise without a rejection handler, so an
// un-guarded throw becomes an unhandled rejection. Treat constructor failure
// as a delivery miss (return false) and log the failed delivery.
try {
const notification = new window.Notification(payload.title, {
body: payload.body,
silent: true,
extra: notificationExtra(payload.target),
} as DesktopNotificationOptions);

const target = payload.target;
if (!isTauri() && target) {
notification.onclick = () => {
dispatchDesktopNotificationTarget(target);
notification.close();
};
}

return true;
return true;
} catch (error) {
console.warn(
"[desktop] window.Notification constructor threw — notification dropped:",
error,
);
return false;
}
}
Loading