1
0
Fork 0
polaris-task-force/tests/int/modlist-preset.int.spec.ts
Z8MB1E cc9d3b8073 fix(missions): only accept Arma 3 Launcher preset exports as mod lists
Root cause of the upload failures: the launcher writes an
<?xml version="1.0"?> prologue, Payload sniffs uploads with
fileTypeFromBuffer, and the collection mimeTypes allowlist lacked
application/xml, so every genuine launcher export was rejected with
'Invalid MIME type: application/xml'.

- Add application/xml to the mod-lists upload mimeTypes (the document is
  still stored as text/html; verified end to end).
- New pure parser src/lib/modlistPreset.ts validates the launcher export
  structure (arma:Type=preset meta + data-type=ModContainer rows) and is
  shared by the server action (authoritative) and CreateSideOpDialog
  (instant client-side feedback).
- Plain-text lists are no longer accepted anywhere: dialog accept/hint,
  collection mimeTypes, and the action now agree on HTML presets only.
- Upload name defaults to the preset's own arma:PresetName.
- Tests: tests/int/modlist-preset.int.spec.ts (9 cases).
2026-10-08 13:25:08 -04:00

93 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { describeArmaPresetFailure, parseArmaPreset } from "@/lib/modlistPreset";
/** Trimmed fixture mirroring the structure of a real Arma 3 Launcher export. */
function presetHtml(modRows: string, presetName = "The World We Knew"): string {
return `<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<meta name="arma:Type" content="preset" />
<meta name="arma:PresetName" content="${presetName}" />
<meta name="generator" content="Arma 3 Launcher - https://www.arma3.com" />
<title>Arma 3</title>
</head>
<body>
<h1>Arma 3 - Preset <strong>${presetName}</strong></h1>
<div class="mod-list">
<table>
${modRows}
</table>
</div>
<div class="dlc-list">
<table></table>
</div>
<div class="footer">
<span>Created by Arma 3 Launcher by Bohemia Interactive.</span>
</div>
</body>
</html>`;
}
const MOD_ROW = ` <tr data-type="ModContainer">
<td data-type="DisplayName">ACE3</td>
<td><span class="from-steam">Steam</span></td>
<td><a href="https://steamcommunity.com/sharedfiles/filedetails/?id=463939057" data-type="Link">link</a></td>
</tr>`;
describe("parseArmaPreset", () => {
it("accepts a launcher preset export and reports name + mod count", () => {
const result = parseArmaPreset(presetHtml(`${MOD_ROW}\n${MOD_ROW}`));
expect(result).toEqual({ ok: true, presetName: "The World We Knew", modCount: 2 });
});
it("tolerates a UTF-8 BOM", () => {
const result = parseArmaPreset(`\uFEFF${presetHtml(MOD_ROW)}`);
expect(result.ok).toBe(true);
});
it("tolerates single-quoted and uppercase attributes", () => {
const html = presetHtml(MOD_ROW)
.replace('<meta name="arma:Type" content="preset" />', "<META NAME='arma:Type' CONTENT='preset'>")
.replace('data-type="ModContainer"', "DATA-TYPE='ModContainer'");
const result = parseArmaPreset(html);
expect(result.ok).toBe(true);
});
it("reports a null preset name when the meta tag is absent", () => {
const html = presetHtml(MOD_ROW).replace(
/<meta name="arma:PresetName"[^>]*>/,
"",
);
expect(parseArmaPreset(html)).toEqual({ ok: true, presetName: null, modCount: 1 });
});
it("rejects an unrelated HTML page", () => {
const result = parseArmaPreset(
"<html><body><h1>My mod list</h1><ul><li>ACE3</li></ul></body></html>",
);
expect(result).toEqual({ ok: false, reason: "notPreset" });
});
it("rejects a plain-text mod list", () => {
expect(parseArmaPreset("CUP Units\nCUP Vehicles\nACE3\n")).toEqual({
ok: false,
reason: "notPreset",
});
});
it("rejects launcher exports of other arma:Type values", () => {
const html = presetHtml(MOD_ROW).replace('content="preset"', 'content="save"');
expect(parseArmaPreset(html)).toEqual({ ok: false, reason: "notPreset" });
});
it("rejects a valid preset with zero mods", () => {
expect(parseArmaPreset(presetHtml(""))).toEqual({ ok: false, reason: "empty" });
});
});
describe("describeArmaPresetFailure", () => {
it("gives actionable copy for every reason", () => {
expect(describeArmaPresetFailure("notPreset")).toMatch(/Export preset to HTML/i);
expect(describeArmaPresetFailure("empty")).toMatch(/does not contain any mods/i);
});
});