@slack/radar-mcp 1.26.0 → 1.27.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 +9 -2
- package/dist/mcp/api-paths.d.ts +25 -1
- package/dist/mcp/api-paths.js +85 -9
- package/dist/mcp/index.js +23 -4
- package/dist/mcp/snapshot-cli.d.ts +1 -1
- package/dist/mcp/snapshot-cli.js +16 -1
- package/dist/mcp/snapshot.js +46 -24
- package/dist/mcp/test-runs-tools.d.ts +2 -0
- package/dist/mcp/test-runs-tools.js +58 -5
- package/dist/mcp/tools.js +25 -3
- package/dist/shared/android.d.ts +15 -4
- package/dist/shared/android.js +102 -12
- package/dist/shared/constants.d.ts +6 -5
- package/dist/shared/constants.js +6 -5
- package/dist/shared/switchboard-note.js +2 -1
- package/dist/shared/switchboard-report.js +1 -1
- package/dist/shared/test-case-capture.d.ts +15 -0
- package/dist/shared/test-case-capture.js +235 -0
- package/dist/shared/test-case-store.d.ts +54 -0
- package/dist/shared/test-case-store.js +208 -0
- package/dist/shared/transport.d.ts +9 -1
- package/dist/web/install-app.js +1 -1
- package/dist/web/public/index.html +1 -0
- package/dist/web/public/next/app.js +12 -1
- package/dist/web/public/next/feed.js +17 -2
- package/dist/web/public/next/filters.js +5 -5
- package/dist/web/public/next/registry.js +3 -2
- package/dist/web/public/next/save-case.js +92 -0
- package/dist/web/public/next/store.js +3 -1
- package/dist/web/public/next/switchboard-note.js +2 -1
- package/dist/web/public/next/test-cases.js +328 -0
- package/dist/web/public/next/voice.js +1 -1
- package/dist/web/server.d.ts +1 -0
- package/dist/web/server.js +214 -2
- package/dist/web/spec.d.ts +3 -2
- package/dist/web/spec.js +10 -9
- package/package.json +1 -1
- package/dist/mcp/test-cases.d.ts +0 -14
- package/dist/mcp/test-cases.js +0 -77
- /package/dist/web/public/icons/{radar-1024.png → icon-1024.png} +0 -0
- /package/dist/web/public/icons/{radar.svg → icon.svg} +0 -0
package/dist/shared/android.d.ts
CHANGED
|
@@ -117,6 +117,12 @@ export interface AndroidTransportDependencies {
|
|
|
117
117
|
spawnLogcat?: () => ChildProcess;
|
|
118
118
|
/** Start the `ps` listing that labels log rows with their package. */
|
|
119
119
|
spawnProcessList?: SpawnAndroidProcessList;
|
|
120
|
+
/**
|
|
121
|
+
* Let adb pick a free host port (`adb forward tcp:0`) instead of the shared RADAR_PORT, so
|
|
122
|
+
* parallel runs on one host never meet on a port. Only the headless snapshot command opts in.
|
|
123
|
+
* SLACK_RADAR_DEVICE_PORT, when it gives a usable port, still wins and keeps today's behavior.
|
|
124
|
+
*/
|
|
125
|
+
ownForwardPort?: boolean;
|
|
120
126
|
}
|
|
121
127
|
/** First `versionName` / `versionCode` in a `dumpsys package` dump (one block per user repeats them). */
|
|
122
128
|
export declare function parsePackageVersion(dump: string): {
|
|
@@ -163,17 +169,22 @@ export declare class AndroidTransport implements DeviceTransport {
|
|
|
163
169
|
private lastTokenViaArgv;
|
|
164
170
|
private selectedDevice;
|
|
165
171
|
private readonly dashboardPidPackages;
|
|
172
|
+
private readonly ownForwardPort;
|
|
173
|
+
private ownedPort;
|
|
166
174
|
constructor(dependencies?: AndroidTransportDependencies);
|
|
167
175
|
ensureForward(): boolean;
|
|
168
176
|
private rebuildForward;
|
|
177
|
+
private forwardFreePort;
|
|
178
|
+
private noPinReason;
|
|
179
|
+
releaseForward(): void;
|
|
169
180
|
reassertForward(): boolean;
|
|
170
181
|
private recordForwardIfListed;
|
|
171
182
|
/**
|
|
172
183
|
* reassertForward, off the event loop. Same listing-first-then-rebuild logic and the same
|
|
173
|
-
* state fields, but every adb call runs through execFileAsync so a
|
|
174
|
-
* serving HTTP while adb is slow (the dashboard's boot-arm chain) is
|
|
175
|
-
* inside a single call. The sync twin stays for the MCP request paths,
|
|
176
|
-
* else to do while adb runs.
|
|
184
|
+
* state fields for the shared port, but every adb call runs through execFileAsync so a
|
|
185
|
+
* caller that must keep serving HTTP while adb is slow (the dashboard's boot-arm chain) is
|
|
186
|
+
* never blocked, even inside a single call. The sync twin stays for the MCP request paths,
|
|
187
|
+
* which have nothing else to do while adb runs. An own-port transport takes the sync twin.
|
|
177
188
|
*/
|
|
178
189
|
reassertForwardAsync(): Promise<boolean>;
|
|
179
190
|
private rebuildForwardAsync;
|
package/dist/shared/android.js
CHANGED
|
@@ -5,7 +5,7 @@ import path from "path";
|
|
|
5
5
|
import { promisify } from "util";
|
|
6
6
|
import { readDebugLogBuffer } from "./debug-log.js";
|
|
7
7
|
import { filterLogLines, getCaptureFilePath, isCapturing, readRecentLogs, registerCleanup, rotateCaptureIfNeeded, startCapture, stopCapture, } from "./logcat-capture.js";
|
|
8
|
-
import { RADAR_PORT, RADAR_VERSION, slackDebugPackage, debugPackageFromPsName, debugPackageOverride, debugPackageOverrideError, SHIPPED_DEBUG_PACKAGES, setSlackDebugPackage, } from "./constants.js";
|
|
8
|
+
import { RADAR_PORT, setRadarPort, RADAR_VERSION, slackDebugPackage, debugPackageFromPsName, debugPackageOverride, debugPackageOverrideError, SHIPPED_DEBUG_PACKAGES, setSlackDebugPackage, } from "./constants.js";
|
|
9
9
|
import { getOrCreateRadarToken } from "./radar-token.js";
|
|
10
10
|
import { AmbiguousDebugPackageError, AndroidUserIdError, BareEnableError, DebugPackageOverrideError, NoDebugBuildRunningError, UnknownDebugBuildError, } from "./enable-diagnosis.js";
|
|
11
11
|
import { LogSession } from "./log-session.js";
|
|
@@ -450,9 +450,11 @@ export function parseAdbDevicesOutput(raw) {
|
|
|
450
450
|
if (parts.length < 2)
|
|
451
451
|
continue;
|
|
452
452
|
const serial = parts[0];
|
|
453
|
-
|
|
453
|
+
// adb prints "no permissions" as two words; reading just "no" would make a bad message.
|
|
454
|
+
const twoWord = parts[1] === "no" && parts[2] === "permissions";
|
|
455
|
+
const state = twoWord ? "no permissions" : parts[1];
|
|
454
456
|
let label = serial;
|
|
455
|
-
for (const field of parts.slice(2)) {
|
|
457
|
+
for (const field of parts.slice(twoWord ? 3 : 2)) {
|
|
456
458
|
if (field.startsWith("model:")) {
|
|
457
459
|
label = field.slice("model:".length).replace(/_/g, " ");
|
|
458
460
|
break;
|
|
@@ -600,7 +602,12 @@ export class AndroidTransport {
|
|
|
600
602
|
// body and web /health. Not cleared by resetState: the env pin is sticky by design.
|
|
601
603
|
selectedDevice = null;
|
|
602
604
|
dashboardPidPackages;
|
|
605
|
+
ownForwardPort;
|
|
606
|
+
// The port adb gave this process; null until the first own-port forward lands.
|
|
607
|
+
ownedPort = null;
|
|
603
608
|
constructor(dependencies = {}) {
|
|
609
|
+
this.ownForwardPort =
|
|
610
|
+
dependencies.ownForwardPort === true && !(Number(process.env.SLACK_RADAR_DEVICE_PORT) || 0);
|
|
604
611
|
this.dashboardPidPackages = new AndroidPidPackageMap(dependencies.spawnProcessList);
|
|
605
612
|
const spawnLogcat = dependencies.spawnLogcat ?? spawnAndroidLogcat;
|
|
606
613
|
this.logSession = new LogSession(() => {
|
|
@@ -623,6 +630,8 @@ export class AndroidTransport {
|
|
|
623
630
|
this.lastForwardError = adbNotFoundError();
|
|
624
631
|
return false;
|
|
625
632
|
}
|
|
633
|
+
if (this.ownForwardPort)
|
|
634
|
+
return this.forwardFreePort();
|
|
626
635
|
// Drop any existing mapping first. Re-issuing `adb forward` over a stale mapping
|
|
627
636
|
// (one whose device end died, e.g. after a device disconnect, OR one a separate
|
|
628
637
|
// process on this host repointed to a different device) has been observed to leave
|
|
@@ -652,6 +661,77 @@ export class AndroidTransport {
|
|
|
652
661
|
return false;
|
|
653
662
|
}
|
|
654
663
|
}
|
|
664
|
+
// Own-port mode: ask adb for a free port. Never removes anything on a rebuild, because an
|
|
665
|
+
// old port we no longer see in the listing may be gone or now someone else's.
|
|
666
|
+
forwardFreePort() {
|
|
667
|
+
// Without a pin the listing check is skipped, so every read would take a new port.
|
|
668
|
+
if (!process.env.ANDROID_SERIAL) {
|
|
669
|
+
this.lastForwardError = this.noPinReason();
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
try {
|
|
673
|
+
const out = execSync(`${ADB} forward tcp:0 localabstract:${SOCKET_NAME}`, {
|
|
674
|
+
timeout: 5000,
|
|
675
|
+
encoding: "utf-8",
|
|
676
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
677
|
+
}).trim();
|
|
678
|
+
const port = /^\d+$/.test(out) ? Number(out) : 0;
|
|
679
|
+
if (port < 1 || port > 65535) {
|
|
680
|
+
this.lastForwardError = "adb did not report the forwarded port";
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
setRadarPort(port);
|
|
684
|
+
this.ownedPort = port;
|
|
685
|
+
this.portForwarded = true;
|
|
686
|
+
this.lastForwardError = null;
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
catch (e) {
|
|
690
|
+
this.lastForwardError = e.message;
|
|
691
|
+
return false;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
// Selecting a device leaves the pin empty when nothing is in the `device` state, so say
|
|
695
|
+
// what the listing shows (an offline or unauthorized device) instead of a bare "not pinned".
|
|
696
|
+
noPinReason() {
|
|
697
|
+
const devices = this.listDevices();
|
|
698
|
+
// A failed listing could not tell us anything, so it never reads as "nothing attached".
|
|
699
|
+
if (devices === null)
|
|
700
|
+
return "could not list devices (adb devices failed)";
|
|
701
|
+
if (devices.length === 0)
|
|
702
|
+
return "no device is attached";
|
|
703
|
+
if (devices.some((d) => d.state === "device")) {
|
|
704
|
+
return "no device is pinned, so the forwarded port cannot be checked";
|
|
705
|
+
}
|
|
706
|
+
return `no device is ready: ${devices.map((d) => `${d.serial} (${d.state})`).join(", ")}`;
|
|
707
|
+
}
|
|
708
|
+
// Removes the forward this process made, and only while the listing still shows it as ours
|
|
709
|
+
// (same serial, same socket). Best effort: a forward left behind stays until the device
|
|
710
|
+
// disconnects or the adb server restarts.
|
|
711
|
+
releaseForward() {
|
|
712
|
+
const serial = process.env.ANDROID_SERIAL;
|
|
713
|
+
if (!this.ownForwardPort || this.ownedPort === null || !serial || !ADB)
|
|
714
|
+
return;
|
|
715
|
+
try {
|
|
716
|
+
const list = execSync(`${ADB} forward --list`, {
|
|
717
|
+
timeout: 5000,
|
|
718
|
+
encoding: "utf-8",
|
|
719
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
720
|
+
});
|
|
721
|
+
const ours = `${serial} tcp:${this.ownedPort} localabstract:${SOCKET_NAME}`;
|
|
722
|
+
if (!list.split("\n").some((line) => line.trim() === ours))
|
|
723
|
+
return;
|
|
724
|
+
execSync(`${ADB} forward --remove tcp:${this.ownedPort}`, {
|
|
725
|
+
timeout: 5000,
|
|
726
|
+
stdio: ["ignore", "ignore", "ignore"],
|
|
727
|
+
});
|
|
728
|
+
// Gone now, so a second call (end of run, then exit hook) has nothing to remove.
|
|
729
|
+
this.ownedPort = null;
|
|
730
|
+
}
|
|
731
|
+
catch {
|
|
732
|
+
// Best effort.
|
|
733
|
+
}
|
|
734
|
+
}
|
|
655
735
|
// Called before every HTTP-based device read (see transport.ts's doc comment for why:
|
|
656
736
|
// this process's own portForwarded cache cannot detect a repoint made by a SEPARATE
|
|
657
737
|
// process sharing the host-global port). A full forward --remove + add on every read
|
|
@@ -661,7 +741,9 @@ export class AndroidTransport {
|
|
|
661
741
|
// RADAR_PORT at our pinned serial.
|
|
662
742
|
reassertForward() {
|
|
663
743
|
const serial = process.env.ANDROID_SERIAL;
|
|
664
|
-
|
|
744
|
+
// Before our own port exists, RADAR_PORT is still the shared default and may be someone else's.
|
|
745
|
+
const portIsOurs = !this.ownForwardPort || this.ownedPort !== null;
|
|
746
|
+
if (serial && ADB && portIsOurs) {
|
|
665
747
|
try {
|
|
666
748
|
const list = execSync(`${ADB} forward --list`, {
|
|
667
749
|
timeout: 5000,
|
|
@@ -672,8 +754,12 @@ export class AndroidTransport {
|
|
|
672
754
|
return true;
|
|
673
755
|
}
|
|
674
756
|
catch {
|
|
675
|
-
//
|
|
676
|
-
//
|
|
757
|
+
// Could not tell is not "gone": a rebuild here would take a second port while the
|
|
758
|
+
// first is still live. Only own-port mode refuses; the shared port keeps the rebuild.
|
|
759
|
+
if (this.ownForwardPort) {
|
|
760
|
+
this.lastForwardError = "adb could not list forwards";
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
677
763
|
}
|
|
678
764
|
}
|
|
679
765
|
return this.rebuildForward();
|
|
@@ -690,12 +776,16 @@ export class AndroidTransport {
|
|
|
690
776
|
}
|
|
691
777
|
/**
|
|
692
778
|
* reassertForward, off the event loop. Same listing-first-then-rebuild logic and the same
|
|
693
|
-
* state fields, but every adb call runs through execFileAsync so a
|
|
694
|
-
* serving HTTP while adb is slow (the dashboard's boot-arm chain) is
|
|
695
|
-
* inside a single call. The sync twin stays for the MCP request paths,
|
|
696
|
-
* else to do while adb runs.
|
|
779
|
+
* state fields for the shared port, but every adb call runs through execFileAsync so a
|
|
780
|
+
* caller that must keep serving HTTP while adb is slow (the dashboard's boot-arm chain) is
|
|
781
|
+
* never blocked, even inside a single call. The sync twin stays for the MCP request paths,
|
|
782
|
+
* which have nothing else to do while adb runs. An own-port transport takes the sync twin.
|
|
697
783
|
*/
|
|
698
784
|
async reassertForwardAsync() {
|
|
785
|
+
// Own-port rules (keep the port on a failed listing, never touch the shared port) live
|
|
786
|
+
// only in the sync path, so this one defers to it instead of repeating them.
|
|
787
|
+
if (this.ownForwardPort)
|
|
788
|
+
return this.reassertForward();
|
|
699
789
|
const serial = process.env.ANDROID_SERIAL;
|
|
700
790
|
if (serial && ADB) {
|
|
701
791
|
try {
|
|
@@ -712,8 +802,8 @@ export class AndroidTransport {
|
|
|
712
802
|
}
|
|
713
803
|
return this.rebuildForwardAsync();
|
|
714
804
|
}
|
|
715
|
-
// rebuildForward, off the event loop
|
|
716
|
-
//
|
|
805
|
+
// rebuildForward's shared-port branch, off the event loop: drop any existing mapping
|
|
806
|
+
// (failure is harmless), then add ours and record the outcome.
|
|
717
807
|
async rebuildForwardAsync() {
|
|
718
808
|
if (!ADB) {
|
|
719
809
|
this.lastForwardError = adbNotFoundError();
|
|
@@ -25,11 +25,12 @@ export declare const RADAR_VERSION: string;
|
|
|
25
25
|
*/
|
|
26
26
|
export declare let RADAR_PORT: number;
|
|
27
27
|
/**
|
|
28
|
-
* Override RADAR_PORT after module load. Called
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
28
|
+
* Override RADAR_PORT after module load. Called by the webapp auto-detect/explicit-platform
|
|
29
|
+
* path in mcp/index.ts's createTransport(), and by the headless snapshot command's own-port
|
|
30
|
+
* forward (AndroidTransport with ownForwardPort) -- never on the MCP server's android path,
|
|
31
|
+
* so android's RADAR_PORT resolution there stays byte-for-byte identical to today. ES module
|
|
32
|
+
* `let` exports are live bindings, so every existing importer (stream.ts, android.ts,
|
|
33
|
+
* index.ts's radarRequest) sees the new value immediately, with no re-import.
|
|
33
34
|
*/
|
|
34
35
|
export declare function setRadarPort(port: number): void;
|
|
35
36
|
/** Host address for the forwarded connection. */
|
package/dist/shared/constants.js
CHANGED
|
@@ -38,11 +38,12 @@ export const RADAR_VERSION = (() => {
|
|
|
38
38
|
*/
|
|
39
39
|
export let RADAR_PORT = Number(process.env.SLACK_RADAR_DEVICE_PORT) || 8099;
|
|
40
40
|
/**
|
|
41
|
-
* Override RADAR_PORT after module load. Called
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
41
|
+
* Override RADAR_PORT after module load. Called by the webapp auto-detect/explicit-platform
|
|
42
|
+
* path in mcp/index.ts's createTransport(), and by the headless snapshot command's own-port
|
|
43
|
+
* forward (AndroidTransport with ownForwardPort) -- never on the MCP server's android path,
|
|
44
|
+
* so android's RADAR_PORT resolution there stays byte-for-byte identical to today. ES module
|
|
45
|
+
* `let` exports are live bindings, so every existing importer (stream.ts, android.ts,
|
|
46
|
+
* index.ts's radarRequest) sees the new value immediately, with no re-import.
|
|
46
47
|
*/
|
|
47
48
|
export function setRadarPort(port) {
|
|
48
49
|
RADAR_PORT = port;
|
|
@@ -67,7 +67,8 @@ export const SAVED_TURN_OUTCOMES = [...Object.keys(SAVED_TURN_KEPT), ...SAVED_TU
|
|
|
67
67
|
// A card record in words, shared by the feed row, the voice panel and the report. A kind or event
|
|
68
68
|
// the device adds later shows as sent, and a missing field reads "?", so nothing throws.
|
|
69
69
|
const CARD_KIND_WORDS = { tool_approval: "tool approval", auth_prompt: "connect prompt" };
|
|
70
|
-
// An event in this set takes the card down; "shown" and "carried" put it up.
|
|
70
|
+
// An event in this set takes the card down; "shown" and "carried" put it up. "answer_failed" is in
|
|
71
|
+
// neither: the card stays as it was.
|
|
71
72
|
const CARD_CLOSING_EVENTS = ["answered", "refused", "declined", "closed", "replaced", "expired", "cleared"];
|
|
72
73
|
const CARD_OPENING_EVENTS = ["shown", "carried"];
|
|
73
74
|
export function cardKindWord(kind) {
|
|
@@ -267,7 +267,7 @@ export function buildSwitchboardReport(assembly, now = Date.now()) {
|
|
|
267
267
|
const up = openCards(events).map((c) => `${stripSwitchboardControlChars(c.kind)} ${stripSwitchboardControlChars(c.id)}`);
|
|
268
268
|
// A card shown in a record this report lacks looks closed or never up, so say so.
|
|
269
269
|
const partial = !holdsEveryRecord;
|
|
270
|
-
const note = partial ? "shown or carried
|
|
270
|
+
const note = partial ? "shown or carried and not taken down since, among the records this report holds" : "shown or carried and not taken down since";
|
|
271
271
|
lines.push(`cards_open: ${up.length ? `${up.join(", ")} (${note})` : partial ? "none seen (the report does not hold every record)" : "none"}`);
|
|
272
272
|
}
|
|
273
273
|
lines.push(`team_id: ${teamId ?? "(none)"}`);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface CapturedRecord {
|
|
2
|
+
id: number;
|
|
3
|
+
direction?: unknown;
|
|
4
|
+
data?: unknown;
|
|
5
|
+
is_base64?: unknown;
|
|
6
|
+
}
|
|
7
|
+
export interface Capture {
|
|
8
|
+
events: Record<string, unknown>[];
|
|
9
|
+
kept: Record<string, number>;
|
|
10
|
+
dropped: Record<string, number>;
|
|
11
|
+
seeded: boolean;
|
|
12
|
+
targets: number;
|
|
13
|
+
removed: Record<string, number>;
|
|
14
|
+
}
|
|
15
|
+
export declare function captureCase(records: CapturedRecord[]): Capture;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { BUILTIN_SEED, pairLabel } from "./test-case-store.js";
|
|
2
|
+
const TEST_USER = "UZRDR0000001";
|
|
3
|
+
const CHANNEL = "{{test_channel}}";
|
|
4
|
+
const SEQ_TS = "{{seq_ts}}";
|
|
5
|
+
const FRAME_TS = "{{frame_ts}}";
|
|
6
|
+
const SEQ_UUID = "{{seq_uuid}}";
|
|
7
|
+
const PAIRS = new Set(["message", "message.message_changed", "message.message_replied"]);
|
|
8
|
+
const SAFE_WORD = /^[a-z_]+$/;
|
|
9
|
+
const SAFE_MIME = /^[\w.+/-]*$/;
|
|
10
|
+
const MESSAGE_KEYS = ["type", "subtype", "channel", "ts", "thread_ts", "user", "client_msg_id", "text", "blocks", "files", "reply_count", "replies", "hidden"];
|
|
11
|
+
const isObj = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
|
+
const bump = (counts, key) => { counts[key] = (counts[key] ?? 0) + 1; };
|
|
13
|
+
// Plain ASCII of the same byte length, so sizes survive and no real text does.
|
|
14
|
+
function filler(original) {
|
|
15
|
+
const bytes = Buffer.byteLength(original);
|
|
16
|
+
return "made up replay text ".repeat(Math.ceil(bytes / 20)).slice(0, bytes);
|
|
17
|
+
}
|
|
18
|
+
// Only fields that shape how a block renders survive; anything else (urls aside, ids, counts)
|
|
19
|
+
// could be real, so an unknown key is removed rather than scrubbed. Fields the app needs to parse
|
|
20
|
+
// a block or chunk stay with made-up values: image sizes as 1, mention ids as test ids, and a
|
|
21
|
+
// fixed date and color.
|
|
22
|
+
const BLOCK_KEYS = new Set(["type", "block_id", "elements", "text", "style", "indent", "border", "offset", "user_id", "channel_id", "name", "url", "image_url", "alt_text", "image_height", "image_width", "image_bytes", "range", "usergroup_id", "team_id", "timestamp", "format", "fallback", "value", "fields", "accessory", "action_id", "bold", "italic", "strike", "code"]);
|
|
23
|
+
const BLOCK_NUMBERS = new Set(["indent", "border", "offset"]);
|
|
24
|
+
// the app needs these on an image block, but a real size says something about the image
|
|
25
|
+
const BLOCK_SIZES = new Set(["image_height", "image_width", "image_bytes"]);
|
|
26
|
+
// rich text chunks the app needs whole: each value is fixed, a filler, or one of a few known words
|
|
27
|
+
const BROADCAST_RANGES = new Set(["here", "channel", "everyone"]);
|
|
28
|
+
const FIXED_STRINGS = { usergroup_id: "SZRDR0000001", team_id: "TZRDR0000001", format: "{date_short}", value: "#000000" };
|
|
29
|
+
const BLOCK_FLAGS = new Set(["bold", "italic", "strike", "code"]);
|
|
30
|
+
// removed counts the keys left out, by name, so the caller can say what a saved case lacks
|
|
31
|
+
function walk(value, key, removed) {
|
|
32
|
+
if (Array.isArray(value))
|
|
33
|
+
return value.map((item) => walk(item, key, removed)).filter((item) => item !== undefined);
|
|
34
|
+
if (key === "action_id" && typeof value !== "string")
|
|
35
|
+
return undefined;
|
|
36
|
+
if (isObj(value)) {
|
|
37
|
+
const out = {};
|
|
38
|
+
for (const [name, item] of Object.entries(value)) {
|
|
39
|
+
if (item === undefined || item === null)
|
|
40
|
+
continue;
|
|
41
|
+
const kept = BLOCK_KEYS.has(name) ? walk(item, name, removed) : undefined;
|
|
42
|
+
if (kept === undefined)
|
|
43
|
+
bump(removed, name);
|
|
44
|
+
else
|
|
45
|
+
out[name] = kept;
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
if (typeof value === "number")
|
|
50
|
+
return key === "timestamp" ? 4000000000 : BLOCK_SIZES.has(key) ? 1 : BLOCK_NUMBERS.has(key) ? value : undefined;
|
|
51
|
+
if (typeof value === "boolean")
|
|
52
|
+
return BLOCK_FLAGS.has(key) ? value : undefined;
|
|
53
|
+
if (typeof value !== "string" || BLOCK_FLAGS.has(key) || BLOCK_SIZES.has(key) || key === "timestamp")
|
|
54
|
+
return undefined;
|
|
55
|
+
if (key === "range")
|
|
56
|
+
return BROADCAST_RANGES.has(value) ? value : undefined;
|
|
57
|
+
if (key in FIXED_STRINGS)
|
|
58
|
+
return FIXED_STRINGS[key];
|
|
59
|
+
if (key === "type" || key === "style")
|
|
60
|
+
return SAFE_WORD.test(value) ? value : filler(value);
|
|
61
|
+
if (key === "user_id")
|
|
62
|
+
return TEST_USER;
|
|
63
|
+
if (key === "channel_id")
|
|
64
|
+
return CHANNEL;
|
|
65
|
+
return filler(value);
|
|
66
|
+
}
|
|
67
|
+
function buildFile(file, pass) {
|
|
68
|
+
// the app needs SlackFile.id
|
|
69
|
+
if (!isObj(file) || typeof file.id !== "string")
|
|
70
|
+
return undefined;
|
|
71
|
+
const out = {};
|
|
72
|
+
if (!pass.fileIds.has(file.id))
|
|
73
|
+
pass.fileIds.set(file.id, `CZRDRF${pass.fileIds.size + 1}`);
|
|
74
|
+
out.id = pass.fileIds.get(file.id);
|
|
75
|
+
for (const key of ["name", "title"])
|
|
76
|
+
if (typeof file[key] === "string")
|
|
77
|
+
out[key] = filler(file[key]);
|
|
78
|
+
for (const key of ["mimetype", "filetype", "mode"])
|
|
79
|
+
if (typeof file[key] === "string" && SAFE_MIME.test(file[key]))
|
|
80
|
+
out[key] = file[key];
|
|
81
|
+
if (typeof file.size === "number")
|
|
82
|
+
out.size = file.size;
|
|
83
|
+
if (typeof file.user === "string")
|
|
84
|
+
out.user = TEST_USER;
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
// tsValue is what this level's ts becomes; undefined means the level has no ts
|
|
88
|
+
function buildLevel(src, top, tsValue, pass) {
|
|
89
|
+
const out = {};
|
|
90
|
+
for (const key of MESSAGE_KEYS) {
|
|
91
|
+
const value = src[key];
|
|
92
|
+
if (value === undefined)
|
|
93
|
+
continue;
|
|
94
|
+
switch (key) {
|
|
95
|
+
case "type":
|
|
96
|
+
if (top)
|
|
97
|
+
out.type = "message";
|
|
98
|
+
else if (value === "message")
|
|
99
|
+
out.type = value;
|
|
100
|
+
break;
|
|
101
|
+
case "subtype":
|
|
102
|
+
if (top && typeof value === "string")
|
|
103
|
+
out.subtype = value;
|
|
104
|
+
break;
|
|
105
|
+
case "channel":
|
|
106
|
+
if (typeof value === "string")
|
|
107
|
+
out.channel = CHANNEL;
|
|
108
|
+
break;
|
|
109
|
+
case "ts":
|
|
110
|
+
if (tsValue !== undefined)
|
|
111
|
+
out.ts = tsValue;
|
|
112
|
+
break;
|
|
113
|
+
case "thread_ts":
|
|
114
|
+
if (tsValue !== undefined)
|
|
115
|
+
out.thread_ts = tsValue;
|
|
116
|
+
break;
|
|
117
|
+
case "user":
|
|
118
|
+
if (typeof value === "string")
|
|
119
|
+
out.user = TEST_USER;
|
|
120
|
+
break;
|
|
121
|
+
case "client_msg_id":
|
|
122
|
+
if (typeof value === "string" && tsValue === SEQ_TS)
|
|
123
|
+
out.client_msg_id = SEQ_UUID;
|
|
124
|
+
break;
|
|
125
|
+
case "text":
|
|
126
|
+
if (typeof value === "string")
|
|
127
|
+
out.text = filler(value);
|
|
128
|
+
break;
|
|
129
|
+
case "blocks":
|
|
130
|
+
if (Array.isArray(value))
|
|
131
|
+
out.blocks = walk(value, key, pass.removed);
|
|
132
|
+
break;
|
|
133
|
+
case "files":
|
|
134
|
+
if (Array.isArray(value))
|
|
135
|
+
out.files = value.map((file) => buildFile(file, pass)).filter((file) => file !== undefined);
|
|
136
|
+
break;
|
|
137
|
+
case "reply_count":
|
|
138
|
+
if (Number.isInteger(value))
|
|
139
|
+
out.reply_count = value;
|
|
140
|
+
break;
|
|
141
|
+
case "replies":
|
|
142
|
+
if (Array.isArray(value))
|
|
143
|
+
out.replies = value.map(() => ({ user: TEST_USER, ts: FRAME_TS }));
|
|
144
|
+
break;
|
|
145
|
+
case "hidden":
|
|
146
|
+
if (typeof value === "boolean")
|
|
147
|
+
out.hidden = value;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
function classify(record) {
|
|
154
|
+
if (String(record.direction).toUpperCase() !== "INCOMING")
|
|
155
|
+
return "sent by the app";
|
|
156
|
+
let frame;
|
|
157
|
+
if (record.is_base64 !== true && typeof record.data === "string") {
|
|
158
|
+
try {
|
|
159
|
+
frame = JSON.parse(record.data);
|
|
160
|
+
}
|
|
161
|
+
catch { }
|
|
162
|
+
}
|
|
163
|
+
if (!isObj(frame))
|
|
164
|
+
return "not a JSON object";
|
|
165
|
+
const label = pairLabel(frame);
|
|
166
|
+
if (!PAIRS.has(label))
|
|
167
|
+
return label;
|
|
168
|
+
const message = frame.message;
|
|
169
|
+
const previous = frame.previous_message;
|
|
170
|
+
if ((isObj(message) && message.subtype !== undefined) || (isObj(previous) && previous.subtype !== undefined))
|
|
171
|
+
return "nested subtype";
|
|
172
|
+
const update = label !== "message";
|
|
173
|
+
if (!update && typeof frame.ts !== "string")
|
|
174
|
+
return "no ts";
|
|
175
|
+
if (update && !(isObj(message) && typeof message.ts === "string"))
|
|
176
|
+
return "no message.ts";
|
|
177
|
+
const levels = update ? [message, previous] : [frame];
|
|
178
|
+
for (const level of levels) {
|
|
179
|
+
if (isObj(level) && level.thread_ts !== undefined && level.thread_ts !== level.ts)
|
|
180
|
+
return "reply";
|
|
181
|
+
}
|
|
182
|
+
return { label, frame, update };
|
|
183
|
+
}
|
|
184
|
+
export function captureCase(records) {
|
|
185
|
+
const byId = new Map();
|
|
186
|
+
for (const record of records)
|
|
187
|
+
if (!byId.has(record.id))
|
|
188
|
+
byId.set(record.id, record);
|
|
189
|
+
const ordered = [...byId.values()].sort((a, b) => a.id - b.id);
|
|
190
|
+
const kept = {};
|
|
191
|
+
const dropped = {};
|
|
192
|
+
const candidates = [];
|
|
193
|
+
const targets = new Set();
|
|
194
|
+
for (const record of ordered) {
|
|
195
|
+
const result = classify(record);
|
|
196
|
+
if (typeof result === "string") {
|
|
197
|
+
bump(dropped, result);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
bump(kept, result.label);
|
|
201
|
+
candidates.push(result);
|
|
202
|
+
if (result.update)
|
|
203
|
+
targets.add(result.frame.message.ts);
|
|
204
|
+
}
|
|
205
|
+
const pass = { fileIds: new Map(), removed: {} };
|
|
206
|
+
const events = [];
|
|
207
|
+
// {{seq_ts}} is the ts of the iteration's first frame, so a {{frame_ts}} message at index 0
|
|
208
|
+
// would share it and get overwritten by the seed. A seq message must come first.
|
|
209
|
+
const first = candidates[0];
|
|
210
|
+
const firstIsTarget = first && !first.update && typeof first.frame.ts === "string" && targets.has(first.frame.ts);
|
|
211
|
+
const seeded = targets.size > 0 && !firstIsTarget;
|
|
212
|
+
if (seeded)
|
|
213
|
+
events.push(structuredClone(BUILTIN_SEED));
|
|
214
|
+
for (const { frame, update } of candidates) {
|
|
215
|
+
let event;
|
|
216
|
+
if (update) {
|
|
217
|
+
event = buildLevel(frame, true, undefined, pass);
|
|
218
|
+
for (const key of ["message", "previous_message"]) {
|
|
219
|
+
const level = frame[key];
|
|
220
|
+
if (isObj(level))
|
|
221
|
+
event[key] = buildLevel(level, false, typeof level.ts === "string" ? SEQ_TS : undefined, pass);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
const isTarget = typeof frame.ts === "string" && targets.has(frame.ts);
|
|
226
|
+
event = buildLevel(frame, true, isTarget ? SEQ_TS : FRAME_TS, pass);
|
|
227
|
+
event.user = TEST_USER;
|
|
228
|
+
if (isTarget)
|
|
229
|
+
event.client_msg_id = SEQ_UUID;
|
|
230
|
+
}
|
|
231
|
+
event.channel = CHANNEL;
|
|
232
|
+
events.push(event);
|
|
233
|
+
}
|
|
234
|
+
return { events, kept, dropped, seeded, targets: targets.size, removed: pass.removed };
|
|
235
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export declare const CASE_NAME: RegExp;
|
|
2
|
+
export declare const MAX_CASE_FILE_BYTES: number;
|
|
3
|
+
export declare const testCaseDir: () => string;
|
|
4
|
+
export interface TestCase {
|
|
5
|
+
repeat?: number;
|
|
6
|
+
rate_per_second: number | null;
|
|
7
|
+
events: Record<string, unknown>[];
|
|
8
|
+
}
|
|
9
|
+
export declare const BUILTIN_SEED: {
|
|
10
|
+
type: string;
|
|
11
|
+
channel: string;
|
|
12
|
+
ts: string;
|
|
13
|
+
client_msg_id: string;
|
|
14
|
+
text: string;
|
|
15
|
+
user: string;
|
|
16
|
+
};
|
|
17
|
+
export declare const BUILTIN_CASES: Map<string, TestCase>;
|
|
18
|
+
export declare function caseStem(name: string): string;
|
|
19
|
+
export declare function loadCase(name: string, dir?: string): TestCase | {
|
|
20
|
+
error: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function buildRunBody(caseDef: TestCase, overrides: Partial<Pick<TestCase, "repeat" | "rate_per_second">>, runId: string): string;
|
|
23
|
+
export declare function pairLabel(event: Record<string, unknown>): string;
|
|
24
|
+
export declare function checkCaseName(name: string, overwrite: boolean, dir?: string): {
|
|
25
|
+
error: string;
|
|
26
|
+
reason: string;
|
|
27
|
+
} | null;
|
|
28
|
+
export declare function saveCase(name: string, caseDef: TestCase, overwrite: boolean, dir?: string): {
|
|
29
|
+
path: string;
|
|
30
|
+
} | {
|
|
31
|
+
error: string;
|
|
32
|
+
reason?: string;
|
|
33
|
+
};
|
|
34
|
+
/** Removes a saved case file. lstat counts a dangling link as present, and unlink never follows a link, so deleting one leaves its target. */
|
|
35
|
+
export declare function deleteCase(name: string, dir?: string): {
|
|
36
|
+
deleted: string;
|
|
37
|
+
} | {
|
|
38
|
+
error: string;
|
|
39
|
+
};
|
|
40
|
+
export interface CaseSummary {
|
|
41
|
+
name: string;
|
|
42
|
+
built_in?: boolean;
|
|
43
|
+
path?: string;
|
|
44
|
+
replaces_built_in?: boolean;
|
|
45
|
+
repeat?: number;
|
|
46
|
+
rate_per_second?: number | null;
|
|
47
|
+
events?: number;
|
|
48
|
+
types?: Record<string, number>;
|
|
49
|
+
error?: string;
|
|
50
|
+
}
|
|
51
|
+
export declare function listCases(dir?: string): {
|
|
52
|
+
dir: string;
|
|
53
|
+
cases: CaseSummary[];
|
|
54
|
+
};
|