@tachybase/test 1.6.12 → 1.6.13-alpha.3

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.
@@ -40,13 +40,36 @@ __export(server_exports, {
40
40
  });
41
41
  module.exports = __toCommonJS(server_exports);
42
42
  var import_globals = __toESM(require("@tachybase/globals"));
43
- var import_vitest = require("vitest");
44
43
  var import_ws = __toESM(require("ws"));
45
44
  var import_database = require("@tachybase/database");
46
45
  var import_supertest = __toESM(require("supertest"));
47
46
  __reExport(server_exports, require("./mockServer"), module.exports);
48
- const pgOnly = /* @__PURE__ */ __name(() => import_globals.default.settings.database.dialect === "postgres" ? import_vitest.describe : import_vitest.describe.skip, "pgOnly");
49
- const isPg = /* @__PURE__ */ __name(() => import_globals.default.settings.database.dialect === "postgres", "isPg");
47
+ __reExport(server_exports, require("./setupTestEnvironment"), module.exports);
48
+ const noopDescribe = /* @__PURE__ */ __name(() => {
49
+ }, "noopDescribe");
50
+ function getDescribe() {
51
+ const desc = globalThis.describe;
52
+ if (!desc) {
53
+ throw new Error(
54
+ "globalThis.describe is not available. Ensure your test runner (vitest/jest) provides it globally or configure globals: true."
55
+ );
56
+ }
57
+ return desc;
58
+ }
59
+ __name(getDescribe, "getDescribe");
60
+ const pgOnly = /* @__PURE__ */ __name(() => {
61
+ var _a, _b;
62
+ const isPostgres = ((_b = (_a = import_globals.default.settings) == null ? void 0 : _a.database) == null ? void 0 : _b.dialect) === "postgres";
63
+ const desc = getDescribe();
64
+ if (isPostgres) {
65
+ return desc;
66
+ }
67
+ return desc.skip || noopDescribe;
68
+ }, "pgOnly");
69
+ const isPg = /* @__PURE__ */ __name(() => {
70
+ var _a, _b;
71
+ return ((_b = (_a = import_globals.default.settings) == null ? void 0 : _a.database) == null ? void 0 : _b.dialect) === "postgres";
72
+ }, "isPg");
50
73
  function randomStr() {
51
74
  return Math.random().toString(36).substring(2);
52
75
  }
@@ -102,5 +125,6 @@ const createWsClient = /* @__PURE__ */ __name(async ({ serverPort, options = {}
102
125
  startServerWithRandomPort,
103
126
  supertest,
104
127
  waitSecond,
105
- ...require("./mockServer")
128
+ ...require("./mockServer"),
129
+ ...require("./setupTestEnvironment")
106
130
  });
@@ -78,10 +78,11 @@ const _MockServer = class _MockServer extends import_core.Application {
78
78
  get(target, method, receiver) {
79
79
  if (["login", "loginUsingId"].includes(method)) {
80
80
  return (userOrId) => {
81
+ var _a;
81
82
  return proxy.auth(
82
83
  import_jsonwebtoken.default.sign(
83
84
  {
84
- userId: typeof userOrId === "number" ? userOrId : userOrId == null ? void 0 : userOrId.id
85
+ userId: typeof userOrId === "number" ? userOrId : (userOrId == null ? void 0 : userOrId.id) ?? ((_a = userOrId == null ? void 0 : userOrId.get) == null ? void 0 : _a.call(userOrId, "id"))
85
86
  },
86
87
  process.env.APP_KEY,
87
88
  {
@@ -196,6 +197,47 @@ async function createMockServer(options = {}) {
196
197
  if (!skipStart) {
197
198
  await app.runCommandThrowError("start");
198
199
  }
200
+ if (!app.authManager.tokenController) {
201
+ app.authManager.setTokenControlService({
202
+ async getConfig() {
203
+ return {
204
+ tokenExpirationTime: 24 * 60 * 60 * 1e3,
205
+ sessionExpirationTime: 7 * 24 * 60 * 60 * 1e3,
206
+ expiredTokenRenewLimit: 24 * 60 * 60 * 1e3
207
+ };
208
+ },
209
+ async setConfig() {
210
+ },
211
+ async renew() {
212
+ return {
213
+ jti: `test-jti-${Date.now()}-${Math.random().toString(36).slice(2)}`,
214
+ issuedTime: Date.now()
215
+ };
216
+ },
217
+ async add({ userId }) {
218
+ return {
219
+ jti: `test-jti-${Date.now()}-${Math.random().toString(36).slice(2)}`,
220
+ userId,
221
+ issuedTime: Date.now(),
222
+ signInTime: Date.now(),
223
+ renewed: false
224
+ };
225
+ },
226
+ async removeSessionExpiredTokens() {
227
+ }
228
+ });
229
+ }
230
+ if (!app.authManager.userStatusService) {
231
+ app.authManager.setUserStatusService({
232
+ async checkUserStatus() {
233
+ return {
234
+ allowed: true,
235
+ status: "active",
236
+ isExpired: false
237
+ };
238
+ }
239
+ });
240
+ }
199
241
  return app;
200
242
  }
201
243
  __name(createMockServer, "createMockServer");
@@ -0,0 +1,8 @@
1
+ export interface ServerTestEnvironmentOptions {
2
+ workspaceRoot?: string;
3
+ pluginPaths?: string[];
4
+ packageDirByPluginName?: Record<string, string>;
5
+ disableRuntimePlugins?: boolean;
6
+ disableOtherPlugins?: boolean;
7
+ }
8
+ export declare function setupServerTestEnvironment(options?: ServerTestEnvironmentOptions): void;
@@ -0,0 +1,443 @@
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var setupTestEnvironment_exports = {};
30
+ __export(setupTestEnvironment_exports, {
31
+ setupServerTestEnvironment: () => setupServerTestEnvironment
32
+ });
33
+ module.exports = __toCommonJS(setupTestEnvironment_exports);
34
+ var import_node_fs = __toESM(require("node:fs"));
35
+ var import_node_module = require("node:module");
36
+ var import_node_os = __toESM(require("node:os"));
37
+ var import_node_path = __toESM(require("node:path"));
38
+ var import_node_url = require("node:url");
39
+ var import_globals = __toESM(require("@tachybase/globals"));
40
+ const runtimeRequire = (0, import_node_module.createRequire)(import_node_path.default.resolve(process.cwd(), "package.json"));
41
+ const selfRequire = (0, import_node_module.createRequire)(require.resolve("@tachybase/test/package.json"));
42
+ const workspacePluginNameAliases = {
43
+ map: "block-map"
44
+ };
45
+ const ImportedTachybaseGlobal = import_globals.default.getInstance ? import_globals.default : import_globals.default.default;
46
+ const testUnsafeBuiltinPlugins = /* @__PURE__ */ new Set(["event-source", "worker-thread"]);
47
+ function createRuntimeRequire(workspaceRoot) {
48
+ const hostPackageJson = import_node_path.default.resolve(workspaceRoot, "package.json");
49
+ if (import_node_fs.default.existsSync(hostPackageJson)) {
50
+ return (0, import_node_module.createRequire)(hostPackageJson);
51
+ }
52
+ return runtimeRequire;
53
+ }
54
+ __name(createRuntimeRequire, "createRuntimeRequire");
55
+ function getTachybaseGlobal(runtimeRequire2) {
56
+ try {
57
+ const tachybaseGlobalModule = runtimeRequire2("@tachybase/globals");
58
+ return tachybaseGlobalModule.getInstance ? tachybaseGlobalModule : tachybaseGlobalModule.default;
59
+ } catch (error) {
60
+ if (error.code === "MODULE_NOT_FOUND") {
61
+ return ImportedTachybaseGlobal;
62
+ }
63
+ throw error;
64
+ }
65
+ }
66
+ __name(getTachybaseGlobal, "getTachybaseGlobal");
67
+ function getCoreModules() {
68
+ const cores = [];
69
+ try {
70
+ cores.push(selfRequire("@tego/core"));
71
+ } catch (e) {
72
+ if ((e == null ? void 0 : e.code) !== "MODULE_NOT_FOUND") throw e;
73
+ }
74
+ try {
75
+ cores.push(selfRequire("@tego/server"));
76
+ cores.push((0, import_node_module.createRequire)(selfRequire.resolve("@tego/server/package.json"))("@tego/core"));
77
+ } catch (e) {
78
+ if ((e == null ? void 0 : e.code) !== "MODULE_NOT_FOUND") throw e;
79
+ }
80
+ return [...new Set(cores)];
81
+ }
82
+ __name(getCoreModules, "getCoreModules");
83
+ function createTestDbStorage() {
84
+ const testDbName = `test-${Date.now()}-${Math.random().toString(36).slice(2)}.sqlite`;
85
+ const testDbDir = import_node_path.default.join(import_node_os.default.tmpdir(), "tego-test");
86
+ import_node_fs.default.mkdirSync(testDbDir, { recursive: true });
87
+ return import_node_path.default.join(testDbDir, testDbName);
88
+ }
89
+ __name(createTestDbStorage, "createTestDbStorage");
90
+ function workspacePackageNameByShortName(workspaceRoot, name, map) {
91
+ const normalizedName = workspacePluginNameAliases[name] || name;
92
+ const candidates = [map[name], map[normalizedName], `module-${normalizedName}`, `plugin-${normalizedName}`].filter(
93
+ Boolean
94
+ );
95
+ for (const packageDir of candidates) {
96
+ const packageJsonPath = import_node_path.default.resolve(workspaceRoot, "packages", packageDir, "package.json");
97
+ if (import_node_fs.default.existsSync(packageJsonPath)) {
98
+ const packageJson = runtimeRequire(packageJsonPath);
99
+ return packageJson.name;
100
+ }
101
+ }
102
+ return null;
103
+ }
104
+ __name(workspacePackageNameByShortName, "workspacePackageNameByShortName");
105
+ function workspacePackageDirByPackageName(workspaceRoot, packageName, map = {}) {
106
+ if (!packageName) {
107
+ return null;
108
+ }
109
+ const mappedPackageDir = Object.values(map).find((packageDir2) => {
110
+ const packageJsonPath2 = import_node_path.default.resolve(workspaceRoot, "packages", packageDir2, "package.json");
111
+ return import_node_fs.default.existsSync(packageJsonPath2) && runtimeRequire(packageJsonPath2).name === packageName;
112
+ });
113
+ if (mappedPackageDir) {
114
+ return mappedPackageDir;
115
+ }
116
+ const packageDir = packageName.replace("@tachybase/", "");
117
+ const packageJsonPath = import_node_path.default.resolve(workspaceRoot, "packages", packageDir, "package.json");
118
+ return import_node_fs.default.existsSync(packageJsonPath) ? packageDir : null;
119
+ }
120
+ __name(workspacePackageDirByPackageName, "workspacePackageDirByPackageName");
121
+ function workspaceServerEntry(workspaceRoot, packageDir) {
122
+ const libEntry = import_node_path.default.resolve(workspaceRoot, "packages", packageDir, "lib/server/index.js");
123
+ if (import_node_fs.default.existsSync(libEntry)) {
124
+ return libEntry;
125
+ }
126
+ const distEntry = import_node_path.default.resolve(workspaceRoot, "packages", packageDir, "dist/server/index.js");
127
+ if (import_node_fs.default.existsSync(distEntry)) {
128
+ return distEntry;
129
+ }
130
+ const sourceEntry = import_node_path.default.resolve(workspaceRoot, "packages", packageDir, "src/server/index.ts");
131
+ if (import_node_fs.default.existsSync(sourceEntry)) {
132
+ return sourceEntry;
133
+ }
134
+ return null;
135
+ }
136
+ __name(workspaceServerEntry, "workspaceServerEntry");
137
+ function getModuleDefault(mod) {
138
+ var _a;
139
+ return ((_a = mod == null ? void 0 : mod.default) == null ? void 0 : _a.default) || (mod == null ? void 0 : mod.default) || mod;
140
+ }
141
+ __name(getModuleDefault, "getModuleDefault");
142
+ function getServerTestEnvironmentOptions(core) {
143
+ return core.Plugin.__serverTestEnvironmentOptions;
144
+ }
145
+ __name(getServerTestEnvironmentOptions, "getServerTestEnvironmentOptions");
146
+ function patchPluginRuntime(core, workspaceRoot, packageDirByPluginName) {
147
+ core.Plugin.__serverTestEnvironmentOptions = { workspaceRoot, packageDirByPluginName };
148
+ if (core.Plugin.prototype.__serverTestEnvironmentPatched) {
149
+ return;
150
+ }
151
+ const originalLoadCollections = core.Plugin.prototype.loadCollections;
152
+ core.Plugin.prototype.loadCollections = /* @__PURE__ */ __name(async function loadWorkspaceCollections() {
153
+ var _a, _b;
154
+ const currentOptions = getServerTestEnvironmentOptions(core);
155
+ const currentWorkspaceRoot = (currentOptions == null ? void 0 : currentOptions.workspaceRoot) || workspaceRoot;
156
+ const currentPackageDirByPluginName = (currentOptions == null ? void 0 : currentOptions.packageDirByPluginName) || packageDirByPluginName;
157
+ const packageDir = ((_a = this.options) == null ? void 0 : _a.workspaceSource) ? workspacePackageDirByPackageName(currentWorkspaceRoot, (_b = this.options) == null ? void 0 : _b.packageName, currentPackageDirByPluginName) : null;
158
+ if (!packageDir) {
159
+ return originalLoadCollections.call(this);
160
+ }
161
+ const sourceDirectory = import_node_path.default.resolve(currentWorkspaceRoot, "packages", packageDir, "src/server/collections");
162
+ const compiledDirectory = import_node_path.default.resolve(currentWorkspaceRoot, "packages", packageDir, "dist/server/collections");
163
+ const directory = import_node_fs.default.existsSync(compiledDirectory) ? compiledDirectory : sourceDirectory;
164
+ if (!import_node_fs.default.existsSync(directory)) {
165
+ return;
166
+ }
167
+ await this.db.import({
168
+ directory,
169
+ from: this.options.packageName
170
+ });
171
+ }, "loadWorkspaceCollections");
172
+ core.Plugin.prototype.__serverTestEnvironmentPatched = true;
173
+ }
174
+ __name(patchPluginRuntime, "patchPluginRuntime");
175
+ function getPluginManagerOptions(PluginManager) {
176
+ return PluginManager.__serverTestEnvironmentOptions;
177
+ }
178
+ __name(getPluginManagerOptions, "getPluginManagerOptions");
179
+ function patchPluginManager(core, options) {
180
+ const PluginManager = core.PluginManager;
181
+ PluginManager.__serverTestEnvironmentOptions = options;
182
+ if (PluginManager.__serverTestEnvironmentPatchVersion === 2) {
183
+ return;
184
+ }
185
+ const resolvePlugin = (PluginManager.__serverTestEnvironmentOriginalResolvePlugin || PluginManager.resolvePlugin).bind(PluginManager);
186
+ const originalGetPackageName = (PluginManager.__serverTestEnvironmentOriginalGetPackageName || PluginManager.getPackageName).bind(PluginManager);
187
+ const originalAdd = PluginManager.__serverTestEnvironmentOriginalAdd || PluginManager.prototype.add;
188
+ const originalEnable = PluginManager.__serverTestEnvironmentOriginalEnable || PluginManager.prototype.enable;
189
+ const originalInitRuntimePlugins = PluginManager.__serverTestEnvironmentOriginalInitRuntimePlugins || PluginManager.prototype.initRuntimePlugins;
190
+ const originalInitOtherPlugins = PluginManager.__serverTestEnvironmentOriginalInitOtherPlugins || PluginManager.prototype.initOtherPlugins;
191
+ PluginManager.__serverTestEnvironmentOriginalResolvePlugin = resolvePlugin;
192
+ PluginManager.__serverTestEnvironmentOriginalGetPackageName = originalGetPackageName;
193
+ PluginManager.__serverTestEnvironmentOriginalAdd = originalAdd;
194
+ PluginManager.__serverTestEnvironmentOriginalEnable = originalEnable;
195
+ PluginManager.__serverTestEnvironmentOriginalInitRuntimePlugins = originalInitRuntimePlugins;
196
+ PluginManager.__serverTestEnvironmentOriginalInitOtherPlugins = originalInitOtherPlugins;
197
+ const workspaceSourcePackages = /* @__PURE__ */ new Set();
198
+ PluginManager.getPackageName = async (name) => {
199
+ const currentOptions = getPluginManagerOptions(PluginManager);
200
+ const currentWorkspaceRoot = (currentOptions == null ? void 0 : currentOptions.workspaceRoot) || process.cwd();
201
+ const currentPackageDirByPluginName = (currentOptions == null ? void 0 : currentOptions.packageDirByPluginName) || {};
202
+ const workspacePackageName = workspacePackageNameByShortName(
203
+ currentWorkspaceRoot,
204
+ name,
205
+ currentPackageDirByPluginName
206
+ );
207
+ return workspacePackageName || originalGetPackageName(name);
208
+ };
209
+ PluginManager.getPackageJson = async (packageName) => {
210
+ const currentOptions = getPluginManagerOptions(PluginManager);
211
+ const currentWorkspaceRoot = (currentOptions == null ? void 0 : currentOptions.workspaceRoot) || process.cwd();
212
+ const currentPackageDirByPluginName = (currentOptions == null ? void 0 : currentOptions.packageDirByPluginName) || {};
213
+ const packageDir = workspacePackageDirByPackageName(
214
+ currentWorkspaceRoot,
215
+ packageName,
216
+ currentPackageDirByPluginName
217
+ );
218
+ if (packageDir) {
219
+ return runtimeRequire(import_node_path.default.resolve(currentWorkspaceRoot, "packages", packageDir, "package.json"));
220
+ }
221
+ return runtimeRequire(runtimeRequire.resolve(import_node_path.default.join(packageName, "package.json")));
222
+ };
223
+ PluginManager.resolvePlugin = async (pluginName, isUpgrade = false, isPkg = false) => {
224
+ if (typeof pluginName !== "string") {
225
+ return pluginName;
226
+ }
227
+ const packageName = isPkg ? pluginName : await PluginManager.getPackageName(pluginName);
228
+ if (workspaceSourcePackages.has(packageName)) {
229
+ const currentOptions = getPluginManagerOptions(PluginManager);
230
+ const currentWorkspaceRoot = (currentOptions == null ? void 0 : currentOptions.workspaceRoot) || process.cwd();
231
+ const currentPackageDirByPluginName = (currentOptions == null ? void 0 : currentOptions.packageDirByPluginName) || {};
232
+ const packageDir = workspacePackageDirByPackageName(
233
+ currentWorkspaceRoot,
234
+ packageName,
235
+ currentPackageDirByPluginName
236
+ );
237
+ const entry = packageDir ? workspaceServerEntry(currentWorkspaceRoot, packageDir) : null;
238
+ if (entry) {
239
+ const mod = await import((0, import_node_url.pathToFileURL)(entry).href);
240
+ return getModuleDefault(mod);
241
+ }
242
+ return resolvePlugin(packageName, isUpgrade, true);
243
+ }
244
+ return resolvePlugin(pluginName, isUpgrade, isPkg);
245
+ };
246
+ PluginManager.prototype.initRuntimePlugins = /* @__PURE__ */ __name(async function initTestRuntimePlugins() {
247
+ const currentOptions = getPluginManagerOptions(PluginManager);
248
+ if (currentOptions == null ? void 0 : currentOptions.disableRuntimePlugins) {
249
+ this["_initRuntimePlugins"] = true;
250
+ return;
251
+ }
252
+ return originalInitRuntimePlugins.call(this);
253
+ }, "initTestRuntimePlugins");
254
+ PluginManager.prototype.initOtherPlugins = /* @__PURE__ */ __name(async function initTestOtherPlugins() {
255
+ const currentOptions = getPluginManagerOptions(PluginManager);
256
+ if (currentOptions == null ? void 0 : currentOptions.disableOtherPlugins) {
257
+ this["_initOtherPlugins"] = true;
258
+ return;
259
+ }
260
+ return originalInitOtherPlugins.call(this);
261
+ }, "initTestOtherPlugins");
262
+ PluginManager.prototype.add = /* @__PURE__ */ __name(async function addWorkspaceSourcePlugin(plugin, pluginOptions = {}, insert = false, isUpgrade = false) {
263
+ if (typeof plugin === "string" && (pluginOptions == null ? void 0 : pluginOptions.workspaceSource) && (pluginOptions == null ? void 0 : pluginOptions.packageName)) {
264
+ const currentOptions = getPluginManagerOptions(PluginManager);
265
+ const currentWorkspaceRoot = (currentOptions == null ? void 0 : currentOptions.workspaceRoot) || process.cwd();
266
+ const currentPackageDirByPluginName = (currentOptions == null ? void 0 : currentOptions.packageDirByPluginName) || {};
267
+ const packageDir = workspacePackageDirByPackageName(
268
+ currentWorkspaceRoot,
269
+ pluginOptions.packageName,
270
+ currentPackageDirByPluginName
271
+ );
272
+ const entry = packageDir ? workspaceServerEntry(currentWorkspaceRoot, packageDir) : null;
273
+ if (entry) {
274
+ const mod = await import((0, import_node_url.pathToFileURL)(entry).href);
275
+ return originalAdd.call(this, getModuleDefault(mod), pluginOptions, insert, isUpgrade);
276
+ }
277
+ }
278
+ return originalAdd.call(this, plugin, pluginOptions, insert, isUpgrade);
279
+ }, "addWorkspaceSourcePlugin");
280
+ PluginManager.prototype.enable = /* @__PURE__ */ __name(async function enableWorkspacePlugin(name) {
281
+ const normalize = /* @__PURE__ */ __name((pluginName) => workspacePluginNameAliases[pluginName] || pluginName, "normalize");
282
+ const normalizedName = Array.isArray(name) ? name.map(normalize) : normalize(name);
283
+ return originalEnable.call(this, normalizedName);
284
+ }, "enableWorkspacePlugin");
285
+ PluginManager.prototype.initPresetPlugins = /* @__PURE__ */ __name(async function initWorkspacePresetPlugins() {
286
+ var _a, _b;
287
+ if (this["_initPresetPlugins"]) {
288
+ return;
289
+ }
290
+ const addWorkspacePlugin = /* @__PURE__ */ __name(async (pluginName, pluginOptions = {}) => {
291
+ const normalizedPluginName = typeof pluginName === "string" ? workspacePluginNameAliases[pluginName] || pluginName : pluginName;
292
+ const currentOptions = getPluginManagerOptions(PluginManager);
293
+ const currentWorkspaceRoot = (currentOptions == null ? void 0 : currentOptions.workspaceRoot) || process.cwd();
294
+ const currentPackageDirByPluginName = (currentOptions == null ? void 0 : currentOptions.packageDirByPluginName) || {};
295
+ const packageName = typeof pluginName === "string" ? workspacePackageNameByShortName(currentWorkspaceRoot, pluginName, currentPackageDirByPluginName) : null;
296
+ if (packageName) {
297
+ workspaceSourcePackages.add(packageName);
298
+ const packageDir = workspacePackageDirByPackageName(
299
+ currentWorkspaceRoot,
300
+ packageName,
301
+ currentPackageDirByPluginName
302
+ );
303
+ const entry = packageDir ? workspaceServerEntry(currentWorkspaceRoot, packageDir) : null;
304
+ if (entry) {
305
+ const mod = await import((0, import_node_url.pathToFileURL)(entry).href);
306
+ const P = getModuleDefault(mod);
307
+ await this.add(P, {
308
+ name: pluginName,
309
+ ...pluginOptions,
310
+ packageName,
311
+ workspaceSource: true
312
+ });
313
+ } else {
314
+ await this.add(normalizedPluginName, {
315
+ name: pluginName,
316
+ ...pluginOptions,
317
+ packageName,
318
+ workspaceSource: true
319
+ });
320
+ }
321
+ } else if (typeof pluginName === "function") {
322
+ await this.add(pluginName, pluginOptions);
323
+ } else {
324
+ await this.add(normalizedPluginName, { name: pluginName, isPreset: true, ...pluginOptions });
325
+ }
326
+ }, "addWorkspacePlugin");
327
+ const addTachybasePresetPlugin = /* @__PURE__ */ __name(async (pluginName, pluginOptions = {}) => {
328
+ if (testUnsafeBuiltinPlugins.has(pluginName)) {
329
+ return;
330
+ }
331
+ await addWorkspacePlugin(pluginName, { enabled: true, ...pluginOptions });
332
+ }, "addTachybasePresetPlugin");
333
+ const addTachybaseExternalPlugin = /* @__PURE__ */ __name(async (plugin, pluginOptions = {}) => {
334
+ const pluginName = typeof plugin === "string" ? plugin : plugin == null ? void 0 : plugin.name;
335
+ if (!pluginName || testUnsafeBuiltinPlugins.has(pluginName)) {
336
+ return;
337
+ }
338
+ await addWorkspacePlugin(pluginName, { enabled: !!(plugin == null ? void 0 : plugin.enabledByDefault), ...pluginOptions });
339
+ }, "addTachybaseExternalPlugin");
340
+ for (const plugin of this.options.plugins || []) {
341
+ const [pluginName, pluginOptions = {}] = Array.isArray(plugin) ? plugin : [plugin];
342
+ if (pluginName === "tachybase") {
343
+ const currentOptions = getPluginManagerOptions(PluginManager);
344
+ const currentSettings = (currentOptions == null ? void 0 : currentOptions.settings) || options.settings;
345
+ for (const builtinPlugin of ((_a = currentSettings == null ? void 0 : currentSettings.presets) == null ? void 0 : _a.builtinPlugins) || []) {
346
+ await addTachybasePresetPlugin(builtinPlugin, pluginOptions);
347
+ }
348
+ for (const externalPlugin of ((_b = currentSettings == null ? void 0 : currentSettings.presets) == null ? void 0 : _b.externalPlugins) || []) {
349
+ await addTachybaseExternalPlugin(externalPlugin, pluginOptions);
350
+ }
351
+ continue;
352
+ }
353
+ await addWorkspacePlugin(pluginName, { enabled: true, ...pluginOptions });
354
+ }
355
+ this["_initPresetPlugins"] = true;
356
+ }, "initWorkspacePresetPlugins");
357
+ PluginManager.__serverTestEnvironmentPatchVersion = 2;
358
+ PluginManager.__serverTestEnvironmentPatchOptions = options;
359
+ PluginManager.findPackagePatched = true;
360
+ }
361
+ __name(patchPluginManager, "patchPluginManager");
362
+ function patchAppSupervisor(core, options) {
363
+ const AppSupervisor = core.AppSupervisor;
364
+ if (!(AppSupervisor == null ? void 0 : AppSupervisor.getInstance)) {
365
+ return;
366
+ }
367
+ AppSupervisor.__serverTestEnvironmentOptions = options;
368
+ if (AppSupervisor.__serverTestEnvironmentPatchVersion === 1) {
369
+ return;
370
+ }
371
+ const supervisor = AppSupervisor.getInstance();
372
+ const originalAddApp = supervisor.addApp.bind(supervisor);
373
+ supervisor.addApp = (app) => {
374
+ var _a;
375
+ if ((_a = app == null ? void 0 : app.pm) == null ? void 0 : _a.constructor) {
376
+ patchPluginManager(
377
+ {
378
+ PluginManager: app.pm.constructor
379
+ },
380
+ AppSupervisor.__serverTestEnvironmentOptions
381
+ );
382
+ }
383
+ return originalAddApp(app);
384
+ };
385
+ AppSupervisor.__serverTestEnvironmentPatchVersion = 1;
386
+ }
387
+ __name(patchAppSupervisor, "patchAppSupervisor");
388
+ function setupServerTestEnvironment(options = {}) {
389
+ var _a;
390
+ const workspaceRoot = options.workspaceRoot || process.cwd();
391
+ const pluginPaths = options.pluginPaths || [];
392
+ const packageDirByPluginName = options.packageDirByPluginName || {};
393
+ const runtimeRequire2 = createRuntimeRequire(workspaceRoot);
394
+ const TachybaseGlobal = getTachybaseGlobal(runtimeRequire2);
395
+ const settings = runtimeRequire2("tego/presets/settings");
396
+ const testSettings = {
397
+ ...settings,
398
+ env: {
399
+ ...settings.env,
400
+ APP_ENV: "test"
401
+ },
402
+ logger: {
403
+ ...settings.logger,
404
+ level: "error"
405
+ },
406
+ database: {
407
+ dialect: ((_a = settings.database) == null ? void 0 : _a.dialect) || "sqlite",
408
+ ...settings.database,
409
+ storage: createTestDbStorage()
410
+ },
411
+ presets: {
412
+ ...settings.presets,
413
+ runtimePlugins: options.disableRuntimePlugins ? [] : settings.presets.runtimePlugins
414
+ }
415
+ };
416
+ ImportedTachybaseGlobal.settings = testSettings;
417
+ TachybaseGlobal.settings = testSettings;
418
+ ImportedTachybaseGlobal.getInstance().set("PLUGIN_PATHS", pluginPaths);
419
+ TachybaseGlobal.getInstance().set("PLUGIN_PATHS", pluginPaths);
420
+ process.env.TEGO_RUNTIME_HOME = import_node_path.default.join(import_node_os.default.tmpdir(), "test-sqlite");
421
+ process.env.APP_ENV_PATH = process.env.APP_ENV_PATH || ".env.test";
422
+ const coreModules = getCoreModules();
423
+ for (const core of coreModules) {
424
+ patchPluginRuntime(core, workspaceRoot, packageDirByPluginName);
425
+ patchPluginManager(core, {
426
+ ...options,
427
+ workspaceRoot,
428
+ packageDirByPluginName,
429
+ settings: testSettings
430
+ });
431
+ patchAppSupervisor(core, {
432
+ ...options,
433
+ workspaceRoot,
434
+ packageDirByPluginName,
435
+ settings: testSettings
436
+ });
437
+ }
438
+ }
439
+ __name(setupServerTestEnvironment, "setupServerTestEnvironment");
440
+ // Annotate the CommonJS export names for ESM import in node:
441
+ 0 && (module.exports = {
442
+ setupServerTestEnvironment
443
+ });
@@ -0,0 +1 @@
1
+ export * from './server/setupTestEnvironment';
@@ -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 setup_server_exports = {};
16
+ module.exports = __toCommonJS(setup_server_exports);
17
+ __reExport(setup_server_exports, require("./server/setupTestEnvironment"), module.exports);
18
+ // Annotate the CommonJS export names for ESM import in node:
19
+ 0 && (module.exports = {
20
+ ...require("./server/setupTestEnvironment")
21
+ });
@@ -0,0 +1,11 @@
1
+ export interface TegoVitestConfigOptions {
2
+ server?: {
3
+ setupFile?: string;
4
+ };
5
+ client?: {
6
+ setupFile?: string;
7
+ };
8
+ }
9
+ export declare function defineTegoVitestConfig(options?: TegoVitestConfigOptions): import("vite").UserConfig & Promise<import("vite").UserConfig> & (import("vitest/config.js").UserConfigFnObject & import("vitest/config.js").UserConfigExport);
10
+ declare const _default: import("vite").UserConfig & Promise<import("vite").UserConfig> & (import("vitest/config.js").UserConfigFnObject & import("vitest/config.js").UserConfigExport);
11
+ export default _default;