feat(discord): eyes reaction as a read receipt on every admitted message (#1509)
QUEUE row 15, MVP iteration 1 after the Sage pilot. rest.react is best effort (2xx true, anything else false, never throws); the connector reacts at admission before the engine runs and records the outcome in the turn record as receipt. Drops and refusals get no reaction. Suite 28/28, 90 node tests. Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
@@ -2,7 +2,7 @@ import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync, writeFileSync, existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { createConnector, FIXED_LINES, RECONCILE_WINDOW_MS } from "../src/connector.mjs";
|
||||
import { createConnector, FIXED_LINES, RECONCILE_WINDOW_MS, READ_RECEIPT } from "../src/connector.mjs";
|
||||
import { readOutbox, readDrops, listTurns, readInboxIds, appendOutbox, appendInbox, ensureJournal, requestStop, writeTurn, countAdmissionsOn, noticeOn } from "../src/journal.mjs";
|
||||
import { makeRoot, binding, message, IDS, fakeRest, fakeGateway, fakeEngine } from "./helpers.mjs";
|
||||
|
||||
@@ -403,3 +403,35 @@ test("journal: no token-shaped string and no model output on the drop path reach
|
||||
}
|
||||
await connector.stop();
|
||||
});
|
||||
|
||||
test("receipt: an admitted message gets one eyes reaction on the inbound message; drops and refusals get none; a failed reaction is recorded and does not fail the turn", async () => {
|
||||
const { journalDir, rest, connector } = setup({ bindingOverrides: { limits: { turnsPerDay: 1, turnTimeoutSeconds: 180, replyChunkChars: 1900, inboundMaxChars: 4000 } } });
|
||||
await connector.start();
|
||||
const r1 = await connector.handleMessage(message({ id: "300000000000000301", content: "first" }));
|
||||
assert.equal(await r1.turn, "ok");
|
||||
assert.deepEqual(rest.reactions, [{ channelId: IDS.admin, messageId: "300000000000000301", emoji: READ_RECEIPT }]);
|
||||
assert.equal(READ_RECEIPT, "\u{1F440}");
|
||||
let turns = listTurns(journalDir);
|
||||
assert.deepEqual(turns[0].receipt, { emoji: READ_RECEIPT, ok: true });
|
||||
// Unlisted author: dropped, no reaction.
|
||||
const r2 = await connector.handleMessage(message({ id: "300000000000000302", author: { id: IDS.stranger, username: "s" } }));
|
||||
assert.equal(r2.accepted, false);
|
||||
// Over the ceiling: refused with the fixed line, no reaction.
|
||||
const r3 = await connector.handleMessage(message({ id: "300000000000000303", content: "second" }));
|
||||
assert.deepEqual(r3, { accepted: false, reason: "ceiling" });
|
||||
assert.equal(rest.reactions.length, 1);
|
||||
await connector.stop();
|
||||
});
|
||||
|
||||
test("receipt: Discord refusing the reaction leaves the turn intact and records ok false", async () => {
|
||||
const { journalDir, rest, connector } = setup();
|
||||
rest.reactOk = false;
|
||||
await connector.start();
|
||||
const r = await connector.handleMessage(message({ id: "300000000000000311", content: "hi" }));
|
||||
assert.equal(await r.turn, "ok");
|
||||
assert.equal(rest.calls.length, 1, "the reply still went out");
|
||||
const turns = listTurns(journalDir);
|
||||
assert.equal(turns[0].status, "ok");
|
||||
assert.deepEqual(turns[0].receipt, { emoji: READ_RECEIPT, ok: false });
|
||||
await connector.stop();
|
||||
});
|
||||
|
||||
@@ -100,6 +100,9 @@ export function fakeRest({ outcomes = [] } = {}) {
|
||||
return {
|
||||
calls,
|
||||
typingCalls: [],
|
||||
reactions: [],
|
||||
// When false, react resolves false (Discord refused the reaction).
|
||||
reactOk: true,
|
||||
channels: new Map(),
|
||||
lookups: 0,
|
||||
// When set, getChannel waits on this promise before answering (held lookup).
|
||||
@@ -116,6 +119,10 @@ export function fakeRest({ outcomes = [] } = {}) {
|
||||
async typing(channelId) {
|
||||
this.typingCalls.push(channelId);
|
||||
},
|
||||
async react(channelId, messageId, emoji) {
|
||||
this.reactions.push({ channelId, messageId, emoji });
|
||||
return this.reactOk;
|
||||
},
|
||||
async getChannel(id) {
|
||||
this.lookups += 1;
|
||||
if (this.holdLookup) await this.holdLookup;
|
||||
|
||||
@@ -71,3 +71,20 @@ test("rest: content and nonce limits are enforced locally; typing never throws",
|
||||
await rest.typing("c");
|
||||
assert.equal(f.calls.length, 2);
|
||||
});
|
||||
|
||||
test("rest: react PUTs the encoded emoji on the inbound message for @me; 2xx is true, anything else is false and never throws", async () => {
|
||||
const f = fakeFetch([{ status: 204 }, { status: 403, body: { message: "Missing Permissions", code: 50013 } }, { throw: "down" }]);
|
||||
const logs = [];
|
||||
const rest = createRest({ token: TOKEN, fetch: f.fetch, log: (m) => logs.push(m) });
|
||||
assert.equal(await rest.react("c1", "m1", "\u{1F440}"), true);
|
||||
assert.equal(f.calls[0].url, "https://discord.com/api/v10/channels/c1/messages/m1/reactions/%F0%9F%91%80/@me");
|
||||
assert.equal(f.calls[0].init.method, "PUT");
|
||||
assert.equal(f.calls[0].init.body, undefined);
|
||||
assert.equal(f.calls[0].init.headers.Authorization, `Bot ${TOKEN}`);
|
||||
assert.equal(await rest.react("c1", "m2", "\u{1F440}"), false);
|
||||
assert.equal(await rest.react("c1", "m3", "\u{1F440}"), false);
|
||||
assert.equal(logs.length, 2);
|
||||
assert.match(logs[0], /HTTP 403/);
|
||||
assert.ok(logs.every((l) => !l.includes(TOKEN)));
|
||||
await assert.rejects(rest.react("c1", "m4", ""), /emoji required/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user