swaggerInspect.spec.js
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const {
buildContract,
extractRequestContract,
detectPagination,
methodSuffixToPascal,
} = require("@/libs/rop/swaggerInspect");
const miniSwagger = {
paths: {
"/router/dj.api.smartcarlife.demo.list/1.0.0": {
post: {
summary: "演示列表",
parameters: [
{ name: "currentPage", in: "query", type: "int", required: false, description: "页码" },
{ name: "sessionId", in: "query", type: "string", required: true, description: "会话" },
],
responses: {
200: {
schema: { $ref: "#/definitions/RopResponse<RpcPagination<DemoRow>>" },
},
},
},
},
},
definitions: {
"RopResponse<RpcPagination<DemoRow>>": {
properties: {
code: { type: "string" },
data: { $ref: "#/definitions/RpcPagination<DemoRow>" },
},
},
"RpcPagination<DemoRow>": {
properties: {
pageNum: { type: "integer" },
pageSize: { type: "integer" },
total: { type: "integer" },
list: { type: "array", items: { $ref: "#/definitions/DemoRow" } },
},
},
DemoRow: {
properties: {
id: { type: "integer" },
name: { type: "string" },
},
},
},
};
describe("swaggerInspect", () => {
it("应解析请求 query 参数", () => {
const op = miniSwagger.paths["/router/dj.api.smartcarlife.demo.list/1.0.0"].post;
const request = extractRequestContract(op);
expect(request.parameters.map((p) => p.name)).toEqual(["currentPage", "sessionId"]);
});
it("应识别 RpcPagination", () => {
const pagination = detectPagination(miniSwagger, "RopResponse<RpcPagination<DemoRow>>");
expect(pagination.type).toContain("RpcPagination");
expect(pagination.fields).toContain("list");
});
it("应生成完整 contract", () => {
const contract = buildContract(miniSwagger, "dj.api.smartcarlife.demo.list");
expect(contract.method).toBe("dj.api.smartcarlife.demo.list");
expect(contract.summary).toBe("演示列表");
expect(contract.response.pagination).toBeTruthy();
});
it("methodSuffixToPascal 应组合末两段", () => {
expect(methodSuffixToPascal("dj.api.smartcarlife.dailyReport.overview.page")).toBe("OverviewPage");
expect(methodSuffixToPascal("dj.api.smartcarlife.dailyReport.detail.select")).toBe("DetailSelect");
});
it("listMethodsByPrefix 应按前缀过滤", () => {
const { listMethodsByPrefix } = require("@/libs/rop/swaggerInspect");
const all = listMethodsByPrefix(miniSwagger, "dj.api.smartcarlife");
expect(all).toEqual(["dj.api.smartcarlife.demo.list"]);
const none = listMethodsByPrefix(miniSwagger, "dj.api.other");
expect(none).toEqual([]);
});
it("buildJsdocTypedefs 应包含请求与分页 typedef", () => {
const { buildJsdocTypedefs } = require("@/libs/rop/swaggerInspect");
const contract = buildContract(miniSwagger, "dj.api.smartcarlife.demo.list");
const typedefs = buildJsdocTypedefs(contract);
expect(typedefs).toContain("@typedef {Object} DemoListParams");
expect(typedefs).toContain("@property {number} currentPage");
expect(typedefs).toContain("@typedef {Object} DemoListPageResponse");
expect(typedefs).toContain("@property {DemoListRow[]} [list]");
});
});