fix(gateway): canonicalize notification wake session

This commit is contained in:
Ayaan Zaidi
2026-02-28 10:50:47 +05:30
committed by Ayaan Zaidi
parent f1bb26642c
commit a8bcad3db1
2 changed files with 29 additions and 1 deletions

View File

@@ -355,6 +355,8 @@ describe("notifications changed events", () => {
beforeEach(() => {
enqueueSystemEventMock.mockClear();
requestHeartbeatNowMock.mockClear();
loadSessionEntryMock.mockClear();
loadSessionEntryMock.mockImplementation((sessionKey: string) => buildSessionLookup(sessionKey));
});
it("enqueues notifications.changed posted events", async () => {
@@ -418,6 +420,31 @@ describe("notifications changed events", () => {
});
});
it("canonicalizes notifications session key before enqueue and wake", async () => {
loadSessionEntryMock.mockReturnValueOnce({
...buildSessionLookup("node-node-n5"),
canonicalKey: "agent:main:node-node-n5",
});
const ctx = buildCtx();
await handleNodeEvent(ctx, "node-n5", {
event: "notifications.changed",
payloadJSON: JSON.stringify({
change: "posted",
key: "notif-5",
}),
});
expect(loadSessionEntryMock).toHaveBeenCalledWith("node-node-n5");
expect(enqueueSystemEventMock).toHaveBeenCalledWith(
"Notification posted (node=node-n5 key=notif-5)",
{ sessionKey: "agent:main:node-node-n5", contextKey: "notification:notif-5" },
);
expect(requestHeartbeatNowMock).toHaveBeenCalledWith({
reason: "notifications-event",
sessionKey: "agent:main:node-node-n5",
});
});
it("ignores notifications.changed payloads missing required fields", async () => {
const ctx = buildCtx();
await handleNodeEvent(ctx, "node-n3", {

View File

@@ -467,7 +467,8 @@ export const handleNodeEvent = async (ctx: NodeEventContext, nodeId: string, evt
if (!key) {
return;
}
const sessionKey = normalizeNonEmptyString(obj.sessionKey) ?? `node-${nodeId}`;
const sessionKeyRaw = normalizeNonEmptyString(obj.sessionKey) ?? `node-${nodeId}`;
const { canonicalKey: sessionKey } = loadSessionEntry(sessionKeyRaw);
const packageName = normalizeNonEmptyString(obj.packageName);
const title = compactNotificationEventText(normalizeNonEmptyString(obj.title) ?? "");
const text = compactNotificationEventText(normalizeNonEmptyString(obj.text) ?? "");