Files
MineGeo/tests/moderation.test.ts

69 lines
2.0 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { contributionCutoff, reportSchema } from "../shared/moderation";
const datasetId = "11111111-1111-4111-8111-111111111111";
const sessionId = "22222222-2222-4222-8222-222222222222";
const report = {
datasetId,
target: "map",
category: "broken",
reason: "Terrain is missing from the map.",
};
test("reports require an unambiguous target and a meaningful explanation", () => {
assert.equal(reportSchema.parse(report).target, "map");
for (const change of [
{ datasetId: undefined },
{ sessionId },
{ target: "panorama" },
{ reason: " " },
{ category: "unknown" },
])
assert.equal(
reportSchema.safeParse({ ...report, ...change }).success,
false,
);
assert.equal(
reportSchema.parse({ ...report, reason: " Terrain is missing. " }).reason,
"Terrain is missing.",
);
});
test("panorama reports identify the played session and round without client-provided location IDs", () => {
const panorama = {
...report,
datasetId: undefined,
sessionId,
target: "panorama",
round: 3,
};
assert.equal(reportSchema.safeParse(panorama).success, true);
for (const round of [undefined, -1, 5, 0.5])
assert.equal(reportSchema.safeParse({ ...panorama, round }).success, false);
});
test("evidence links allow only HTTP and HTTPS", () => {
for (const evidenceUrl of [
"https://example.com/proof",
"http://example.com/proof",
])
assert.equal(
reportSchema.safeParse({ ...report, evidenceUrl }).success,
true,
);
for (const evidenceUrl of [
"javascript:alert(1)",
"data:text/html,test",
"file:///secret",
"ftp://example.com",
])
assert.equal(
reportSchema.safeParse({ ...report, evidenceUrl }).success,
false,
);
});
test("contribution list cutoff is exactly five days and crosses month boundaries", () => {
assert.equal(
contributionCutoff(Date.parse("2026-10-03T12:34:56Z")),
"2026-09-28T12:34:56.000Z",
);
});