@desplega.ai/slack-mock 0.1.0

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.
@@ -0,0 +1,392 @@
1
+ // Block Kit and legacy attachments -> HTML.
2
+ // Everything unknown degrades to a small gray "unsupported: <type>" box instead
3
+ // of throwing, because agents can post arbitrary blocks through slack-post.
4
+
5
+ import {
6
+ displayName,
7
+ emojiChar,
8
+ emojiFromUnicode,
9
+ escapeHtml,
10
+ mrkdwnToHtml,
11
+ plainTextToHtml,
12
+ type RenderContext,
13
+ } from "./mrkdwn.ts";
14
+
15
+ type Rec = Record<string, unknown>;
16
+
17
+ function rec(v: unknown): Rec | undefined {
18
+ return typeof v === "object" && v !== null && !Array.isArray(v) ? (v as Rec) : undefined;
19
+ }
20
+
21
+ function str(v: unknown): string {
22
+ return typeof v === "string" ? v : "";
23
+ }
24
+
25
+ function arr(v: unknown): unknown[] {
26
+ return Array.isArray(v) ? v : [];
27
+ }
28
+
29
+ function safeUrl(url: string): string | undefined {
30
+ return /^(https?:\/\/|\/)/i.test(url) ? escapeHtml(url) : undefined;
31
+ }
32
+
33
+ function unsupported(what: string): string {
34
+ return `<div class="sm-unsupported">unsupported: ${escapeHtml(what || "unknown")}</div>`;
35
+ }
36
+
37
+ /** mrkdwn / plain_text text object (or a bare string). */
38
+ export function textObject(v: unknown, ctx: RenderContext): string {
39
+ if (typeof v === "string") return plainTextToHtml(v);
40
+ const o = rec(v);
41
+ if (!o) return "";
42
+ const text = str(o.text);
43
+ return o.type === "mrkdwn" ? mrkdwnToHtml(text, ctx) : plainTextToHtml(text);
44
+ }
45
+
46
+ function plainOf(v: unknown): string {
47
+ const o = rec(v);
48
+ return plainTextToHtml(o ? str(o.text) : str(v));
49
+ }
50
+
51
+ function image(url: string, alt: string, cls: string): string {
52
+ const src = safeUrl(url);
53
+ if (!src) return unsupported("image url");
54
+ return `<img class="${cls}" src="${src}" alt="${escapeHtml(alt)}">`;
55
+ }
56
+
57
+ function imageSrcOf(block: Rec): string {
58
+ const direct = str(block.image_url);
59
+ if (direct) return direct;
60
+ const file = rec(block.slack_file);
61
+ return file ? str(file.url) || str(file.url_private) : "";
62
+ }
63
+
64
+ // ------------------------------------------------------------------ elements
65
+
66
+ function button(el: Rec, ctx: RenderContext): string {
67
+ const style = str(el.style);
68
+ const cls = `sm-btn${style === "primary" ? " sm-btn-primary" : style === "danger" ? " sm-btn-danger" : ""}`;
69
+ const label = textObject(el.text, ctx) || "Button";
70
+ const url = safeUrl(str(el.url));
71
+ const confirm = rec(el.confirm)
72
+ ? '<span class="sm-btn-confirm" title="confirmation dialog">⚠</span>'
73
+ : "";
74
+ return url
75
+ ? `<a class="${cls}" href="${url}" target="_blank" rel="noopener">${label}${confirm}</a>`
76
+ : `<span class="${cls}">${label}${confirm}</span>`;
77
+ }
78
+
79
+ function selectPill(el: Rec, ctx: RenderContext, glyph = "▾"): string {
80
+ const initial = rec(el.initial_option);
81
+ const label =
82
+ (initial && textObject(initial.text, ctx)) ||
83
+ textObject(el.placeholder, ctx) ||
84
+ plainTextToHtml(str(el.type).replace(/_/g, " "));
85
+ return `<span class="sm-select">${label}<span class="sm-select-caret">${glyph}</span></span>`;
86
+ }
87
+
88
+ function inputBox(el: Rec, ctx: RenderContext): string {
89
+ const multiline = el.multiline === true;
90
+ const value = str(el.initial_value);
91
+ const inner = value
92
+ ? plainTextToHtml(value)
93
+ : `<span class="sm-input-ph">${textObject(el.placeholder, ctx)}</span>`;
94
+ return `<div class="sm-input${multiline ? " sm-input-multi" : ""}">${inner}</div>`;
95
+ }
96
+
97
+ /** Section accessories and action elements. */
98
+ export function renderElement(v: unknown, ctx: RenderContext): string {
99
+ const el = rec(v);
100
+ if (!el) return "";
101
+ const type = str(el.type);
102
+ switch (type) {
103
+ case "button":
104
+ return button(el, ctx);
105
+ case "image":
106
+ return image(imageSrcOf(el), str(el.alt_text), "sm-acc-img");
107
+ case "overflow":
108
+ return `<span class="sm-select sm-overflow">⋯</span>`;
109
+ case "datepicker":
110
+ return selectPill({ ...el, placeholder: el.placeholder ?? "Select a date" }, ctx, "📅");
111
+ case "timepicker":
112
+ return selectPill({ ...el, placeholder: el.placeholder ?? "Select a time" }, ctx, "🕐");
113
+ case "plain_text_input":
114
+ return inputBox(el, ctx);
115
+ case "checkboxes":
116
+ case "radio_buttons":
117
+ return `<div class="sm-options">${arr(el.options)
118
+ .map((o) => {
119
+ const opt = rec(o);
120
+ const mark = type === "checkboxes" ? "☐" : "◯";
121
+ return `<div class="sm-option">${mark} ${opt ? textObject(opt.text, ctx) : ""}</div>`;
122
+ })
123
+ .join("")}</div>`;
124
+ default:
125
+ if (type.endsWith("_select") || type.startsWith("multi_")) return selectPill(el, ctx);
126
+ return unsupported(type);
127
+ }
128
+ }
129
+
130
+ // ---------------------------------------------------------------- rich text
131
+
132
+ function richStyle(html: string, style: Rec | undefined): string {
133
+ if (!html || !style) return html;
134
+ let out = html;
135
+ if (style.code === true) out = `<code class="sm-code">${out}</code>`;
136
+ if (style.bold === true) out = `<strong>${out}</strong>`;
137
+ if (style.italic === true) out = `<em>${out}</em>`;
138
+ if (style.strike === true) out = `<s>${out}</s>`;
139
+ return out;
140
+ }
141
+
142
+ function richElement(v: unknown, ctx: RenderContext): string {
143
+ const el = rec(v);
144
+ if (!el) return "";
145
+ const style = rec(el.style);
146
+ switch (str(el.type)) {
147
+ case "text":
148
+ return richStyle(plainTextToHtml(str(el.text)).replace(/\n/g, "<br>"), style);
149
+ case "link": {
150
+ const url = safeUrl(str(el.url));
151
+ const label = plainTextToHtml(str(el.text) || str(el.url));
152
+ if (!url) return label;
153
+ return richStyle(
154
+ `<a class="sm-link" href="${url}" target="_blank" rel="noopener">${label}</a>`,
155
+ style,
156
+ );
157
+ }
158
+ case "user": {
159
+ const user = ctx.users.get(str(el.user_id));
160
+ return `<span class="sm-pill">@${escapeHtml(user ? displayName(user) : str(el.user_id))}</span>`;
161
+ }
162
+ case "usergroup":
163
+ return `<span class="sm-pill">@${escapeHtml(str(el.usergroup_id))}</span>`;
164
+ case "channel": {
165
+ const channel = ctx.channels.get(str(el.channel_id));
166
+ return `<span class="sm-pill">#${escapeHtml(channel ? channel.name : str(el.channel_id))}</span>`;
167
+ }
168
+ case "broadcast":
169
+ return `<span class="sm-pill">@${escapeHtml(str(el.range) || "channel")}</span>`;
170
+ case "emoji": {
171
+ const char =
172
+ emojiChar(str(el.name)) ?? (el.unicode ? emojiFromUnicode(str(el.unicode)) : undefined);
173
+ return char ?? `<span class="sm-emoji-name">:${escapeHtml(str(el.name))}:</span>`;
174
+ }
175
+ case "date":
176
+ return plainTextToHtml(str(el.fallback) || str(el.timestamp));
177
+ case "color":
178
+ return plainTextToHtml(str(el.value));
179
+ default:
180
+ return "";
181
+ }
182
+ }
183
+
184
+ function richSection(v: unknown, ctx: RenderContext): string {
185
+ const block = rec(v);
186
+ if (!block) return "";
187
+ const inner = arr(block.elements)
188
+ .map((e) => richElement(e, ctx))
189
+ .join("");
190
+ switch (str(block.type)) {
191
+ case "rich_text_section":
192
+ return `<div class="sm-rt-section">${inner}</div>`;
193
+ case "rich_text_quote":
194
+ return `<blockquote class="sm-quote">${inner}</blockquote>`;
195
+ case "rich_text_preformatted":
196
+ return `<pre class="sm-pre">${inner}</pre>`;
197
+ case "rich_text_list": {
198
+ const tag = str(block.style) === "ordered" ? "ol" : "ul";
199
+ const indent = Number(block.indent) || 0;
200
+ const items = arr(block.elements)
201
+ .map((item) => `<li>${richSection(item, ctx)}</li>`)
202
+ .join("");
203
+ const margin = indent ? ` style="margin-left:${indent * 18}px"` : "";
204
+ return `<${tag} class="sm-list"${margin}>${items}</${tag}>`;
205
+ }
206
+ default:
207
+ return inner;
208
+ }
209
+ }
210
+
211
+ // ------------------------------------------------------------------- blocks
212
+
213
+ function sectionBlock(block: Rec, ctx: RenderContext): string {
214
+ const parts: string[] = [];
215
+ if (block.text) parts.push(`<div class="sm-section-text">${textObject(block.text, ctx)}</div>`);
216
+ const fields = arr(block.fields);
217
+ if (fields.length) {
218
+ parts.push(
219
+ `<div class="sm-fields">${fields
220
+ .map((f) => `<div class="sm-field">${textObject(f, ctx)}</div>`)
221
+ .join("")}</div>`,
222
+ );
223
+ }
224
+ const accessory = block.accessory ? renderElement(block.accessory, ctx) : "";
225
+ const body = parts.join("");
226
+ if (!accessory) return `<div class="sm-block sm-section">${body}</div>`;
227
+ return `<div class="sm-block sm-section sm-section-split"><div class="sm-section-main">${body}</div><div class="sm-section-acc">${accessory}</div></div>`;
228
+ }
229
+
230
+ function contextBlock(block: Rec, ctx: RenderContext): string {
231
+ const inner = arr(block.elements)
232
+ .map((e) => {
233
+ const el = rec(e);
234
+ if (!el) return "";
235
+ if (str(el.type) === "image")
236
+ return image(imageSrcOf(el), str(el.alt_text), "sm-context-img");
237
+ return `<span class="sm-context-text">${textObject(el, ctx)}</span>`;
238
+ })
239
+ .join("");
240
+ return `<div class="sm-block sm-context">${inner}</div>`;
241
+ }
242
+
243
+ function imageBlock(block: Rec, ctx: RenderContext): string {
244
+ const title = block.title
245
+ ? `<div class="sm-img-title">${textObject(block.title, ctx)}</div>`
246
+ : "";
247
+ const src = imageSrcOf(block);
248
+ const alt = str(block.alt_text);
249
+ if (!src) {
250
+ const file = rec(block.slack_file);
251
+ return `<div class="sm-block">${title}${unsupported(`image (file ${str(file?.id) || "?"})`)}</div>`;
252
+ }
253
+ return `<div class="sm-block sm-image">${title}${image(src, alt, "sm-img")}</div>`;
254
+ }
255
+
256
+ function inputBlock(block: Rec, ctx: RenderContext): string {
257
+ const label = block.label ? `<div class="sm-label">${plainOf(block.label)}</div>` : "";
258
+ const element = block.element ? renderElement(block.element, ctx) : "";
259
+ const hint = block.hint ? `<div class="sm-hint">${plainOf(block.hint)}</div>` : "";
260
+ return `<div class="sm-block sm-input-block">${label}${element}${hint}</div>`;
261
+ }
262
+
263
+ function videoBlock(block: Rec, ctx: RenderContext): string {
264
+ const thumb = safeUrl(str(block.thumbnail_url));
265
+ const url = safeUrl(str(block.title_url) || str(block.video_url));
266
+ const title = textObject(block.title, ctx) || plainTextToHtml(str(block.alt_text));
267
+ const inner = `${thumb ? `<img class="sm-video-thumb" src="${thumb}" alt="${escapeHtml(str(block.alt_text))}">` : ""}<div class="sm-video-title">▶ ${title}</div>`;
268
+ return `<div class="sm-block sm-video">${url ? `<a class="sm-video-link" href="${url}" target="_blank" rel="noopener">${inner}</a>` : inner}</div>`;
269
+ }
270
+
271
+ function renderBlock(v: unknown, ctx: RenderContext): string {
272
+ const block = rec(v);
273
+ if (!block) return unsupported("block");
274
+ switch (str(block.type)) {
275
+ case "header":
276
+ return `<div class="sm-block sm-header-block">${textObject(block.text, ctx)}</div>`;
277
+ case "section":
278
+ return sectionBlock(block, ctx);
279
+ case "divider":
280
+ return `<div class="sm-block"><hr class="sm-hr"></div>`;
281
+ case "context":
282
+ return contextBlock(block, ctx);
283
+ case "actions":
284
+ return `<div class="sm-block sm-actions">${arr(block.elements)
285
+ .map((e) => renderElement(e, ctx))
286
+ .join("")}</div>`;
287
+ case "image":
288
+ return imageBlock(block, ctx);
289
+ case "rich_text":
290
+ return `<div class="sm-block sm-rich-text">${arr(block.elements)
291
+ .map((e) => richSection(e, ctx))
292
+ .join("")}</div>`;
293
+ case "input":
294
+ return inputBlock(block, ctx);
295
+ case "file":
296
+ return `<div class="sm-block sm-file-block">📎 ${escapeHtml(str(block.external_id) || str(block.file_id) || "file")}</div>`;
297
+ case "video":
298
+ return videoBlock(block, ctx);
299
+ default:
300
+ return `<div class="sm-block">${unsupported(str(block.type))}</div>`;
301
+ }
302
+ }
303
+
304
+ export function renderBlocks(blocks: unknown[], ctx: RenderContext): string {
305
+ const list = arr(blocks);
306
+ if (!list.length) return "";
307
+ return `<div class="sm-blocks">${list.map((b) => renderBlock(b, ctx)).join("")}</div>`;
308
+ }
309
+
310
+ // --------------------------------------------------------------- attachments
311
+
312
+ const NAMED_COLORS: Record<string, string> = {
313
+ good: "#2eb67d",
314
+ warning: "#ecb22e",
315
+ danger: "#e01e5a",
316
+ };
317
+
318
+ function barColor(v: unknown): string {
319
+ const raw = str(v).trim();
320
+ if (!raw) return "#dddddd";
321
+ const named = NAMED_COLORS[raw.toLowerCase()];
322
+ if (named) return named;
323
+ const hex = raw.startsWith("#") ? raw : `#${raw}`;
324
+ return /^#[0-9a-fA-F]{3,8}$/.test(hex) ? hex : "#dddddd";
325
+ }
326
+
327
+ function attachmentFields(fields: unknown[], ctx: RenderContext): string {
328
+ if (!fields.length) return "";
329
+ return `<div class="sm-fields">${fields
330
+ .map((f) => {
331
+ const field = rec(f);
332
+ if (!field) return "";
333
+ const wide = field.short === true ? "" : " sm-field-wide";
334
+ return `<div class="sm-field${wide}"><div class="sm-field-title">${plainTextToHtml(str(field.title))}</div><div>${mrkdwnToHtml(str(field.value), ctx)}</div></div>`;
335
+ })
336
+ .join("")}</div>`;
337
+ }
338
+
339
+ function attachment(v: unknown, ctx: RenderContext): string {
340
+ const att = rec(v);
341
+ if (!att) return "";
342
+ const parts: string[] = [];
343
+ const author = str(att.author_name);
344
+ if (author) {
345
+ const icon = safeUrl(str(att.author_icon));
346
+ parts.push(
347
+ `<div class="sm-att-author">${icon ? `<img class="sm-att-author-icon" src="${icon}" alt="">` : ""}${plainTextToHtml(author)}</div>`,
348
+ );
349
+ }
350
+ const title = str(att.title);
351
+ if (title) {
352
+ const link = safeUrl(str(att.title_link));
353
+ const inner = plainTextToHtml(title);
354
+ parts.push(
355
+ `<div class="sm-att-title">${link ? `<a class="sm-link" href="${link}" target="_blank" rel="noopener">${inner}</a>` : inner}</div>`,
356
+ );
357
+ }
358
+ const text = str(att.text) || (parts.length ? "" : str(att.fallback));
359
+ if (text) parts.push(`<div class="sm-att-text">${mrkdwnToHtml(text, ctx)}</div>`);
360
+ parts.push(attachmentFields(arr(att.fields), ctx));
361
+ const img = safeUrl(str(att.image_url));
362
+ if (img) parts.push(`<img class="sm-att-img" src="${img}" alt="">`);
363
+ const thumb = !img ? safeUrl(str(att.thumb_url)) : undefined;
364
+ if (thumb) parts.push(`<img class="sm-att-thumb" src="${thumb}" alt="">`);
365
+ const blocks = arr(att.blocks);
366
+ if (blocks.length) parts.push(renderBlocks(blocks, ctx));
367
+ const footer = str(att.footer);
368
+ const ts = att.ts ? formatAttachmentTs(att.ts) : "";
369
+ if (footer || ts) {
370
+ const icon = safeUrl(str(att.footer_icon));
371
+ parts.push(
372
+ `<div class="sm-att-footer">${icon ? `<img class="sm-att-footer-icon" src="${icon}" alt="">` : ""}${plainTextToHtml(footer)}${footer && ts ? " | " : ""}${escapeHtml(ts)}</div>`,
373
+ );
374
+ }
375
+ const pretext = str(att.pretext);
376
+ const box = `<div class="sm-att" style="border-left-color:${barColor(att.color)}">${parts.join("")}</div>`;
377
+ return pretext ? `<div class="sm-att-pretext">${mrkdwnToHtml(pretext, ctx)}</div>${box}` : box;
378
+ }
379
+
380
+ function formatAttachmentTs(v: unknown): string {
381
+ const secs = Number(v);
382
+ if (!Number.isFinite(secs) || secs <= 0) return str(v);
383
+ const d = new Date(secs * 1000);
384
+ const pad = (n: number) => String(n).padStart(2, "0");
385
+ return `${pad(d.getHours())}:${pad(d.getMinutes())}`;
386
+ }
387
+
388
+ export function renderAttachments(attachments: unknown[], ctx: RenderContext): string {
389
+ const list = arr(attachments);
390
+ if (!list.length) return "";
391
+ return `<div class="sm-atts">${list.map((a) => attachment(a, ctx)).join("")}</div>`;
392
+ }