@desplega.ai/slack-mock 0.1.2 → 0.2.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 +33 -0
- package/dist/cli.js +45 -6
- package/dist/frames.d.ts +41 -0
- package/dist/{index-hqq20tz9.js → index-6c88z809.js} +154 -70
- package/dist/{index-v7mbr1cx.js → index-v2hesj4s.js} +8 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +14 -12
- package/dist/screenshot.d.ts +2 -0
- package/dist/screenshot.js +7 -5
- package/dist/server.d.ts +4 -0
- package/dist/store.d.ts +9 -0
- package/package.json +1 -1
- package/src/cli.ts +37 -0
- package/src/frames.ts +128 -0
- package/src/index.ts +2 -0
- package/src/screenshot.ts +9 -3
- package/src/server.ts +16 -8
- package/src/store.ts +33 -14
package/README.md
CHANGED
|
@@ -72,6 +72,39 @@ What tests can do:
|
|
|
72
72
|
The same operations exist over HTTP under `/mock/*` for use from another
|
|
73
73
|
process (see `docs/design.md`).
|
|
74
74
|
|
|
75
|
+
## Frames from a journal
|
|
76
|
+
|
|
77
|
+
A run recorded with `dataFile` can be replayed into one PNG per event that touched a
|
|
78
|
+
thread (or, without a thread, a channel): the view after each `message.add`,
|
|
79
|
+
`message.update`, `message.delete`, `reaction.add` and `reaction.remove` line, in journal
|
|
80
|
+
order, plus `final-thread.png` (a copy of the last frame) and `final-desktop.png` (the full
|
|
81
|
+
UI with the thread panel, 1280x900). Nothing is served: the journal is replayed in memory and
|
|
82
|
+
each page is screenshotted from a temp file by the same headless Chrome helper as
|
|
83
|
+
`screenshot`. A `file.add` line carries no message, so a shared file first appears in the
|
|
84
|
+
frame of the message that shares it.
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
slack-mock frames --journal run.jsonl --channel C0GENERAL0 --thread 1788518410.672000 --out ./frames
|
|
88
|
+
# 5 message.add /abs/frames/01-message.add.png
|
|
89
|
+
# 6 reaction.add /abs/frames/02-reaction.add.png
|
|
90
|
+
# ...
|
|
91
|
+
# final-thread=/abs/frames/final-thread.png
|
|
92
|
+
# final-desktop=/abs/frames/final-desktop.png
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Leave out `--thread` for the channel view. `--manifest app.json` (bot display name),
|
|
96
|
+
`--width`, `--height` and `--no-desktop` are optional. The same from code:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { frames } from "@desplega.ai/slack-mock";
|
|
100
|
+
|
|
101
|
+
const result = await frames({ journal: "run.jsonl", channel: "general", thread: ask.ts, out: "./frames" });
|
|
102
|
+
// result.frames[1] = { index: 6, kind: "reaction.add", path: "/abs/frames/02-reaction.add.png" }
|
|
103
|
+
// result.finalThread, result.finalDesktop
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Stitch the PNGs into a GIF with ffmpeg if you want motion; the mock only produces frames.
|
|
107
|
+
|
|
75
108
|
## Slack behaviour that is modelled
|
|
76
109
|
|
|
77
110
|
- Socket Mode: `apps.connections.open`, `hello`, envelopes for `events_api`,
|
package/dist/cli.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
// @bun
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
SlackMock2,
|
|
5
|
+
frames2
|
|
6
|
+
} from "./index-6c88z809.js";
|
|
6
7
|
import {
|
|
7
|
-
|
|
8
|
-
} from "./index-
|
|
8
|
+
screenshot2
|
|
9
|
+
} from "./index-v2hesj4s.js";
|
|
9
10
|
|
|
10
11
|
// src/cli.ts
|
|
11
12
|
import { parseArgs } from "util";
|
|
@@ -15,6 +16,11 @@ Usage:
|
|
|
15
16
|
slack-mock serve [--port 4040] [--host 127.0.0.1] [--data ./data/slack.jsonl] [--manifest app.json]
|
|
16
17
|
[--no-seed] [--quiet] [--panel 50] [--auth user:pass] [--public-url https://mock.example.com]
|
|
17
18
|
slack-mock screenshot <url> --out shot.png [--width 800] [--height 1000]
|
|
19
|
+
slack-mock frames --journal data.jsonl --channel C0GENERAL0 [--thread 1788516626.286000] --out ./frames
|
|
20
|
+
[--manifest app.json] [--width 800] [--height 700] [--no-desktop]
|
|
21
|
+
|
|
22
|
+
frames renders the thread (or, without --thread, the channel) as it looked after every journal
|
|
23
|
+
line that touched it: one PNG per line, then final-thread.png and final-desktop.png.
|
|
18
24
|
|
|
19
25
|
serve prints the env vars to give your bot (SLACK_BOT_TOKEN, SLACK_APP_TOKEN, SLACK_API_URL)
|
|
20
26
|
and the URLs of the HTML views. Inject messages with the admin API, e.g.
|
|
@@ -38,7 +44,7 @@ async function main(argv) {
|
|
|
38
44
|
"public-url": { type: "string" }
|
|
39
45
|
}
|
|
40
46
|
});
|
|
41
|
-
const mock = await
|
|
47
|
+
const mock = await SlackMock2.start({
|
|
42
48
|
panelWidth: values.panel,
|
|
43
49
|
adminAuth: values.auth,
|
|
44
50
|
publicUrl: values["public-url"],
|
|
@@ -81,7 +87,7 @@ async function main(argv) {
|
|
|
81
87
|
const url = positionals[0];
|
|
82
88
|
if (!url)
|
|
83
89
|
throw new Error("screenshot needs a URL");
|
|
84
|
-
const out = await
|
|
90
|
+
const out = await screenshot2(url, {
|
|
85
91
|
out: values.out,
|
|
86
92
|
width: Number(values.width),
|
|
87
93
|
height: Number(values.height)
|
|
@@ -89,6 +95,39 @@ async function main(argv) {
|
|
|
89
95
|
console.log(out);
|
|
90
96
|
return;
|
|
91
97
|
}
|
|
98
|
+
if (command === "frames") {
|
|
99
|
+
const { values } = parseArgs({
|
|
100
|
+
args: rest,
|
|
101
|
+
options: {
|
|
102
|
+
journal: { type: "string" },
|
|
103
|
+
channel: { type: "string" },
|
|
104
|
+
thread: { type: "string" },
|
|
105
|
+
out: { type: "string" },
|
|
106
|
+
manifest: { type: "string" },
|
|
107
|
+
width: { type: "string", default: "800" },
|
|
108
|
+
height: { type: "string", default: "700" },
|
|
109
|
+
"no-desktop": { type: "boolean", default: false }
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
if (!values.journal || !values.channel || !values.out)
|
|
113
|
+
throw new Error("frames needs --journal, --channel and --out");
|
|
114
|
+
const result = await frames2({
|
|
115
|
+
journal: values.journal,
|
|
116
|
+
channel: values.channel,
|
|
117
|
+
thread: values.thread,
|
|
118
|
+
out: values.out,
|
|
119
|
+
manifest: values.manifest,
|
|
120
|
+
width: Number(values.width),
|
|
121
|
+
height: Number(values.height),
|
|
122
|
+
desktop: !values["no-desktop"]
|
|
123
|
+
});
|
|
124
|
+
for (const f of result.frames)
|
|
125
|
+
console.log(`${f.index} ${f.kind} ${f.path}`);
|
|
126
|
+
console.log(`final-thread=${result.finalThread}`);
|
|
127
|
+
if (result.finalDesktop)
|
|
128
|
+
console.log(`final-desktop=${result.finalDesktop}`);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
92
131
|
console.log(USAGE);
|
|
93
132
|
if (command && command !== "help" && command !== "--help")
|
|
94
133
|
process.exit(1);
|
package/dist/frames.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type SlackManifest } from "./server.ts";
|
|
2
|
+
import { type JournalEntry } from "./store.ts";
|
|
3
|
+
export interface FramesOptions {
|
|
4
|
+
/** Path to a JSONL journal written by `dataFile`. */
|
|
5
|
+
journal: string;
|
|
6
|
+
/** Channel id or name. */
|
|
7
|
+
channel: string;
|
|
8
|
+
/** Thread root ts. When absent, render the channel view instead of the thread view. */
|
|
9
|
+
thread?: string;
|
|
10
|
+
/** Output directory. Created when missing. */
|
|
11
|
+
out: string;
|
|
12
|
+
/** Slack app manifest path or object, same as `SlackMockOptions.manifest`. */
|
|
13
|
+
manifest?: string | SlackManifest;
|
|
14
|
+
/** Viewport width in px. Default 800. */
|
|
15
|
+
width?: number;
|
|
16
|
+
/** Viewport height in px. Default 700. */
|
|
17
|
+
height?: number;
|
|
18
|
+
/** Also render `final-desktop.png` (`?screenshot=0`, 1280x900). Default true. */
|
|
19
|
+
desktop?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface Frame {
|
|
22
|
+
/** 1-based journal line number the frame was rendered after. */
|
|
23
|
+
index: number;
|
|
24
|
+
/** Journal `kind` of that line. */
|
|
25
|
+
kind: string;
|
|
26
|
+
/** Absolute PNG path. */
|
|
27
|
+
path: string;
|
|
28
|
+
}
|
|
29
|
+
export interface FramesResult {
|
|
30
|
+
frames: Frame[];
|
|
31
|
+
/** Copy of the last frame. */
|
|
32
|
+
finalThread: string;
|
|
33
|
+
/** Present when `desktop` is true. */
|
|
34
|
+
finalDesktop?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Journal entries that change what the thread (or, without `thread`, the channel) shows.
|
|
38
|
+
* `file.add` carries no message, so a file only shows up with the message that shares it.
|
|
39
|
+
*/
|
|
40
|
+
export declare function selectFrames(entries: JournalEntry[], channel: string, thread?: string): JournalEntry[];
|
|
41
|
+
export declare function frames(opts: FramesOptions): Promise<FramesResult>;
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
import {
|
|
3
|
+
requireChrome2,
|
|
4
|
+
screenshot2
|
|
5
|
+
} from "./index-v2hesj4s.js";
|
|
6
|
+
|
|
2
7
|
// src/store.ts
|
|
3
8
|
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "fs";
|
|
4
9
|
import { dirname } from "path";
|
|
@@ -26,7 +31,7 @@ function nowUnix() {
|
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
// src/store.ts
|
|
29
|
-
class
|
|
34
|
+
class SlackApiError2 extends Error {
|
|
30
35
|
code;
|
|
31
36
|
extra;
|
|
32
37
|
constructor(code, extra = {}) {
|
|
@@ -35,8 +40,24 @@ class SlackApiError extends Error {
|
|
|
35
40
|
this.extra = extra;
|
|
36
41
|
}
|
|
37
42
|
}
|
|
43
|
+
function parseJournal(text, source = "journal") {
|
|
44
|
+
const entries = [];
|
|
45
|
+
let line = 0;
|
|
46
|
+
for (const raw of text.split(`
|
|
47
|
+
`)) {
|
|
48
|
+
line += 1;
|
|
49
|
+
if (!raw.trim())
|
|
50
|
+
continue;
|
|
51
|
+
try {
|
|
52
|
+
entries.push({ line, change: JSON.parse(raw) });
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.warn(`[slack-mock] skipping corrupt journal line ${line} in ${source}: ${e instanceof Error ? e.message : e}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return entries;
|
|
58
|
+
}
|
|
38
59
|
|
|
39
|
-
class
|
|
60
|
+
class Store2 {
|
|
40
61
|
team;
|
|
41
62
|
app;
|
|
42
63
|
bot;
|
|
@@ -67,7 +88,9 @@ class Store {
|
|
|
67
88
|
};
|
|
68
89
|
this.dataFile = opts.dataFile;
|
|
69
90
|
if (this.dataFile && existsSync(this.dataFile))
|
|
70
|
-
this.replay(this.dataFile);
|
|
91
|
+
this.replay(parseJournal(readFileSync(this.dataFile, "utf8"), this.dataFile).map((e) => e.change));
|
|
92
|
+
if (opts.replay)
|
|
93
|
+
this.replay(opts.replay);
|
|
71
94
|
if (!this.users.has(this.bot.userId)) {
|
|
72
95
|
this.addUser({
|
|
73
96
|
id: this.bot.userId,
|
|
@@ -92,21 +115,11 @@ class Store {
|
|
|
92
115
|
for (const l of this.listeners)
|
|
93
116
|
l(change);
|
|
94
117
|
}
|
|
95
|
-
replay(
|
|
118
|
+
replay(changes) {
|
|
96
119
|
this.replaying = true;
|
|
97
120
|
try {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
`)) {
|
|
101
|
-
lineNo += 1;
|
|
102
|
-
if (!line.trim())
|
|
103
|
-
continue;
|
|
104
|
-
try {
|
|
105
|
-
this.applyReplayed(JSON.parse(line));
|
|
106
|
-
} catch (e) {
|
|
107
|
-
console.warn(`[slack-mock] skipping corrupt journal line ${lineNo} in ${file}: ${e instanceof Error ? e.message : e}`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
121
|
+
for (const change of changes)
|
|
122
|
+
this.applyReplayed(change);
|
|
110
123
|
} finally {
|
|
111
124
|
this.replaying = false;
|
|
112
125
|
}
|
|
@@ -181,7 +194,7 @@ class Store {
|
|
|
181
194
|
user(id) {
|
|
182
195
|
const u = this.users.get(id);
|
|
183
196
|
if (!u)
|
|
184
|
-
throw new
|
|
197
|
+
throw new SlackApiError2("user_not_found");
|
|
185
198
|
return u;
|
|
186
199
|
}
|
|
187
200
|
userByEmail(email) {
|
|
@@ -197,7 +210,7 @@ class Store {
|
|
|
197
210
|
const name = input.name.replace(/^#/, "");
|
|
198
211
|
for (const c of this.channels.values()) {
|
|
199
212
|
if (c.name === name && !c.is_im)
|
|
200
|
-
throw new
|
|
213
|
+
throw new SlackApiError2("name_taken");
|
|
201
214
|
}
|
|
202
215
|
const id = input.id ?? slackId(input.is_private ? "G" : "C");
|
|
203
216
|
const creator = input.creator ?? this.bot.userId;
|
|
@@ -254,7 +267,7 @@ class Store {
|
|
|
254
267
|
channel(id) {
|
|
255
268
|
const c = this.channels.get(id);
|
|
256
269
|
if (!c)
|
|
257
|
-
throw new
|
|
270
|
+
throw new SlackApiError2("channel_not_found");
|
|
258
271
|
return c;
|
|
259
272
|
}
|
|
260
273
|
channelByName(name) {
|
|
@@ -276,7 +289,7 @@ class Store {
|
|
|
276
289
|
join(channelId, userId, inviter) {
|
|
277
290
|
const channel = this.channel(channelId);
|
|
278
291
|
if (channel.is_archived)
|
|
279
|
-
throw new
|
|
292
|
+
throw new SlackApiError2("is_archived");
|
|
280
293
|
if (channel.members.includes(userId))
|
|
281
294
|
return { alreadyMember: true };
|
|
282
295
|
channel.members.push(userId);
|
|
@@ -286,9 +299,9 @@ class Store {
|
|
|
286
299
|
archive(channelId) {
|
|
287
300
|
const channel = this.channel(channelId);
|
|
288
301
|
if (channel.is_archived)
|
|
289
|
-
throw new
|
|
302
|
+
throw new SlackApiError2("already_archived");
|
|
290
303
|
if (channel.is_general)
|
|
291
|
-
throw new
|
|
304
|
+
throw new SlackApiError2("cant_archive_general");
|
|
292
305
|
channel.is_archived = true;
|
|
293
306
|
this.emit({ kind: "channel.update", channel });
|
|
294
307
|
}
|
|
@@ -323,28 +336,28 @@ class Store {
|
|
|
323
336
|
updateEphemeral(channelId, ts, patch) {
|
|
324
337
|
const m = this.ephemerals.find((x) => x.channel === channelId && x.ts === ts);
|
|
325
338
|
if (!m)
|
|
326
|
-
throw new
|
|
339
|
+
throw new SlackApiError2("message_not_found");
|
|
327
340
|
Object.assign(m, patch);
|
|
328
341
|
return m;
|
|
329
342
|
}
|
|
330
343
|
deleteEphemeral(channelId, ts) {
|
|
331
344
|
const i = this.ephemerals.findIndex((x) => x.channel === channelId && x.ts === ts);
|
|
332
345
|
if (i < 0)
|
|
333
|
-
throw new
|
|
346
|
+
throw new SlackApiError2("message_not_found");
|
|
334
347
|
this.ephemerals.splice(i, 1);
|
|
335
348
|
}
|
|
336
349
|
message(channelId, ts) {
|
|
337
350
|
const m = this.findMessage(channelId, ts);
|
|
338
351
|
if (!m)
|
|
339
|
-
throw new
|
|
352
|
+
throw new SlackApiError2("message_not_found");
|
|
340
353
|
return m;
|
|
341
354
|
}
|
|
342
355
|
addMessage(input) {
|
|
343
356
|
const channel = this.channel(input.channel);
|
|
344
357
|
if (channel.is_archived)
|
|
345
|
-
throw new
|
|
358
|
+
throw new SlackApiError2("is_archived");
|
|
346
359
|
if (input.thread_ts && !this.findMessage(input.channel, input.thread_ts)) {
|
|
347
|
-
throw new
|
|
360
|
+
throw new SlackApiError2("thread_not_found");
|
|
348
361
|
}
|
|
349
362
|
const message = {
|
|
350
363
|
type: "message",
|
|
@@ -370,7 +383,7 @@ class Store {
|
|
|
370
383
|
const list = this.messages.get(channelId);
|
|
371
384
|
const i = list?.findIndex((m) => m.ts === ts) ?? -1;
|
|
372
385
|
if (!list || i < 0)
|
|
373
|
-
throw new
|
|
386
|
+
throw new SlackApiError2("message_not_found");
|
|
374
387
|
const [message] = list.splice(i, 1);
|
|
375
388
|
if (message.thread_ts && message.thread_ts !== ts) {
|
|
376
389
|
const parent = this.findMessage(channelId, message.thread_ts);
|
|
@@ -385,7 +398,7 @@ class Store {
|
|
|
385
398
|
message.reactions ??= [];
|
|
386
399
|
let reaction = message.reactions.find((r) => r.name === name);
|
|
387
400
|
if (reaction?.users.includes(userId))
|
|
388
|
-
throw new
|
|
401
|
+
throw new SlackApiError2("already_reacted");
|
|
389
402
|
if (!reaction) {
|
|
390
403
|
reaction = { name, users: [], count: 0 };
|
|
391
404
|
message.reactions.push(reaction);
|
|
@@ -399,7 +412,7 @@ class Store {
|
|
|
399
412
|
const message = this.message(channelId, ts);
|
|
400
413
|
const reaction = message.reactions?.find((r) => r.name === name);
|
|
401
414
|
if (!reaction?.users.includes(userId))
|
|
402
|
-
throw new
|
|
415
|
+
throw new SlackApiError2("no_reaction");
|
|
403
416
|
reaction.users = reaction.users.filter((u) => u !== userId);
|
|
404
417
|
reaction.count = reaction.users.length;
|
|
405
418
|
message.reactions = message.reactions.filter((r) => r.count > 0);
|
|
@@ -458,7 +471,7 @@ class Store {
|
|
|
458
471
|
file(id) {
|
|
459
472
|
const f = this.files.get(id);
|
|
460
473
|
if (!f)
|
|
461
|
-
throw new
|
|
474
|
+
throw new SlackApiError2("file_not_found");
|
|
462
475
|
return f;
|
|
463
476
|
}
|
|
464
477
|
openView(view) {
|
|
@@ -590,9 +603,9 @@ async function parseArgs(req) {
|
|
|
590
603
|
return args;
|
|
591
604
|
const contentType = req.headers.get("content-type") ?? "";
|
|
592
605
|
if (contentType.includes("application/json")) {
|
|
593
|
-
const
|
|
594
|
-
if (
|
|
595
|
-
Object.assign(args, JSON.parse(
|
|
606
|
+
const text = await req.text();
|
|
607
|
+
if (text.trim())
|
|
608
|
+
Object.assign(args, JSON.parse(text));
|
|
596
609
|
return args;
|
|
597
610
|
}
|
|
598
611
|
if (contentType.includes("multipart/form-data")) {
|
|
@@ -2308,7 +2321,7 @@ class SocketModeHub {
|
|
|
2308
2321
|
function required(args, key) {
|
|
2309
2322
|
const v = str(args, key);
|
|
2310
2323
|
if (!v)
|
|
2311
|
-
throw new
|
|
2324
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2312
2325
|
response_metadata: { messages: [`[ERROR] missing required field: ${key}`] }
|
|
2313
2326
|
});
|
|
2314
2327
|
return v;
|
|
@@ -2317,7 +2330,7 @@ function resolveChannel(store, raw) {
|
|
|
2317
2330
|
if (raw.startsWith("#")) {
|
|
2318
2331
|
const c = store.channelByName(raw);
|
|
2319
2332
|
if (!c)
|
|
2320
|
-
throw new
|
|
2333
|
+
throw new SlackApiError2("channel_not_found");
|
|
2321
2334
|
return c;
|
|
2322
2335
|
}
|
|
2323
2336
|
if (raw.startsWith("U") || raw.startsWith("W"))
|
|
@@ -2326,7 +2339,7 @@ function resolveChannel(store, raw) {
|
|
|
2326
2339
|
}
|
|
2327
2340
|
function requireMember(channel, actor) {
|
|
2328
2341
|
if (!channel.members.includes(actor.userId))
|
|
2329
|
-
throw new
|
|
2342
|
+
throw new SlackApiError2("not_in_channel");
|
|
2330
2343
|
}
|
|
2331
2344
|
function botFields(ctx) {
|
|
2332
2345
|
if (!ctx.actor.isBot)
|
|
@@ -2348,9 +2361,9 @@ function messageContent(args) {
|
|
|
2348
2361
|
const blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
2349
2362
|
const attachments = Array.isArray(args.attachments) ? args.attachments : undefined;
|
|
2350
2363
|
if (!text && !blocks?.length && !attachments?.length)
|
|
2351
|
-
throw new
|
|
2364
|
+
throw new SlackApiError2("no_text");
|
|
2352
2365
|
if (blocks && blocks.length > 50)
|
|
2353
|
-
throw new
|
|
2366
|
+
throw new SlackApiError2("invalid_blocks", {
|
|
2354
2367
|
response_metadata: { messages: ["[ERROR] must have at most 50 blocks"] }
|
|
2355
2368
|
});
|
|
2356
2369
|
const metadata = args.metadata && typeof args.metadata === "object" ? args.metadata : undefined;
|
|
@@ -2451,7 +2464,7 @@ var handlers = {
|
|
|
2451
2464
|
const ts = required(args, "ts");
|
|
2452
2465
|
const existing = ctx.store.message(channel.id, ts);
|
|
2453
2466
|
if (existing.user !== ctx.actor.userId && !ctx.actor.isBot)
|
|
2454
|
-
throw new
|
|
2467
|
+
throw new SlackApiError2("cant_update_message");
|
|
2455
2468
|
const patch = {};
|
|
2456
2469
|
if (args.text !== undefined)
|
|
2457
2470
|
patch.text = str(args, "text") ?? "";
|
|
@@ -2462,7 +2475,7 @@ var handlers = {
|
|
|
2462
2475
|
if (args.metadata !== undefined)
|
|
2463
2476
|
patch.metadata = args.metadata;
|
|
2464
2477
|
if (patch.text === "" && !patch.blocks?.length && !patch.attachments?.length && !existing.blocks?.length)
|
|
2465
|
-
throw new
|
|
2478
|
+
throw new SlackApiError2("no_text");
|
|
2466
2479
|
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId);
|
|
2467
2480
|
return { channel: channel.id, ts, text: message.text, message: wireMessage(message) };
|
|
2468
2481
|
},
|
|
@@ -2493,7 +2506,7 @@ var handlers = {
|
|
|
2493
2506
|
const ts = required(args, "ts");
|
|
2494
2507
|
const existing = ctx.store.message(channel.id, ts);
|
|
2495
2508
|
if (existing.streaming_state !== "in_progress")
|
|
2496
|
-
throw new
|
|
2509
|
+
throw new SlackApiError2("message_not_in_streaming_state");
|
|
2497
2510
|
const message = ctx.store.updateMessage(channel.id, ts, { text: existing.text + streamText(args) }, ctx.actor.userId, { markEdited: false });
|
|
2498
2511
|
return { channel: channel.id, ts: message.ts };
|
|
2499
2512
|
},
|
|
@@ -2502,7 +2515,7 @@ var handlers = {
|
|
|
2502
2515
|
const ts = required(args, "ts");
|
|
2503
2516
|
const existing = ctx.store.message(channel.id, ts);
|
|
2504
2517
|
if (existing.streaming_state !== "in_progress")
|
|
2505
|
-
throw new
|
|
2518
|
+
throw new SlackApiError2("message_not_in_streaming_state");
|
|
2506
2519
|
const patch = {
|
|
2507
2520
|
streaming_state: "completed",
|
|
2508
2521
|
text: existing.text + streamText(args)
|
|
@@ -2553,7 +2566,7 @@ var handlers = {
|
|
|
2553
2566
|
"conversations.create": (ctx, args) => {
|
|
2554
2567
|
const name = required(args, "name");
|
|
2555
2568
|
if (!/^[a-z0-9_-]{1,80}$/.test(name))
|
|
2556
|
-
throw new
|
|
2569
|
+
throw new SlackApiError2("invalid_name_specials");
|
|
2557
2570
|
const channel = ctx.store.addChannel({
|
|
2558
2571
|
name,
|
|
2559
2572
|
is_private: bool(args, "is_private") ?? false,
|
|
@@ -2564,7 +2577,7 @@ var handlers = {
|
|
|
2564
2577
|
"conversations.join": (ctx, args) => {
|
|
2565
2578
|
const channel = ctx.store.channel(required(args, "channel"));
|
|
2566
2579
|
if (channel.is_private)
|
|
2567
|
-
throw new
|
|
2580
|
+
throw new SlackApiError2("method_not_supported_for_channel_type");
|
|
2568
2581
|
const { alreadyMember } = ctx.store.join(channel.id, ctx.actor.userId);
|
|
2569
2582
|
return {
|
|
2570
2583
|
channel: channelInfo(ctx.store, channel, ctx.actor),
|
|
@@ -2585,7 +2598,7 @@ var handlers = {
|
|
|
2585
2598
|
errors.push({ user: u, ok: false, error: "already_in_channel" });
|
|
2586
2599
|
}
|
|
2587
2600
|
if (errors.length === users.length)
|
|
2588
|
-
throw new
|
|
2601
|
+
throw new SlackApiError2(String(errors[0].error), { errors });
|
|
2589
2602
|
return {
|
|
2590
2603
|
channel: channelInfo(ctx.store, channel, ctx.actor),
|
|
2591
2604
|
...errors.length ? { errors } : {}
|
|
@@ -2602,7 +2615,7 @@ var handlers = {
|
|
|
2602
2615
|
"conversations.open": (ctx, args) => {
|
|
2603
2616
|
const users = (str(args, "users") ?? "").split(",").map((u) => u.trim()).filter(Boolean);
|
|
2604
2617
|
if (users.length !== 1)
|
|
2605
|
-
throw new
|
|
2618
|
+
throw new SlackApiError2("users_list_not_supplied");
|
|
2606
2619
|
const dm = ctx.store.openDm(users[0]);
|
|
2607
2620
|
return { channel: { id: dm.id, is_im: true, user: dm.user, created: dm.created } };
|
|
2608
2621
|
},
|
|
@@ -2628,7 +2641,7 @@ var handlers = {
|
|
|
2628
2641
|
requireMember(channel, ctx.actor);
|
|
2629
2642
|
const ts = required(args, "ts");
|
|
2630
2643
|
if (!ctx.store.findMessage(channel.id, ts))
|
|
2631
|
-
throw new
|
|
2644
|
+
throw new SlackApiError2("thread_not_found");
|
|
2632
2645
|
const p = ctx.store.replies(channel.id, ts, {
|
|
2633
2646
|
oldest: str(args, "oldest"),
|
|
2634
2647
|
latest: str(args, "latest"),
|
|
@@ -2650,7 +2663,7 @@ var handlers = {
|
|
|
2650
2663
|
"users.lookupByEmail": (ctx, args) => {
|
|
2651
2664
|
const user = ctx.store.userByEmail(required(args, "email"));
|
|
2652
2665
|
if (!user)
|
|
2653
|
-
throw new
|
|
2666
|
+
throw new SlackApiError2("users_not_found");
|
|
2654
2667
|
return { user };
|
|
2655
2668
|
},
|
|
2656
2669
|
"users.conversations": (ctx, args) => {
|
|
@@ -2684,7 +2697,7 @@ var handlers = {
|
|
|
2684
2697
|
const filename = required(args, "filename");
|
|
2685
2698
|
const length = num(args, "length", -1);
|
|
2686
2699
|
if (length < 0)
|
|
2687
|
-
throw new
|
|
2700
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2688
2701
|
response_metadata: { messages: ["[ERROR] missing required field: length"] }
|
|
2689
2702
|
});
|
|
2690
2703
|
const id = slackId("F");
|
|
@@ -2699,7 +2712,7 @@ var handlers = {
|
|
|
2699
2712
|
"files.completeUploadExternal": (ctx, args) => {
|
|
2700
2713
|
const list = Array.isArray(args.files) ? args.files : [];
|
|
2701
2714
|
if (list.length === 0)
|
|
2702
|
-
throw new
|
|
2715
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2703
2716
|
response_metadata: { messages: ["[ERROR] missing required field: files"] }
|
|
2704
2717
|
});
|
|
2705
2718
|
const files = list.map(({ id, title }) => {
|
|
@@ -2737,13 +2750,13 @@ var handlers = {
|
|
|
2737
2750
|
const triggerId = required(args, "trigger_id");
|
|
2738
2751
|
const expires = ctx.triggerIds.get(triggerId);
|
|
2739
2752
|
if (expires === undefined)
|
|
2740
|
-
throw new
|
|
2753
|
+
throw new SlackApiError2("invalid_trigger_id");
|
|
2741
2754
|
ctx.triggerIds.delete(triggerId);
|
|
2742
2755
|
if (Date.now() > expires)
|
|
2743
|
-
throw new
|
|
2756
|
+
throw new SlackApiError2("expired_trigger_id");
|
|
2744
2757
|
const view = viewArg(args);
|
|
2745
2758
|
if (view.type !== "modal")
|
|
2746
|
-
throw new
|
|
2759
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2747
2760
|
response_metadata: { messages: ["[ERROR] view type must be modal"] }
|
|
2748
2761
|
});
|
|
2749
2762
|
const stored = ctx.store.openView({
|
|
@@ -2767,7 +2780,7 @@ var handlers = {
|
|
|
2767
2780
|
const id = required(args, "view_id");
|
|
2768
2781
|
const existing = ctx.store.views.get(id);
|
|
2769
2782
|
if (!existing)
|
|
2770
|
-
throw new
|
|
2783
|
+
throw new SlackApiError2("not_found");
|
|
2771
2784
|
const view = viewArg(args);
|
|
2772
2785
|
Object.assign(existing, {
|
|
2773
2786
|
callback_id: str(view, "callback_id") ?? existing.callback_id,
|
|
@@ -2848,14 +2861,14 @@ function viewArg(args) {
|
|
|
2848
2861
|
try {
|
|
2849
2862
|
return JSON.parse(v);
|
|
2850
2863
|
} catch {
|
|
2851
|
-
throw new
|
|
2864
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2852
2865
|
response_metadata: { messages: ["[ERROR] view must be valid JSON"] }
|
|
2853
2866
|
});
|
|
2854
2867
|
}
|
|
2855
2868
|
}
|
|
2856
2869
|
if (v && typeof v === "object")
|
|
2857
2870
|
return v;
|
|
2858
|
-
throw new
|
|
2871
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2859
2872
|
response_metadata: { messages: ["[ERROR] missing required field: view"] }
|
|
2860
2873
|
});
|
|
2861
2874
|
}
|
|
@@ -2876,6 +2889,12 @@ function newTriggerId() {
|
|
|
2876
2889
|
}
|
|
2877
2890
|
|
|
2878
2891
|
// src/server.ts
|
|
2892
|
+
function loadManifest(manifest) {
|
|
2893
|
+
return typeof manifest === "string" ? JSON.parse(readFileSync2(manifest, "utf8")) : manifest;
|
|
2894
|
+
}
|
|
2895
|
+
function manifestAppName(manifest) {
|
|
2896
|
+
return manifest?.features?.bot_user?.display_name ?? manifest?.display_information?.name;
|
|
2897
|
+
}
|
|
2879
2898
|
var DEFAULT_EVENTS = [
|
|
2880
2899
|
"message.channels",
|
|
2881
2900
|
"message.groups",
|
|
@@ -2889,7 +2908,7 @@ var DEFAULT_EVENTS = [
|
|
|
2889
2908
|
"member_joined_channel"
|
|
2890
2909
|
];
|
|
2891
2910
|
|
|
2892
|
-
class
|
|
2911
|
+
class SlackMock2 {
|
|
2893
2912
|
store;
|
|
2894
2913
|
hub;
|
|
2895
2914
|
opts;
|
|
@@ -2903,15 +2922,15 @@ class SlackMock {
|
|
|
2903
2922
|
faults = [];
|
|
2904
2923
|
lastRequestAt = 0;
|
|
2905
2924
|
log;
|
|
2906
|
-
static Error =
|
|
2925
|
+
static Error = SlackApiError2;
|
|
2907
2926
|
constructor(opts = {}) {
|
|
2908
|
-
const manifest =
|
|
2909
|
-
const appName = opts.appName ?? manifest
|
|
2927
|
+
const manifest = loadManifest(opts.manifest);
|
|
2928
|
+
const appName = opts.appName ?? manifestAppName(manifest);
|
|
2910
2929
|
this.opts = { ...opts, appName };
|
|
2911
2930
|
this.log = typeof opts.log === "function" ? opts.log : opts.log ? (m) => console.log(`[slack-mock] ${m}`) : () => {};
|
|
2912
2931
|
if (opts.dataFile)
|
|
2913
2932
|
ensureDir(opts.dataFile);
|
|
2914
|
-
this.store = new
|
|
2933
|
+
this.store = new Store2({ ...opts, appName });
|
|
2915
2934
|
this.hub = new SocketModeHub({
|
|
2916
2935
|
appId: this.store.app.id,
|
|
2917
2936
|
pingIntervalMs: opts.pingIntervalMs,
|
|
@@ -2927,7 +2946,7 @@ class SlackMock {
|
|
|
2927
2946
|
this.store.onChange((change) => this.dispatch(change));
|
|
2928
2947
|
}
|
|
2929
2948
|
static async start(opts = {}) {
|
|
2930
|
-
const mock = new
|
|
2949
|
+
const mock = new SlackMock2(opts);
|
|
2931
2950
|
mock.listen();
|
|
2932
2951
|
return mock;
|
|
2933
2952
|
}
|
|
@@ -3150,7 +3169,7 @@ class SlackMock {
|
|
|
3150
3169
|
record(true);
|
|
3151
3170
|
return Response.json({ ok: true, ...body });
|
|
3152
3171
|
} catch (e) {
|
|
3153
|
-
if (e instanceof
|
|
3172
|
+
if (e instanceof SlackApiError2) {
|
|
3154
3173
|
record(false, e.code);
|
|
3155
3174
|
this.log(`${method} -> ${e.code}`);
|
|
3156
3175
|
return slackError(e.code, e.extra);
|
|
@@ -3232,7 +3251,7 @@ class SlackMock {
|
|
|
3232
3251
|
return new Response("ok");
|
|
3233
3252
|
}
|
|
3234
3253
|
} catch (e) {
|
|
3235
|
-
if (e instanceof
|
|
3254
|
+
if (e instanceof SlackApiError2)
|
|
3236
3255
|
return Response.json({ ok: false, error: e.code });
|
|
3237
3256
|
throw e;
|
|
3238
3257
|
}
|
|
@@ -3452,7 +3471,7 @@ data: ${JSON.stringify(data)}
|
|
|
3452
3471
|
for (const u of this.store.users.values())
|
|
3453
3472
|
if (u.name === idOrName)
|
|
3454
3473
|
return u;
|
|
3455
|
-
throw new
|
|
3474
|
+
throw new SlackApiError2("user_not_found");
|
|
3456
3475
|
}
|
|
3457
3476
|
channel(idOrName) {
|
|
3458
3477
|
return this.resolveChannel(idOrName);
|
|
@@ -3573,7 +3592,7 @@ data: ${JSON.stringify(data)}
|
|
|
3573
3592
|
const channel = this.resolveChannel(input.channel);
|
|
3574
3593
|
const message = this.store.findMessage(channel.id, input.ts) ?? this.store.ephemerals.find((m) => m.channel === channel.id && m.ts === input.ts);
|
|
3575
3594
|
if (!message)
|
|
3576
|
-
throw new
|
|
3595
|
+
throw new SlackApiError2("message_not_found");
|
|
3577
3596
|
const found = findButton(message.blocks, input.action_id);
|
|
3578
3597
|
if (!found && !input.value && !input.block_id)
|
|
3579
3598
|
throw new Error(`no button with action_id ${input.action_id} on message ${input.ts}`);
|
|
@@ -3770,7 +3789,7 @@ data: ${JSON.stringify(data)}
|
|
|
3770
3789
|
return this.store.onChange(listener);
|
|
3771
3790
|
}
|
|
3772
3791
|
async handleAdmin(req, path, url) {
|
|
3773
|
-
const json = (
|
|
3792
|
+
const json = (body, status = 200) => Response.json(body, { status });
|
|
3774
3793
|
const body = req.method === "POST" ? await req.json().catch(() => ({})) : {};
|
|
3775
3794
|
try {
|
|
3776
3795
|
if (path === "presenter")
|
|
@@ -3822,7 +3841,7 @@ data: ${JSON.stringify(data)}
|
|
|
3822
3841
|
return json((m[2] ? this.thread(m[1], m[2]) : this.messages(m[1])).map(wireMessage));
|
|
3823
3842
|
return json({ ok: false, error: "unknown_admin_route" }, 404);
|
|
3824
3843
|
} catch (e) {
|
|
3825
|
-
return json({ ok: false, error: e instanceof
|
|
3844
|
+
return json({ ok: false, error: e instanceof SlackApiError2 ? e.code : String(e) }, 400);
|
|
3826
3845
|
}
|
|
3827
3846
|
}
|
|
3828
3847
|
}
|
|
@@ -3874,4 +3893,69 @@ function denied(req, path) {
|
|
|
3874
3893
|
});
|
|
3875
3894
|
}
|
|
3876
3895
|
|
|
3877
|
-
|
|
3896
|
+
// src/frames.ts
|
|
3897
|
+
import { copyFileSync, mkdirSync as mkdirSync2, mkdtempSync, readFileSync as readFileSync3, rmSync, writeFileSync } from "fs";
|
|
3898
|
+
import { tmpdir } from "os";
|
|
3899
|
+
import { join, resolve } from "path";
|
|
3900
|
+
var FRAME_KINDS = new Set([
|
|
3901
|
+
"message.add",
|
|
3902
|
+
"message.update",
|
|
3903
|
+
"message.delete",
|
|
3904
|
+
"reaction.add",
|
|
3905
|
+
"reaction.remove",
|
|
3906
|
+
"file.add"
|
|
3907
|
+
]);
|
|
3908
|
+
function selectFrames(entries, channel, thread) {
|
|
3909
|
+
return entries.filter(({ change }) => {
|
|
3910
|
+
if (!FRAME_KINDS.has(change.kind) || !("message" in change))
|
|
3911
|
+
return false;
|
|
3912
|
+
const m = change.message;
|
|
3913
|
+
if (m.channel !== channel)
|
|
3914
|
+
return false;
|
|
3915
|
+
return thread ? m.ts === thread || m.thread_ts === thread : true;
|
|
3916
|
+
});
|
|
3917
|
+
}
|
|
3918
|
+
async function frames2(opts) {
|
|
3919
|
+
const entries = parseJournal(readFileSync3(opts.journal, "utf8"), opts.journal);
|
|
3920
|
+
const appName = manifestAppName(loadManifest(opts.manifest));
|
|
3921
|
+
const storeAfter = (line) => new Store2({ replay: entries.filter((e) => e.line <= line).map((e) => e.change), appName });
|
|
3922
|
+
const full = storeAfter(Number.POSITIVE_INFINITY);
|
|
3923
|
+
const channel = full.channels.get(opts.channel) ?? full.channelByName(opts.channel);
|
|
3924
|
+
if (!channel)
|
|
3925
|
+
throw new Error(`channel ${opts.channel} not found in ${opts.journal}`);
|
|
3926
|
+
const what = opts.thread ? `thread ${opts.thread} in ${channel.id}` : `channel ${channel.id}`;
|
|
3927
|
+
const selected = selectFrames(entries, channel.id, opts.thread);
|
|
3928
|
+
if (!selected.length)
|
|
3929
|
+
throw new Error(`no line of ${opts.journal} touches ${what}`);
|
|
3930
|
+
requireChrome2();
|
|
3931
|
+
const view = opts.thread ? { kind: "thread", channel: channel.id, ts: opts.thread } : { kind: "channel", channel: channel.id };
|
|
3932
|
+
const out = resolve(opts.out);
|
|
3933
|
+
mkdirSync2(out, { recursive: true });
|
|
3934
|
+
const tmp = mkdtempSync(join(tmpdir(), "slack-mock-frames-"));
|
|
3935
|
+
const html = join(tmp, "frame.html");
|
|
3936
|
+
const shoot = async (store, file, render, size) => {
|
|
3937
|
+
writeFileSync(html, renderPage(store, view, render));
|
|
3938
|
+
await screenshot2(Bun.pathToFileURL(html).href, { out: file, ...size });
|
|
3939
|
+
};
|
|
3940
|
+
try {
|
|
3941
|
+
const frames = [];
|
|
3942
|
+
const size = { width: opts.width ?? 800, height: opts.height ?? 700 };
|
|
3943
|
+
for (const [i, { line, change }] of selected.entries()) {
|
|
3944
|
+
const path = join(out, `${String(i + 1).padStart(2, "0")}-${change.kind}.png`);
|
|
3945
|
+
await shoot(storeAfter(line), path, { screenshot: true }, size);
|
|
3946
|
+
frames.push({ index: line, kind: change.kind, path });
|
|
3947
|
+
}
|
|
3948
|
+
const finalThread = join(out, "final-thread.png");
|
|
3949
|
+
copyFileSync(frames[frames.length - 1].path, finalThread);
|
|
3950
|
+
let finalDesktop;
|
|
3951
|
+
if (opts.desktop !== false) {
|
|
3952
|
+
finalDesktop = join(out, "final-desktop.png");
|
|
3953
|
+
await shoot(full, finalDesktop, { screenshot: false, live: false }, { width: 1280, height: 900 });
|
|
3954
|
+
}
|
|
3955
|
+
return { frames, finalThread, finalDesktop };
|
|
3956
|
+
} finally {
|
|
3957
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
3958
|
+
}
|
|
3959
|
+
}
|
|
3960
|
+
|
|
3961
|
+
export { SlackApiError2, Store2, SlackMock2, frames2 };
|
|
@@ -10,17 +10,21 @@ var CHROME_CANDIDATES = [
|
|
|
10
10
|
"/usr/bin/chromium",
|
|
11
11
|
"/usr/bin/chromium-browser"
|
|
12
12
|
];
|
|
13
|
-
function
|
|
13
|
+
function findChrome2() {
|
|
14
14
|
for (const c of CHROME_CANDIDATES)
|
|
15
15
|
if (c && existsSync(c))
|
|
16
16
|
return c;
|
|
17
17
|
const fromPath = Bun.which("google-chrome") ?? Bun.which("chromium") ?? Bun.which("chrome");
|
|
18
18
|
return fromPath ?? undefined;
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
const chrome =
|
|
20
|
+
function requireChrome2() {
|
|
21
|
+
const chrome = findChrome2();
|
|
22
22
|
if (!chrome)
|
|
23
23
|
throw new Error("no Chrome/Chromium binary found; set SLACK_MOCK_CHROME=/path/to/chrome");
|
|
24
|
+
return chrome;
|
|
25
|
+
}
|
|
26
|
+
async function screenshot2(url, opts) {
|
|
27
|
+
const chrome = requireChrome2();
|
|
24
28
|
const target = new URL(url);
|
|
25
29
|
if (!target.searchParams.has("screenshot"))
|
|
26
30
|
target.searchParams.set("screenshot", "1");
|
|
@@ -45,4 +49,4 @@ async function screenshot(url, opts) {
|
|
|
45
49
|
return opts.out;
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
export {
|
|
52
|
+
export { findChrome2, requireChrome2, screenshot2 };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export type { Frame, FramesOptions, FramesResult } from "./frames.ts";
|
|
2
|
+
export { frames } from "./frames.ts";
|
|
1
3
|
export { findChrome, screenshot } from "./screenshot.ts";
|
|
2
4
|
export type { Fault, MessageQuery, PostMessageInput, SlackManifest, SlackMockOptions, } from "./server.ts";
|
|
3
5
|
export { SlackApiError, SlackMock } from "./server.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
SlackApiError2,
|
|
4
|
+
Store2,
|
|
5
|
+
SlackMock2,
|
|
6
|
+
frames2
|
|
7
|
+
} from "./index-6c88z809.js";
|
|
7
8
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "./index-
|
|
9
|
+
findChrome2,
|
|
10
|
+
screenshot2
|
|
11
|
+
} from "./index-v2hesj4s.js";
|
|
11
12
|
export {
|
|
12
|
-
SlackApiError,
|
|
13
|
-
SlackMock,
|
|
14
|
-
Store,
|
|
15
|
-
findChrome,
|
|
16
|
-
|
|
13
|
+
SlackApiError2 as SlackApiError,
|
|
14
|
+
SlackMock2 as SlackMock,
|
|
15
|
+
Store2 as Store,
|
|
16
|
+
findChrome2 as findChrome,
|
|
17
|
+
frames2 as frames,
|
|
18
|
+
screenshot2 as screenshot
|
|
17
19
|
};
|
package/dist/screenshot.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Locate a Chrome/Chromium binary for headless screenshots, or undefined. */
|
|
2
2
|
export declare function findChrome(): string | undefined;
|
|
3
|
+
/** Like `findChrome`, but throws when no binary is found. */
|
|
4
|
+
export declare function requireChrome(): string;
|
|
3
5
|
export interface ScreenshotOptions {
|
|
4
6
|
out: string;
|
|
5
7
|
width?: number;
|
package/dist/screenshot.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
findChrome2,
|
|
4
|
+
requireChrome2,
|
|
5
|
+
screenshot2
|
|
6
|
+
} from "./index-v2hesj4s.js";
|
|
6
7
|
export {
|
|
7
|
-
findChrome,
|
|
8
|
-
|
|
8
|
+
findChrome2 as findChrome,
|
|
9
|
+
requireChrome2 as requireChrome,
|
|
10
|
+
screenshot2 as screenshot
|
|
9
11
|
};
|
package/dist/server.d.ts
CHANGED
|
@@ -20,6 +20,10 @@ export interface SlackManifest {
|
|
|
20
20
|
};
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
+
/** Read a manifest given as a path or an object. */
|
|
24
|
+
export declare function loadManifest(manifest: string | SlackManifest | undefined): SlackManifest | undefined;
|
|
25
|
+
/** The bot display name a manifest declares, if any. */
|
|
26
|
+
export declare function manifestAppName(manifest: SlackManifest | undefined): string | undefined;
|
|
23
27
|
export interface SlackMockOptions extends StoreOptions {
|
|
24
28
|
/** 0 picks a free port. */
|
|
25
29
|
port?: number;
|
package/dist/store.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export declare class SlackApiError extends Error {
|
|
|
7
7
|
export interface StoreOptions {
|
|
8
8
|
/** Append every change as one JSON line to this file and replay it on start. */
|
|
9
9
|
dataFile?: string;
|
|
10
|
+
/** Changes to apply on construction, as if read from `dataFile`. Nothing is written. */
|
|
11
|
+
replay?: Change[];
|
|
10
12
|
teamId?: string;
|
|
11
13
|
teamName?: string;
|
|
12
14
|
teamDomain?: string;
|
|
@@ -48,6 +50,13 @@ export interface Page<T> {
|
|
|
48
50
|
has_more: boolean;
|
|
49
51
|
next_cursor: string;
|
|
50
52
|
}
|
|
53
|
+
/** One parsed journal line and its 1-based line number. */
|
|
54
|
+
export interface JournalEntry {
|
|
55
|
+
line: number;
|
|
56
|
+
change: Change;
|
|
57
|
+
}
|
|
58
|
+
/** Parse a JSONL journal. Corrupt lines are skipped with a warning; blank lines are ignored. */
|
|
59
|
+
export declare function parseJournal(text: string, source?: string): JournalEntry[];
|
|
51
60
|
/** In-memory Slack workspace with an optional append-only JSONL journal. */
|
|
52
61
|
export declare class Store {
|
|
53
62
|
readonly team: {
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
import { parseArgs } from "node:util";
|
|
3
|
+
import { frames } from "./frames.ts";
|
|
3
4
|
import { screenshot } from "./screenshot.ts";
|
|
4
5
|
import { SlackMock } from "./server.ts";
|
|
5
6
|
|
|
@@ -9,6 +10,11 @@ Usage:
|
|
|
9
10
|
slack-mock serve [--port 4040] [--host 127.0.0.1] [--data ./data/slack.jsonl] [--manifest app.json]
|
|
10
11
|
[--no-seed] [--quiet] [--panel 50] [--auth user:pass] [--public-url https://mock.example.com]
|
|
11
12
|
slack-mock screenshot <url> --out shot.png [--width 800] [--height 1000]
|
|
13
|
+
slack-mock frames --journal data.jsonl --channel C0GENERAL0 [--thread 1788516626.286000] --out ./frames
|
|
14
|
+
[--manifest app.json] [--width 800] [--height 700] [--no-desktop]
|
|
15
|
+
|
|
16
|
+
frames renders the thread (or, without --thread, the channel) as it looked after every journal
|
|
17
|
+
line that touched it: one PNG per line, then final-thread.png and final-desktop.png.
|
|
12
18
|
|
|
13
19
|
serve prints the env vars to give your bot (SLACK_BOT_TOKEN, SLACK_APP_TOKEN, SLACK_API_URL)
|
|
14
20
|
and the URLs of the HTML views. Inject messages with the admin API, e.g.
|
|
@@ -84,6 +90,37 @@ async function main(argv: string[]): Promise<void> {
|
|
|
84
90
|
console.log(out);
|
|
85
91
|
return;
|
|
86
92
|
}
|
|
93
|
+
if (command === "frames") {
|
|
94
|
+
const { values } = parseArgs({
|
|
95
|
+
args: rest,
|
|
96
|
+
options: {
|
|
97
|
+
journal: { type: "string" },
|
|
98
|
+
channel: { type: "string" },
|
|
99
|
+
thread: { type: "string" },
|
|
100
|
+
out: { type: "string" },
|
|
101
|
+
manifest: { type: "string" },
|
|
102
|
+
width: { type: "string", default: "800" },
|
|
103
|
+
height: { type: "string", default: "700" },
|
|
104
|
+
"no-desktop": { type: "boolean", default: false },
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
if (!values.journal || !values.channel || !values.out)
|
|
108
|
+
throw new Error("frames needs --journal, --channel and --out");
|
|
109
|
+
const result = await frames({
|
|
110
|
+
journal: values.journal,
|
|
111
|
+
channel: values.channel,
|
|
112
|
+
thread: values.thread,
|
|
113
|
+
out: values.out,
|
|
114
|
+
manifest: values.manifest,
|
|
115
|
+
width: Number(values.width),
|
|
116
|
+
height: Number(values.height),
|
|
117
|
+
desktop: !values["no-desktop"],
|
|
118
|
+
});
|
|
119
|
+
for (const f of result.frames) console.log(`${f.index}\t${f.kind}\t${f.path}`);
|
|
120
|
+
console.log(`final-thread=${result.finalThread}`);
|
|
121
|
+
if (result.finalDesktop) console.log(`final-desktop=${result.finalDesktop}`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
87
124
|
console.log(USAGE);
|
|
88
125
|
if (command && command !== "help" && command !== "--help") process.exit(1);
|
|
89
126
|
}
|
package/src/frames.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// Render a thread (or a channel) as it looked after each journal line that touched it: one PNG
|
|
2
|
+
// per line. The journal is replayed in memory and each page is written to a temp file that
|
|
3
|
+
// headless Chrome opens over file://, so no server is started.
|
|
4
|
+
|
|
5
|
+
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join, resolve } from "node:path";
|
|
8
|
+
import { type RenderOptions, renderPage, type View } from "./render/index.ts";
|
|
9
|
+
import { requireChrome, screenshot } from "./screenshot.ts";
|
|
10
|
+
import { loadManifest, manifestAppName, type SlackManifest } from "./server.ts";
|
|
11
|
+
import { type JournalEntry, parseJournal, Store } from "./store.ts";
|
|
12
|
+
|
|
13
|
+
export interface FramesOptions {
|
|
14
|
+
/** Path to a JSONL journal written by `dataFile`. */
|
|
15
|
+
journal: string;
|
|
16
|
+
/** Channel id or name. */
|
|
17
|
+
channel: string;
|
|
18
|
+
/** Thread root ts. When absent, render the channel view instead of the thread view. */
|
|
19
|
+
thread?: string;
|
|
20
|
+
/** Output directory. Created when missing. */
|
|
21
|
+
out: string;
|
|
22
|
+
/** Slack app manifest path or object, same as `SlackMockOptions.manifest`. */
|
|
23
|
+
manifest?: string | SlackManifest;
|
|
24
|
+
/** Viewport width in px. Default 800. */
|
|
25
|
+
width?: number;
|
|
26
|
+
/** Viewport height in px. Default 700. */
|
|
27
|
+
height?: number;
|
|
28
|
+
/** Also render `final-desktop.png` (`?screenshot=0`, 1280x900). Default true. */
|
|
29
|
+
desktop?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Frame {
|
|
33
|
+
/** 1-based journal line number the frame was rendered after. */
|
|
34
|
+
index: number;
|
|
35
|
+
/** Journal `kind` of that line. */
|
|
36
|
+
kind: string;
|
|
37
|
+
/** Absolute PNG path. */
|
|
38
|
+
path: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface FramesResult {
|
|
42
|
+
frames: Frame[];
|
|
43
|
+
/** Copy of the last frame. */
|
|
44
|
+
finalThread: string;
|
|
45
|
+
/** Present when `desktop` is true. */
|
|
46
|
+
finalDesktop?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const FRAME_KINDS = new Set([
|
|
50
|
+
"message.add",
|
|
51
|
+
"message.update",
|
|
52
|
+
"message.delete",
|
|
53
|
+
"reaction.add",
|
|
54
|
+
"reaction.remove",
|
|
55
|
+
"file.add",
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Journal entries that change what the thread (or, without `thread`, the channel) shows.
|
|
60
|
+
* `file.add` carries no message, so a file only shows up with the message that shares it.
|
|
61
|
+
*/
|
|
62
|
+
export function selectFrames(
|
|
63
|
+
entries: JournalEntry[],
|
|
64
|
+
channel: string,
|
|
65
|
+
thread?: string,
|
|
66
|
+
): JournalEntry[] {
|
|
67
|
+
return entries.filter(({ change }) => {
|
|
68
|
+
if (!FRAME_KINDS.has(change.kind) || !("message" in change)) return false;
|
|
69
|
+
const m = change.message;
|
|
70
|
+
if (m.channel !== channel) return false;
|
|
71
|
+
return thread ? m.ts === thread || m.thread_ts === thread : true;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function frames(opts: FramesOptions): Promise<FramesResult> {
|
|
76
|
+
const entries = parseJournal(readFileSync(opts.journal, "utf8"), opts.journal);
|
|
77
|
+
const appName = manifestAppName(loadManifest(opts.manifest));
|
|
78
|
+
const storeAfter = (line: number) =>
|
|
79
|
+
new Store({ replay: entries.filter((e) => e.line <= line).map((e) => e.change), appName });
|
|
80
|
+
const full = storeAfter(Number.POSITIVE_INFINITY);
|
|
81
|
+
const channel = full.channels.get(opts.channel) ?? full.channelByName(opts.channel);
|
|
82
|
+
if (!channel) throw new Error(`channel ${opts.channel} not found in ${opts.journal}`);
|
|
83
|
+
const what = opts.thread ? `thread ${opts.thread} in ${channel.id}` : `channel ${channel.id}`;
|
|
84
|
+
const selected = selectFrames(entries, channel.id, opts.thread);
|
|
85
|
+
if (!selected.length) throw new Error(`no line of ${opts.journal} touches ${what}`);
|
|
86
|
+
requireChrome();
|
|
87
|
+
|
|
88
|
+
const view: View = opts.thread
|
|
89
|
+
? { kind: "thread", channel: channel.id, ts: opts.thread }
|
|
90
|
+
: { kind: "channel", channel: channel.id };
|
|
91
|
+
const out = resolve(opts.out);
|
|
92
|
+
mkdirSync(out, { recursive: true });
|
|
93
|
+
const tmp = mkdtempSync(join(tmpdir(), "slack-mock-frames-"));
|
|
94
|
+
const html = join(tmp, "frame.html");
|
|
95
|
+
const shoot = async (
|
|
96
|
+
store: Store,
|
|
97
|
+
file: string,
|
|
98
|
+
render: RenderOptions,
|
|
99
|
+
size: { width: number; height: number },
|
|
100
|
+
) => {
|
|
101
|
+
writeFileSync(html, renderPage(store, view, render));
|
|
102
|
+
await screenshot(Bun.pathToFileURL(html).href, { out: file, ...size });
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
const frames: Frame[] = [];
|
|
106
|
+
const size = { width: opts.width ?? 800, height: opts.height ?? 700 };
|
|
107
|
+
for (const [i, { line, change }] of selected.entries()) {
|
|
108
|
+
const path = join(out, `${String(i + 1).padStart(2, "0")}-${change.kind}.png`);
|
|
109
|
+
await shoot(storeAfter(line), path, { screenshot: true }, size);
|
|
110
|
+
frames.push({ index: line, kind: change.kind, path });
|
|
111
|
+
}
|
|
112
|
+
const finalThread = join(out, "final-thread.png");
|
|
113
|
+
copyFileSync(frames[frames.length - 1]!.path, finalThread);
|
|
114
|
+
let finalDesktop: string | undefined;
|
|
115
|
+
if (opts.desktop !== false) {
|
|
116
|
+
finalDesktop = join(out, "final-desktop.png");
|
|
117
|
+
await shoot(
|
|
118
|
+
full,
|
|
119
|
+
finalDesktop,
|
|
120
|
+
{ screenshot: false, live: false },
|
|
121
|
+
{ width: 1280, height: 900 },
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return { frames, finalThread, finalDesktop };
|
|
125
|
+
} finally {
|
|
126
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
127
|
+
}
|
|
128
|
+
}
|
package/src/index.ts
CHANGED
package/src/screenshot.ts
CHANGED
|
@@ -17,6 +17,14 @@ export function findChrome(): string | undefined {
|
|
|
17
17
|
return fromPath ?? undefined;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/** Like `findChrome`, but throws when no binary is found. */
|
|
21
|
+
export function requireChrome(): string {
|
|
22
|
+
const chrome = findChrome();
|
|
23
|
+
if (!chrome)
|
|
24
|
+
throw new Error("no Chrome/Chromium binary found; set SLACK_MOCK_CHROME=/path/to/chrome");
|
|
25
|
+
return chrome;
|
|
26
|
+
}
|
|
27
|
+
|
|
20
28
|
export interface ScreenshotOptions {
|
|
21
29
|
out: string;
|
|
22
30
|
width?: number;
|
|
@@ -29,9 +37,7 @@ export interface ScreenshotOptions {
|
|
|
29
37
|
* mock's pages drop their navigation chrome.
|
|
30
38
|
*/
|
|
31
39
|
export async function screenshot(url: string, opts: ScreenshotOptions): Promise<string> {
|
|
32
|
-
const chrome =
|
|
33
|
-
if (!chrome)
|
|
34
|
-
throw new Error("no Chrome/Chromium binary found; set SLACK_MOCK_CHROME=/path/to/chrome");
|
|
40
|
+
const chrome = requireChrome();
|
|
35
41
|
const target = new URL(url);
|
|
36
42
|
if (!target.searchParams.has("screenshot")) target.searchParams.set("screenshot", "1");
|
|
37
43
|
const proc = Bun.spawn(
|
package/src/server.ts
CHANGED
|
@@ -45,6 +45,20 @@ export interface SlackManifest {
|
|
|
45
45
|
settings?: { event_subscriptions?: { bot_events?: string[] } };
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/** Read a manifest given as a path or an object. */
|
|
49
|
+
export function loadManifest(
|
|
50
|
+
manifest: string | SlackManifest | undefined,
|
|
51
|
+
): SlackManifest | undefined {
|
|
52
|
+
return typeof manifest === "string"
|
|
53
|
+
? (JSON.parse(readFileSync(manifest, "utf8")) as SlackManifest)
|
|
54
|
+
: manifest;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The bot display name a manifest declares, if any. */
|
|
58
|
+
export function manifestAppName(manifest: SlackManifest | undefined): string | undefined {
|
|
59
|
+
return manifest?.features?.bot_user?.display_name ?? manifest?.display_information?.name;
|
|
60
|
+
}
|
|
61
|
+
|
|
48
62
|
export interface SlackMockOptions extends StoreOptions {
|
|
49
63
|
/** 0 picks a free port. */
|
|
50
64
|
port?: number;
|
|
@@ -165,14 +179,8 @@ export class SlackMock {
|
|
|
165
179
|
static Error = SlackApiError;
|
|
166
180
|
|
|
167
181
|
constructor(opts: SlackMockOptions = {}) {
|
|
168
|
-
const manifest =
|
|
169
|
-
|
|
170
|
-
? (JSON.parse(readFileSync(opts.manifest, "utf8")) as SlackManifest)
|
|
171
|
-
: opts.manifest;
|
|
172
|
-
const appName =
|
|
173
|
-
opts.appName ??
|
|
174
|
-
manifest?.features?.bot_user?.display_name ??
|
|
175
|
-
manifest?.display_information?.name;
|
|
182
|
+
const manifest = loadManifest(opts.manifest);
|
|
183
|
+
const appName = opts.appName ?? manifestAppName(manifest);
|
|
176
184
|
this.opts = { ...opts, appName };
|
|
177
185
|
this.log =
|
|
178
186
|
typeof opts.log === "function"
|
package/src/store.ts
CHANGED
|
@@ -24,6 +24,8 @@ export class SlackApiError extends Error {
|
|
|
24
24
|
export interface StoreOptions {
|
|
25
25
|
/** Append every change as one JSON line to this file and replay it on start. */
|
|
26
26
|
dataFile?: string;
|
|
27
|
+
/** Changes to apply on construction, as if read from `dataFile`. Nothing is written. */
|
|
28
|
+
replay?: Change[];
|
|
27
29
|
teamId?: string;
|
|
28
30
|
teamName?: string;
|
|
29
31
|
teamDomain?: string;
|
|
@@ -70,6 +72,30 @@ export interface Page<T> {
|
|
|
70
72
|
next_cursor: string;
|
|
71
73
|
}
|
|
72
74
|
|
|
75
|
+
/** One parsed journal line and its 1-based line number. */
|
|
76
|
+
export interface JournalEntry {
|
|
77
|
+
line: number;
|
|
78
|
+
change: Change;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Parse a JSONL journal. Corrupt lines are skipped with a warning; blank lines are ignored. */
|
|
82
|
+
export function parseJournal(text: string, source = "journal"): JournalEntry[] {
|
|
83
|
+
const entries: JournalEntry[] = [];
|
|
84
|
+
let line = 0;
|
|
85
|
+
for (const raw of text.split("\n")) {
|
|
86
|
+
line += 1;
|
|
87
|
+
if (!raw.trim()) continue;
|
|
88
|
+
try {
|
|
89
|
+
entries.push({ line, change: JSON.parse(raw) as Change });
|
|
90
|
+
} catch (e) {
|
|
91
|
+
console.warn(
|
|
92
|
+
`[slack-mock] skipping corrupt journal line ${line} in ${source}: ${e instanceof Error ? e.message : e}`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return entries;
|
|
97
|
+
}
|
|
98
|
+
|
|
73
99
|
/** In-memory Slack workspace with an optional append-only JSONL journal. */
|
|
74
100
|
export class Store {
|
|
75
101
|
readonly team: { id: string; name: string; domain: string };
|
|
@@ -112,7 +138,11 @@ export class Store {
|
|
|
112
138
|
userToken: opts.userToken,
|
|
113
139
|
};
|
|
114
140
|
this.dataFile = opts.dataFile;
|
|
115
|
-
if (this.dataFile && existsSync(this.dataFile))
|
|
141
|
+
if (this.dataFile && existsSync(this.dataFile))
|
|
142
|
+
this.replay(
|
|
143
|
+
parseJournal(readFileSync(this.dataFile, "utf8"), this.dataFile).map((e) => e.change),
|
|
144
|
+
);
|
|
145
|
+
if (opts.replay) this.replay(opts.replay);
|
|
116
146
|
if (!this.users.has(this.bot.userId)) {
|
|
117
147
|
this.addUser({
|
|
118
148
|
id: this.bot.userId,
|
|
@@ -145,21 +175,10 @@ export class Store {
|
|
|
145
175
|
for (const l of this.listeners) l(change);
|
|
146
176
|
}
|
|
147
177
|
|
|
148
|
-
private replay(
|
|
178
|
+
private replay(changes: Change[]): void {
|
|
149
179
|
this.replaying = true;
|
|
150
180
|
try {
|
|
151
|
-
|
|
152
|
-
for (const line of readFileSync(file, "utf8").split("\n")) {
|
|
153
|
-
lineNo += 1;
|
|
154
|
-
if (!line.trim()) continue;
|
|
155
|
-
try {
|
|
156
|
-
this.applyReplayed(JSON.parse(line) as Change);
|
|
157
|
-
} catch (e) {
|
|
158
|
-
console.warn(
|
|
159
|
-
`[slack-mock] skipping corrupt journal line ${lineNo} in ${file}: ${e instanceof Error ? e.message : e}`,
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
181
|
+
for (const change of changes) this.applyReplayed(change);
|
|
163
182
|
} finally {
|
|
164
183
|
this.replaying = false;
|
|
165
184
|
}
|