Files
magpie/site/worker.test.mjs
T
yetone aa42142ea5 update: What's new and the waiting update's notes follow the app's language (freecss on Discord: 更新后的公告的语言是不是应该设置里面的走) — the release notes were English alone. A release's notes are to carry a Simplified Chinese translation below a <!-- lang:zh --> marker (the magpie-releases workflow, changed separately); usemagpie.ai's /api/latest and /api/notes take ?lang=zh for the Chinese where a release has it, and give anything else, an app from before included, the English alone, everything above the marker. The page asks /api/whatsnew, /api/update and /api/update/check in its own language (?lang=); magpie asks the site in it, keeps What's new's notes per language, and asks again for the waiting update's notes when a page's language isn't the one they are held in (at most once a minute); a check by itself asks in the pages' last language, else the app's, as the tray menu has it. Notes that come back whole (a site not yet deployed) are cut in the app too; notes without the marker (every release so far) are English, in either language. The worker must be deployed (npx wrangler deploy in site/) before the workflow change ships, or apps would get both languages. TestInLang, TestFeedsAskInLang, TestAskedLang, TestWhatsNewInPageLang, TestUpdateNotesInPageLang, site/worker.test.mjs (node --test site/worker.test.mjs), whats-new.test.cjs's language check (Chromium and WebKit, en and zh); update-check, update-hide, update-auto, sync-update pass in WebKit. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (TestClaudeRunEndsWhenTheClientLeaves flaked once under the full run, passes alone; gateway untouched).
2026-10-02 11:31:11 +08:00

60 lines
2.7 KiB
JavaScript

// node --test site/: the update feed's notes follow the app's language
// (freecss on Discord). GitHub and the edge cache are stood in for.
import { test } from "node:test";
import assert from "node:assert/strict";
import worker from "./worker.js";
const EN = "### Features\n\n- One thing (#1)\n\n### Install\n\nDownload it.";
const ZH = "### 新功能\n\n- 一件事 (#1)";
const BODIES = {
"v0.1.3": `${EN}\n\n<!-- lang:zh -->\n\n${ZH}\n`,
"v0.1.2": "### Fixes\n\n- Older, English alone",
"v0.1.1": "- first",
};
const store = new Map();
globalThis.caches = {
default: {
match: async (req) => (store.has(req.url) ? new Response(store.get(req.url)) : undefined),
put: async (req, res) => void store.set(req.url, await res.text()),
},
};
globalThis.fetch = async (u) => {
u = String(u);
const gh = (tag) => ({ tag_name: tag, body: BODIES[tag], html_url: "https://github.com/r/" + tag, published_at: "2026-10-01T00:00:00Z", draft: false, prerelease: false, assets: [{ name: "magpie-linux-amd64", size: 1, browser_download_url: "https://dl/x" }] });
if (u.endsWith("/releases/latest")) return Response.json(gh("v0.1.3"));
if (u.includes("/releases?per_page=")) return Response.json(Object.keys(BODIES).map(gh));
if (u.endsWith("/SHA256SUMS")) return new Response("abc magpie-linux-amd64\n");
return new Response("not found", { status: 404 });
};
const ctx = { waitUntil: (p) => p };
const get = async (path) => {
const res = await worker.fetch(new Request("https://usemagpie.ai" + path), {}, ctx);
assert.equal(res.status, 200, path);
return { body: await res.json(), cc: res.headers.get("Cache-Control") };
};
test("latest: the English alone without lang, the Chinese with zh", async () => {
for (const q of ["", "?lang=en", "?lang=fr"]) {
const { body, cc } = await get("/api/latest" + q);
assert.equal(body.version, "0.1.3");
assert.equal(body.notes, EN, q);
assert.match(cc, /max-age=/);
}
for (const q of ["?lang=zh", "?lang=zh-CN", "?lang=zh_Hans"]) {
assert.equal((await get("/api/latest" + q)).body.notes, ZH, q);
}
// asked in Chinese first, the edge's copy still has both
assert.equal((await get("/api/latest")).body.notes, EN);
});
test("notes: each release in the language, English where it has no Chinese", async () => {
const zh = (await get("/api/notes?after=0.1.1&upto=0.1.3&lang=zh")).body.releases;
assert.deepEqual(zh.map((r) => [r.version, r.notes]), [["0.1.3", ZH], ["0.1.2", BODIES["v0.1.2"]]]);
const en = (await get("/api/notes?after=0.1.1&upto=0.1.3")).body.releases;
assert.deepEqual(en.map((r) => [r.version, r.notes]), [["0.1.3", EN], ["0.1.2", BODIES["v0.1.2"]]]);
const again = (await get("/api/notes?after=0.1.1&upto=0.1.3&lang=zh")).body.releases;
assert.equal(again[0].notes, ZH);
});