-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-api-cli.js
More file actions
executable file
Β·153 lines (135 loc) Β· 3.94 KB
/
Copy pathtest-api-cli.js
File metadata and controls
executable file
Β·153 lines (135 loc) Β· 3.94 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
/**
* Test script for API generator CLI functionality
* Uses yo from test-project/node_modules when present (yo 7.x), else global yo.
*/
const { spawnYo, resolveYo } = require("./scripts/resolve-yo");
const { cliTestPassed } = require("./scripts/cli-test-shared");
const repoRoot = __dirname;
const testCases = [
{
name: "Basic GET endpoint",
args: ["g-next:api", "--route", "users", "--method", "get"],
expect: "cli-success",
},
{
name: "POST endpoint with dynamic parameter",
args: ["g-next:api", "--route", "posts/{postId}", "--method", "post"],
expect: "cli-success",
},
{
name: "PUT endpoint with cookie auth",
args: [
"g-next:api",
"--route",
"users/{userId}",
"--method",
"put",
"--useCookieAuth",
"--cookieRole",
"admin",
],
expect: "cli-success",
},
{
name: "Invalid HTTP method",
args: ["g-next:api", "--route", "users", "--method", "invalid"],
expect: "validation",
},
{
name: "Missing cookie role with auth",
args: ["g-next:api", "--route", "users", "--method", "get", "--useCookieAuth"],
expect: "validation",
},
{
name: "Invalid route format - empty parameter",
args: ["g-next:api", "--route", "users/{}", "--method", "get"],
expect: "validation",
},
{
name: "Invalid route format - invalid parameter name",
args: ["g-next:api", "--route", "users/{123invalid}", "--method", "get"],
expect: "validation",
},
{
name: "Invalid route format - special characters",
args: ["g-next:api", "--route", "users@#$", "--method", "get"],
expect: "validation",
},
];
console.log("π§ͺ Testing API Generator CLI functionality...");
const yoInfo = resolveYo(repoRoot);
console.log(`π Working directory: ${yoInfo.cwd}`);
console.log(`π Yeoman CLI: ${yoInfo.label}\n`);
function runTest(testCase) {
return new Promise((resolve) => {
console.log(`\nπ Test: ${testCase.name}`);
console.log(` Command: yo ${testCase.args.join(" ")}`);
console.log(` Expected: ${testCase.expect}`);
const child = spawnYo(repoRoot, testCase.args, { stdio: "pipe" });
let stdout = "";
let stderr = "";
child.stdout.on("data", (data) => {
stdout += data.toString();
});
child.stderr.on("data", (data) => {
stderr += data.toString();
});
let settled = false;
const finish = (code) => {
if (settled) return;
settled = true;
console.log(` Exit code: ${code}`);
const ok = cliTestPassed({
code,
stdout,
stderr,
expect: testCase.expect,
});
if (ok) {
console.log(" β
Passed");
} else {
console.log(" β Failed");
if (stdout) console.log(` stdout (trim): ${stdout.slice(0, 400)}`);
if (stderr) console.log(` stderr (trim): ${stderr.slice(0, 400)}`);
}
resolve(ok);
};
const timer = setTimeout(() => {
child.kill("SIGKILL");
console.log(" β° Test timed out");
finish(-1);
}, 10000);
child.on("close", (code) => {
clearTimeout(timer);
finish(code);
});
});
}
async function main() {
const yoCheck = spawnYo(repoRoot, ["--version"], { stdio: "pipe" });
const yoAvailable = await new Promise((resolve) => {
yoCheck.on("error", () => resolve(false));
yoCheck.on("close", (code) => resolve(code === 0));
});
if (!yoAvailable) {
console.log("β Yeoman (yo) is not available. Install test-project deps or global yo:");
console.log(" npm install --prefix test-project");
process.exit(1);
}
let failed = 0;
for (const tc of testCases) {
const ok = await runTest(tc);
if (!ok) failed++;
}
console.log("\nπ API CLI tests finished.");
if (failed > 0) {
console.log(`\nβ ${failed} test(s) failed.`);
process.exit(1);
}
console.log("\nβ
All API CLI tests passed.");
}
main().catch((err) => {
console.error(err);
process.exit(1);
});