@topolo/sdk 0.4.7 → 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,11 +2,23 @@ import { describe, expect, it } from "vitest";
2
2
  import {
3
3
  createTopoloWidgetError,
4
4
  createTopoloWidgetResponse,
5
+ defineTopoloChartWidget,
6
+ defineTopoloNotificationsWidget,
7
+ defineTopoloQuickActionsWidget,
8
+ defineTopoloRecentActivityWidget,
9
+ defineTopoloStatsWidget,
5
10
  TOPOLO_WIDGET_ENDPOINT,
6
11
  TOPOLO_WIDGET_REFRESH_INTERVAL_MS,
7
12
  validateTopoloWidgetResponse,
8
13
  } from "./widgets.js";
9
14
 
15
+ const BASE = {
16
+ serviceId: "service_x",
17
+ serviceName: "X",
18
+ lastUpdated: "2026-04-25T00:00:00.000Z",
19
+ snapshot: { summary: "A summary that is long enough to read." },
20
+ };
21
+
10
22
  describe("Topolo widget contract", () => {
11
23
  it("exports the platform widget endpoint defaults", () => {
12
24
  expect(TOPOLO_WIDGET_ENDPOINT).toBe("/api/widget");
@@ -177,6 +189,210 @@ describe("Topolo widget contract", () => {
177
189
  ]);
178
190
  });
179
191
 
192
+ it("stamps the literal type onto each define* helper", () => {
193
+ expect(defineTopoloStatsWidget({ ...BASE, stats: [] }).type).toBe("stats");
194
+ expect(
195
+ defineTopoloRecentActivityWidget({ ...BASE, activities: [] }).type,
196
+ ).toBe("recent-activity");
197
+ expect(
198
+ defineTopoloQuickActionsWidget({ ...BASE, actions: [] }).type,
199
+ ).toBe("quick-actions");
200
+ expect(
201
+ defineTopoloNotificationsWidget({ ...BASE, notifications: [], unreadCount: 0 })
202
+ .type,
203
+ ).toBe("notifications");
204
+ expect(
205
+ defineTopoloChartWidget({
206
+ ...BASE,
207
+ chartType: "line",
208
+ title: "Trend",
209
+ data: { labels: ["a"], values: [1] },
210
+ }).type,
211
+ ).toBe("chart");
212
+ });
213
+
214
+ it("derives lastUpdated from a Date instance passed as now", () => {
215
+ const response = createTopoloWidgetResponse({
216
+ serviceId: "service_x",
217
+ serviceName: "X",
218
+ now: new Date("2026-05-01T12:00:00.000Z"),
219
+ widgets: [
220
+ {
221
+ type: "stats",
222
+ snapshot: { summary: "Date branch summary content here." },
223
+ stats: [],
224
+ },
225
+ ],
226
+ });
227
+ expect(response.widgets[0]!.lastUpdated).toBe("2026-05-01T12:00:00.000Z");
228
+ });
229
+
230
+ it("generates a current timestamp when now is omitted", () => {
231
+ const before = Date.now();
232
+ const response = createTopoloWidgetResponse({
233
+ serviceId: "service_x",
234
+ serviceName: "X",
235
+ widgets: [
236
+ {
237
+ type: "stats",
238
+ snapshot: { summary: "Omitted-now summary content here." },
239
+ stats: [],
240
+ },
241
+ ],
242
+ });
243
+ const stamped = Date.parse(response.widgets[0]!.lastUpdated);
244
+ expect(stamped).toBeGreaterThanOrEqual(before);
245
+ expect(Number.isNaN(stamped)).toBe(false);
246
+ });
247
+
248
+ it("rejects a non-object response", () => {
249
+ expect(validateTopoloWidgetResponse(null)).toEqual({
250
+ ok: false,
251
+ errors: ["response must be an object"],
252
+ });
253
+ });
254
+
255
+ it("rejects a non-boolean success and a non-array widgets field", () => {
256
+ const result = validateTopoloWidgetResponse({ success: "yes", widgets: "nope" });
257
+ expect(result.ok).toBe(false);
258
+ expect(result.errors).toContain("success must be a boolean");
259
+ expect(result.errors).toContain("widgets must be an array");
260
+ });
261
+
262
+ it("flags a non-string error on a failed response", () => {
263
+ const result = validateTopoloWidgetResponse({
264
+ success: false,
265
+ widgets: [],
266
+ error: 42,
267
+ });
268
+ expect(result.errors).toContain("error must be a string when provided");
269
+ });
270
+
271
+ it("flags a chart widget whose data is not an object", () => {
272
+ const result = validateTopoloWidgetResponse({
273
+ success: true,
274
+ widgets: [
275
+ {
276
+ ...BASE,
277
+ type: "chart",
278
+ chartType: "bar",
279
+ title: "T",
280
+ data: "not-an-object",
281
+ },
282
+ ],
283
+ });
284
+ expect(result.errors).toContain("widgets[0].data must be an object");
285
+ });
286
+
287
+ it("flags non-number unreadCount and missing notifications array", () => {
288
+ const result = validateTopoloWidgetResponse({
289
+ success: true,
290
+ widgets: [
291
+ {
292
+ ...BASE,
293
+ type: "notifications",
294
+ notifications: "x",
295
+ unreadCount: "many",
296
+ },
297
+ ],
298
+ });
299
+ expect(result.errors).toContain("widgets[0].notifications must be an array");
300
+ expect(result.errors).toContain("widgets[0].unreadCount must be a number");
301
+ });
302
+
303
+ it("flags malformed snapshot metadata arrays and metric entries", () => {
304
+ const result = validateTopoloWidgetResponse({
305
+ success: true,
306
+ widgets: [
307
+ {
308
+ ...BASE,
309
+ snapshot: {
310
+ summary: "Snapshot with bad metadata arrays present here.",
311
+ defaultMetricKeys: "not-array",
312
+ highlights: "not-array",
313
+ availableMetrics: [{ key: "", label: "", value: "NaN" }],
314
+ },
315
+ type: "stats",
316
+ stats: [],
317
+ },
318
+ ],
319
+ });
320
+ expect(result.errors).toContain(
321
+ "widgets[0].snapshot.defaultMetricKeys must be an array when provided",
322
+ );
323
+ expect(result.errors).toContain(
324
+ "widgets[0].snapshot.highlights must be an array when provided",
325
+ );
326
+ expect(result.errors).toContain(
327
+ "widgets[0].snapshot.availableMetrics[0].key must be a non-empty string",
328
+ );
329
+ expect(result.errors).toContain(
330
+ "widgets[0].snapshot.availableMetrics[0].value must be a number",
331
+ );
332
+ });
333
+
334
+ it("flags availableMetrics when it is present but not an array", () => {
335
+ const result = validateTopoloWidgetResponse({
336
+ success: true,
337
+ widgets: [
338
+ {
339
+ ...BASE,
340
+ snapshot: {
341
+ summary: "Snapshot summary with bad availableMetrics field.",
342
+ availableMetrics: "nope",
343
+ },
344
+ type: "stats",
345
+ stats: [],
346
+ },
347
+ ],
348
+ });
349
+ expect(result.errors).toContain(
350
+ "widgets[0].snapshot.availableMetrics must be an array when provided",
351
+ );
352
+ });
353
+
354
+ it("flags a non-object widget entry and a missing snapshot", () => {
355
+ const result = validateTopoloWidgetResponse({
356
+ success: true,
357
+ widgets: [42],
358
+ });
359
+ expect(result.errors).toContain("widgets[0] must be an object");
360
+
361
+ const result2 = validateTopoloWidgetResponse({
362
+ success: true,
363
+ widgets: [
364
+ {
365
+ serviceId: "s",
366
+ serviceName: "n",
367
+ lastUpdated: "2026-04-25T00:00:00.000Z",
368
+ type: "stats",
369
+ stats: [],
370
+ },
371
+ ],
372
+ });
373
+ expect(result2.errors).toContain("widgets[0].snapshot must be an object");
374
+ });
375
+
376
+ it("flags a non-object availableMetrics entry", () => {
377
+ const result = validateTopoloWidgetResponse({
378
+ success: true,
379
+ widgets: [
380
+ {
381
+ ...BASE,
382
+ snapshot: {
383
+ summary: "Snapshot with a non-object metric entry here.",
384
+ availableMetrics: [42],
385
+ },
386
+ type: "stats",
387
+ stats: [],
388
+ },
389
+ ],
390
+ });
391
+ expect(result.errors).toContain(
392
+ "widgets[0].snapshot.availableMetrics[0] must be an object",
393
+ );
394
+ });
395
+
180
396
  it("reports missing snapshot summaries", () => {
181
397
  const result = validateTopoloWidgetResponse({
182
398
  success: true,