|
| 1 | +const assert = require("node:assert/strict"); |
| 2 | +const {describe, test} = require("node:test"); |
| 3 | + |
| 4 | +const apiModule = require("../../API/api.js"); |
| 5 | + |
| 6 | +describe("updateModuleApiMenu", () => { |
| 7 | + test("generates menu structure from externalApiRoutes", () => { |
| 8 | + const sentNotifications = []; |
| 9 | + const context = { |
| 10 | + thisConfig: {showModuleApiMenu: true}, |
| 11 | + externalApiRoutes: { |
| 12 | + "weather-api": { |
| 13 | + path: "weather-api", |
| 14 | + module: "MMM-Weather", |
| 15 | + actions: { |
| 16 | + UPDATE: {notification: "WEATHER_UPDATE", payload: {}}, |
| 17 | + REFRESH: {notification: "WEATHER_REFRESH"} |
| 18 | + } |
| 19 | + }, |
| 20 | + "calendar-ext": { |
| 21 | + path: "calendar-ext", |
| 22 | + module: "MMM-CalendarExt3", |
| 23 | + actions: { |
| 24 | + NEXT: {notification: "CALENDAR_NEXT"}, |
| 25 | + PREV: {notification: "CALENDAR_PREV"} |
| 26 | + } |
| 27 | + } |
| 28 | + }, |
| 29 | + moduleApiMenu: null, |
| 30 | + translate: (key) => key.replace("%%TRANSLATE:", "").replace("%%", ""), |
| 31 | + sendSocketNotification: (notification, payload) => { |
| 32 | + sentNotifications.push({notification, payload}); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + apiModule.updateModuleApiMenu.call(context); |
| 37 | + |
| 38 | + assert.ok(context.moduleApiMenu, "moduleApiMenu should be created"); |
| 39 | + assert.equal(context.moduleApiMenu.id, "module-control"); |
| 40 | + assert.equal(context.moduleApiMenu.type, "menu"); |
| 41 | + assert.ok(Array.isArray(context.moduleApiMenu.items), "should have items array"); |
| 42 | + assert.equal(context.moduleApiMenu.items.length, 2, "should have 2 module submenus"); |
| 43 | + |
| 44 | + // Check first submenu (MMM-Weather) |
| 45 | + const weatherMenu = context.moduleApiMenu.items[0]; |
| 46 | + assert.equal(weatherMenu.id, "mc-weather-api"); |
| 47 | + assert.equal(weatherMenu.type, "menu"); |
| 48 | + assert.equal(weatherMenu.text, "MMM-Weather", "module name should be unchanged"); |
| 49 | + assert.equal(weatherMenu.items.length, 2, "should have 2 actions"); |
| 50 | + |
| 51 | + // Check action items |
| 52 | + const updateAction = weatherMenu.items[0]; |
| 53 | + assert.equal(updateAction.id, "mc-weather-api-UPDATE"); |
| 54 | + assert.equal(updateAction.action, "NOTIFICATION"); |
| 55 | + assert.deepEqual(updateAction.content, {notification: "WEATHER_UPDATE", payload: {}}); |
| 56 | + |
| 57 | + // Check notification was sent |
| 58 | + assert.equal(sentNotifications.length, 1); |
| 59 | + assert.equal(sentNotifications[0].notification, "REMOTE_CLIENT_MODULEAPI_MENU"); |
| 60 | + }); |
| 61 | + |
| 62 | + test("handles empty externalApiRoutes", () => { |
| 63 | + const context = { |
| 64 | + thisConfig: {showModuleApiMenu: true}, |
| 65 | + externalApiRoutes: {}, |
| 66 | + moduleApiMenu: null, |
| 67 | + translate: (key) => key, |
| 68 | + sendSocketNotification: () => {} |
| 69 | + }; |
| 70 | + |
| 71 | + apiModule.updateModuleApiMenu.call(context); |
| 72 | + |
| 73 | + assert.ok(context.moduleApiMenu, "moduleApiMenu should still be created"); |
| 74 | + assert.equal(context.moduleApiMenu.items.length, 0, "should have no items"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("skips when showModuleApiMenu is false", () => { |
| 78 | + const context = { |
| 79 | + thisConfig: {showModuleApiMenu: false}, |
| 80 | + externalApiRoutes: {test: {path: "test", module: "MMM-Test", actions: {}}}, |
| 81 | + moduleApiMenu: null |
| 82 | + }; |
| 83 | + |
| 84 | + apiModule.updateModuleApiMenu.call(context); |
| 85 | + |
| 86 | + assert.equal(context.moduleApiMenu, null, "should not create menu when disabled"); |
| 87 | + }); |
| 88 | + |
| 89 | + test("preserves module name unchanged", () => { |
| 90 | + const context = { |
| 91 | + thisConfig: {showModuleApiMenu: true}, |
| 92 | + externalApiRoutes: { |
| 93 | + test: { |
| 94 | + path: "test", |
| 95 | + module: "MMM-PublicTransportHafas", |
| 96 | + actions: {TEST: {notification: "TEST"}} |
| 97 | + } |
| 98 | + }, |
| 99 | + moduleApiMenu: null, |
| 100 | + translate: (key) => key, |
| 101 | + sendSocketNotification: () => {} |
| 102 | + }; |
| 103 | + |
| 104 | + apiModule.updateModuleApiMenu.call(context); |
| 105 | + |
| 106 | + const menu = context.moduleApiMenu.items[0]; |
| 107 | + assert.equal(menu.text, "MMM-PublicTransportHafas", "should keep module name unchanged"); |
| 108 | + }); |
| 109 | +}); |
0 commit comments