MatchMaster/server/laf-cloud/tests/set-cat-arr.test.mjs
2026-07-30 12:22:02 +08:00

147 lines
3.6 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { registerHooks } from "node:module";
const functionUrl = new URL("../functions/setCatArr.ts", import.meta.url);
const configUrl = new URL("../functions/setCatArr.yaml", import.meta.url);
const state = {
user: null,
collections: [],
updates: [],
};
globalThis.__catArrCloudMock = {
database() {
return {
collection(name) {
state.collections.push(name);
return {
where() {
return {
async getOne() {
return { data: state.user };
},
async update(payload) {
state.updates.push(payload);
return {};
},
};
},
};
},
};
},
};
globalThis.__catArrUtilsMock = {
checkToken(received, expected) {
return received === expected;
},
};
registerHooks({
resolve(specifier, context, nextResolve) {
if (specifier === "@lafjs/cloud") {
return {
url: "data:text/javascript,export default globalThis.__catArrCloudMock",
shortCircuit: true,
};
}
if (specifier === "@/Utils") {
return {
url: "data:text/javascript,export default globalThis.__catArrUtilsMock",
shortCircuit: true,
};
}
return nextResolve(specifier, context);
},
});
const { default: setCatArr } = await import(functionUrl);
function reset(user) {
state.user = user;
state.collections.length = 0;
state.updates.length = 0;
}
test("setCatArr cloud function and configuration exist", async () => {
assert.equal(existsSync(functionUrl), true);
assert.equal(existsSync(configUrl), true);
const yaml = await readFile(configUrl, "utf8");
assert.match(yaml, /^name:\s*setCatArr$/m);
});
test("read returns defaults for a legacy user", async () => {
reset({ _id: "u1", token: "secret" });
const result = await setCatArr({
body: { action: "read", uid: "u1", token: "secret" },
});
assert.equal(result.code, 1);
assert.deepEqual(result.data.catArr, [1, 2, 3]);
});
test("save appends and persists a new cat ID", async () => {
reset({ _id: "u1", token: "secret" });
const result = await setCatArr({
body: { action: "save", uid: "u1", token: "secret", catNum: 4 },
});
assert.equal(result.code, 1);
assert.deepEqual(result.data.catArr, [1, 2, 3, 4]);
assert.deepEqual(state.updates, [{ catArr: [1, 2, 3, 4] }]);
});
test("save rejects a duplicate cat ID", async () => {
reset({ _id: "u1", token: "secret", catArr: [1, 2, 3] });
const result = await setCatArr({
body: { action: "save", uid: "u1", token: "secret", catNum: 2 },
});
assert.equal(result.code, 0);
assert.deepEqual(state.updates, []);
});
test("save rejects an invalid cat ID", async () => {
reset({ _id: "u1", token: "secret", catArr: [1, 2, 3] });
const result = await setCatArr({
body: { action: "save", uid: "u1", token: "secret", catNum: -1 },
});
assert.equal(result.code, 0);
assert.deepEqual(state.updates, []);
});
test("gameName iaa routes to usersAd", async () => {
reset({ _id: "u1", token: "secret", catArr: [1, 2, 3] });
await setCatArr({
body: {
action: "read",
uid: "u1",
token: "secret",
gameName: "iaa",
},
});
assert.equal(state.collections[0], "usersAd");
});
test("missing user returns an error", async () => {
reset(null);
const result = await setCatArr({
body: { action: "read", uid: "missing", token: "secret" },
});
assert.equal(result.code, 0);
});