scanner/scanner.test.mjs
2026-07-29 21:03:59 +03:00

106 lines
3.5 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
escapeHtml,
extractLiveIds,
extractPrice,
matchesPrice,
parseLiveMessage,
parseRateLimitWaitMs,
publicConfig,
serveHTML,
validateConfig,
} from "./scanner.mjs";
test("parses both supported live message formats", () => {
assert.deepEqual(extractLiveIds({ new: ["a"] }), ["a"]);
assert.deepEqual(extractLiveIds({ message: { result: ["b", "c"] } }), ["b", "c"]);
assert.deepEqual(extractLiveIds({ message: {} }), []);
});
test("parses the current PoE live JWT package format", () => {
assert.deepEqual(parseLiveMessage({ auth: true }), { auth: true, ids: [], token: "", count: 0 });
assert.deepEqual(
parseLiveMessage({ result: "signed.jwt.package", count: 2 }),
{ auth: false, ids: [], token: "signed.jwt.package", count: 2 },
);
});
test("parses the PoE rate-limit wait time with a safety second", () => {
const body = JSON.stringify({ error: { message: "Rate limit exceeded; Please wait 12 seconds before trying again." } });
assert.equal(parseRateLimitWaitMs(body), 13000);
assert.equal(parseRateLimitWaitMs("not json"), 0);
});
test("extracts listing price and secure tokens", () => {
const result = extractPrice({
id: "item-1",
listing: {
price: { amount: 4, currency: "Divine" },
account: { name: "Seller" },
whisper: "@Seller hello",
hideout_token: "hideout-token",
},
});
assert.equal(result.amount, 4);
assert.equal(result.currency, "divine");
assert.equal(result.seller, "Seller");
assert.equal(result.token, "hideout-token");
});
test("does not treat a whisper token as a hideout travel token", () => {
const result = extractPrice({
id: "item-2",
listing: {
price: { amount: 5, currency: "divine" },
whisper_token: "whisper-only-token",
},
});
assert.equal(result.hideoutToken, "");
assert.equal(result.whisperToken, "whisper-only-token");
assert.equal(result.token, "");
});
test("matches only the requested price and currency", () => {
assert.equal(matchesPrice({ amount: 5, currency: "divine" }, { maxPrice: 5, priceCurrency: "divine" }), true);
assert.equal(matchesPrice({ amount: 6, currency: "divine" }, { maxPrice: 5, priceCurrency: "divine" }), false);
assert.equal(matchesPrice({ amount: 1, currency: "chaos" }, { maxPrice: 5, priceCurrency: "divine" }), false);
});
test("validates browser configuration and allows zero price", () => {
const cfg = validateConfig({
league: "Test League",
itemName: "Test Item",
itemType: "Test Type",
maxPrice: 0,
priceCurrency: "DIVINE",
poeSessionId: "must-not-pass-through",
});
assert.equal(cfg.maxPrice, 0);
assert.equal(cfg.priceCurrency, "divine");
assert.equal("poeSessionId" in cfg, false);
assert.throws(() => validateConfig({ ...cfg, maxPrice: -1 }), /Invalid max price/);
});
test("public config and HTML escaping do not leak dangerous values", () => {
const cfg = publicConfig({
league: "Test",
itemName: "Item",
itemType: "Type",
maxPrice: 5,
priceCurrency: "divine",
poeSessionId: "secret",
poeToken: "secret",
});
assert.equal("poeSessionId" in cfg, false);
assert.equal("poeToken" in cfg, false);
assert.equal(escapeHtml(`"><script>'&`), "&quot;&gt;&lt;script&gt;&#39;&amp;");
});
test("generated browser script has valid JavaScript syntax", () => {
const html = serveHTML();
const script = html.match(/<script>([\s\S]*?)<\/script>/)?.[1];
assert.ok(script);
assert.doesNotThrow(() => new Function(script));
});