@desplega.ai/slack-mock 0.6.0 → 0.7.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.
- package/README.md +4 -1
- package/dist/cli.js +1 -1
- package/dist/{index-5hsxaq74.js → index-h3vm22sx.js} +41 -4
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/render/blocks.ts +26 -0
- package/src/render/index.ts +3 -0
- package/src/server.ts +17 -3
- package/src/store.ts +3 -1
package/README.md
CHANGED
|
@@ -175,7 +175,10 @@ Stitch the PNGs into a GIF with ffmpeg if you want motion; the mock only produce
|
|
|
175
175
|
`data_visualization` renders pie, bar, line and area charts as SVG. Their
|
|
176
176
|
documented limits (rows, cells, caption, 2 charts per message, chart labels
|
|
177
177
|
matching `axis_config.categories`) reject with `invalid_blocks` on
|
|
178
|
-
`chat.postMessage` and `chat.update`.
|
|
178
|
+
`chat.postMessage` and `chat.update`. `context_actions` renders
|
|
179
|
+
`feedback_buttons` as 👍/👎 and `icon_button`; `clickButton` on a
|
|
180
|
+
`feedback_buttons` action sends the side whose `value` you pass (👍 by
|
|
181
|
+
default). Unknown blocks show a gray
|
|
179
182
|
`unsupported: <type>` box.
|
|
180
183
|
Errors come back as `{ ok: false, error }` with the real codes
|
|
181
184
|
(`not_in_channel`, `already_reacted`, `message_not_found`, `name_taken`,
|
package/dist/cli.js
CHANGED
|
@@ -482,6 +482,25 @@ function contextBlock(block, ctx) {
|
|
|
482
482
|
}).join("");
|
|
483
483
|
return `<div class="sm-block sm-context">${inner}</div>`;
|
|
484
484
|
}
|
|
485
|
+
function feedbackButton(v, glyph) {
|
|
486
|
+
const b = rec(v) ?? {};
|
|
487
|
+
const label = escapeHtml(str(rec(b.text)?.text));
|
|
488
|
+
const aria = escapeHtml(str(b.accessibility_label)) || label;
|
|
489
|
+
return `<span class="sm-btn sm-icon-btn" title="${label}" aria-label="${aria}">${glyph}</span>`;
|
|
490
|
+
}
|
|
491
|
+
function contextActionsBlock(block, ctx) {
|
|
492
|
+
const inner = arr(block.elements).map((e) => {
|
|
493
|
+
const el = rec(e);
|
|
494
|
+
if (!el)
|
|
495
|
+
return "";
|
|
496
|
+
if (str(el.type) === "feedback_buttons")
|
|
497
|
+
return `<span class="sm-feedback">${feedbackButton(el.positive_button, "\uD83D\uDC4D")}${feedbackButton(el.negative_button, "\uD83D\uDC4E")}</span>`;
|
|
498
|
+
if (str(el.type) === "icon_button")
|
|
499
|
+
return `<span class="sm-btn sm-icon-btn">${str(el.icon) === "trash" ? "\uD83D\uDDD1" : ""} ${textObject(el.text, ctx)}</span>`;
|
|
500
|
+
return unsupported(str(el.type));
|
|
501
|
+
}).join("");
|
|
502
|
+
return `<div class="sm-block sm-context-actions">${inner}</div>`;
|
|
503
|
+
}
|
|
485
504
|
function imageBlock(block, ctx) {
|
|
486
505
|
const title = block.title ? `<div class="sm-img-title">${textObject(block.title, ctx)}</div>` : "";
|
|
487
506
|
const src = imageSrcOf(block);
|
|
@@ -625,6 +644,8 @@ function renderBlock(v, ctx) {
|
|
|
625
644
|
return `<div class="sm-block"><hr class="sm-hr"></div>`;
|
|
626
645
|
case "context":
|
|
627
646
|
return contextBlock(block, ctx);
|
|
647
|
+
case "context_actions":
|
|
648
|
+
return contextActionsBlock(block, ctx);
|
|
628
649
|
case "actions":
|
|
629
650
|
return `<div class="sm-block sm-actions">${arr(block.elements).map((e) => renderElement(e, ctx)).join("")}</div>`;
|
|
630
651
|
case "image":
|
|
@@ -887,6 +908,9 @@ a:hover{text-decoration:underline}
|
|
|
887
908
|
.sm-context-img{width:16px;height:16px;border-radius:2px;object-fit:cover}
|
|
888
909
|
.sm-hr{border:0;border-top:1px solid #ddd;margin:10px 0}
|
|
889
910
|
.sm-actions{display:flex;flex-wrap:wrap;gap:8px;align-items:center}
|
|
911
|
+
.sm-context-actions{display:flex;flex-wrap:wrap;gap:4px;align-items:center}
|
|
912
|
+
.sm-feedback{display:inline-flex;gap:4px}
|
|
913
|
+
.sm-icon-btn{padding:4px 8px;font-weight:400;border-color:#ddd}
|
|
890
914
|
.sm-btn{display:inline-block;border:1px solid #bbb;border-radius:4px;padding:6px 12px;font-size:14px;font-weight:700;color:#1d1c1d;background:#fff;line-height:16px}
|
|
891
915
|
.sm-btn:hover{text-decoration:none;background:#f8f8f8;color:#1d1c1d}
|
|
892
916
|
.sm-btn-primary{background:#007a5a;border-color:#007a5a;color:#fff}
|
|
@@ -1868,7 +1892,10 @@ class Store2 {
|
|
|
1868
1892
|
break;
|
|
1869
1893
|
}
|
|
1870
1894
|
case "message.add":
|
|
1871
|
-
|
|
1895
|
+
if (change.message.ephemeral_user)
|
|
1896
|
+
this.ephemerals.push(change.message);
|
|
1897
|
+
else
|
|
1898
|
+
this.insertMessage(change.message);
|
|
1872
1899
|
break;
|
|
1873
1900
|
case "message.update":
|
|
1874
1901
|
case "reaction.add":
|
|
@@ -4317,7 +4344,7 @@ data: ${JSON.stringify(data)}
|
|
|
4317
4344
|
const message = this.store.findMessage(channel.id, input.ts) ?? this.store.ephemerals.find((m) => m.channel === channel.id && m.ts === input.ts);
|
|
4318
4345
|
if (!message)
|
|
4319
4346
|
throw new SlackApiError2("message_not_found");
|
|
4320
|
-
const found = findButton(message.blocks, input.action_id);
|
|
4347
|
+
const found = findButton(message.blocks, input.action_id, input.value);
|
|
4321
4348
|
if (!found && !input.value && !input.block_id)
|
|
4322
4349
|
throw new Error(`no button with action_id ${input.action_id} on message ${input.ts}`);
|
|
4323
4350
|
const responseUrl = this.issueResponseUrl({
|
|
@@ -4333,7 +4360,7 @@ data: ${JSON.stringify(data)}
|
|
|
4333
4360
|
triggerId: this.issueTriggerId(),
|
|
4334
4361
|
actions: [
|
|
4335
4362
|
{
|
|
4336
|
-
type: "button",
|
|
4363
|
+
type: found?.type ?? "button",
|
|
4337
4364
|
action_id: input.action_id,
|
|
4338
4365
|
block_id: input.block_id ?? found?.block_id ?? slackId("", 5),
|
|
4339
4366
|
text: found?.text ?? {
|
|
@@ -4578,7 +4605,7 @@ data: ${JSON.stringify(data)}
|
|
|
4578
4605
|
function slackError(code, extra = {}) {
|
|
4579
4606
|
return Response.json({ ok: false, error: code, ...extra });
|
|
4580
4607
|
}
|
|
4581
|
-
function findButton(blocks, actionId) {
|
|
4608
|
+
function findButton(blocks, actionId, value) {
|
|
4582
4609
|
for (const b of blocks ?? []) {
|
|
4583
4610
|
const elements = [
|
|
4584
4611
|
...Array.isArray(b.elements) ? b.elements : [],
|
|
@@ -4593,6 +4620,16 @@ function findButton(blocks, actionId) {
|
|
|
4593
4620
|
style: el.style
|
|
4594
4621
|
};
|
|
4595
4622
|
}
|
|
4623
|
+
if (el.type === "feedback_buttons" && el.action_id === actionId) {
|
|
4624
|
+
const sides = [el.positive_button, el.negative_button];
|
|
4625
|
+
const side = sides.find((s) => s?.value === value) ?? sides[0];
|
|
4626
|
+
return {
|
|
4627
|
+
type: "feedback_buttons",
|
|
4628
|
+
block_id: b.block_id,
|
|
4629
|
+
text: side?.text,
|
|
4630
|
+
value: side?.value
|
|
4631
|
+
};
|
|
4632
|
+
}
|
|
4596
4633
|
}
|
|
4597
4634
|
}
|
|
4598
4635
|
return;
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
package/src/render/blocks.ts
CHANGED
|
@@ -244,6 +244,30 @@ function contextBlock(block: Rec, ctx: RenderContext): string {
|
|
|
244
244
|
return `<div class="sm-block sm-context">${inner}</div>`;
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
+
/** One 👍 or 👎 of a `feedback_buttons` element; Slack shows the icon, and the label on hover. */
|
|
248
|
+
function feedbackButton(v: unknown, glyph: string): string {
|
|
249
|
+
const b = rec(v) ?? {};
|
|
250
|
+
const label = escapeHtml(str(rec(b.text)?.text));
|
|
251
|
+
const aria = escapeHtml(str(b.accessibility_label)) || label;
|
|
252
|
+
return `<span class="sm-btn sm-icon-btn" title="${label}" aria-label="${aria}">${glyph}</span>`;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** `context_actions`: a row of `feedback_buttons` and `icon_button` under a message. */
|
|
256
|
+
function contextActionsBlock(block: Rec, ctx: RenderContext): string {
|
|
257
|
+
const inner = arr(block.elements)
|
|
258
|
+
.map((e) => {
|
|
259
|
+
const el = rec(e);
|
|
260
|
+
if (!el) return "";
|
|
261
|
+
if (str(el.type) === "feedback_buttons")
|
|
262
|
+
return `<span class="sm-feedback">${feedbackButton(el.positive_button, "👍")}${feedbackButton(el.negative_button, "👎")}</span>`;
|
|
263
|
+
if (str(el.type) === "icon_button")
|
|
264
|
+
return `<span class="sm-btn sm-icon-btn">${str(el.icon) === "trash" ? "🗑" : ""} ${textObject(el.text, ctx)}</span>`;
|
|
265
|
+
return unsupported(str(el.type));
|
|
266
|
+
})
|
|
267
|
+
.join("");
|
|
268
|
+
return `<div class="sm-block sm-context-actions">${inner}</div>`;
|
|
269
|
+
}
|
|
270
|
+
|
|
247
271
|
function imageBlock(block: Rec, ctx: RenderContext): string {
|
|
248
272
|
const title = block.title
|
|
249
273
|
? `<div class="sm-img-title">${textObject(block.title, ctx)}</div>`
|
|
@@ -440,6 +464,8 @@ function renderBlock(v: unknown, ctx: RenderContext): string {
|
|
|
440
464
|
return `<div class="sm-block"><hr class="sm-hr"></div>`;
|
|
441
465
|
case "context":
|
|
442
466
|
return contextBlock(block, ctx);
|
|
467
|
+
case "context_actions":
|
|
468
|
+
return contextActionsBlock(block, ctx);
|
|
443
469
|
case "actions":
|
|
444
470
|
return `<div class="sm-block sm-actions">${arr(block.elements)
|
|
445
471
|
.map((e) => renderElement(e, ctx))
|
package/src/render/index.ts
CHANGED
|
@@ -194,6 +194,9 @@ a:hover{text-decoration:underline}
|
|
|
194
194
|
.sm-context-img{width:16px;height:16px;border-radius:2px;object-fit:cover}
|
|
195
195
|
.sm-hr{border:0;border-top:1px solid #ddd;margin:10px 0}
|
|
196
196
|
.sm-actions{display:flex;flex-wrap:wrap;gap:8px;align-items:center}
|
|
197
|
+
.sm-context-actions{display:flex;flex-wrap:wrap;gap:4px;align-items:center}
|
|
198
|
+
.sm-feedback{display:inline-flex;gap:4px}
|
|
199
|
+
.sm-icon-btn{padding:4px 8px;font-weight:400;border-color:#ddd}
|
|
197
200
|
.sm-btn{display:inline-block;border:1px solid #bbb;border-radius:4px;padding:6px 12px;font-size:14px;font-weight:700;color:#1d1c1d;background:#fff;line-height:16px}
|
|
198
201
|
.sm-btn:hover{text-decoration:none;background:#f8f8f8;color:#1d1c1d}
|
|
199
202
|
.sm-btn-primary{background:#007a5a;border-color:#007a5a;color:#fff}
|
package/src/server.ts
CHANGED
|
@@ -1014,7 +1014,7 @@ export class SlackMock {
|
|
|
1014
1014
|
this.store.findMessage(channel.id, input.ts) ??
|
|
1015
1015
|
this.store.ephemerals.find((m) => m.channel === channel.id && m.ts === input.ts);
|
|
1016
1016
|
if (!message) throw new SlackApiError("message_not_found");
|
|
1017
|
-
const found = findButton(message.blocks, input.action_id);
|
|
1017
|
+
const found = findButton(message.blocks, input.action_id, input.value);
|
|
1018
1018
|
if (!found && !input.value && !input.block_id)
|
|
1019
1019
|
throw new Error(`no button with action_id ${input.action_id} on message ${input.ts}`);
|
|
1020
1020
|
const responseUrl = this.issueResponseUrl({
|
|
@@ -1030,7 +1030,7 @@ export class SlackMock {
|
|
|
1030
1030
|
triggerId: this.issueTriggerId(),
|
|
1031
1031
|
actions: [
|
|
1032
1032
|
{
|
|
1033
|
-
type: "button",
|
|
1033
|
+
type: found?.type ?? "button",
|
|
1034
1034
|
action_id: input.action_id,
|
|
1035
1035
|
block_id: input.block_id ?? found?.block_id ?? slackId("", 5),
|
|
1036
1036
|
text: found?.text ?? {
|
|
@@ -1373,7 +1373,10 @@ function slackError(code: string, extra: Record<string, unknown> = {}): Response
|
|
|
1373
1373
|
function findButton(
|
|
1374
1374
|
blocks: unknown[] | undefined,
|
|
1375
1375
|
actionId: string,
|
|
1376
|
-
|
|
1376
|
+
value?: string,
|
|
1377
|
+
):
|
|
1378
|
+
| { type?: string; block_id?: string; text?: unknown; value?: string; style?: string }
|
|
1379
|
+
| undefined {
|
|
1377
1380
|
for (const b of (blocks ?? []) as Array<Record<string, unknown>>) {
|
|
1378
1381
|
const elements = [
|
|
1379
1382
|
...(Array.isArray(b.elements) ? (b.elements as Array<Record<string, unknown>>) : []),
|
|
@@ -1388,6 +1391,17 @@ function findButton(
|
|
|
1388
1391
|
style: el.style as string | undefined,
|
|
1389
1392
|
};
|
|
1390
1393
|
}
|
|
1394
|
+
// context_actions feedback_buttons: the click names the button by its value (👍 by default).
|
|
1395
|
+
if (el.type === "feedback_buttons" && el.action_id === actionId) {
|
|
1396
|
+
const sides = [el.positive_button, el.negative_button] as Array<Record<string, unknown>>;
|
|
1397
|
+
const side = sides.find((s) => s?.value === value) ?? sides[0];
|
|
1398
|
+
return {
|
|
1399
|
+
type: "feedback_buttons",
|
|
1400
|
+
block_id: b.block_id as string | undefined,
|
|
1401
|
+
text: side?.text,
|
|
1402
|
+
value: side?.value as string | undefined,
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1391
1405
|
}
|
|
1392
1406
|
}
|
|
1393
1407
|
return undefined;
|
package/src/store.ts
CHANGED
|
@@ -208,7 +208,9 @@ export class Store {
|
|
|
208
208
|
break;
|
|
209
209
|
}
|
|
210
210
|
case "message.add":
|
|
211
|
-
|
|
211
|
+
// Same split as addMessage: an ephemeral never joins channel history.
|
|
212
|
+
if (change.message.ephemeral_user) this.ephemerals.push(change.message);
|
|
213
|
+
else this.insertMessage(change.message);
|
|
212
214
|
break;
|
|
213
215
|
case "message.update":
|
|
214
216
|
case "reaction.add":
|