From 6bab5b4405c9adafb2ce76540595a62eea80acc0 Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Fri, 7 Nov 2025 21:39:39 -0500
Subject: De minimis "send me a notification" implementation.

When a user clicks "send a test notification," Pilcrow delivers a push message (with a fixed payload) to all active subscriptions. The included client then displays this as a notification, using browser APIs to do so. This lets us verify that push notification works, end to end - and it appears to.

The API endpoint for sending a test notification is not documented. I didn't feel it prudent to extensively document an endpoint that is intended to be temporary and whose side effects are very much subject to change. However, for posterity, the endpoint is

    POST /api/push/ping

    {}

and the push message payload is

    ping

Subscriptions with permanent delivery failures are nuked when we encounter them. Subscriptions with temporary failures cause the `ping` endpoint to return an internal server error, and are not retried. We'll likely want retry logic - including retry logic to handle server restarts - for any more serious use, but for a smoke test, giving up immediately is fine.

To make the push implementation testable, `App` is now generic over it. Tests use a dummy implementation that stores sent messages in memory. This has some significant limitations, documented in the test suite, but it beats sending real notifications to nowhere in tests.
---
 ui/lib/components/PushSubscription.svelte | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

(limited to 'ui/lib/components/PushSubscription.svelte')

diff --git a/ui/lib/components/PushSubscription.svelte b/ui/lib/components/PushSubscription.svelte
index a85cbb3..aab4929 100644
--- a/ui/lib/components/PushSubscription.svelte
+++ b/ui/lib/components/PushSubscription.svelte
@@ -1,5 +1,5 @@
 <script>
-  let { vapid, subscription, subscribe = async () => null } = $props();
+  let { vapid, subscription, subscribe = async () => null, ping = async () => null } = $props();
   let pending = $state(false);
 
   function onsubmit(callback) {
@@ -16,12 +16,15 @@
   }
 </script>
 
-{#if vapid !== null}
-  {#if subscription === null}
+{#if !!vapid}
+  {#if !subscription}
     <form class="form" onsubmit={onsubmit(subscribe)}>
       <button disabled={pending} type="submit">create push subscription</button>
     </form>
   {/if}
+  <form class="form" onsubmit={onsubmit(ping)}>
+    <button disabled={pending} type="submit">send test notification</button>
+  </form>
 {:else}
   Waiting for VAPID key…
 {/if}
-- 
cgit v1.2.3

