@kiosko/sdk 0.2.5 → 0.2.7

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.
package/src/testing.ts CHANGED
@@ -18,7 +18,7 @@ import { createCanvas, loadImage, type Canvas } from '@napi-rs/canvas';
18
18
  import * as kit from '@loksly/lcd-canvas-kit';
19
19
  import { createBaseContext } from './context.ts';
20
20
  import type { TimeFormat } from './format.ts';
21
- import type { Box, PluginDefinition, RenderContext, StateContext, WidgetDefinition } from './plugin.ts';
21
+ import type { Alert, Box, PluginDefinition, RenderContext, SourceDefinition, StateContext, WidgetDefinition } from './plugin.ts';
22
22
  import { validate } from './schema.ts';
23
23
  import { THEMES, themeById, type Theme } from './theme.ts';
24
24
  import { DEFAULT_WORKDAY } from './workday.ts';
@@ -138,6 +138,37 @@ export function widgetState(w: WidgetDefinition<any, any>, hook: 'urgent' | 'idl
138
138
  return fn(ctx);
139
139
  }
140
140
 
141
+ /** Asks a widget's `alerts` with the same inputs `renderWidget` would use; `[]` when it has none. */
142
+ export function widgetAlerts(w: WidgetDefinition<any, any>, o: RenderOptions = {}): Alert[] {
143
+ if (!w.alerts) return [];
144
+ const { now, options, data, sources } = resolve(w, o);
145
+ return w.alerts({ options, data, sources, now, working: true, workday: DEFAULT_WORKDAY });
146
+ }
147
+
148
+ export interface SourceAlertOptions {
149
+ /** The source's options (validated against its schema, defaults filled in). */
150
+ options?: Record<string, unknown>;
151
+ /** Its latest data. Default: its `demo` data. */
152
+ data?: unknown;
153
+ now?: number;
154
+ /** For its `demo`. */
155
+ locale?: string;
156
+ /** Use the source's `demo` when `data` isn't given. Default true. */
157
+ demo?: boolean;
158
+ }
159
+
160
+ /**
161
+ * Asks a source's `alerts` the way Kiosko does: its options validated with
162
+ * their defaults, its demo data unless given other; `[]` when it has none.
163
+ */
164
+ export function sourceAlerts(s: SourceDefinition<any, any>, o: SourceAlertOptions = {}): Alert[] {
165
+ if (!s.alerts) return [];
166
+ const now = o.now ?? Date.UTC(2026, 10, 5, 9, 42);
167
+ const options = s.options ? (validate(s.options, o.options ?? {}).value as Record<string, unknown>) : {};
168
+ const data = 'data' in o ? o.data : o.demo === false ? undefined : s.demo?.({ now, locale: o.locale ?? 'en' });
169
+ return s.alerts({ data, options, now });
170
+ }
171
+
141
172
  export interface PluginIssue {
142
173
  /** `plugin/widget`. */
143
174
  type: string;
@@ -149,13 +180,13 @@ export interface PluginIssue {
149
180
  /**
150
181
  * Paints every widget of a plugin with its demo data at its minimum and
151
182
  * preferred sizes and a wide and a tall one, in every theme and in each
152
- * language given, and lists what went wrong: a `render` that throws, one
183
+ * language given (by default the ones the plugin declares), and lists what went wrong: a `render` that throws, one
153
184
  * that paints nothing, paints outside its box, or takes longer than
154
185
  * `slowMs` (Kiosko warns at 50 ms). An empty list is a pass.
155
186
  */
156
187
  export function checkPlugin(p: PluginDefinition, o: { locales?: string[]; themes?: string[]; slowMs?: number; extraSizes?: { w: number; h: number }[] } = {}): PluginIssue[] {
157
188
  const issues: PluginIssue[] = [];
158
- const locales = o.locales ?? ['en', 'es'];
189
+ const locales = o.locales ?? (p.languages?.length ? p.languages : ['en', 'es']);
159
190
  const themes = o.themes ?? THEMES.map((t) => t.id);
160
191
  const slow = o.slowMs ?? 50;
161
192
  for (const w of p.widgets ?? []) {