milkio 0.6.0 → 0.7.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api-test/index.ts +43 -2
- package/defines/define-api-test.ts +22 -0
- package/package.json +1 -1
package/api-test/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
1
2
|
import schema from "../../../generated/api-schema";
|
|
2
3
|
import { ExecuteResultFail, type MilkioApp } from "..";
|
|
3
|
-
import chalk from "chalk";
|
|
4
4
|
import { handleCatchError } from "../utils/handle-catch-error.ts";
|
|
5
5
|
|
|
6
6
|
export const executeApiTests = async <Path extends Array<keyof (typeof schema)["apiTestsSchema"]>>(app: MilkioApp, path: Path | string | true | 1 | undefined) => {
|
|
@@ -16,9 +16,49 @@ export const executeApiTests = async <Path extends Array<keyof (typeof schema)["
|
|
|
16
16
|
const startedAt = new Date().getTime();
|
|
17
17
|
const apiTestHooks = await import("../../../src/api-test.ts");
|
|
18
18
|
await apiTestHooks.default.onBootstrap();
|
|
19
|
+
const clientPackage = apiTestHooks.default?.client ? (await apiTestHooks.default?.client()) : undefined;
|
|
19
20
|
const results: Array<{ path: string, case: number, fail: boolean, failMessage?: string }> = [];
|
|
20
21
|
console.log(chalk.hex("#0B346E")(`₋₋₋₋₋₋₋₋`));
|
|
21
22
|
|
|
23
|
+
if (!clientPackage) {
|
|
24
|
+
console.log(`🚨 For testing purposes, if the client package does not exist, subsequent operations might result in errors.`);
|
|
25
|
+
console.log(`🚨 To disable this warning and ensure the test system runs correctly:`);
|
|
26
|
+
console.log(` - Run: \`bun i packages/client && cd packages/client && bun i milkio-client\``);
|
|
27
|
+
console.log(` - Edit: \`src/api-test.ts\``);
|
|
28
|
+
console.log(` - Add: \`client: () => createClient({ baseUrl: "http://localhost:9000/", memoryStorage: true }),\``);
|
|
29
|
+
console.log(` - The \`createClient\` method is imported from your client package. If you haven't changed the name in \`packages/client/package.json\`, you can use: \`import { createClient } from 'client';\`.`);
|
|
30
|
+
} else {
|
|
31
|
+
let done = false;
|
|
32
|
+
for (let index = 0; index < 160; index++) {
|
|
33
|
+
let response;
|
|
34
|
+
try {
|
|
35
|
+
response = await fetch(typeof clientPackage.options.baseUrl === "string" ? clientPackage.options.baseUrl : await clientPackage.options.baseUrl(), { method: "HEAD" });
|
|
36
|
+
} catch (error: any) {
|
|
37
|
+
if (error?.status && error.status < 500) {
|
|
38
|
+
done = true;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (response?.status && response.status < 500) {
|
|
43
|
+
done = true;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (!done) {
|
|
50
|
+
console.log(`🚨 The server startup exceeded the maximum waiting time (1600ms).`);
|
|
51
|
+
console.log(`This is likely an error encountered during the startup of the Milkio Server. Please check the 'Milkio Run & Watch' tab in your VS Code terminal panel. This is only a warning, and the tests will continue, but there is a chance they might fail due to network issues.\n`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const client = clientPackage ? {
|
|
56
|
+
execute: async (options?: any) => clientPackage!.execute((path as any), options),
|
|
57
|
+
executeOther: async (path: any, options?: any) => clientPackage!.execute((path as any), options),
|
|
58
|
+
executeStream: async (options?: any) => clientPackage!.executeStream((path as any), options),
|
|
59
|
+
executeStreamOther: async (path: any, options?: any) => clientPackage!.executeStream((path as any), options),
|
|
60
|
+
} : undefined;
|
|
61
|
+
|
|
22
62
|
for (const pathRaw of pathArr) {
|
|
23
63
|
let path = pathRaw.replaceAll("\\", "/");
|
|
24
64
|
if (path.startsWith("/")) path = path.slice(1) as Path[number];
|
|
@@ -36,10 +76,11 @@ export const executeApiTests = async <Path extends Array<keyof (typeof schema)["
|
|
|
36
76
|
await cs.handler({
|
|
37
77
|
...((await apiTestHooks.default.onBefore()) ?? {}),
|
|
38
78
|
log: (...args: Array<unknown>) => console.log(...args),
|
|
39
|
-
// @ts-ignore
|
|
40
79
|
execute: async (options?: any) => app.execute((path as any), options),
|
|
41
80
|
executeOther: async (path: any, options?: any) => app.execute((path as any), options),
|
|
42
81
|
executeStream: async (options?: any) => app.executeStream((path as any), options),
|
|
82
|
+
executeStreamOther: async (path: any, options?: any) => app.executeStream((path as any), options),
|
|
83
|
+
client,
|
|
43
84
|
randParams: () => app.randParams(path as any),
|
|
44
85
|
randOtherParams: (path: any) => app.randParams(path),
|
|
45
86
|
reject: (message?: string) => {
|
|
@@ -25,6 +25,28 @@ export type ApiTestCases<ApiT extends Api> = {
|
|
|
25
25
|
getResult: () => ExecuteResult<undefined>,
|
|
26
26
|
stream: AsyncGenerator<Awaited<ReturnType<ApiT["action"]>>>
|
|
27
27
|
}>;
|
|
28
|
+
executeStreamOther: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(path: Path, options: { params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headers?: Record<string, string> } & ExecuteOptions) => Promise<{
|
|
29
|
+
getResult: () => ExecuteResult<undefined>,
|
|
30
|
+
stream: AsyncGenerator<Result>
|
|
31
|
+
}>;
|
|
32
|
+
client: {
|
|
33
|
+
execute: (options: { params: Parameters<ApiT["action"]>[0], headers?: Record<string, string> } & ExecuteOptions) => Promise<ExecuteResult<Awaited<ReturnType<ApiT["action"]>>>>;
|
|
34
|
+
executeOther: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(
|
|
35
|
+
path: Path,
|
|
36
|
+
options: {
|
|
37
|
+
params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string,
|
|
38
|
+
headers?: Record<string, string>
|
|
39
|
+
} & ExecuteOptions,
|
|
40
|
+
) => Promise<ExecuteResult<Result>>;
|
|
41
|
+
executeStream: (options: { params: Parameters<ApiT["action"]>[0], headers?: Record<string, string> } & ExecuteOptions) => Promise<{
|
|
42
|
+
getResult: () => ExecuteResult<undefined>,
|
|
43
|
+
stream: AsyncGenerator<Awaited<ReturnType<ApiT["action"]>>>
|
|
44
|
+
}>;
|
|
45
|
+
executeStreamOther: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(path: Path, options: { params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headers?: Record<string, string> } & ExecuteOptions) => Promise<{
|
|
46
|
+
getResult: () => ExecuteResult<undefined>,
|
|
47
|
+
stream: AsyncGenerator<Result>
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
28
50
|
randParams: () => Promise<Parameters<ApiT["action"]>[0]>;
|
|
29
51
|
randOtherParams: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path) => Promise<Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0]>;
|
|
30
52
|
reject: (message?: string) => void;
|