-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticWebRootCompatibility.test.mjs
More file actions
82 lines (73 loc) · 3.47 KB
/
Copy pathStaticWebRootCompatibility.test.mjs
File metadata and controls
82 lines (73 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { startStaticWebServer } from "../../scripts/start-dev.mjs";
import {
DEFAULT_LOCAL_WEB_ROOT,
LOCAL_WEB_ROOT_ENV,
WWW_LOCAL_WEB_ROOT,
resolveBrowserRoutePath,
resolveLocalWebRoot,
resolveStaticRouteTarget,
} from "../../../src/dev-runtime/server/static-web-root.mjs";
const repoRoot = path.resolve(fileURLToPath(new URL("../../..", import.meta.url)));
test("local web root defaults to repository root and can be pointed at www", () => {
const defaultRoot = resolveLocalWebRoot({ env: {}, repoRoot });
assert.equal(defaultRoot.relativePath, DEFAULT_LOCAL_WEB_ROOT);
assert.equal(defaultRoot.absolutePath, repoRoot);
assert.equal(defaultRoot.source, "default");
const wwwRoot = resolveLocalWebRoot({
env: {
[LOCAL_WEB_ROOT_ENV]: WWW_LOCAL_WEB_ROOT,
},
repoRoot,
});
assert.equal(wwwRoot.relativePath, WWW_LOCAL_WEB_ROOT);
assert.equal(wwwRoot.absolutePath, path.join(repoRoot, "www"));
assert.equal(wwwRoot.source, "configured");
});
test("browser route compatibility preserves public URLs while normalizing filesystem lookup", () => {
assert.equal(resolveBrowserRoutePath("/"), "/index.html");
assert.equal(resolveBrowserRoutePath("/index.html"), "/index.html");
assert.equal(resolveBrowserRoutePath("/toolbox/index.html"), "/toolbox/index.html");
assert.equal(resolveBrowserRoutePath("/tools/game-design/index.html"), "/toolbox/game-design/index.html");
assert.equal(resolveBrowserRoutePath("/assets/theme-v2/css/theme.css"), "/assets/theme-v2/css/theme.css");
assert.equal(resolveBrowserRoutePath("/account/sign-in.html"), "/account/sign-in.html");
assert.equal(resolveBrowserRoutePath("/admin/system-health.html"), "/admin/system-health.html");
assert.equal(resolveBrowserRoutePath("/games/index.html"), "/games/index.html");
});
test("static route target can prefer a configured www root before repo-root fallback", async () => {
const routeTarget = await resolveStaticRouteTarget({
decodedPath: "/index.html",
repoRoot,
webRoot: "www",
});
assert.equal(routeTarget.routePath, "/index.html");
assert.equal(routeTarget.targetPath, path.join(repoRoot, "index.html"));
assert.equal(routeTarget.webRoot.relativePath, "www");
});
test("local static web server can serve from a configurable web root", async () => {
const fixtureRoot = path.join(repoRoot, "dev", "workspace", "tmp", "static-web-root-compatibility");
await fs.rm(fixtureRoot, { force: true, recursive: true });
await fs.mkdir(path.join(fixtureRoot, "assets"), { recursive: true });
await fs.writeFile(path.join(fixtureRoot, "index.html"), "<!doctype html><title>Fixture Root</title>", "utf8");
await fs.writeFile(path.join(fixtureRoot, "assets", "probe.txt"), "fixture asset", "utf8");
const server = await startStaticWebServer({
apiBaseUrl: "http://127.0.0.1:1",
port: 0,
webRoot: fixtureRoot,
});
try {
const indexResponse = await fetch(`${server.baseUrl}/index.html`);
assert.equal(indexResponse.status, 200);
assert.match(await indexResponse.text(), /Fixture Root/);
const assetResponse = await fetch(`${server.baseUrl}/assets/probe.txt`);
assert.equal(assetResponse.status, 200);
assert.equal(await assetResponse.text(), "fixture asset");
} finally {
await server.close();
await fs.rm(fixtureRoot, { force: true, recursive: true });
}
});