mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-26 15:47:13 +09:00
Address review of exp_discord_sync
- DiscordRest and DiscordGateway are made in fromContext and passed to the constructor. Both read the token from config, and guildId is a getter over config, so the plugin has no nullable bot state. - DiscordGateway is an EventEmitter and the plugin listens with bound methods (onGatewayReady, onGatewayDispatch, onGatewayFatalClose, onLinkChanged). - Private members start with an underscore. - DiscordGateway has an explicit state (stopped, connecting, identifying, ready, waiting) and _setState throws on a transition not in the table. Errors while handling a gateway message are logged so a failed assert can't take down the controller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cf968c02bb
commit
e88cbf990f
4 files changed
+236
-134
No files matched your search
@@ -79,11 +79,11 @@ async function startPlugin(t2, { fakeOptions, mappings = defaultMappings } = {})
|
||||
plugin.mappings.set(new messages.RoleMappingRecord(Number(roleId), ids));
|
||||
}
|
||||
|
||||
plugin.apiBase = fake.apiBase;
|
||||
plugin.gatewayUrl = fake.gatewayUrl;
|
||||
plugin.rest.baseUrl = fake.apiBase;
|
||||
plugin.gateway.url = fake.gatewayUrl;
|
||||
controller.config.set("exp_discord_sync.bot_token", "token");
|
||||
controller.config.set("exp_discord_sync.guild_id", guildId);
|
||||
if (!plugin.gateway) {
|
||||
if (plugin.gateway.state === "stopped") {
|
||||
plugin.connect();
|
||||
}
|
||||
return { plugin, auth, controller, fake };
|
||||
@@ -206,7 +206,7 @@ t.test("syncing", t2 => {
|
||||
setRoles(controller, "Player1", [5]);
|
||||
auth.linkAccount(alice, "Player1");
|
||||
await until(() => rolesOf(fake, alice.id).length === 1);
|
||||
t3.ok(plugin.rest, "rest is kept");
|
||||
t3.equal(plugin.gateway.state, "stopped", "gateway gave up, REST carries on");
|
||||
t3.ok(warnings.some(msg => /missing the Server Members intent/.test(msg)));
|
||||
});
|
||||
|
||||
@@ -241,7 +241,7 @@ t.test("syncing", t2 => {
|
||||
});
|
||||
const [plugin] = loaded.values();
|
||||
t3.equal(plugin.discordAuth, undefined);
|
||||
t3.equal(plugin.gateway, undefined, "does not connect");
|
||||
t3.equal(plugin.gateway.state, "stopped", "does not connect");
|
||||
t3.ok(warnings.some(msg => /exp_discord_auth is not loaded/.test(msg)));
|
||||
});
|
||||
t2.end();
|
||||
|
||||
@@ -13,13 +13,13 @@ t.test("class DiscordRest", async t2 => {
|
||||
fake.members.set(memberId, new Set());
|
||||
|
||||
t2.test(".getGuildMember() returns the member, or undefined when not in the guild", async t3 => {
|
||||
const rest = new DiscordRest("token", fake.apiBase);
|
||||
const rest = new DiscordRest(() => "token", fake.apiBase);
|
||||
t3.same(await rest.getGuildMember(guildId, memberId), { user: { id: memberId }, roles: [] });
|
||||
t3.equal(await rest.getGuildMember(guildId, "1"), undefined);
|
||||
});
|
||||
|
||||
t2.test(".addMemberRole() and .removeMemberRole() change roles", async t3 => {
|
||||
const rest = new DiscordRest("token", fake.apiBase);
|
||||
const rest = new DiscordRest(() => "token", fake.apiBase);
|
||||
await rest.addMemberRole(guildId, memberId, roleId);
|
||||
t3.same([...fake.members.get(memberId)], [roleId]);
|
||||
await rest.removeMemberRole(guildId, memberId, roleId);
|
||||
@@ -27,7 +27,7 @@ t.test("class DiscordRest", async t2 => {
|
||||
});
|
||||
|
||||
t2.test("waits out 429 responses and retries", async t3 => {
|
||||
const rest = new DiscordRest("token", fake.apiBase);
|
||||
const rest = new DiscordRest(() => "token", fake.apiBase);
|
||||
fake.requests.length = 0;
|
||||
fake.rateLimit = 2;
|
||||
const start = Date.now();
|
||||
@@ -37,21 +37,21 @@ t.test("class DiscordRest", async t2 => {
|
||||
});
|
||||
|
||||
t2.test("gives up after repeated 429 responses", async t3 => {
|
||||
const rest = new DiscordRest("token", fake.apiBase);
|
||||
const rest = new DiscordRest(() => "token", fake.apiBase);
|
||||
fake.rateLimit = 6;
|
||||
await t3.rejects(rest.getGuildRoles(guildId), { status: 429 });
|
||||
fake.rateLimit = 0;
|
||||
});
|
||||
|
||||
t2.test("throws DiscordApiError with the status", async t3 => {
|
||||
const rest = new DiscordRest("wrong", fake.apiBase);
|
||||
const rest = new DiscordRest(() => "wrong", fake.apiBase);
|
||||
const err = await rest.getGuildRoles(guildId).catch(e => e);
|
||||
t3.ok(err instanceof DiscordApiError);
|
||||
t3.equal(err.status, 401);
|
||||
});
|
||||
|
||||
t2.test("sends one request at a time", async t3 => {
|
||||
const rest = new DiscordRest("token", fake.apiBase);
|
||||
const rest = new DiscordRest(() => "token", fake.apiBase);
|
||||
fake.requests.length = 0;
|
||||
fake.rateLimit = 1;
|
||||
await Promise.all([rest.getGuildRoles(guildId), rest.getGuildMember(guildId, memberId)]);
|
||||
@@ -68,10 +68,9 @@ t.test("class DiscordGateway", async t2 => {
|
||||
const fake = await startFakeDiscord();
|
||||
t3.teardown(fake.close);
|
||||
const events = [];
|
||||
const gateway = new DiscordGateway("token", gatewayIntents.GUILD_MEMBERS, logger, {
|
||||
onReady: () => events.push("ready"),
|
||||
onDispatch: (type, data) => events.push([type, data]),
|
||||
}, fake.gatewayUrl);
|
||||
const gateway = new DiscordGateway(() => "token", gatewayIntents.GUILD_MEMBERS, logger, fake.gatewayUrl);
|
||||
gateway.on("ready", () => events.push("ready"));
|
||||
gateway.on("dispatch", (type, data) => events.push([type, data]));
|
||||
t3.teardown(() => gateway.stop());
|
||||
gateway.start();
|
||||
await until(() => events.length);
|
||||
@@ -86,7 +85,8 @@ t.test("class DiscordGateway", async t2 => {
|
||||
const fake = await startFakeDiscord();
|
||||
t3.teardown(fake.close);
|
||||
let ready = 0;
|
||||
const gateway = new DiscordGateway("token", 0, logger, { onReady: () => { ready += 1; } }, fake.gatewayUrl);
|
||||
const gateway = new DiscordGateway(() => "token", 0, logger, fake.gatewayUrl);
|
||||
gateway.on("ready", () => { ready += 1; });
|
||||
gateway.backoff = new lib.ExponentialBackoff({ base: 0.01, max: 0.02 });
|
||||
t3.teardown(() => gateway.stop());
|
||||
gateway.start();
|
||||
@@ -102,13 +102,13 @@ t.test("class DiscordGateway", async t2 => {
|
||||
const fake = await startFakeDiscord({ closeCode: 4014 });
|
||||
t3.teardown(fake.close);
|
||||
let closed;
|
||||
const gateway = new DiscordGateway("token", 0, logger, {
|
||||
onFatalClose: (code, reason) => { closed = { code, reason }; },
|
||||
}, fake.gatewayUrl);
|
||||
const gateway = new DiscordGateway(() => "token", 0, logger, fake.gatewayUrl);
|
||||
gateway.on("fatalClose", (code, reason) => { closed = { code, reason }; });
|
||||
gateway.backoff = new lib.ExponentialBackoff({ base: 0.01, max: 0.02 });
|
||||
gateway.start();
|
||||
await until(() => closed);
|
||||
t3.same(closed, { code: 4014, reason: "closed by fake" });
|
||||
t3.equal(gateway.state, "stopped");
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
t3.equal(fake.identifies.length, 1, "did not reconnect");
|
||||
});
|
||||
@@ -117,7 +117,8 @@ t.test("class DiscordGateway", async t2 => {
|
||||
const fake = await startFakeDiscord();
|
||||
t3.teardown(fake.close);
|
||||
let ready = 0;
|
||||
const gateway = new DiscordGateway("token", 0, logger, { onReady: () => { ready += 1; } }, fake.gatewayUrl);
|
||||
const gateway = new DiscordGateway(() => "token", 0, logger, fake.gatewayUrl);
|
||||
gateway.on("ready", () => { ready += 1; });
|
||||
gateway.backoff = new lib.ExponentialBackoff({ base: 0.01, max: 0.02 });
|
||||
gateway.start();
|
||||
await until(() => ready === 1);
|
||||
@@ -125,5 +126,53 @@ t.test("class DiscordGateway", async t2 => {
|
||||
await until(() => fake.sockets.size === 0);
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
t3.equal(fake.identifies.length, 1);
|
||||
t3.equal(gateway.state, "stopped");
|
||||
});
|
||||
|
||||
t2.test("goes through the states in order", async t3 => {
|
||||
const fake = await startFakeDiscord();
|
||||
t3.teardown(fake.close);
|
||||
const gateway = new DiscordGateway(() => "token", 0, logger, fake.gatewayUrl);
|
||||
gateway.backoff = new lib.ExponentialBackoff({ base: 0.01, max: 0.02 });
|
||||
t3.teardown(() => gateway.stop());
|
||||
const states = [gateway.state];
|
||||
const setState = gateway._setState;
|
||||
gateway._setState = function(next) {
|
||||
setState.call(this, next);
|
||||
states.push(next);
|
||||
};
|
||||
gateway.start();
|
||||
await until(() => gateway.state === "ready");
|
||||
for (const ws of fake.sockets) {
|
||||
ws.terminate();
|
||||
}
|
||||
await until(() => states.length === 8);
|
||||
gateway.stop();
|
||||
t3.same(states, [
|
||||
"stopped", "connecting", "identifying", "ready", "waiting", "connecting", "identifying", "ready", "stopped",
|
||||
]);
|
||||
});
|
||||
|
||||
t2.test("asserts on invalid transitions", async t3 => {
|
||||
const gateway = new DiscordGateway(() => "token", 0, logger, "ws://127.0.0.1:1");
|
||||
t3.throws(() => gateway._setState("ready"), /from stopped to ready/);
|
||||
t3.equal(gateway.state, "stopped");
|
||||
gateway.stop();
|
||||
t3.equal(gateway.state, "stopped", "stopping while stopped does nothing");
|
||||
});
|
||||
|
||||
t2.test("a message in the wrong state is logged, not thrown", async t3 => {
|
||||
const fake = await startFakeDiscord();
|
||||
t3.teardown(fake.close);
|
||||
const errors = [];
|
||||
const errorLogger = { ...logger, error: msg => errors.push(msg) };
|
||||
const gateway = new DiscordGateway(() => "token", 0, errorLogger, fake.gatewayUrl);
|
||||
t3.teardown(() => gateway.stop());
|
||||
gateway.start();
|
||||
await until(() => gateway.state === "ready");
|
||||
fake.dispatch("READY", {});
|
||||
await until(() => errors.length);
|
||||
t3.match(errors[0], /from ready to ready/);
|
||||
t3.equal(gateway.state, "ready");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user