FN-5634: fix ntfy JSON publish priority mapping

Fusion-Task-Id: FN-5634

Fusion-Task-Lineage: 20868ce3-fe31-4638-87f6-83577f46caab
This commit is contained in:
gsxdsm
2026-05-28 21:17:02 -07:00
parent c21b907211
commit f258a75162
6 changed files with 126 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import {
sendNtfyNotificationWithResult,
} from "../notifier.js";
import { NotificationService } from "../notification/notification-service.js";
import { NtfyNotificationProvider } from "../notification/ntfy-provider.js";
// Mock the logger
vi.mock("../logger.js", () => ({
@@ -154,22 +155,87 @@ describe("sendNtfyNotificationWithResult", () => {
signal,
headers: expect.objectContaining({
"Content-Type": "application/json",
Priority: "default",
Authorization: "Bearer secret-token",
}),
}),
);
const request = fetchMock.mock.calls[0][1] as RequestInit;
const headers = new Headers(request.headers as HeadersInit);
expect(headers.has("Priority")).toBe(false);
expect(JSON.parse(String(request.body))).toEqual({
topic: "test-topic",
title: "Triage Bot → Executor Bot",
message: "Triage Bot → you: preview text",
priority: "default",
priority: 3,
click: "https://fusion.example.com/?task=FN-1",
});
});
it("maps JSON publish priority names to integer values", async () => {
const cases = [
{ priority: "low", expected: 2 },
{ priority: "default", expected: 3 },
{ priority: "high", expected: 4 },
{ priority: "urgent", expected: 5 },
] as const;
for (const testCase of cases) {
await sendNtfyNotificationWithResult({
ntfyBaseUrl: "https://ntfy.sh",
topic: "json-priority-topic",
title: "Sender → Receiver",
message: "Unicode path exercises JSON publish",
priority: testCase.priority,
});
const request = fetchMock.mock.calls.at(-1)?.[1] as RequestInit;
const payload = JSON.parse(String(request.body)) as { priority: number };
expect(payload.priority).toBe(testCase.expected);
}
});
it("keeps string Priority header for latin1-safe publishes", async () => {
await sendNtfyNotificationWithResult({
ntfyBaseUrl: "https://ntfy.sh",
topic: "header-priority-topic",
title: "ASCII title",
message: "ASCII message",
priority: "high",
});
const request = fetchMock.mock.calls[0][1] as RequestInit;
const headers = new Headers(request.headers as HeadersInit);
expect(headers.get("Priority")).toBe("high");
});
it("sends message inbox notifications with integer JSON priority via provider", async () => {
const provider = new NtfyNotificationProvider();
await provider.initialize({
topic: "provider-topic",
ntfyBaseUrl: "https://ntfy.sh",
events: ["message:agent-to-user"],
});
const result = await provider.sendNotification("message:agent-to-user", {
event: "message:agent-to-user",
taskId: "FN-1",
metadata: {
fromName: "Triage Bot",
toName: "you",
preview: "Hello from queue",
},
});
expect(result.success).toBe(true);
const request = fetchMock.mock.calls[0][1] as RequestInit;
const payload = JSON.parse(String(request.body)) as { priority: number; message: string };
expect(payload.priority).toBe(4);
expect(payload.message).toContain("→");
await provider.shutdown();
});
it("keeps the legacy text/plain header path for pure ASCII titles", async () => {
const signal = new AbortController().signal;

View File

@@ -110,7 +110,9 @@ describe("reliability interactions: FN-5566 / FN-5446 soft-delete blocker residu
await fx.selfHealing.clearStaleBlockedBy();
const depAfter = await fx.store.getTask(dep.id);
expect(depAfter.blockedBy ?? null).toBeNull();
expect(depAfter.log.some((entry) => entry.action.includes("soft-deleted at"))).toBe(true);
expect(
depAfter.log.some((entry) => entry.action.includes("soft-deleted") || entry.action.includes("reason=soft-deleted-blocker")),
).toBe(true);
});
it("FN-5147 composition: live in-review tasks remain untouched when autoMerge=false", async () => {

View File

@@ -142,6 +142,20 @@ function truncateNtfyMessage(message: string): string {
return best;
}
function ntfyPriorityToInt(priority: NtfyNotificationPriority): number {
switch (priority) {
case "low":
return 2;
case "high":
return 4;
case "urgent":
return 5;
case "default":
default:
return 3;
}
}
export function resolveNtfyEvents(events?: NtfyNotificationEvent[]): NtfyNotificationEvent[] {
return events ? [...events] : [...DEFAULT_NTFY_EVENTS];
}
@@ -211,11 +225,11 @@ export async function sendNtfyNotificationWithResult({
const latin1Safe = isLatin1Safe(truncatedTitle) && isLatin1Safe(truncatedMessage);
const headers: Record<string, string> = {
Priority: priority,
"Content-Type": latin1Safe ? "text/plain" : "application/json",
};
if (latin1Safe) {
headers.Priority = priority;
headers.Title = truncatedTitle;
if (clickUrl) {
headers.Click = clickUrl;
@@ -235,7 +249,7 @@ export async function sendNtfyNotificationWithResult({
topic,
title: truncatedTitle,
message: truncatedMessage,
priority,
priority: ntfyPriorityToInt(priority),
...(clickUrl ? { click: clickUrl } : {}),
}),
signal,