107 lines
4.3 KiB
TypeScript
107 lines
4.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { assertEquals } from "@std/assert";
|
|
import { join } from "@std/path";
|
|
import { CLIENT_MANIFEST, client_manifest, sha256, update_client } from "$/desktop/client_updates.ts";
|
|
|
|
const PROTOCOL = 7;
|
|
|
|
// a build folder with these files and their manifest
|
|
async function make_build(dir: string, files: Record<string, string>, protocol = PROTOCOL) {
|
|
const hashes: Record<string, string> = {};
|
|
for (const [path, text] of Object.entries(files)) {
|
|
const bytes = new TextEncoder().encode(text);
|
|
Deno.mkdirSync(join(dir, path, ".."), { recursive: true });
|
|
Deno.writeFileSync(join(dir, path), bytes);
|
|
hashes[path] = await sha256(bytes);
|
|
}
|
|
const manifest = await client_manifest(hashes, protocol);
|
|
Deno.writeTextFileSync(join(dir, CLIENT_MANIFEST), JSON.stringify(manifest));
|
|
return manifest;
|
|
}
|
|
|
|
// the site as a folder, and what's been downloaded from it
|
|
function site(dir: string) {
|
|
const downloaded: string[] = [];
|
|
const fetch = (url: string | URL | Request) => {
|
|
const path = String(url).replace("https://site.test/", "");
|
|
downloaded.push(path);
|
|
try {
|
|
return Promise.resolve(new Response(Deno.readFileSync(join(dir, path))));
|
|
} catch {
|
|
return Promise.resolve(new Response("not found", { status: 404 }));
|
|
}
|
|
};
|
|
return { fetch: fetch as typeof globalThis.fetch, downloaded };
|
|
}
|
|
|
|
const FILES = { "client/main.js": "the game", "client/index.html": "<html>", "assets/sprites/gui.png": "pixels" };
|
|
|
|
function setup() {
|
|
const root = Deno.makeTempDirSync();
|
|
const dirs = { bundled: join(root, "bundled"), site: join(root, "site"), cache: join(root, "cache") };
|
|
return {
|
|
root,
|
|
dirs,
|
|
options: (fetch: typeof globalThis.fetch) => ({
|
|
source: "https://site.test",
|
|
bundled_dir: dirs.bundled,
|
|
cache_dir: dirs.cache,
|
|
protocol: PROTOCOL,
|
|
fetch,
|
|
}),
|
|
};
|
|
}
|
|
|
|
Deno.test("the app plays its own client when the site's is the same", async () => {
|
|
const { root, dirs, options } = setup();
|
|
await make_build(dirs.bundled, FILES);
|
|
await make_build(dirs.site, FILES);
|
|
const { fetch, downloaded } = site(dirs.site);
|
|
assertEquals(await update_client(options(fetch)), dirs.bundled);
|
|
assertEquals(downloaded, [CLIENT_MANIFEST]);
|
|
Deno.removeSync(root, { recursive: true });
|
|
});
|
|
|
|
Deno.test("a newer client on the site is downloaded, only the files that changed", async () => {
|
|
const { root, dirs, options } = setup();
|
|
await make_build(dirs.bundled, FILES);
|
|
const newer = await make_build(dirs.site, { ...FILES, "client/main.js": "the game, but better" });
|
|
const { fetch, downloaded } = site(dirs.site);
|
|
|
|
const played = await update_client(options(fetch));
|
|
assertEquals(played, join(dirs.cache, newer.build));
|
|
assertEquals(Deno.readTextFileSync(join(played, "client/main.js")), "the game, but better");
|
|
assertEquals(Deno.readTextFileSync(join(played, "assets/sprites/gui.png")), "pixels");
|
|
assertEquals(downloaded, [CLIENT_MANIFEST, "client/main.js"]);
|
|
|
|
// the next launch already has it, and offline it's still what's played
|
|
assertEquals(await update_client(options(fetch)), played);
|
|
const offline = (() => Promise.reject(new Error("offline"))) as typeof globalThis.fetch;
|
|
assertEquals(await update_client(options(offline)), played);
|
|
Deno.removeSync(root, { recursive: true });
|
|
});
|
|
|
|
Deno.test("a client for another protocol isn't used, the singleplayer server couldn't talk to it", async () => {
|
|
const { root, dirs, options } = setup();
|
|
await make_build(dirs.bundled, FILES);
|
|
await make_build(dirs.site, { ...FILES, "client/main.js": "a newer protocol" }, PROTOCOL + 1);
|
|
const { fetch, downloaded } = site(dirs.site);
|
|
assertEquals(await update_client(options(fetch)), dirs.bundled);
|
|
assertEquals(downloaded, [CLIENT_MANIFEST]);
|
|
Deno.removeSync(root, { recursive: true });
|
|
});
|
|
|
|
Deno.test("a download that doesn't match the manifest is thrown away", async () => {
|
|
const { root, dirs, options } = setup();
|
|
await make_build(dirs.bundled, FILES);
|
|
await make_build(dirs.site, { ...FILES, "client/main.js": "the game, but better" });
|
|
// changed after the manifest was made, like a half uploaded site
|
|
Deno.writeTextFileSync(join(dirs.site, "client/main.js"), "something else");
|
|
const { fetch } = site(dirs.site);
|
|
assertEquals(await update_client(options(fetch)), dirs.bundled);
|
|
assertEquals([...Deno.readDirSync(dirs.cache)], []);
|
|
Deno.removeSync(root, { recursive: true });
|
|
});
|