@tachybase/test 0.23.8
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/.turbo/turbo-build.log +24 -0
- package/LICENSE +201 -0
- package/client.d.ts +1 -0
- package/client.js +1 -0
- package/e2e.d.ts +1 -0
- package/e2e.js +1 -0
- package/es/client/index.d.ts +7 -0
- package/es/client/index.mjs +20 -0
- package/es/e2e/defineConfig.d.ts +2 -0
- package/es/e2e/e2eUtils.d.ts +298 -0
- package/es/e2e/index.d.ts +3 -0
- package/es/e2e/index.mjs +20547 -0
- package/es/e2e/templatesOfCollection.d.ts +41 -0
- package/es/e2e/templatesOfPage.d.ts +573 -0
- package/es/index.d.ts +1 -0
- package/es/index.mjs +223 -0
- package/es/scripts/test-db-creator.d.ts +1 -0
- package/es/scripts/test-db-distributor.d.ts +1 -0
- package/es/server/index.d.ts +17 -0
- package/es/server/mockServer.d.ts +75 -0
- package/lib/client/index.d.ts +7 -0
- package/lib/client/index.js +59 -0
- package/lib/e2e/defineConfig.d.ts +2 -0
- package/lib/e2e/defineConfig.js +78 -0
- package/lib/e2e/e2eUtils.d.ts +298 -0
- package/lib/e2e/e2eUtils.js +633 -0
- package/lib/e2e/index.d.ts +3 -0
- package/lib/e2e/index.js +25 -0
- package/lib/e2e/templatesOfCollection.d.ts +41 -0
- package/lib/e2e/templatesOfCollection.js +720 -0
- package/lib/e2e/templatesOfPage.d.ts +573 -0
- package/lib/e2e/templatesOfPage.js +19345 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +21 -0
- package/lib/scripts/test-db-creator.d.ts +1 -0
- package/lib/scripts/test-db-creator.js +163 -0
- package/lib/scripts/test-db-distributor.d.ts +1 -0
- package/lib/scripts/test-db-distributor.js +234 -0
- package/lib/server/index.d.ts +17 -0
- package/lib/server/index.js +105 -0
- package/lib/server/mockServer.d.ts +75 -0
- package/lib/server/mockServer.js +209 -0
- package/package.json +74 -0
- package/playwright/tests/auth.setup.ts +21 -0
- package/server.d.ts +1 -0
- package/server.js +1 -0
- package/setup/client.ts +69 -0
- package/setup/server.ts +5 -0
- package/vitest.d.ts +24 -0
- package/vitest.mjs +119 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './server';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var src_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(src_exports);
|
|
17
|
+
__reExport(src_exports, require("./server"), module.exports);
|
|
18
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
19
|
+
0 && (module.exports = {
|
|
20
|
+
...require("./server")
|
|
21
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var import_http = __toESM(require("http"));
|
|
25
|
+
var import_path = __toESM(require("path"));
|
|
26
|
+
var import_url = __toESM(require("url"));
|
|
27
|
+
var import_dotenv = __toESM(require("dotenv"));
|
|
28
|
+
var import_mariadb = __toESM(require("mariadb"));
|
|
29
|
+
var import_promise = __toESM(require("mysql2/promise"));
|
|
30
|
+
var import_pg = __toESM(require("pg"));
|
|
31
|
+
import_dotenv.default.config({ path: import_path.default.resolve(process.cwd(), ".env.test") });
|
|
32
|
+
const _BaseClient = class _BaseClient {
|
|
33
|
+
_client = null;
|
|
34
|
+
createdDBs = /* @__PURE__ */ new Set();
|
|
35
|
+
async createDB(name) {
|
|
36
|
+
if (this.createdDBs.has(name)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!this._client) {
|
|
40
|
+
this._client = await this._createConnection();
|
|
41
|
+
}
|
|
42
|
+
await this._createDB(name);
|
|
43
|
+
this.createdDBs.add(name);
|
|
44
|
+
}
|
|
45
|
+
async releaseAll() {
|
|
46
|
+
if (!this._client) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const dbNames = Array.from(this.createdDBs);
|
|
50
|
+
for (const name of dbNames) {
|
|
51
|
+
console.log(`Removing database: ${name}`);
|
|
52
|
+
await this._removeDB(name);
|
|
53
|
+
this.createdDBs.delete(name);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
__name(_BaseClient, "BaseClient");
|
|
58
|
+
let BaseClient = _BaseClient;
|
|
59
|
+
const _PostgresClient = class _PostgresClient extends BaseClient {
|
|
60
|
+
async _removeDB(name) {
|
|
61
|
+
await this._client.query(`DROP DATABASE IF EXISTS ${name}`);
|
|
62
|
+
}
|
|
63
|
+
async _createDB(name) {
|
|
64
|
+
await this._client.query(`DROP DATABASE IF EXISTS ${name}`);
|
|
65
|
+
await this._client.query(`CREATE DATABASE ${name};`);
|
|
66
|
+
}
|
|
67
|
+
async _createConnection() {
|
|
68
|
+
const client2 = new import_pg.default.Client({
|
|
69
|
+
host: process.env["DB_HOST"],
|
|
70
|
+
port: Number(process.env["DB_PORT"]),
|
|
71
|
+
user: process.env["DB_USER"],
|
|
72
|
+
password: process.env["DB_PASSWORD"],
|
|
73
|
+
database: process.env["DB_DATABASE"]
|
|
74
|
+
});
|
|
75
|
+
await client2.connect();
|
|
76
|
+
return client2;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
__name(_PostgresClient, "PostgresClient");
|
|
80
|
+
let PostgresClient = _PostgresClient;
|
|
81
|
+
const _MySQLClient = class _MySQLClient extends BaseClient {
|
|
82
|
+
async _removeDB(name) {
|
|
83
|
+
await this._client.query(`DROP DATABASE IF EXISTS ${name}`);
|
|
84
|
+
}
|
|
85
|
+
async _createDB(name) {
|
|
86
|
+
await this._client.query(`CREATE DATABASE IF NOT EXISTS ${name}`);
|
|
87
|
+
}
|
|
88
|
+
async _createConnection() {
|
|
89
|
+
return import_promise.default.createConnection({
|
|
90
|
+
host: process.env["DB_HOST"],
|
|
91
|
+
port: Number(process.env["DB_PORT"]),
|
|
92
|
+
user: process.env["DB_USER"],
|
|
93
|
+
password: process.env["DB_PASSWORD"],
|
|
94
|
+
database: process.env["DB_DATABASE"]
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
__name(_MySQLClient, "MySQLClient");
|
|
99
|
+
let MySQLClient = _MySQLClient;
|
|
100
|
+
const _MariaDBClient = class _MariaDBClient extends BaseClient {
|
|
101
|
+
async _removeDB(name) {
|
|
102
|
+
await this._client.query(`DROP DATABASE IF EXISTS ${name}`);
|
|
103
|
+
}
|
|
104
|
+
async _createDB(name) {
|
|
105
|
+
await this._client.query(`CREATE DATABASE IF NOT EXISTS ${name}`);
|
|
106
|
+
}
|
|
107
|
+
async _createConnection() {
|
|
108
|
+
return await import_mariadb.default.createConnection({
|
|
109
|
+
host: process.env["DB_HOST"],
|
|
110
|
+
port: Number(process.env["DB_PORT"]),
|
|
111
|
+
user: process.env["DB_USER"],
|
|
112
|
+
password: process.env["DB_PASSWORD"],
|
|
113
|
+
database: process.env["DB_DATABASE"]
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
__name(_MariaDBClient, "MariaDBClient");
|
|
118
|
+
let MariaDBClient = _MariaDBClient;
|
|
119
|
+
const client = {
|
|
120
|
+
postgres: /* @__PURE__ */ __name(() => {
|
|
121
|
+
return new PostgresClient();
|
|
122
|
+
}, "postgres"),
|
|
123
|
+
mysql: /* @__PURE__ */ __name(() => {
|
|
124
|
+
return new MySQLClient();
|
|
125
|
+
}, "mysql"),
|
|
126
|
+
mariadb: /* @__PURE__ */ __name(() => {
|
|
127
|
+
return new MariaDBClient();
|
|
128
|
+
}, "mariadb")
|
|
129
|
+
};
|
|
130
|
+
const dialect = process.env["DB_DIALECT"];
|
|
131
|
+
if (!client[dialect]) {
|
|
132
|
+
throw new Error(`Unknown dialect: ${dialect}`);
|
|
133
|
+
}
|
|
134
|
+
const dbClient = client[dialect]();
|
|
135
|
+
const server = import_http.default.createServer((req, res) => {
|
|
136
|
+
const parsedUrl = import_url.default.parse(req.url, true);
|
|
137
|
+
const path2 = parsedUrl.pathname;
|
|
138
|
+
const trimmedPath = path2.replace(/^\/+|\/+$/g, "");
|
|
139
|
+
if (trimmedPath === "acquire") {
|
|
140
|
+
const name = parsedUrl.query.name;
|
|
141
|
+
dbClient.createDB(name).then(() => {
|
|
142
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
143
|
+
res.end();
|
|
144
|
+
}).catch((error) => {
|
|
145
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
146
|
+
res.end(JSON.stringify({ error }));
|
|
147
|
+
});
|
|
148
|
+
} else if (trimmedPath === "release") {
|
|
149
|
+
dbClient.releaseAll().then(() => {
|
|
150
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
151
|
+
res.end();
|
|
152
|
+
}).catch((error) => {
|
|
153
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
154
|
+
res.end(JSON.stringify({ error }));
|
|
155
|
+
});
|
|
156
|
+
} else {
|
|
157
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
158
|
+
res.end("Not Found\n");
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
server.listen(23450, "127.0.0.1", () => {
|
|
162
|
+
console.log("Server is running at http://127.0.0.1:23450/");
|
|
163
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var import_fs = __toESM(require("fs"));
|
|
25
|
+
var import_http = __toESM(require("http"));
|
|
26
|
+
var import_path = __toESM(require("path"));
|
|
27
|
+
var import_url = __toESM(require("url"));
|
|
28
|
+
var import_dotenv = __toESM(require("dotenv"));
|
|
29
|
+
var import_pg = __toESM(require("pg"));
|
|
30
|
+
import_dotenv.default.config({ path: import_path.default.resolve(process.cwd(), ".env.test") });
|
|
31
|
+
const delay = /* @__PURE__ */ __name((ms) => new Promise((resolve) => setTimeout(resolve, ms)), "delay");
|
|
32
|
+
const _DBManager = class _DBManager {
|
|
33
|
+
acquiredDBs = /* @__PURE__ */ new Map();
|
|
34
|
+
acquire(name, via) {
|
|
35
|
+
console.log("acquire", name, "via", via);
|
|
36
|
+
if (this.acquiredDBs.has(name)) {
|
|
37
|
+
this.acquiredDBs.get(name).add(via);
|
|
38
|
+
} else {
|
|
39
|
+
this.acquiredDBs.set(name, /* @__PURE__ */ new Set([via]));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async release(name, via, relaseDb) {
|
|
43
|
+
var _a;
|
|
44
|
+
console.log("release", name, "via", via);
|
|
45
|
+
const vias = this.acquiredDBs.get(name);
|
|
46
|
+
if (!vias || !vias.has(via)) {
|
|
47
|
+
console.log(`Cannot release ${name}, it is not acquired via ${via}`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
vias.delete(via);
|
|
51
|
+
if (vias.size === 0) {
|
|
52
|
+
console.log("DB", name, "is not used anymore, release it");
|
|
53
|
+
await delay(1e3);
|
|
54
|
+
if (((_a = this.acquiredDBs.get(name)) == null ? void 0 : _a.size) === 0) {
|
|
55
|
+
console.log("start to release DB", name);
|
|
56
|
+
await (relaseDb == null ? void 0 : relaseDb());
|
|
57
|
+
this.acquiredDBs.delete(name);
|
|
58
|
+
console.log("DB", name, "is released, current usesd db count:", this.acquiredDBs.size);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
isAcquired(name) {
|
|
64
|
+
return this.acquiredDBs.has(name);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
__name(_DBManager, "DBManager");
|
|
68
|
+
let DBManager = _DBManager;
|
|
69
|
+
const getDBNames = /* @__PURE__ */ __name((size, name) => {
|
|
70
|
+
const names = [];
|
|
71
|
+
for (let i = 0; i < size; i++) {
|
|
72
|
+
names.push(`auto_named_${name}_${i}`);
|
|
73
|
+
}
|
|
74
|
+
return names;
|
|
75
|
+
}, "getDBNames");
|
|
76
|
+
const _BasePool = class _BasePool {
|
|
77
|
+
constructor(size) {
|
|
78
|
+
this.size = size;
|
|
79
|
+
}
|
|
80
|
+
dbManager = new DBManager();
|
|
81
|
+
async init() {
|
|
82
|
+
const promises = [];
|
|
83
|
+
for (const name of getDBNames(this.size, this.getConfiguredDatabaseName())) {
|
|
84
|
+
promises.push(
|
|
85
|
+
(async () => {
|
|
86
|
+
console.log("create database", name);
|
|
87
|
+
await this.createDatabase(name);
|
|
88
|
+
})()
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
await Promise.all(promises);
|
|
92
|
+
}
|
|
93
|
+
async acquire(name, via) {
|
|
94
|
+
if (!name) {
|
|
95
|
+
name = getDBNames(this.size, this.getConfiguredDatabaseName()).find((name2) => !this.dbManager.isAcquired(name2));
|
|
96
|
+
}
|
|
97
|
+
if (!name) {
|
|
98
|
+
throw new Error("No available database");
|
|
99
|
+
}
|
|
100
|
+
this.dbManager.acquire(name, via);
|
|
101
|
+
return name;
|
|
102
|
+
}
|
|
103
|
+
async release(name, via) {
|
|
104
|
+
await this.dbManager.release(name, via, async () => {
|
|
105
|
+
await this.cleanDatabase(name);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
__name(_BasePool, "BasePool");
|
|
110
|
+
let BasePool = _BasePool;
|
|
111
|
+
const _PostgresPool = class _PostgresPool extends BasePool {
|
|
112
|
+
async _createConnection(options, callback) {
|
|
113
|
+
const config = this.getDatabaseConfiguration();
|
|
114
|
+
const databaseName = this.getConfiguredDatabaseName();
|
|
115
|
+
const client = new import_pg.default.Client({
|
|
116
|
+
host: config["host"],
|
|
117
|
+
port: config["port"],
|
|
118
|
+
user: config["username"],
|
|
119
|
+
password: config["password"],
|
|
120
|
+
database: databaseName,
|
|
121
|
+
...options
|
|
122
|
+
});
|
|
123
|
+
await client.connect();
|
|
124
|
+
await callback(client);
|
|
125
|
+
await client.end();
|
|
126
|
+
}
|
|
127
|
+
async cleanDatabase(name) {
|
|
128
|
+
await this._createConnection({ database: name }, async (client) => {
|
|
129
|
+
await client.query(`DROP SCHEMA public CASCADE;CREATE SCHEMA public;`);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async createDatabase(name, options) {
|
|
133
|
+
const { log } = options || {};
|
|
134
|
+
await this._createConnection({}, async (client) => {
|
|
135
|
+
if (log) {
|
|
136
|
+
console.log(`DROP DATABASE IF EXISTS ${name}`);
|
|
137
|
+
}
|
|
138
|
+
await client.query(`DROP DATABASE IF EXISTS ${name}`);
|
|
139
|
+
if (log) {
|
|
140
|
+
console.log(`CREATE DATABASE ${name}`);
|
|
141
|
+
}
|
|
142
|
+
await client.query(`CREATE DATABASE ${name}`);
|
|
143
|
+
if (log) {
|
|
144
|
+
console.log(`end`);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
getDatabaseConfiguration() {
|
|
149
|
+
return {
|
|
150
|
+
host: process.env.DB_HOST,
|
|
151
|
+
port: process.env.DB_PORT,
|
|
152
|
+
username: process.env.DB_USERNAME,
|
|
153
|
+
password: process.env.DB_PASSWORD
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
getConfiguredDatabaseName() {
|
|
157
|
+
return process.env.DB_DATABASE;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
__name(_PostgresPool, "PostgresPool");
|
|
161
|
+
let PostgresPool = _PostgresPool;
|
|
162
|
+
const _SqlitePool = class _SqlitePool extends BasePool {
|
|
163
|
+
async createDatabase(name, options) {
|
|
164
|
+
return import_fs.default.promises.writeFile(import_path.default.resolve(this.getStoragePath(), name), "");
|
|
165
|
+
}
|
|
166
|
+
async cleanDatabase(name) {
|
|
167
|
+
return import_fs.default.promises.unlink(import_path.default.resolve(this.getStoragePath(), name));
|
|
168
|
+
}
|
|
169
|
+
getDatabaseConfiguration() {
|
|
170
|
+
return {
|
|
171
|
+
storage: process.env.DB_STORAGE
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
getConfiguredDatabaseName() {
|
|
175
|
+
const storagePath = process.env.DB_STORAGE;
|
|
176
|
+
if (storagePath && storagePath !== ":memory:") {
|
|
177
|
+
return import_path.default.basename(storagePath);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
getStoragePath() {
|
|
181
|
+
const storagePath = process.env.DB_STORAGE;
|
|
182
|
+
if (storagePath && storagePath !== ":memory:") {
|
|
183
|
+
return import_path.default.dirname(storagePath);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
__name(_SqlitePool, "SqlitePool");
|
|
188
|
+
let SqlitePool = _SqlitePool;
|
|
189
|
+
const pools = {
|
|
190
|
+
postgres: PostgresPool,
|
|
191
|
+
sqlite: SqlitePool
|
|
192
|
+
};
|
|
193
|
+
(async () => {
|
|
194
|
+
const poolSize = process.env.TEST_DB_POOL_SIZE || 100;
|
|
195
|
+
const poolClass = pools[process.env.DB_DIALECT];
|
|
196
|
+
if (!poolClass) {
|
|
197
|
+
throw new Error(`Unknown pool class ${process.env.DB_DIALECT}`);
|
|
198
|
+
}
|
|
199
|
+
const pool = new poolClass(poolSize);
|
|
200
|
+
await pool.init();
|
|
201
|
+
return pool;
|
|
202
|
+
})().then((pool) => {
|
|
203
|
+
const server = import_http.default.createServer((req, res) => {
|
|
204
|
+
const parsedUrl = import_url.default.parse(req.url, true);
|
|
205
|
+
const path2 = parsedUrl.pathname;
|
|
206
|
+
const trimmedPath = path2.replace(/^\/+|\/+$/g, "");
|
|
207
|
+
if (trimmedPath === "acquire") {
|
|
208
|
+
const via = parsedUrl.query.via;
|
|
209
|
+
const name = parsedUrl.query.name;
|
|
210
|
+
pool.acquire(name, via).then((name2) => {
|
|
211
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
212
|
+
res.end(JSON.stringify({ name: name2 }));
|
|
213
|
+
}).catch((err) => {
|
|
214
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
215
|
+
res.end(JSON.stringify({ error: err.message }));
|
|
216
|
+
});
|
|
217
|
+
} else if (trimmedPath === "release") {
|
|
218
|
+
const via = parsedUrl.query.via;
|
|
219
|
+
const name = parsedUrl.query.name;
|
|
220
|
+
pool.release(name, via);
|
|
221
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
222
|
+
res.end();
|
|
223
|
+
} else {
|
|
224
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
225
|
+
res.end("Not Found\n");
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
server.listen(23450, "127.0.0.1", () => {
|
|
229
|
+
console.log("Server is running at http://127.0.0.1:23450/");
|
|
230
|
+
});
|
|
231
|
+
}).catch((err) => {
|
|
232
|
+
console.error(err);
|
|
233
|
+
process.exit(1);
|
|
234
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { mockDatabase } from '@tachybase/database';
|
|
2
|
+
export { default as supertest } from 'supertest';
|
|
3
|
+
export * from './mockServer';
|
|
4
|
+
export declare const pgOnly: () => any;
|
|
5
|
+
export declare const isPg: () => boolean;
|
|
6
|
+
export declare function randomStr(): string;
|
|
7
|
+
export declare const waitSecond: (timeout?: number) => Promise<void>;
|
|
8
|
+
export declare const startServerWithRandomPort: (startServer: any) => Promise<unknown>;
|
|
9
|
+
export declare const createWsClient: ({ serverPort, options }: {
|
|
10
|
+
serverPort: any;
|
|
11
|
+
options?: {};
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
wsc: any;
|
|
14
|
+
messages: any[];
|
|
15
|
+
stop(): Promise<void>;
|
|
16
|
+
lastMessage(): any;
|
|
17
|
+
}>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var server_exports = {};
|
|
31
|
+
__export(server_exports, {
|
|
32
|
+
createWsClient: () => createWsClient,
|
|
33
|
+
isPg: () => isPg,
|
|
34
|
+
mockDatabase: () => import_database.mockDatabase,
|
|
35
|
+
pgOnly: () => pgOnly,
|
|
36
|
+
randomStr: () => randomStr,
|
|
37
|
+
startServerWithRandomPort: () => startServerWithRandomPort,
|
|
38
|
+
supertest: () => import_supertest.default,
|
|
39
|
+
waitSecond: () => waitSecond
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(server_exports);
|
|
42
|
+
var import_vitest = require("vitest");
|
|
43
|
+
var import_ws = __toESM(require("ws"));
|
|
44
|
+
var import_database = require("@tachybase/database");
|
|
45
|
+
var import_supertest = __toESM(require("supertest"));
|
|
46
|
+
__reExport(server_exports, require("./mockServer"), module.exports);
|
|
47
|
+
const pgOnly = /* @__PURE__ */ __name(() => process.env.DB_DIALECT == "postgres" ? import_vitest.describe : import_vitest.describe.skip, "pgOnly");
|
|
48
|
+
const isPg = /* @__PURE__ */ __name(() => process.env.DB_DIALECT == "postgres", "isPg");
|
|
49
|
+
function randomStr() {
|
|
50
|
+
return Math.random().toString(36).substring(2);
|
|
51
|
+
}
|
|
52
|
+
__name(randomStr, "randomStr");
|
|
53
|
+
const waitSecond = /* @__PURE__ */ __name(async (timeout = 1e3) => {
|
|
54
|
+
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
55
|
+
}, "waitSecond");
|
|
56
|
+
const startServerWithRandomPort = /* @__PURE__ */ __name(async (startServer) => {
|
|
57
|
+
return await new Promise((resolve) => {
|
|
58
|
+
startServer({
|
|
59
|
+
port: 0,
|
|
60
|
+
host: "localhost",
|
|
61
|
+
callback(server) {
|
|
62
|
+
const port = server.address().port;
|
|
63
|
+
resolve(port);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}, "startServerWithRandomPort");
|
|
68
|
+
const createWsClient = /* @__PURE__ */ __name(async ({ serverPort, options = {} }) => {
|
|
69
|
+
console.log(`connect to ws://localhost:${serverPort}${process.env.WS_PATH}`, options);
|
|
70
|
+
const wsc = new import_ws.default(`ws://localhost:${serverPort}${process.env.WS_PATH}`, options);
|
|
71
|
+
const messages = [];
|
|
72
|
+
wsc.on("message", (data) => {
|
|
73
|
+
const message = data.toString();
|
|
74
|
+
messages.push(message);
|
|
75
|
+
});
|
|
76
|
+
await new Promise((resolve) => {
|
|
77
|
+
wsc.on("open", resolve);
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
wsc,
|
|
81
|
+
messages,
|
|
82
|
+
async stop() {
|
|
83
|
+
const promise = new Promise((resolve) => {
|
|
84
|
+
wsc.on("close", resolve);
|
|
85
|
+
});
|
|
86
|
+
wsc.close();
|
|
87
|
+
await promise;
|
|
88
|
+
},
|
|
89
|
+
lastMessage() {
|
|
90
|
+
return JSON.parse(messages[messages.length - 1]);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}, "createWsClient");
|
|
94
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
95
|
+
0 && (module.exports = {
|
|
96
|
+
createWsClient,
|
|
97
|
+
isPg,
|
|
98
|
+
mockDatabase,
|
|
99
|
+
pgOnly,
|
|
100
|
+
randomStr,
|
|
101
|
+
startServerWithRandomPort,
|
|
102
|
+
supertest,
|
|
103
|
+
waitSecond,
|
|
104
|
+
...require("./mockServer")
|
|
105
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Application, ApplicationOptions } from '@tachybase/server';
|
|
2
|
+
import supertest, { SuperAgentTest } from 'supertest';
|
|
3
|
+
interface ActionParams {
|
|
4
|
+
filterByTk?: any;
|
|
5
|
+
fields?: string[];
|
|
6
|
+
filter?: any;
|
|
7
|
+
sort?: string[];
|
|
8
|
+
page?: number;
|
|
9
|
+
pageSize?: number;
|
|
10
|
+
values?: any;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated
|
|
13
|
+
*/
|
|
14
|
+
resourceName?: string;
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated
|
|
17
|
+
*/
|
|
18
|
+
resourceIndex?: string;
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated
|
|
21
|
+
*/
|
|
22
|
+
associatedName?: string;
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated
|
|
25
|
+
*/
|
|
26
|
+
associatedIndex?: string;
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
}
|
|
29
|
+
interface SortActionParams {
|
|
30
|
+
resourceName?: string;
|
|
31
|
+
resourceIndex?: any;
|
|
32
|
+
associatedName?: string;
|
|
33
|
+
associatedIndex?: any;
|
|
34
|
+
sourceId?: any;
|
|
35
|
+
targetId?: any;
|
|
36
|
+
sortField?: string;
|
|
37
|
+
method?: string;
|
|
38
|
+
target?: any;
|
|
39
|
+
sticky?: boolean;
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
}
|
|
42
|
+
interface Resource {
|
|
43
|
+
get: (params?: ActionParams) => Promise<supertest.Response>;
|
|
44
|
+
list: (params?: ActionParams) => Promise<supertest.Response>;
|
|
45
|
+
create: (params?: ActionParams) => Promise<supertest.Response>;
|
|
46
|
+
update: (params?: ActionParams) => Promise<supertest.Response>;
|
|
47
|
+
destroy: (params?: ActionParams) => Promise<supertest.Response>;
|
|
48
|
+
sort: (params?: SortActionParams) => Promise<supertest.Response>;
|
|
49
|
+
[name: string]: (params?: ActionParams) => Promise<supertest.Response>;
|
|
50
|
+
}
|
|
51
|
+
interface ExtendedAgent extends SuperAgentTest {
|
|
52
|
+
login: (user: any) => ExtendedAgent;
|
|
53
|
+
loginUsingId: (userId: number) => ExtendedAgent;
|
|
54
|
+
resource: (name: string, resourceOf?: any) => Resource;
|
|
55
|
+
}
|
|
56
|
+
export declare class MockServer extends Application {
|
|
57
|
+
loadAndInstall(options?: any): Promise<void>;
|
|
58
|
+
cleanDb(): Promise<void>;
|
|
59
|
+
quickstart(options?: {
|
|
60
|
+
clean?: boolean;
|
|
61
|
+
}): Promise<void>;
|
|
62
|
+
destroy(options?: any): Promise<void>;
|
|
63
|
+
agent(): ExtendedAgent;
|
|
64
|
+
protected createDatabase(options: ApplicationOptions): import("@tachybase/database").MockDatabase;
|
|
65
|
+
}
|
|
66
|
+
export declare function mockServer(options?: ApplicationOptions): MockServer;
|
|
67
|
+
export declare function startMockServer(options?: ApplicationOptions): Promise<MockServer>;
|
|
68
|
+
type BeforeInstallFn = (app: any) => Promise<void>;
|
|
69
|
+
export declare function createMockServer(options?: ApplicationOptions & {
|
|
70
|
+
version?: string;
|
|
71
|
+
beforeInstall?: BeforeInstallFn;
|
|
72
|
+
skipInstall?: boolean;
|
|
73
|
+
skipStart?: boolean;
|
|
74
|
+
}): Promise<any>;
|
|
75
|
+
export default mockServer;
|