Files
ItxiaohuiContentGuard/scripts/feature-client.cjs
T

164 lines
6.7 KiB
JavaScript

const { createClient } = require(process.argv[2]);
const { addMovement } = require("./client-movement.cjs");
const [port, version, scenario = "normal", mode = "STRICT"] = process.argv.slice(3);
const wait = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
const clients = [];
const expectedEnds = new Set();
async function connect(username) {
const client = createClient({ host: "127.0.0.1", port: Number(port), version, username, auth: "offline", hideErrors: true });
addMovement(client, version);
clients.push(client);
const messages = [];
for (const event of ["systemChat", "playerChat"]) {
client.on(event, data => messages.push(JSON.stringify(data)));
}
await new Promise((resolve, reject) => {
client.once("login", resolve);
client.on("error", reject);
});
return { client, messages };
}
async function delivered(messages, text, timeout = 5000) {
const expires = Date.now() + timeout;
while (Date.now() < expires) {
if (messages.some(line => line.includes(text))) return;
await wait(70);
}
throw new Error("Missing delivery: " + text + " Received: " + JSON.stringify(messages.slice(-7)));
}
async function main() {
const receiver = await connect("ICGReceiver");
const sender = await connect("ICGProbe");
if (scenario === "db-fill") {
await wait(5000);
const pages = Array.from({ length: 50 }, (_, page) =>
"ICG-disk " + page + " 违规内容 " + "test data ".repeat(45));
sender.client.write("edit_book", { hand: 0, pages, title: "ICG storage limit" });
await wait(1700);
sender.client.chat("ICG-filter-after-disk 违规内容");
await delivered(receiver.messages, "ICG-filter-after-disk ***");
console.log("LOCAL_AUDIT_WHILE_DISK_FULL=PASS");
return;
}
await wait(700);
sender.client.chat("ICG-public 违规内容");
await delivered(receiver.messages, "ICG-public ***");
console.log("PUBLIC_FILTER=PASS");
await wait(500);
sender.client.chat("/msg ICGReceiver ICG-private 违规内容");
await delivered(receiver.messages, "ICG-private ***");
console.log("PRIVATE_FILTER=PASS");
if (scenario === "commands") {
for (const alias of ["tell", "w"]) {
await wait(400);
sender.client.chat("/" + alias + " ICGReceiver ICG-" + alias + " 违规内容");
await delivered(receiver.messages, "ICG-" + alias + " ***");
}
console.log("PRIVATE_ALIASES=PASS");
}
await wait(500);
sender.client.chat("ICG-denied marker");
if (mode === "STRICT" || mode === "AUTO") {
await delivered(sender.messages, "内容未通过审核");
if (receiver.messages.some(line => line.includes("ICG-denied"))) throw new Error("Unsafe message reached recipient");
console.log("AI_BLOCK=PASS");
} else {
await delivered(receiver.messages, "ICG-denied marker");
console.log("AI_AUDIT_WITHOUT_BLOCK=PASS");
}
await wait(4200);
const pages = ["ICG-page 违 规内容", "Second page"];
sender.client.write("edit_book", { hand: 0, pages });
await wait(2200);
sender.client.chat("/icgprobe book-edit");
await wait(1600);
sender.client.write("edit_book", { hand: 0, pages, title: "ICG 违规内容" });
await wait(1400);
sender.client.chat("/icgprobe book-sign");
await wait(600);
if (scenario === "fault") {
for (let i = 0; i < 4; i++) {
sender.client.chat("ICG-invalid " + i);
await wait(500);
}
await delivered(sender.messages, "审核暂不可用");
if (receiver.messages.some(line => line.includes("ICG-invalid"))) throw new Error("Invalid review reached recipient");
console.log("AI_INVALID_FAIL_CLOSED=PASS");
await wait(2200);
}
if (scenario === "folia") {
sender.client.chat("ICG-move-pending");
await wait(250);
sender.client.chat("/icgprobe move");
await delivered(receiver.messages, "ICG-move-pending", 11000);
console.log("AI_DURING_MOVE=PASS");
sender.client.chat("/icgprobe retire");
await wait(400);
sender.client.chat("/icgprobe regions");
const offline = await connect("ICGOffline");
await wait(1000);
offline.client.chat("ICG-offline-pending");
await wait(250);
expectedEnds.add(offline.client);
offline.client.end("Deliberate disconnect before audit completion");
await wait(1500);
if (receiver.messages.some(line => line.includes("ICG-offline-pending"))) {
throw new Error("A disconnected player's pending chat was delivered");
}
console.log("OFFLINE_RETURN_DROPPED=PASS");
}
if (scenario === "permissions") {
const admin = await connect("ICGAdmin");
const bypass = await connect("ICGBypass");
await wait(1300);
receiver.client.chat("ICG-high-test 死全家");
await delivered(admin.messages, "ICG-high-test");
if (!admin.messages.some(line => line.includes("/icg logs "))) {
throw new Error("Admin alert lacked a clickable record command");
}
receiver.client.chat("/icg logs");
await delivered(receiver.messages, "你没有权限");
if (receiver.messages.some(line => line.includes("ICG-high-test") || line.includes("/icg logs "))) {
throw new Error("Admin-only moderation notice reached a regular player");
}
console.log("ADMIN_NOTIFICATION_ONLY=PASS");
bypass.client.chat("ICG-bypass 违规内容");
await delivered(receiver.messages, "ICG-bypass 违规内容");
await wait(3800);
bypass.client.write("edit_book", { hand: 0, pages, title: "ICG 违规内容" });
await wait(2300);
bypass.client.chat("/icgprobe book-sign");
await wait(500);
console.log("CHAT_BYPASS_GRANTED=PASS");
}
}
const deadline = setTimeout(() => {
console.error("Feature client timed out");
process.exit(1);
}, ["folia", "permissions"].includes(scenario) ? 45_000 : 30_000);
let closing = false;
function game() {
return new Promise((resolve, reject) => {
const monitor = setInterval(() => {
if (!closing && clients.some(client => client.ended && !expectedEnds.has(client))) {
clearInterval(monitor);
reject(new Error("A Minecraft client disconnected during feature verification"));
}
}, 100);
main().then(resolve, reject).finally(() => clearInterval(monitor));
});
}
game().catch(error => {
console.error(error.message);
process.exitCode = 1;
}).finally(() => {
closing = true;
clearTimeout(deadline);
for (const client of clients) client.end("Test finished");
});