cabloy 5.1.204 → 5.1.205
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/.cabloy-version +1 -1
- package/.gitignore +2 -0
- package/CHANGELOG.md +11 -0
- package/lint-staged.config.mjs +2 -0
- package/oxfmt.config.ts +2 -0
- package/oxlint.config.ts +2 -0
- package/package.json +2 -3
- package/repo-agent-governance/managed-assets.json +6 -6
- package/repo-agent-governance/policies/repository.md +3 -2
- package/repo-agent-governance/skills/cabloy-workflow/SKILL.md +2 -0
- package/repo-docs/backend/migration-and-changes.md +13 -12
- package/repo-docs/backend/runtime-and-flavors.md +2 -1
- package/repo-docs/backend/unit-testing.md +12 -8
- package/repo-docs/blogs/ai-coding-one-prompt-crud-ssr-multitasking/index.md +9 -9
- package/repo-docs/frontend/scripts.md +1 -1
- package/repo-docs/fullstack/parallel-worktree-environment.md +6 -6
- package/repo-docs/fullstack/ssr-entry-modes.md +19 -0
- package/repo-docs/reference/repo-scripts.md +22 -1
- package/repo-e2e/config/playwright.config.ts +32 -14
- package/repo-e2e/scripts/e2e.test.ts +39 -0
- package/repo-e2e/scripts/e2e.ts +19 -1
- package/repo-e2e/scripts/managedTest.test.ts +389 -0
- package/repo-e2e/scripts/managedTest.ts +210 -0
- package/repo-e2e/scripts/managedTestImports.test.ts +74 -0
- package/repo-e2e/scripts/managedTestImports.ts +110 -0
- package/repo-e2e/scripts/runE2e.ts +12 -1
- package/repo-e2e/scripts/startE2eVona.ts +37 -5
- package/repo-e2e/specs/a-commerce.spec.ts +1 -1
- package/repo-e2e/specs/cabloy-basic.spec.ts +1 -1
- package/repo-e2e/specs/controller-boundary.spec.ts +1 -1
- package/repo-e2e/specs/home-user-account.spec.ts +2 -1
- package/repo-e2e/specs/markdown.spec.ts +1 -1
- package/repo-e2e/specs/routed-dialog.spec.ts +1 -1
- package/scripts/runIsolatedTest.test.ts +426 -0
- package/scripts/runIsolatedTest.ts +454 -0
- package/scripts/testClaimCleanup.test.ts +346 -0
- package/scripts/testClaimCleanup.ts +375 -0
- package/scripts/testClaimProcesses.test.ts +106 -0
- package/scripts/testClaimProcesses.ts +129 -0
- package/scripts/testProcessGuardian.ts +163 -0
- package/scripts/upgrade.ts +1 -16
- package/vona/oxfmt.config.ts +2 -0
- package/vona/oxlint.config.ts +2 -0
- package/vona/package.original.json +2 -2
- package/vona/packages-cli/cli/package.json +1 -1
- package/vona/packages-cli/cli/src/play.test.ts +37 -0
- package/vona/packages-cli/cli/src/play.ts +23 -1
- package/vona/packages-cli/cli-set-api/package.json +1 -1
- package/vona/packages-cli/cli-set-api/src/lib/bean/cli.bin.dbReset.ts +16 -7
- package/vona/packages-cli/cli-set-api/src/lib/bean/cli.bin.dev.ts +13 -7
- package/vona/packages-cli/cli-set-api/src/lib/bean/cli.bin.play.ts +5 -1
- package/vona/packages-cli/cli-set-api/src/lib/bean/cli.bin.test.ts +12 -7
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/configUtils.ts +21 -2
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/generateVonaMeta.ts +10 -0
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/resolveTestCleanupConfig.test.ts +153 -0
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/resolveTestCleanupConfig.ts +141 -0
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/testCleanupAdapters.test.ts +148 -0
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/testCleanupAdapters.ts +127 -0
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/testCleanupProcess.test.ts +47 -0
- package/vona/packages-cli/cli-set-api/src/lib/bean/toolsBin/testCleanupProcess.ts +139 -0
- package/vona/packages-cli/cli-set-api/src/lib/utils.ts +2 -3
- package/vona/packages-cli/cli-set-api/templates/config/_tsconfig_project.json +1 -0
- package/vona/packages-cli/cli-set-api/templates/config/_tsconfig_source.json +1 -0
- package/vona/packages-cli/cli-set-api/test/binDevMode.test.ts +18 -0
- package/vona/packages-cli/cli-set-api/toolsIsolate/dbReset.ts +15 -2
- package/vona/packages-cli/cli-set-api/tsconfig.build.json +2 -1
- package/vona/packages-utils/babel-plugin-vona-bean-module/package.json +1 -1
- package/vona/packages-utils/babel-plugin-vona-bean-module/src/index.test.ts +16 -0
- package/vona/packages-utils/babel-plugin-vona-bean-module/src/index.ts +3 -2
- package/vona/packages-vona/vona/package.json +1 -1
- package/vona/packages-vona/vona-core/package.json +1 -1
- package/vona/packages-vona/vona-core/src/lib/addon/sqlite3.ts +1 -1
- package/vona/packages-vona/vona-core/src/lib/core/config.ts +44 -6
- package/vona/packages-vona/vona-core/src/lib/core/configCleanup.test.ts +40 -0
- package/vona/packages-vona/vona-core/src/lib/core/index.ts +3 -0
- package/vona/packages-vona/vona-core/src/lib/core/isolationGuard.test.ts +301 -0
- package/vona/packages-vona/vona-core/src/lib/core/isolationGuard.ts +312 -0
- package/vona/packages-vona/vona-core/src/lib/core/testClaim.test.ts +577 -0
- package/vona/packages-vona/vona-core/src/lib/core/testClaim.ts +985 -0
- package/vona/packages-vona/vona-core/src/lib/core/testIsolation.ts +129 -0
- package/vona/packages-vona/vona-core/src/lib/framework/createApp.ts +2 -0
- package/vona/packages-vona/vona-core/src/lib/framework/start.ts +4 -0
- package/vona/packages-vona/vona-core/src/lib/framework/useApp.ts +5 -1
- package/vona/packages-vona/vona-core/src/types/application/app.ts +2 -0
- package/vona/packages-vona/vona-core/src/types/utils/env.ts +1 -0
- package/vona/packages-vona/vona-mock/package.json +1 -1
- package/vona/pnpm-lock.yaml +1 -1
- package/vona/src/suite/a-commerce/modules/commerce-payment/test/paymentIndexes.test.ts +73 -10
- package/vona/src/suite-vendor/a-cabloy/modules/a-rbac/package.json +1 -1
- package/vona/src/suite-vendor/a-cabloy/package.json +2 -2
- package/vona/src/suite-vendor/a-vona/modules/a-core/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-logger/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-logger/src/bean/middlewareSystem.httpLog.ts +15 -1
- package/vona/src/suite-vendor/a-vona/modules/a-logger/src/lib/redactIsolatedReadyHeader.test.ts +24 -0
- package/vona/src/suite-vendor/a-vona/modules/a-logger/src/lib/redactIsolatedReadyHeader.ts +11 -0
- package/vona/src/suite-vendor/a-vona/modules/a-metrics/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-metrics/src/bean/middlewareSystem.health.ts +15 -0
- package/vona/src/suite-vendor/a-vona/modules/a-orm/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-orm/src/extend/schemaBuilder.ts +11 -4
- package/vona/src/suite-vendor/a-vona/modules/a-orm/src/service/databaseClient_.ts +6 -1
- package/vona/src/suite-vendor/a-vona/modules/a-ormdialect/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-ormdialect/src/bean/databaseDialect.betterSqlite3.ts +41 -1
- package/vona/src/suite-vendor/a-vona/modules/a-ormdialect/src/bean/databaseDialect.mysql.ts +4 -1
- package/vona/src/suite-vendor/a-vona/modules/a-ormdialect/src/bean/databaseDialect.pg.ts +2 -1
- package/vona/src/suite-vendor/a-vona/modules/a-permission/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-redis/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-redis/src/lib/redis.ts +8 -0
- package/vona/src/suite-vendor/a-vona/modules/a-redis/src/service/redis.ts +23 -6
- package/vona/src/suite-vendor/a-vona/modules/a-redis/src/service/redisClient_.ts +18 -1
- package/vona/src/suite-vendor/a-vona/modules/a-startup/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-startup/src/service/startup.ts +43 -15
- package/vona/src/suite-vendor/a-vona/modules/a-version/package.json +1 -1
- package/vona/src/suite-vendor/a-vona/modules/a-version/src/lib/databaseTestName.ts +3 -37
- package/vona/src/suite-vendor/a-vona/modules/a-version/src/service/database.ts +93 -15
- package/vona/src/suite-vendor/a-vona/package.json +1 -1
- package/vona/tsconfig.json +1 -0
- package/scripts/initTestData.ts +0 -25
|
@@ -26,8 +26,9 @@ export class DatabaseDialectPg extends ServiceDatabaseDialectBase {
|
|
|
26
26
|
schemaBuilder: Knex.SchemaBuilder,
|
|
27
27
|
databasePrefix: string,
|
|
28
28
|
): Promise<IFetchDatabasesResultItem[]> {
|
|
29
|
+
const escapedPrefix = databasePrefix.replaceAll("'", "''");
|
|
29
30
|
const res: any = await schemaBuilder.raw(
|
|
30
|
-
`select datname from pg_database where datname
|
|
31
|
+
`select datname from pg_database where left(datname, ${databasePrefix.length}) = '${escapedPrefix}'`,
|
|
31
32
|
);
|
|
32
33
|
let dbs = res.rows;
|
|
33
34
|
dbs = dbs.map(db => {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { VonaApplication } from 'vona';
|
|
2
2
|
|
|
3
|
+
import { resolveTestRunId } from 'vona';
|
|
4
|
+
|
|
3
5
|
export function getRedisClientKeyPrefix(clientName: string, app: VonaApplication): string {
|
|
4
6
|
const mode = ['test', 'dev'].includes(app.config.meta.mode) ? '_local' : '';
|
|
5
7
|
return `${clientName}_${app.name}${mode}:`;
|
|
@@ -10,5 +12,11 @@ export function prepareRedisClientKeyPrefix(
|
|
|
10
12
|
clientName: string,
|
|
11
13
|
app: VonaApplication,
|
|
12
14
|
): string | undefined {
|
|
15
|
+
const isolated = resolveTestRunId(app.meta.env.META_MODE, app.meta.env.VONA_TEST_RUN_ID);
|
|
16
|
+
const specialClient =
|
|
17
|
+
['queue', 'broadcast'].includes(clientName) || clientName.startsWith('redlock');
|
|
18
|
+
if (isolated && (specialClient ? keyPrefix !== undefined : keyPrefix !== true)) {
|
|
19
|
+
throw new Error(`Isolated test requires a framework-managed Redis prefix: ${clientName}`);
|
|
20
|
+
}
|
|
13
21
|
return keyPrefix === true ? getRedisClientKeyPrefix(clientName, app) : keyPrefix;
|
|
14
22
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Redis } from 'ioredis';
|
|
2
2
|
|
|
3
|
-
import { BeanBase } from 'vona';
|
|
3
|
+
import { BeanBase, resolveTestRunId } from 'vona';
|
|
4
4
|
import { Service } from 'vona-module-a-bean';
|
|
5
5
|
|
|
6
6
|
import type { IRedisClientRecord } from '../types/redis.ts';
|
|
@@ -14,7 +14,8 @@ export class ServiceRedis extends BeanBase {
|
|
|
14
14
|
// clear keys
|
|
15
15
|
await this._clearRedisKeys(
|
|
16
16
|
app.bean.redis.get('queue'),
|
|
17
|
-
|
|
17
|
+
getRedisClientKeyPrefix('bull', app),
|
|
18
|
+
true,
|
|
18
19
|
);
|
|
19
20
|
// broadcast channel has subscribed
|
|
20
21
|
// await _clearRedisKeys(app.redis.get('broadcast'), `${getRedisClientKeyPrefix('broadcast', app)}*`);
|
|
@@ -24,22 +25,38 @@ export class ServiceRedis extends BeanBase {
|
|
|
24
25
|
if (clientName.startsWith('redlock')) {
|
|
25
26
|
await this._clearRedisKeys(
|
|
26
27
|
app.bean.redis.get(clientName),
|
|
27
|
-
|
|
28
|
+
getRedisClientKeyPrefix('redlock', app),
|
|
29
|
+
true,
|
|
28
30
|
);
|
|
29
31
|
} else {
|
|
30
32
|
const client = app.config.redis.clients[clientName];
|
|
31
33
|
const keyPrefix = prepareRedisClientKeyPrefix(client.keyPrefix, clientName, this.app);
|
|
32
|
-
await this._clearRedisKeys(app.bean.redis.get(clientName),
|
|
34
|
+
await this._clearRedisKeys(app.bean.redis.get(clientName), keyPrefix!);
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
private async _clearRedisKeys(redis: Redis,
|
|
39
|
+
private async _clearRedisKeys(redis: Redis, expectedPrefix: string, unprefixedClient = false) {
|
|
38
40
|
if (!redis) return;
|
|
41
|
+
const isolated = resolveTestRunId(
|
|
42
|
+
this.app.meta.env.META_MODE,
|
|
43
|
+
this.app.meta.env.VONA_TEST_RUN_ID,
|
|
44
|
+
);
|
|
39
45
|
const keyPrefix = redis.options.keyPrefix;
|
|
40
|
-
|
|
46
|
+
if (isolated) {
|
|
47
|
+
if (!expectedPrefix.includes(this.app.name) || !expectedPrefix.endsWith(':')) {
|
|
48
|
+
throw new Error('Isolated test Redis cleanup prefix does not belong to this run');
|
|
49
|
+
}
|
|
50
|
+
if (unprefixedClient ? Boolean(keyPrefix) : keyPrefix !== expectedPrefix) {
|
|
51
|
+
throw new Error('Isolated test Redis cleanup uses an unexpected client prefix');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const keys = await redis.keys(`${expectedPrefix}*`);
|
|
41
55
|
const keysDel: string[] = [];
|
|
42
56
|
for (const fullKey of keys) {
|
|
57
|
+
if (isolated && !fullKey.startsWith(expectedPrefix)) {
|
|
58
|
+
throw new Error('Isolated test Redis lookup returned a key outside the owned prefix');
|
|
59
|
+
}
|
|
43
60
|
const key = keyPrefix ? fullKey.substring(keyPrefix.length) : fullKey;
|
|
44
61
|
keysDel.push(key);
|
|
45
62
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { RedisOptions } from 'ioredis';
|
|
2
2
|
|
|
3
3
|
import { Redis } from 'ioredis';
|
|
4
|
-
import { BeanBase, cast, deepExtend } from 'vona';
|
|
4
|
+
import { assertTestClaimRedisClient, BeanBase, cast, deepExtend, resolveTestRunId } from 'vona';
|
|
5
5
|
import { Service } from 'vona-module-a-bean';
|
|
6
6
|
|
|
7
7
|
import type { ConfigRedisCluster, IRedisClientRecord } from '../types/redis.ts';
|
|
@@ -29,6 +29,22 @@ export class ServiceRedisClient extends BeanBase {
|
|
|
29
29
|
const configRedis = this.app.config.redis;
|
|
30
30
|
const configClient = configRedis.clients[clientName];
|
|
31
31
|
if (!configClient) throw new Error(`redis client not found: ${clientName}`);
|
|
32
|
+
const isolated = resolveTestRunId(
|
|
33
|
+
this.app.meta.env.META_MODE,
|
|
34
|
+
this.app.meta.env.VONA_TEST_RUN_ID,
|
|
35
|
+
);
|
|
36
|
+
if (
|
|
37
|
+
isolated &&
|
|
38
|
+
(cast<RedisOptions>(configClient).sentinels || cast<ConfigRedisCluster>(configClient).nodes)
|
|
39
|
+
) {
|
|
40
|
+
throw new Error(`Isolated test cannot use a Redis sentinel or cluster client: ${clientName}`);
|
|
41
|
+
}
|
|
42
|
+
if (isolated) {
|
|
43
|
+
prepareRedisClientKeyPrefix(configClient.keyPrefix, clientName, this.app);
|
|
44
|
+
if (configRedis.base.keyPrefix !== undefined) {
|
|
45
|
+
throw new Error('Isolated test cannot inherit a Redis base keyPrefix');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
32
48
|
if (cast<RedisOptions>(configClient).sentinels) {
|
|
33
49
|
// sentinels
|
|
34
50
|
return new Redis(configClient as RedisOptions);
|
|
@@ -40,6 +56,7 @@ export class ServiceRedisClient extends BeanBase {
|
|
|
40
56
|
} else {
|
|
41
57
|
const keyPrefix = prepareRedisClientKeyPrefix(configClient.keyPrefix, clientName, this.app);
|
|
42
58
|
const configNode = deepExtend({}, configRedis.base, configClient, { keyPrefix });
|
|
59
|
+
assertTestClaimRedisClient(this.app, clientName, configNode);
|
|
43
60
|
return new Redis(configNode);
|
|
44
61
|
}
|
|
45
62
|
}
|
|
@@ -3,7 +3,13 @@ import type { IOnionSlice } from 'vona-module-a-onion';
|
|
|
3
3
|
|
|
4
4
|
import { isNil } from '@cabloy/utils';
|
|
5
5
|
import fse from 'fs-extra';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
assertIsolatedTestConfig,
|
|
8
|
+
BeanBase,
|
|
9
|
+
cast,
|
|
10
|
+
getRuntimePathPhysicalRoot,
|
|
11
|
+
withIsolatedTestDatabaseDiscovery,
|
|
12
|
+
} from 'vona';
|
|
7
13
|
import { Service } from 'vona-module-a-bean';
|
|
8
14
|
|
|
9
15
|
import type {
|
|
@@ -16,6 +22,10 @@ import type {
|
|
|
16
22
|
@Service()
|
|
17
23
|
export class ServiceStartup extends BeanBase {
|
|
18
24
|
async appStart() {
|
|
25
|
+
// db:reset is a one-shot data preparation command, not a development server.
|
|
26
|
+
if (this.app.meta.isDev && process.env.VONA_DB_RESET === 'true') {
|
|
27
|
+
this.app.config.server.listen.disable = true;
|
|
28
|
+
}
|
|
19
29
|
// clear keys
|
|
20
30
|
await this._clearResources();
|
|
21
31
|
|
|
@@ -23,7 +33,23 @@ export class ServiceStartup extends BeanBase {
|
|
|
23
33
|
for (const startup of this._startups) {
|
|
24
34
|
const startupOptions = startup.beanOptions.options;
|
|
25
35
|
if (!startupOptions?.instance && startupOptions?.after !== true) {
|
|
26
|
-
|
|
36
|
+
// A previous dev startup may have left databaseInit's debounce key in Redis.
|
|
37
|
+
// Only the explicit reset must bypass that key to guarantee recreation.
|
|
38
|
+
const force =
|
|
39
|
+
this.app.meta.isDev &&
|
|
40
|
+
process.env.VONA_DB_RESET === 'true' &&
|
|
41
|
+
startup.name === 'a-version:databaseInit';
|
|
42
|
+
const run = () => this.runStartup(startup.name, null, force ? { force: true } : undefined);
|
|
43
|
+
if (this.app.meta.env.VONA_TEST_RUN_ID && startup.name === 'a-version:databaseInit') {
|
|
44
|
+
const defaultClient = this.app.config.database.defaultClient;
|
|
45
|
+
if (typeof defaultClient !== 'string') {
|
|
46
|
+
throw new TypeError('Isolated test requires a static default database client');
|
|
47
|
+
}
|
|
48
|
+
const client = this.app.config.database.clients[defaultClient];
|
|
49
|
+
await withIsolatedTestDatabaseDiscovery(this.app, defaultClient, client, run);
|
|
50
|
+
} else {
|
|
51
|
+
await run();
|
|
52
|
+
}
|
|
27
53
|
}
|
|
28
54
|
}
|
|
29
55
|
}
|
|
@@ -52,24 +78,25 @@ export class ServiceStartup extends BeanBase {
|
|
|
52
78
|
}
|
|
53
79
|
}
|
|
54
80
|
|
|
55
|
-
//
|
|
56
|
-
if (this.app.meta.isTest) {
|
|
57
|
-
|
|
58
|
-
await this.bean.executor.newCtx(
|
|
59
|
-
async () => {
|
|
60
|
-
await this.$scope.version.service.version.__instanceSeed(instanceName);
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
dbInfo: { level: 1 },
|
|
64
|
-
instanceName,
|
|
65
|
-
},
|
|
66
|
-
);
|
|
81
|
+
// Seed test runs and explicit development resets only, after instance initialization.
|
|
82
|
+
if (this.app.meta.isTest || (this.app.meta.isDev && process.env.VONA_DB_RESET === 'true')) {
|
|
83
|
+
await this.seedDefaultInstance();
|
|
67
84
|
}
|
|
68
85
|
}
|
|
69
86
|
|
|
87
|
+
private async seedDefaultInstance() {
|
|
88
|
+
const instanceName = '';
|
|
89
|
+
await this.bean.executor.newCtx(
|
|
90
|
+
async () => {
|
|
91
|
+
await this.$scope.version.service.version.__instanceSeed(instanceName);
|
|
92
|
+
},
|
|
93
|
+
{ dbInfo: { level: 1 }, instanceName },
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
70
97
|
private async _appReadyInstanceStartup(instanceName: keyof IInstanceRecord, wait: boolean) {
|
|
71
98
|
const res = this.$scope.instance.service.instance.instanceStartupIsolate(instanceName, {
|
|
72
|
-
force:
|
|
99
|
+
force: this.app.meta.isDev && process.env.VONA_DB_RESET === 'true',
|
|
73
100
|
});
|
|
74
101
|
if (wait) {
|
|
75
102
|
await res;
|
|
@@ -172,6 +199,7 @@ export class ServiceStartup extends BeanBase {
|
|
|
172
199
|
|
|
173
200
|
private async _clearResources() {
|
|
174
201
|
if (!this.app.meta.isTest) return;
|
|
202
|
+
assertIsolatedTestConfig(this.app);
|
|
175
203
|
// redis
|
|
176
204
|
await this.$scope.redis.service.redis.clearAllData();
|
|
177
205
|
// .app/public
|
|
@@ -1,21 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getDatabaseTestPrefix } from 'vona';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
const APP_NAME_MAX_BYTES = 13;
|
|
5
|
-
const HASH_LENGTH = 10;
|
|
6
|
-
const TIMESTAMP_LENGTH = 15;
|
|
7
|
-
|
|
8
|
-
export function getDatabaseTestPrefix(appName: string, scope: string): string {
|
|
9
|
-
const readablePrefix = getReadablePrefix(appName, scope);
|
|
10
|
-
if (Buffer.byteLength(readablePrefix, 'utf8') + TIMESTAMP_LENGTH <= DATABASE_NAME_MAX_BYTES) {
|
|
11
|
-
return readablePrefix;
|
|
12
|
-
}
|
|
3
|
+
export { getDatabaseTestPrefix };
|
|
13
4
|
|
|
14
|
-
|
|
15
|
-
const scopeKind = scope === 'share' ? 's' : 'i';
|
|
16
|
-
const namespaceHash = hash(`${appName}\0${scope}`);
|
|
17
|
-
return `vona-test-${appHead}-${scopeKind}-${namespaceHash}-`;
|
|
18
|
-
}
|
|
5
|
+
const DATABASE_NAME_MAX_BYTES = 63;
|
|
19
6
|
|
|
20
7
|
export function createDatabaseTestName(databasePrefix: string, timestamp: string): string {
|
|
21
8
|
const databaseName = `${databasePrefix}${timestamp}`;
|
|
@@ -24,24 +11,3 @@ export function createDatabaseTestName(databasePrefix: string, timestamp: string
|
|
|
24
11
|
}
|
|
25
12
|
return databaseName;
|
|
26
13
|
}
|
|
27
|
-
|
|
28
|
-
function getReadablePrefix(appName: string, scope: string): string {
|
|
29
|
-
if (scope === 'share') return `vona-test-${appName}-s-`;
|
|
30
|
-
return `vona-test-${appName}-i-${scope.slice('isolate-'.length)}-`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function hash(value: string): string {
|
|
34
|
-
return createHash('sha256').update(value).digest('base64url').slice(0, HASH_LENGTH);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function truncateUtf8(value: string, maxBytes: number): string {
|
|
38
|
-
let result = '';
|
|
39
|
-
let bytes = 0;
|
|
40
|
-
for (const char of value) {
|
|
41
|
-
const charBytes = Buffer.byteLength(char, 'utf8');
|
|
42
|
-
if (bytes + charBytes > maxBytes) break;
|
|
43
|
-
result += char;
|
|
44
|
-
bytes += charBytes;
|
|
45
|
-
}
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
@@ -4,7 +4,15 @@ import type { IDatabaseClientRecord, ServiceDatabaseClient } from 'vona-module-a
|
|
|
4
4
|
|
|
5
5
|
import chalk from 'chalk';
|
|
6
6
|
import { DateTime } from 'luxon';
|
|
7
|
-
import
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import {
|
|
9
|
+
BeanBase,
|
|
10
|
+
assertDisposableDatabaseName as assertDisposableDatabaseNameCore,
|
|
11
|
+
assertIsolatedTestPath,
|
|
12
|
+
assertTestRunOwner,
|
|
13
|
+
resolveVonaLocalPaths,
|
|
14
|
+
withIsolatedTestDatabaseDiscovery,
|
|
15
|
+
} from 'vona';
|
|
8
16
|
import { Service } from 'vona-module-a-bean';
|
|
9
17
|
|
|
10
18
|
import { createDatabaseTestName, getDatabaseTestPrefix } from '../lib/databaseTestName.ts';
|
|
@@ -12,6 +20,21 @@ import { createDatabaseTestName, getDatabaseTestPrefix } from '../lib/databaseTe
|
|
|
12
20
|
const __separator = '-';
|
|
13
21
|
const __timeFormat = `yyyyMMdd${__separator}HHmmss`;
|
|
14
22
|
|
|
23
|
+
export function assertDisposableDatabaseName(
|
|
24
|
+
databaseName: string,
|
|
25
|
+
databasePrefix: string,
|
|
26
|
+
sqliteRoot?: string,
|
|
27
|
+
) {
|
|
28
|
+
const filename = sqliteRoot ? path.basename(databaseName, '.db') : databaseName;
|
|
29
|
+
assertDisposableDatabaseNameCore(filename, databasePrefix);
|
|
30
|
+
if (sqliteRoot) {
|
|
31
|
+
if (!path.isAbsolute(databaseName) || path.extname(databaseName) !== '.db') {
|
|
32
|
+
throw new Error(`Database is outside the disposable SQLite directory: ${databaseName}`);
|
|
33
|
+
}
|
|
34
|
+
assertIsolatedTestPath(databaseName, path.join(sqliteRoot, `${filename}.db`));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
15
38
|
@Service()
|
|
16
39
|
export class ServiceDatabase extends BeanBase {
|
|
17
40
|
get configDatabase() {
|
|
@@ -42,9 +65,14 @@ export class ServiceDatabase extends BeanBase {
|
|
|
42
65
|
instanceName?: keyof IInstanceRecord,
|
|
43
66
|
configInstanceBase?: ConfigInstanceBase,
|
|
44
67
|
) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
68
|
+
const clientNameReal =
|
|
69
|
+
clientName === 'default' ? (this.configDatabase.defaultClient as string) : clientName;
|
|
70
|
+
const clientConfig = this.configDatabase.clients[clientNameReal];
|
|
71
|
+
await withIsolatedTestDatabaseDiscovery(this.app, clientNameReal, clientConfig, async () => {
|
|
72
|
+
await this.bean.database.switchDb(async () => {
|
|
73
|
+
await this.__prepareDatabase(versionStart, instanceName, configInstanceBase);
|
|
74
|
+
}, clientName);
|
|
75
|
+
});
|
|
48
76
|
}
|
|
49
77
|
|
|
50
78
|
private async __fetchDatabases(
|
|
@@ -94,13 +122,14 @@ export class ServiceDatabase extends BeanBase {
|
|
|
94
122
|
instanceName?: keyof IInstanceRecord,
|
|
95
123
|
configInstanceBase?: ConfigInstanceBase,
|
|
96
124
|
) {
|
|
97
|
-
await this.__prepareDatabaseInner(instanceName, configInstanceBase);
|
|
125
|
+
await this.__prepareDatabaseInner(versionStart, instanceName, configInstanceBase);
|
|
98
126
|
if (versionStart) {
|
|
99
127
|
await this.scope.service.version.__start();
|
|
100
128
|
}
|
|
101
129
|
}
|
|
102
130
|
|
|
103
131
|
private async __prepareDatabaseInner(
|
|
132
|
+
versionStart: boolean,
|
|
104
133
|
instanceName?: keyof IInstanceRecord,
|
|
105
134
|
configInstanceBase?: ConfigInstanceBase,
|
|
106
135
|
) {
|
|
@@ -109,26 +138,71 @@ export class ServiceDatabase extends BeanBase {
|
|
|
109
138
|
return;
|
|
110
139
|
}
|
|
111
140
|
const databasePrefix = this.getDatabasePrefix(instanceName, configInstanceBase);
|
|
141
|
+
if (this.app.meta.env.VONA_TEST_RUN_ID) {
|
|
142
|
+
const paths = resolveVonaLocalPaths(
|
|
143
|
+
this.app.projectPath,
|
|
144
|
+
this.app.meta.env.META_MODE,
|
|
145
|
+
this.app.meta.env.VONA_TEST_RUN_ID,
|
|
146
|
+
);
|
|
147
|
+
assertTestRunOwner(paths.dataRoot, paths.runId!);
|
|
148
|
+
}
|
|
112
149
|
// client
|
|
113
150
|
const client = this.bean.database.current.client;
|
|
114
151
|
// get current database name
|
|
115
152
|
let databaseName = client.getDatabaseName();
|
|
116
|
-
const isTestDatabase = databaseName.includes(databasePrefix);
|
|
117
153
|
// dev/debug db
|
|
118
154
|
if (this.app.meta.isDev) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
155
|
+
const reset = versionStart && process.env.VONA_DB_RESET === 'true';
|
|
156
|
+
// Never drop a configured, non-disposable development database.
|
|
157
|
+
if (reset && !this.configDatabase.testDatabase) {
|
|
158
|
+
throw new Error('db:reset requires database.testDatabase=true in the dev configuration');
|
|
159
|
+
}
|
|
160
|
+
if (!this.configDatabase.testDatabase) return;
|
|
161
|
+
}
|
|
162
|
+
const sqliteRoot =
|
|
163
|
+
client.db.dialectName === 'better-sqlite3'
|
|
164
|
+
? resolveVonaLocalPaths(
|
|
165
|
+
this.app.projectPath,
|
|
166
|
+
this.app.meta.env.META_MODE,
|
|
167
|
+
this.app.meta.env.VONA_TEST_RUN_ID,
|
|
168
|
+
).sqliteRoot
|
|
169
|
+
: undefined;
|
|
170
|
+
const currentFilename = sqliteRoot ? path.basename(databaseName, '.db') : databaseName;
|
|
171
|
+
const isTestDatabase = currentFilename.startsWith(databasePrefix);
|
|
172
|
+
if (isTestDatabase) assertDisposableDatabaseName(databaseName, databasePrefix, sqliteRoot);
|
|
173
|
+
if (
|
|
174
|
+
this.app.meta.isTest &&
|
|
175
|
+
this.app.meta.env.VONA_TEST_RUN_ID &&
|
|
176
|
+
currentFilename.startsWith('vona-test-') &&
|
|
177
|
+
!isTestDatabase
|
|
178
|
+
) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`Isolated test database is outside its disposable namespace: ${databaseName}`,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
// dev/debug db
|
|
184
|
+
if (this.app.meta.isDev) {
|
|
185
|
+
const reset = versionStart && process.env.VONA_DB_RESET === 'true';
|
|
186
|
+
// The database used to enumerate/drop databases must not itself be dropped.
|
|
187
|
+
if (isTestDatabase) {
|
|
188
|
+
if (reset)
|
|
189
|
+
throw new Error('db:reset requires a connection outside the disposable database');
|
|
124
190
|
return;
|
|
125
191
|
}
|
|
126
192
|
const dbs = await this.__fetchDatabases(client, instanceName, configInstanceBase);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
193
|
+
for (const db of dbs) {
|
|
194
|
+
assertDisposableDatabaseName(db.name, databasePrefix, sqliteRoot);
|
|
195
|
+
}
|
|
196
|
+
if (reset) {
|
|
197
|
+
for (const db of dbs) {
|
|
198
|
+
await client.connection.schema.dropDatabase(db.name);
|
|
199
|
+
}
|
|
131
200
|
}
|
|
201
|
+
databaseName =
|
|
202
|
+
reset || dbs.length === 0
|
|
203
|
+
? await this.__createDatabase(client, instanceName, configInstanceBase)
|
|
204
|
+
: dbs[0].name;
|
|
205
|
+
assertDisposableDatabaseName(databaseName, databasePrefix, sqliteRoot);
|
|
132
206
|
// set config and reload client
|
|
133
207
|
await client.changeConfigConnectionAndReloadWorker(databaseName);
|
|
134
208
|
this.$logger.silly(chalk.cyan(`dialect: ${client.db.dialectName}`));
|
|
@@ -143,11 +217,15 @@ export class ServiceDatabase extends BeanBase {
|
|
|
143
217
|
}
|
|
144
218
|
// drop old databases
|
|
145
219
|
const dbs = await this.__fetchDatabases(client, instanceName, configInstanceBase);
|
|
220
|
+
for (const db of dbs) {
|
|
221
|
+
assertDisposableDatabaseName(db.name, databasePrefix, sqliteRoot);
|
|
222
|
+
}
|
|
146
223
|
for (const db of dbs) {
|
|
147
224
|
await client.connection.schema.dropDatabase(db.name);
|
|
148
225
|
}
|
|
149
226
|
// create database
|
|
150
227
|
const databaseName = await this.__createDatabase(client, instanceName, configInstanceBase);
|
|
228
|
+
assertDisposableDatabaseName(databaseName, databasePrefix, sqliteRoot);
|
|
151
229
|
// set config and reload client
|
|
152
230
|
await client.changeConfigConnectionAndReloadWorker(databaseName);
|
|
153
231
|
// database ready
|
package/vona/tsconfig.json
CHANGED
package/scripts/initTestData.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { execSync } from 'node:child_process';
|
|
2
|
-
import { dirname, resolve } from 'node:path';
|
|
3
|
-
import { fileURLToPath } from 'node:url';
|
|
4
|
-
|
|
5
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
|
-
const ROOT_DIR = resolve(__dirname, '..');
|
|
7
|
-
|
|
8
|
-
function exec(cmd: string): void {
|
|
9
|
-
execSync(cmd, { stdio: 'inherit', cwd: ROOT_DIR });
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function initTestData(): void {
|
|
13
|
-
// eslint-disable-next-line no-console
|
|
14
|
-
console.log('[init:test-data] Initializing test data via npm run test...');
|
|
15
|
-
try {
|
|
16
|
-
exec('npm run test');
|
|
17
|
-
} catch {
|
|
18
|
-
// eslint-disable-next-line no-console
|
|
19
|
-
console.warn(
|
|
20
|
-
'[init:test-data] npm run test failed after init completed; Redis may be unavailable, skipping test data initialization',
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
initTestData();
|