154 lines
5.8 KiB
TypeScript
154 lines
5.8 KiB
TypeScript
import fs from "node:fs";
|
|
import vm from "node:vm";
|
|
import assert from "node:assert/strict";
|
|
process.env.NODE_ENV = "test";
|
|
const { createApp } = await import("../dist/main.js");
|
|
const app = await createApp();
|
|
try {
|
|
await app.init();
|
|
const response = await app
|
|
.getHttpAdapter()
|
|
.getInstance()
|
|
.inject({ method: "GET", url: "/public/workspace?brandSlug=default-brand" });
|
|
assert.equal(response.statusCode, 200);
|
|
const workspace = JSON.parse(response.body) as Record<string, unknown>;
|
|
const trustItems = workspace.trustItems as { id?: string }[];
|
|
assert.ok(trustItems.every((item) => typeof item.id === "string"));
|
|
const live = await fetch(
|
|
`http://127.0.0.1:${process.env.API_PORT ?? 3001}/public/workspace?brandSlug=default-brand`,
|
|
);
|
|
assert.equal(live.status, 200);
|
|
const liveWorkspace = (await live.json()) as Record<string, unknown>;
|
|
assert.deepEqual(liveWorkspace.trustItems, workspace.trustItems);
|
|
assert.deepEqual(liveWorkspace.inquiryForms, workspace.inquiryForms);
|
|
fs.writeFileSync("/tmp/food-template-public-workspace.json", JSON.stringify(workspace, null, 2));
|
|
let page:
|
|
| {
|
|
data: Record<string, unknown>;
|
|
load: () => Promise<void>;
|
|
setData: (value: Record<string, unknown>) => void;
|
|
}
|
|
| undefined;
|
|
vm.runInNewContext(
|
|
fs.readFileSync("../../dist/miniapp/default-brand/pages/stall-service/index.js", "utf8"),
|
|
{
|
|
Page(value: typeof page) {
|
|
page = value;
|
|
},
|
|
require(name: string) {
|
|
return name.includes("runtime-workspace")
|
|
? { loadWorkspaceWithFallback: () => Promise.resolve(workspace) }
|
|
: { openPage() {} };
|
|
},
|
|
},
|
|
);
|
|
assert.ok(page);
|
|
page.setData = (data) => Object.assign(page!.data, data);
|
|
await page.load();
|
|
const data = page.data as {
|
|
schedule: { rows: unknown[] };
|
|
coverage: { config: { pickupDetail: string; deliveryDetail: string } };
|
|
process: { steps: unknown[] };
|
|
};
|
|
assert.equal(data.schedule.rows.length, 2);
|
|
assert.ok(data.coverage.config.pickupDetail && data.coverage.config.deliveryDetail);
|
|
assert.equal(data.process.steps.length, 4);
|
|
const forms = workspace.inquiryForms as { fields: { key: string }[] }[];
|
|
for (const key of ["type", "people", "date", "budget", "delivery", "remark", "phone"])
|
|
assert.ok(forms.some((form) => form.fields.some((field) => field.key === key)));
|
|
fs.writeFileSync("/tmp/food-template-service-page-data.json", JSON.stringify(page.data, null, 2));
|
|
let inquiry:
|
|
| {
|
|
data: Record<string, unknown>;
|
|
onLoad: (options: Record<string, unknown>) => Promise<void>;
|
|
setData: (value: Record<string, unknown>) => void;
|
|
}
|
|
| undefined;
|
|
vm.runInNewContext(
|
|
fs.readFileSync("../../dist/miniapp/default-brand/pages/inquiry/index.js", "utf8"),
|
|
{
|
|
Page(value: typeof inquiry) {
|
|
inquiry = value;
|
|
},
|
|
require(name: string) {
|
|
return name.includes("runtime-workspace")
|
|
? { loadWorkspaceWithFallback: () => Promise.resolve(workspace) }
|
|
: {};
|
|
},
|
|
},
|
|
);
|
|
assert.ok(inquiry);
|
|
inquiry.setData = (value) => Object.assign(inquiry!.data, value);
|
|
await inquiry.onLoad({});
|
|
const inquiryFields = inquiry.data.fields as {
|
|
key: string;
|
|
useChips: boolean;
|
|
halfWidth: boolean;
|
|
}[];
|
|
assert.equal(inquiryFields.find((field) => field.key === "type")?.useChips, true);
|
|
assert.equal(inquiryFields.filter((field) => field.halfWidth).length, 4);
|
|
fs.writeFileSync(
|
|
"/tmp/food-template-inquiry-page-data.json",
|
|
JSON.stringify(inquiry.data, null, 2),
|
|
);
|
|
console.log(
|
|
"Actual database -> published API -> generated page: 2 schedule cards, 2 delivery columns, 4 steps, food inquiry fields verified",
|
|
);
|
|
const doorsResponse = await app
|
|
.getHttpAdapter()
|
|
.getInstance()
|
|
.inject({ method: "GET", url: "/public/workspace?brandSlug=linjing-doors" });
|
|
assert.equal(doorsResponse.statusCode, 200);
|
|
const doors = JSON.parse(doorsResponse.body) as {
|
|
trustItems: { type: string; content: string; config: Record<string, unknown> }[];
|
|
trustPresentation: Record<string, unknown>;
|
|
};
|
|
assert.equal(
|
|
doors.trustItems.some((item: { type: string }) => item.type === "profile"),
|
|
false,
|
|
);
|
|
const credentials = doors.trustItems.filter(
|
|
(item: { type: string }) => item.type === "credential",
|
|
);
|
|
const primary = credentials.find(
|
|
(item: { config: object }) => Object.keys(item.config).length > 0,
|
|
);
|
|
assert.ok(primary);
|
|
assert.deepEqual(doors.trustPresentation.stats, primary.config.stats);
|
|
const liveDoors = await fetch(
|
|
`http://127.0.0.1:${process.env.API_PORT ?? 3001}/public/workspace?brandSlug=linjing-doors`,
|
|
);
|
|
assert.equal(liveDoors.status, 200);
|
|
const liveDoorsWorkspace = (await liveDoors.json()) as Record<string, unknown>;
|
|
assert.deepEqual(liveDoorsWorkspace.trustPresentation, doors.trustPresentation);
|
|
type DoorsPage = {
|
|
data: { trustPresentation: { bio: string } };
|
|
setData: (value: object) => void;
|
|
onLoad: () => void;
|
|
};
|
|
let doorsPage: DoorsPage | undefined;
|
|
vm.runInNewContext(
|
|
fs.readFileSync("../../dist/miniapp/linjing-doors/pages/index/index.js", "utf8"),
|
|
{
|
|
Page(value: DoorsPage) {
|
|
doorsPage = value;
|
|
},
|
|
require() {
|
|
return { loadWorkspaceWithFallback: () => Promise.resolve(doors) };
|
|
},
|
|
wx: { setNavigationBarTitle() {} },
|
|
},
|
|
);
|
|
assert.ok(doorsPage);
|
|
doorsPage.setData = (value: object) => Object.assign(doorsPage!.data, value);
|
|
doorsPage.onLoad();
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.equal(doorsPage.data.trustPresentation.bio, primary.content);
|
|
fs.writeFileSync("/tmp/linjing-template-page-data.json", JSON.stringify(doorsPage.data, null, 2));
|
|
console.log(
|
|
"Linjing published API -> generated page: declared credential stats and biography verified; obsolete profile excluded",
|
|
);
|
|
} finally {
|
|
await app.close();
|
|
}
|