import test from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; test("userLevelTotal stores userCat only when the request provides it", async () => { const source = await readFile( new URL("../userLevelTotal.ts", import.meta.url), "utf8" ); assert.match(source, /let\s+userCat\s*=\s*ctx\.body\.userCat/); assert.match( source, /async function rank\([^)]*useravatarIcon,\s*userCat,\s*unionid\)/ ); assert.match( source, /if\s*\(hasUserCat\(userCat\)\)\s*\{\s*total\["all"\]\[i\]\.userCat\s*=\s*String\(userCat\)/ ); assert.match( source, /if\s*\(hasUserCat\(userCat\)\)\s*\{\s*total\[address\]\[i\]\.userCat\s*=\s*String\(userCat\)/ ); assert.match( source, /\.\.\.\(hasUserCat\(userCat\)\s*\?\s*\{\s*userCat:\s*String\(userCat\)\s*\}\s*:\s*\{\}\)/ ); }); test("userLevelTotal does not backfill a missing userCat", async () => { const source = await readFile( new URL("../userLevelTotal.ts", import.meta.url), "utf8" ); assert.match( source, /return\s+userCat\s*!==\s*undefined\s*&&\s*userCat\s*!==\s*null\s*&&\s*userCat\s*!==\s*""/ ); assert.doesNotMatch(source, /Math\.random/); assert.doesNotMatch(source, /normalizeRankUserCats/); });