ts6-botflow/transport.test.mjs
Bxio 1be8a45dde
All checks were successful
Testar e publicar imagem Docker / publish (push) Successful in 44s
Expand flow editor with TeamSpeak events, permissions and execution controls
2026-10-10 20:38:31 +01:00

97 lines
3.1 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { query } from "./studio.mjs";
import { httpRequest } from "./flow-steps.mjs";
test("WebQuery uses authenticated GET and form POST, handles fragmented UTF-8 and server errors", async () => {
const requests = [];
const server = http.createServer(async (req, res) => {
const chunks = [];
for await (const chunk of req) chunks.push(chunk);
requests.push({
method: req.method,
path: req.url,
key: req.headers["x-api-key"],
body: Buffer.concat(chunks).toString(),
});
res.setHeader("Content-Type", "application/json");
if (req.url.includes("channeledit")) {
res.end(JSON.stringify({ status: { code: 0 }, body: [] }));
return;
}
if (req.url.includes("clientkick")) {
res.end(
JSON.stringify({
status: { code: 2568, message: "insufficient client permissions" },
body: [],
}),
);
return;
}
const bytes = Buffer.from(
JSON.stringify({
status: { code: 0 },
body: [{ virtualserver_name: "Olá | Portugal" }],
}),
),
split = bytes.indexOf(Buffer.from("á")) + 1;
res.write(bytes.subarray(0, split));
setImmediate(() => res.end(bytes.subarray(split)));
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const connection = {
url: `http://127.0.0.1:${server.address().port}`,
serverId: 1,
apiKey: "fixture-key",
};
try {
const result = await query(connection, "serverinfo");
assert.equal(result[0].virtualserver_name, "Olá | Portugal");
const text = "Olá | linha\nmais = texto";
await query(connection, "channeledit", {
cid: 21,
channel_description: text,
});
assert.equal(requests[0].method, "GET");
assert.equal(requests[0].key, "fixture-key");
assert.equal(requests[1].method, "POST");
assert.equal(
new URLSearchParams(requests[1].body).get("channel_description"),
text,
);
await assert.rejects(
() => query(connection, "clientkick", { clid: 1, reasonid: 5 }),
/2568.*permissions/,
);
const generic = await httpRequest(connection.url);
assert.equal(generic.body.body[0].virtualserver_name, "Olá | Portugal");
assert.throws(() => httpRequest("file:///tmp/test"), /HTTP/);
} finally {
await new Promise((resolve) => server.close(resolve));
}
});
test("network requests respond to flow cancellation", async () => {
const server = http.createServer(() => {});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
try {
const controller = new AbortController(),
promise = query(
{
url: `http://127.0.0.1:${server.address().port}`,
serverId: 1,
apiKey: "fixture-key",
},
"serverinfo",
{},
{ signal: controller.signal },
);
controller.abort();
await assert.rejects(() => promise, /cancelada/);
} finally {
server.closeAllConnections();
await new Promise((resolve) => server.close(resolve));
}
});