61 lines
3.1 KiB
JavaScript
61 lines
3.1 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import { createGameServer } from "./server.mjs";
|
|
|
|
test("serves the game and authenticated cloud-save API", async () => {
|
|
const directory = mkdtempSync(path.join(os.tmpdir(), "healer-man-server-test-"));
|
|
const staticDir = path.join(directory, "dist");
|
|
const dataDir = path.join(directory, "data");
|
|
const contentDir = path.join(directory, "content");
|
|
mkdirSync(staticDir, { recursive: true });
|
|
mkdirSync(path.join(contentDir, "objects"), { recursive: true });
|
|
writeFileSync(path.join(staticDir, "index.html"), "<!doctype html><title>Healer Man</title>");
|
|
writeFileSync(path.join(staticDir, "sample.glb"), Buffer.from("0123456789"));
|
|
writeFileSync(path.join(contentDir, "manifest.json"), JSON.stringify({ schemaVersion: 1 }));
|
|
writeFileSync(path.join(contentDir, "objects", "asset.glb"), Buffer.from("abcdefghij"));
|
|
const runtime = createGameServer({ staticDir, contentDir, dataDir, corsOrigins: "https://iwanttoheal.phenomrom.com" });
|
|
await new Promise((resolve) => runtime.server.listen(0, "127.0.0.1", resolve));
|
|
const address = runtime.server.address();
|
|
const origin = `http://127.0.0.1:${address.port}`;
|
|
|
|
try {
|
|
assert.equal((await fetch(`${origin}/`)).status, 200);
|
|
const range = await fetch(`${origin}/sample.glb`, { headers: { Range: "bytes=2-5" } });
|
|
assert.equal(range.status, 206);
|
|
assert.equal(await range.text(), "2345");
|
|
|
|
const manifest = await fetch(`${origin}/content/manifest.json`);
|
|
assert.equal(manifest.status, 200);
|
|
assert.equal(manifest.headers.get("cache-control"), "no-cache");
|
|
assert.equal((await manifest.json()).schemaVersion, 1);
|
|
const contentRange = await fetch(`${origin}/content/objects/asset.glb`, { headers: { Range: "bytes=3-6" } });
|
|
assert.equal(contentRange.status, 206);
|
|
assert.equal(contentRange.headers.get("cache-control"), "public, max-age=31536000, immutable");
|
|
assert.equal(await contentRange.text(), "defg");
|
|
assert.equal((await fetch(`${origin}/content/missing`)).status, 404);
|
|
|
|
const registration = await fetch(`${origin}/api/auth/register`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json", Origin: "https://iwanttoheal.phenomrom.com" },
|
|
body: JSON.stringify({ username: "WebHealer", password: "healing-pass" }),
|
|
});
|
|
assert.equal(registration.status, 201);
|
|
assert.equal(registration.headers.get("access-control-allow-origin"), "https://iwanttoheal.phenomrom.com");
|
|
const auth = await registration.json();
|
|
|
|
const saved = await fetch(`${origin}/api/cloud-save`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json", Authorization: `Bearer ${auth.token}` },
|
|
body: JSON.stringify({ data: { version: 1, characters: [] } }),
|
|
});
|
|
assert.equal(saved.status, 200);
|
|
assert.equal((await saved.json()).revision, 1);
|
|
} finally {
|
|
await new Promise((resolve) => runtime.server.close(resolve));
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|