@topolo/sdk 0.7.0 → 0.8.1

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.
@@ -1,416 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- createTopoloWidgetError,
4
- createTopoloWidgetResponse,
5
- defineTopoloChartWidget,
6
- defineTopoloNotificationsWidget,
7
- defineTopoloQuickActionsWidget,
8
- defineTopoloRecentActivityWidget,
9
- defineTopoloStatsWidget,
10
- TOPOLO_WIDGET_ENDPOINT,
11
- TOPOLO_WIDGET_REFRESH_INTERVAL_MS,
12
- validateTopoloWidgetResponse,
13
- } from "./widgets.js";
14
-
15
- const BASE = {
16
- appId: "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
-
22
- describe("Topolo widget contract", () => {
23
- it("exports the platform widget endpoint defaults", () => {
24
- expect(TOPOLO_WIDGET_ENDPOINT).toBe("/api/widget");
25
- expect(TOPOLO_WIDGET_REFRESH_INTERVAL_MS).toBe(60_000);
26
- });
27
-
28
- it("stamps service metadata and timestamp onto widget drafts", () => {
29
- const response = createTopoloWidgetResponse({
30
- appId: "service_bytes",
31
- serviceName: "Bytes",
32
- now: "2026-04-25T00:00:00.000Z",
33
- widgets: [
34
- {
35
- type: "stats",
36
- snapshot: {
37
- summary: "Bytes is reporting 12 files in the current workspace.",
38
- },
39
- stats: [{ key: "files", label: "Files", value: 12 }],
40
- },
41
- ],
42
- });
43
-
44
- expect(response).toEqual({
45
- success: true,
46
- widgets: [
47
- {
48
- appId: "service_bytes",
49
- serviceName: "Bytes",
50
- lastUpdated: "2026-04-25T00:00:00.000Z",
51
- type: "stats",
52
- snapshot: {
53
- summary: "Bytes is reporting 12 files in the current workspace.",
54
- },
55
- stats: [{ key: "files", label: "Files", value: 12 }],
56
- },
57
- ],
58
- });
59
- });
60
-
61
- it("keeps explicit widget metadata when an app needs multiple service cards", () => {
62
- const response = createTopoloWidgetResponse({
63
- appId: "service_platform",
64
- serviceName: "Platform",
65
- now: "2026-04-25T00:00:00.000Z",
66
- widgets: [
67
- {
68
- appId: "service_crm",
69
- serviceName: "CRM",
70
- lastUpdated: "2026-04-24T23:00:00.000Z",
71
- type: "stats",
72
- snapshot: {
73
- summary: "CRM is tracking 3 contacts for this workspace.",
74
- },
75
- stats: [{ label: "Contacts", value: 3 }],
76
- },
77
- ],
78
- });
79
-
80
- expect(response.widgets[0]).toMatchObject({
81
- appId: "service_crm",
82
- serviceName: "CRM",
83
- lastUpdated: "2026-04-24T23:00:00.000Z",
84
- });
85
- });
86
-
87
- it("returns a stable error response shape", () => {
88
- expect(createTopoloWidgetError("not configured")).toEqual({
89
- success: false,
90
- widgets: [],
91
- error: "not configured",
92
- });
93
- });
94
-
95
- it("validates a compliant response", () => {
96
- const response = createTopoloWidgetResponse({
97
- appId: "service_crm",
98
- serviceName: "CRM",
99
- now: "2026-04-25T00:00:00.000Z",
100
- widgets: [
101
- {
102
- type: "notifications",
103
- snapshot: {
104
- summary: "CRM has one unread lead notification for review.",
105
- },
106
- unreadCount: 1,
107
- notifications: [
108
- {
109
- id: "n_1",
110
- title: "New lead",
111
- message: "A lead was created.",
112
- type: "info",
113
- timestamp: "2026-04-25T00:00:00.000Z",
114
- read: false,
115
- },
116
- ],
117
- },
118
- ],
119
- });
120
-
121
- expect(validateTopoloWidgetResponse(response)).toEqual({
122
- ok: true,
123
- errors: [],
124
- });
125
- });
126
-
127
- it("keeps required snapshot metadata for configurable TopoloOne datapoints", () => {
128
- const response = createTopoloWidgetResponse({
129
- appId: "service_admin",
130
- serviceName: "Admin",
131
- now: "2026-04-28T00:00:00.000Z",
132
- widgets: [
133
- {
134
- type: "stats",
135
- stats: [{ label: "Team Members", value: 5 }],
136
- snapshot: {
137
- presetKey: "admin_morning_snapshot",
138
- summary:
139
- "Admin has 5 team members visible in the current workspace snapshot.",
140
- defaultMetricKeys: ["team_members"],
141
- availableMetrics: [
142
- {
143
- key: "team_members",
144
- label: "Team Members",
145
- value: 5,
146
- unit: "count",
147
- },
148
- ],
149
- },
150
- },
151
- ],
152
- });
153
-
154
- expect(validateTopoloWidgetResponse(response)).toEqual({
155
- ok: true,
156
- errors: [],
157
- });
158
- expect(response.widgets[0]).toMatchObject({
159
- snapshot: {
160
- summary:
161
- "Admin has 5 team members visible in the current workspace snapshot.",
162
- defaultMetricKeys: ["team_members"],
163
- availableMetrics: [
164
- { key: "team_members", label: "Team Members", value: 5 },
165
- ],
166
- },
167
- });
168
- });
169
-
170
- it("reports missing metadata and unsupported widget types", () => {
171
- const result = validateTopoloWidgetResponse({
172
- success: true,
173
- widgets: [
174
- {
175
- appId: "",
176
- serviceName: "Bad App",
177
- lastUpdated: "",
178
- type: "calendar",
179
- },
180
- ],
181
- });
182
-
183
- expect(result.ok).toBe(false);
184
- expect(result.errors).toEqual([
185
- "widgets[0].appId must be a non-empty string",
186
- "widgets[0].lastUpdated must be a non-empty string",
187
- "widgets[0].snapshot must be an object",
188
- "widgets[0].type must be a supported Topolo widget type",
189
- ]);
190
- });
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
- appId: "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
- appId: "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
- appId: "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
-
396
- it("reports missing snapshot summaries", () => {
397
- const result = validateTopoloWidgetResponse({
398
- success: true,
399
- widgets: [
400
- {
401
- appId: "service_bad",
402
- serviceName: "Bad App",
403
- lastUpdated: "2026-04-25T00:00:00.000Z",
404
- type: "stats",
405
- snapshot: { summary: "" },
406
- stats: [],
407
- },
408
- ],
409
- });
410
-
411
- expect(result.ok).toBe(false);
412
- expect(result.errors).toEqual([
413
- "widgets[0].snapshot.summary must be a non-empty string",
414
- ]);
415
- });
416
- });