Rework core integration: validated capability layer, docs, and session state

This commit is contained in:
Fisto
2026-09-11 14:09:04 +02:00
parent 2a15f057df
commit e62d3007d8
33 changed files with 1897 additions and 612 deletions
+75
View File
@@ -0,0 +1,75 @@
/*
* Veskslop — unit tests for the pure communication logic.
*
* Usage: pnpm test:comm
*/
import assert from "node:assert/strict";
import {
MessageLog,
TRUSTED_LOG_LIMIT,
UNTRUSTED_LOG_LIMIT,
extractMentionIds,
isTrusted,
parseCommand,
randomActionDelay,
stripMentions,
type PuppetMessage
} from "./communication.js";
function entry(id: number, trusted: boolean): PuppetMessage {
return {
id: String(id),
channelId: "c",
authorId: trusted ? "owner" : "stranger",
authorName: "tester",
content: `msg ${id}`,
timestamp: new Date().toISOString(),
trusted
};
}
// ring buffer limits
{
const log = new MessageLog();
for (let i = 0; i < 150; i++) log.add(entry(i, true));
assert.equal(log.trusted.length, TRUSTED_LOG_LIMIT);
assert.equal(log.trusted[0].id, "50");
for (let i = 0; i < 40; i++) log.add(entry(i, false));
assert.equal(log.untrusted.length, UNTRUSTED_LOG_LIMIT);
assert.equal(log.untrusted[0].id, "10");
}
// mentions
{
const ids = extractMentionIds("<@!42> hi <@7>", [{ id: "9" }]);
assert.deepEqual([...ids].sort(), ["42", "7", "9"]);
assert.equal(stripMentions("<@!42> whitelist list", new Set(["42"])), "whitelist list");
assert.equal(stripMentions("<@42> <@99> hey", new Set(["42"])), "<@99> hey");
}
// commands
{
assert.deepEqual(parseCommand(" WHITELIST add <@123> "), { name: "whitelist", args: ["add", "<@123>"] });
assert.deepEqual(parseCommand("whitelist list"), { name: "whitelist", args: ["list"] });
assert.equal(parseCommand(" "), undefined);
}
// trust
{
assert.equal(isTrusted("owner", "owner", []), true);
assert.equal(isTrusted("trusted", "owner", ["trusted"]), true);
assert.equal(isTrusted("stranger", "owner", ["trusted"]), false);
}
// pacing stays in the 500-2800 ms window
{
for (let i = 0; i < 200; i++) {
const delay = randomActionDelay();
assert.ok(delay >= 500 && delay <= 2800, `delay out of range: ${delay}`);
}
}
console.log("all communication tests passed");