50 lines
3.4 KiB
JavaScript
50 lines
3.4 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const toDataModuleUrl = (source) => `data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
|
|
|
|
const run = async () => {
|
|
const rawSource = fs.readFileSync(path.join(__dirname, "../utils/api.js"), "utf8");
|
|
const moduleBody = rawSource.slice(rawSource.indexOf("const successCodes"));
|
|
const prelude = `
|
|
const currentUser = {}; const genealogies = []; const publicGenealogies = []; const treeMembers = []; const notifications = []; const joinApplications = [];
|
|
const listFamilyFeedFixtures = () => []; const listFamilyArticleFixtures = () => []; const listFamilyAlbumFixtures = () => []; const listCeremonyFixtures = () => []; const listGrowthRecordFixtures = () => []; const listNotificationFixtures = () => [];
|
|
const runtimeConfig = { baseUrl: "https://backend-api.ddxcjp.cn", clientId: "client-1", tenantId: "000000" };
|
|
const hasRemoteConfig = () => true; const resolveRuntimeMode = () => "remote"; const AUTH_TAC_SCENE = Object.freeze({}); const assertSmsCode = (value) => value;
|
|
const GENEALOGY_ACCESS_PRESET = Object.freeze({ MEMBER_ONLY: "MEMBER_ONLY" }); const fromApiGenealogyAccess = () => null; const session = { getToken: () => "session-1", saveToken() {} };
|
|
`;
|
|
const requests = [];
|
|
let responseData = [{ regionCode: "11", label: "Beijing", parentCode: "0", leaf: false, regionLevel: 1 }];
|
|
globalThis.uni = { request(options) { requests.push(options); queueMicrotask(() => options.success({ statusCode: 200, data: { code: 200, data: responseData } })); return { abort() {} }; } };
|
|
const { appApi } = await import(toDataModuleUrl(`${prelude}\n${moduleBody}`));
|
|
assert.deepStrictEqual(await appApi.getRegionChildren(), [{ regionCode: "11", label: "Beijing", parentCode: "0", leaf: false, regionLevel: 1 }]);
|
|
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/region/children");
|
|
assert.strictEqual(requests.at(-1).method, "GET");
|
|
assert.deepStrictEqual(requests.at(-1).data, { parentCode: "0" });
|
|
await assert.rejects(appApi.getRegionChildren(" "), /地区父级/);
|
|
responseData = [{ regionCode: "110000", label: "Beijing" }];
|
|
assert.deepStrictEqual(await appApi.getRegionPath("110000"), responseData);
|
|
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/region/path/110000");
|
|
await assert.rejects(appApi.getRegionPath(" "), /行政区划编码/);
|
|
|
|
assert.deepStrictEqual(
|
|
await appApi.searchRegions({ keyword: " Beijing ", level: 2, limit: 20 }),
|
|
responseData,
|
|
);
|
|
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/region/search");
|
|
assert.deepStrictEqual(requests.at(-1).data, { keyword: "Beijing", level: 2, limit: 20 });
|
|
await assert.rejects(appApi.searchRegions({ keyword: "" }), /行政区划搜索关键词/);
|
|
await assert.rejects(appApi.searchRegions({ keyword: "Beijing", level: 6 }), /行政区划级别/);
|
|
|
|
responseData = { regionCode: "110000", label: "Beijing" };
|
|
assert.deepStrictEqual(await appApi.getRegion("110000"), responseData);
|
|
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/region/110000");
|
|
delete globalThis.uni;
|
|
process.stdout.write("REGION-API-RUNTIME-SMOKE PASS\n");
|
|
};
|
|
|
|
run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`); process.exit(1); });
|