Files
Veskslop-t/poc/src/test-trigger.ts
T

116 lines
4.1 KiB
TypeScript

/*
* Veskslop — inject a synthetic trigger event into the mock core.
* Use it to test the daemon trigger path without Discord.
*
* Usage:
* VESKSLOP_SOCKET=/tmp/vesktop-puppet-mock.sock pnpm test:trigger
* TEST_TRIGGER_CONTENT="whitelist list" pnpm test:trigger
* TEST_TRIGGER_GUILD=100000000000000001 pnpm test:trigger # mention in a guild channel
*/
import net from "node:net";
import { coreSocketPath } from "./core-client.js";
const authorId = process.env.TEST_TRIGGER_AUTHOR ?? "177117296140091393";
const content = process.env.TEST_TRIGGER_CONTENT ?? "!s whitelist list";
const guildId = process.env.TEST_TRIGGER_GUILD;
const isGuild = !!guildId;
const mention = isGuild && process.env.TEST_TRIGGER_MENTION !== "0";
const isReaction = process.env.TEST_TRIGGER_REACTION === "1";
const messageEvent = {
type: "MESSAGE_CREATE",
id: `30000000000000${Date.now() % 10000}`,
channelId: process.env.TEST_TRIGGER_CHANNEL ?? (isGuild ? "200000000000000010" : "805255536223715370"),
guildId: guildId ?? null,
channelType: isGuild ? 0 : 1,
author: { id: authorId, username: "zzib", bot: false },
content: mention ? `<@999> ${content}` : content,
mentions: mention ? ["999"] : [],
replyTo: process.env.TEST_TRIGGER_REPLY_TO
? {
id: process.env.TEST_TRIGGER_REPLY_TO,
authorId: process.env.TEST_TRIGGER_REPLY_AUTHOR ?? "999",
authorName: "mockuser",
content: "the referenced message"
}
: null,
timestamp: new Date().toISOString()
};
const updateEvent = {
type: "MESSAGE_UPDATE",
id: process.env.TEST_TRIGGER_MESSAGE_ID ?? "400000000000000004",
channelId: process.env.TEST_TRIGGER_CHANNEL ?? "200000000000000001",
guildId: guildId ?? "100000000000000001",
content: content === "!s whitelist list" ? "edited content" : content,
editedAt: new Date().toISOString(),
author: { id: authorId, username: "zzib", bot: false }
};
const deleteEvent = {
type: "MESSAGE_DELETE",
id: process.env.TEST_TRIGGER_MESSAGE_ID ?? "400000000000000004",
channelId: process.env.TEST_TRIGGER_CHANNEL ?? "200000000000000001",
guildId: guildId ?? "100000000000000001"
};
const reactionEvent = {
type: process.env.TEST_TRIGGER_REMOVE === "1" ? "MESSAGE_REACTION_REMOVE" : "MESSAGE_REACTION_ADD",
messageId: process.env.TEST_REACTION_MESSAGE_ID ?? "msg-1",
channelId: process.env.TEST_TRIGGER_CHANNEL ?? "200000000000000010",
guildId: guildId ?? "100000000000000001",
userId: authorId,
emoji: { name: process.env.TEST_REACTION_EMOJI ?? "1️⃣", id: null }
};
const speakingUser = process.env.TEST_TRIGGER_SPEAKING;
const speakingEvent = {
type: "VOICE_SPEAKING",
userId: speakingUser ?? "999",
speaking: process.env.TEST_TRIGGER_SPEAKING_OFF !== "1",
channelId: process.env.TEST_TRIGGER_CHANNEL ?? "200000000000000002"
};
const voiceUser = process.env.TEST_TRIGGER_VOICE_USER;
const voiceChannelRaw = process.env.TEST_TRIGGER_VOICE_CHANNEL;
const voiceStateEvent = {
type: "VOICE_STATE_UPDATE",
userId: voiceUser ?? "999",
channelId: voiceChannelRaw === "null" || voiceChannelRaw === "" ? null : voiceChannelRaw ?? "200000000000000002",
guildId: guildId ?? "100000000000000001",
mute: false,
deaf: false,
selfMute: false,
selfDeaf: false
};
const event =
process.env.TEST_TRIGGER_UPDATE === "1"
? updateEvent
: process.env.TEST_TRIGGER_DELETE === "1"
? deleteEvent
: isReaction
? reactionEvent
: voiceUser
? voiceStateEvent
: speakingUser
? speakingEvent
: messageEvent;
await new Promise<void>((resolve, reject) => {
const socket = net.connect(coreSocketPath);
socket.setEncoding("utf-8");
socket.once("connect", () =>
socket.write(JSON.stringify({ id: 1, action: "debug.emit", params: { event } }) + "\n")
);
socket.once("data", data => {
console.log("[trigger]", data.toString().trim());
socket.destroy();
resolve();
});
socket.once("error", reject);
});