forked from phenom/healer-man
Release Healer Man 0.1.27
This commit is contained in:
1 parent
147c2804d4
commit
ed8b9e3719
6 files changed
+86
-6
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { runInNewContext } from "node:vm";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { ARENA_STAGES } from "../game/arenaStages";
|
||||
import { BROWSER_CONTENT_CACHE_NAME } from "./contentManager";
|
||||
|
||||
const workerSource = readFileSync(
|
||||
@@ -7,7 +9,61 @@ const workerSource = readFileSync(
|
||||
"utf8",
|
||||
);
|
||||
|
||||
function contentWorker(cached?: Response) {
|
||||
const handlers = new Map<string, (event: unknown) => void>();
|
||||
const fetch = vi.fn(async () => new Response("bundled arena", { status: 200 }));
|
||||
const match = vi.fn(async () => cached);
|
||||
runInNewContext(workerSource, {
|
||||
self: {
|
||||
location: { origin: "https://healer.test" },
|
||||
addEventListener: (type: string, handler: (event: unknown) => void) => handlers.set(type, handler),
|
||||
},
|
||||
caches: { open: async () => ({ match }) },
|
||||
fetch,
|
||||
URL,
|
||||
Response,
|
||||
});
|
||||
return {
|
||||
fetch,
|
||||
match,
|
||||
request: (path: string) => {
|
||||
let response: Promise<Response> | undefined;
|
||||
handlers.get("fetch")!({
|
||||
request: new Request(new URL(path, "https://healer.test")),
|
||||
respondWith: (value: Promise<Response>) => { response = value; },
|
||||
});
|
||||
return response;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("browser content cache worker", () => {
|
||||
it.each(ARENA_STAGES)("loads bundled $name assets without an optional content install", async (stage) => {
|
||||
const worker = contentWorker();
|
||||
for (const asset of stage.assets) {
|
||||
const response = await worker.request(`${asset.url}?arena-runtime=1`);
|
||||
expect(response?.status).toBe(200);
|
||||
expect(await response?.text()).toBe("bundled arena");
|
||||
}
|
||||
expect(worker.fetch).toHaveBeenCalledTimes(stage.assets.length);
|
||||
});
|
||||
|
||||
it("prefers installed arena updates over bundled files, ignoring the runtime query", async () => {
|
||||
const worker = contentWorker(new Response("updated arena"));
|
||||
const response = await worker.request(`${ARENA_STAGES[0].assets[0].url}?arena-runtime=1`);
|
||||
expect(await response?.text()).toBe("updated arena");
|
||||
expect(worker.match).toHaveBeenCalledWith(expect.any(Request), { ignoreSearch: true });
|
||||
expect(worker.fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("still blocks optional dungeon assets that have not been installed", async () => {
|
||||
const worker = contentWorker();
|
||||
const response = await worker.request("/assets/game/manastorm/optional/visual.glb");
|
||||
expect(response?.status).toBe(404);
|
||||
expect(await response?.text()).toBe("Optional content is not installed.");
|
||||
expect(worker.fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("serves only cached same-origin asset requests from the active content cache", () => {
|
||||
expect(workerSource).toContain(`const CONTENT_CACHE_NAME = "${BROWSER_CONTENT_CACHE_NAME}";`);
|
||||
expect(workerSource).toContain('request.method !== "GET"');
|
||||
|
||||
Reference in new issue
Block a user