'use strict'; const path = require('path'); const { runIpcSelfTests } = require('../../test-fixtures/ipc-selftest.js'); // The production service, with its real NotificationServer on a private bus. // serviceProof: replace-by-id history rows, toast eligibility and timeouts, do not // disturb pruning, toast dismissal, reply failures, read state and removal. // historyBindingProof: a binding on groups(), as the history panel holds it, follows // an in-place update of a live native object and every history mutation. runIpcSelfTests({ files: [path.join(__dirname, 'Notifications.qml')], body: ` Notifications { id: notifications } QtObject { id: nativeStandIn property string summary: "native" property string body: "" property string appName: "probe" property string appIcon: "" property string image: "" property int urgency: 1 } property var boundGroups: notifications.groups() function serviceProof() { const failures = []; const expect = (condition, message) => { if (!condition) failures.push(message); }; let list = notifications.mergeRow([], { "id": "7", "summary": "10%", "closed": false, "read": false }); list = notifications.mergeRow(list, { "id": "7", "summary": "50%", "closed": false, "read": false }); expect(list.length === 1, "replace-by-id should not duplicate"); expect(list[0].summary === "50%", "replace-by-id should update content"); list = notifications.mergeRow(list, { "id": "8", "summary": "other", "closed": false, "read": false }); expect(list.length === 2, "distinct ids should be separate rows"); let read = notifications.mergeRow([], { "id": "9", "summary": "x", "closed": false, "read": true }); read = notifications.mergeRow(read, { "id": "9", "summary": "y", "closed": false, "read": false }); expect(read[0].read === true, "replace-by-id should preserve original read flag"); expect(notifications.toastEligible({ "urgency": "normal" }, false) === true, "normal toasts when DND off"); expect(notifications.toastEligible({ "urgency": "normal" }, true) === false, "normal suppressed under DND"); expect(notifications.toastEligible({ "urgency": "critical" }, true) === true, "critical bypasses DND"); expect(notifications.toastTimeoutMs({ "urgency": "critical", "expireTimeout": 5 }) === 0, "critical is sticky"); expect(notifications.toastTimeoutMs({ "urgency": "normal", "expireTimeout": 0 }) === 0, "expireTimeout 0 is sticky"); expect(notifications.toastTimeoutMs({ "urgency": "normal", "expireTimeout": -1 }) === notifications.defaultToastTimeoutMs, "default timeout"); notifications.dnd = false; notifications.notifications = [{ "id": "history", "nativeId": "native-history", "urgency": "normal", "closed": false, "read": false }]; notifications.nativeNotifications = { "native-normal": ({ "summary": "normal" }), "native-critical": ({ "summary": "critical" }), "native-history": ({ "summary": "history" }) }; notifications.activeToasts = ["normal-alias", "critical"]; notifications.toastById = { "normal-alias": ({ "id": "normal-alias", "nativeId": "native-normal", "urgency": "normal", "transient": true }), "critical": ({ "id": "critical", "nativeId": "native-critical", "urgency": "critical", "transient": true }) }; notifications.toggleDnd(); expect(notifications.activeToasts.length === 1 && notifications.activeToasts[0] === "critical", "DND prunes only normal toasts"); expect(!!notifications.nativeNotifications["native-critical"], "DND retains critical native references"); expect(!notifications.nativeNotifications["native-normal"], "DND releases transient aliases by native id"); expect(!!notifications.nativeNotifications["native-history"] && notifications.notifications.length === 1, "DND retains history and native lifecycle"); expect(notifications.toastEligible({ "urgency": "normal" }, notifications.dnd) === false, "DND suppresses later normal toasts"); notifications.dismissToasts(); expect(notifications.activeToasts.length === 0 && Object.keys(notifications.toastById).length === 0, "dismissToasts clears critical toasts too"); expect(notifications.notifications.length === 1 && !!notifications.nativeNotifications["native-history"], "dismissToasts preserves history"); notifications.setReplyDraft("history", "keep this"); notifications.sendReply("history", "keep this"); expect(notifications.replyDrafts.history === "keep this", "failed reply preserves draft"); expect(notifications.replyFailures.history === "reply not sent", "failed reply exposes sanitized failure"); notifications.sendReply("missing", "still here"); expect(notifications.replyDrafts.missing === "still here", "missing reply preserves draft"); expect(notifications.replyFailures.missing === "notification no longer available", "missing reply exposes sanitized failure"); notifications.unreadCount = 1; notifications.markAllRead(); expect(notifications.unreadCount === 0 && notifications.notifications[0].read === true, "markAllRead marks history read"); expect(notifications.groups()[0].notifications[0].read === true, "history rows carry the read flag"); notifications.closeNotification(notifications.notifications[0], true); expect(notifications.unreadCount === 0, "closing a read notification keeps the unread count"); expect(notifications.notifications.length === 0, "a closed notification leaves no row behind"); expect(notifications.replyDrafts.history === undefined && notifications.replyFailures.history === undefined, "notification removal clears reply state"); notifications.dnd = false; notifications.discardAllNotifications(); return { "pass": failures.length === 0, "failures": failures }; } function historyBindingProof() { const failures = []; const expect = (condition, message) => { if (!condition) failures.push(message); }; const first = () => boundGroups.length > 0 ? boundGroups[0].visibleNotifications[0] : ({}); notifications.notifications = [{ "id": "live", "nativeId": "live", "appName": "probe", "summary": "stored", "body": "", "time": 1, "urgency": "normal", "actions": [], "closed": false, "read": false }]; notifications.nativeNotifications = { "live": nativeStandIn }; expect(first().summary === "native", "history binding reads the live native object"); nativeStandIn.summary = "replaced"; expect(first().summary === "replaced", "history binding follows an in-place native update"); notifications.setReplyDraft("live", "draft"); expect(first().replyDraft === "draft", "history binding follows reply drafts"); notifications.toggleGroup("probe"); expect(boundGroups[0].expanded === true, "history binding follows group expansion"); notifications.discardAllNotifications(); expect(boundGroups.length === 0, "history binding follows clear all"); nativeStandIn.appName = "constructor"; notifications.notifications = [{ "id": "live", "nativeId": "live", "appName": "constructor", "summary": "stored", "body": "", "time": 1, "urgency": "normal", "actions": [], "closed": false, "read": false }]; notifications.nativeNotifications = { "live": nativeStandIn }; expect(boundGroups.length === 1 && boundGroups[0].appName === "constructor" && boundGroups[0].visibleNotifications.length === 1, "an app named like an object member groups like any other"); notifications.discardAllNotifications(); return { "pass": failures.length === 0, "failures": failures }; }`, methods: [ ['notifications', 'JSON.stringify(serviceProof())'], ['historyBinding', 'JSON.stringify(historyBindingProof())'] ] }); console.log('notifications.test.js: ALL PASS');