Debian Codegraph @

web/src/app/notificationUtils.test.js

raw
1 import { describe, expect, it } from "vitest";
2 import emojisMapped from "./emojisMapped";
3 import { formatTitle, formatMessage, isImage, notificationTag, toNotificationParams, messageWithSequenceId } from "./notificationUtils";
4
5 // Pick a real alias/emoji pair so we don't hardcode a specific glyph that could change upstream.
6 const [emojiAlias, emojiChar] = Object.entries(emojisMapped)[0];
7
8 describe("formatTitle", () => {
9 it("returns the bare title when there are no emoji tags", () => {
10 expect(formatTitle({ title: "Hello", tags: ["not-an-emoji"] })).toBe("Hello");
11 expect(formatTitle({ title: "Hello" })).toBe("Hello");
12 });
13
14 it("prepends mapped emoji for emoji tags", () => {
15 expect(formatTitle({ title: "Hello", tags: [emojiAlias] })).toBe(`${emojiChar} Hello`);
16 });
17 });
18
19 describe("formatMessage", () => {
20 it("returns the message untouched when the notification has a title", () => {
21 expect(formatMessage({ title: "T", message: "Body", tags: [emojiAlias] })).toBe("Body");
22 });
23
24 it("prepends emoji to the message when there is no title", () => {
25 expect(formatMessage({ message: "Body", tags: [emojiAlias] })).toBe(`${emojiChar} Body`);
26 });
27
28 it("falls back to an empty string when message is missing", () => {
29 expect(formatMessage({})).toBe("");
30 });
31 });
32
33 describe("isImage", () => {
34 it("trusts an explicit image MIME type", () => {
35 expect(isImage({ type: "image/png", name: "whatever.txt" })).toBe(true);
36 expect(isImage({ type: "application/pdf", name: "x.png" })).toBe(false);
37 });
38
39 it("falls back to the file extension when there is no type", () => {
40 expect(isImage({ name: "photo.JPEG" })).toBeTruthy();
41 expect(isImage({ url: "https://ntfy.sh/file/x.webp" })).toBeTruthy();
42 expect(isImage({ name: "notes.txt" })).toBeFalsy();
43 });
44
45 it("returns false for a missing attachment", () => {
46 expect(isImage(undefined)).toBe(false);
47 });
48 });
49
50 describe("notificationTag", () => {
51 it("scopes the tag by baseUrl, topic and sequence id", () => {
52 expect(notificationTag("https://ntfy.sh", "mytopic", 42)).toBe("https://ntfy.sh/mytopic/42");
53 });
54 });
55
56 describe("toNotificationParams", () => {
57 const baseMessage = {
58 id: "msg-1",
59 time: 1700000000,
60 title: "Title",
61 message: "Body",
62 actions: [
63 { action: "view", label: "Open" },
64 { action: "http", label: "Send" },
65 { action: "broadcast", label: "Cast" },
66 ],
67 };
68
69 it("builds the [title, options] tuple consumed by the Notifications API", () => {
70 const [title, options] = toNotificationParams({
71 message: baseMessage,
72 defaultTitle: "fallback",
73 topicRoute: "/mytopic",
74 baseUrl: "https://ntfy.sh",
75 topic: "mytopic",
76 });
77
78 expect(title).toBe("Title");
79 expect(options.body).toBe("Body");
80 expect(options.tag).toBe("https://ntfy.sh/mytopic/msg-1");
81 expect(options.timestamp).toBe(baseMessage.time * 1000);
82 expect(options.data.subscriptionId).toBe("https://ntfy.sh/mytopic");
83 expect(options.data.topicRoute).toBe("/mytopic");
84 });
85
86 it("keeps only view/http actions and maps them to {action,title}", () => {
87 const [, options] = toNotificationParams({
88 message: baseMessage,
89 defaultTitle: "fallback",
90 topicRoute: "/mytopic",
91 baseUrl: "https://ntfy.sh",
92 topic: "mytopic",
93 });
94 expect(options.actions).toEqual([
95 { action: "Open", title: "Open" },
96 { action: "Send", title: "Send" },
97 ]);
98 });
99
100 it("uses the default title when the message has none", () => {
101 const [title] = toNotificationParams({
102 message: { id: "x", time: 1, message: "Body" },
103 defaultTitle: "fallback",
104 topicRoute: "/mytopic",
105 baseUrl: "https://ntfy.sh",
106 topic: "mytopic",
107 });
108 expect(title).toBe("fallback");
109 });
110
111 it("prefers sequence_id over id for the notification tag", () => {
112 const [, options] = toNotificationParams({
113 message: { ...baseMessage, sequence_id: 99 },
114 defaultTitle: "fallback",
115 topicRoute: "/mytopic",
116 baseUrl: "https://ntfy.sh",
117 topic: "mytopic",
118 });
119 expect(options.tag).toBe("https://ntfy.sh/mytopic/99");
120 });
121 });
122
123 describe("messageWithSequenceId", () => {
124 it("derives sequenceId from sequence_id when absent", () => {
125 expect(messageWithSequenceId({ id: "a", sequence_id: 7 }).sequenceId).toBe(7);
126 });
127
128 it("falls back to id when sequence_id is missing", () => {
129 expect(messageWithSequenceId({ id: "a" }).sequenceId).toBe("a");
130 });
131
132 it("returns the message unchanged when sequenceId already set", () => {
133 const m = { id: "a", sequenceId: 1 };
134 expect(messageWithSequenceId(m)).toBe(m);
135 });
136 });