@jskit-ai/console-core 0.2.14 → 0.2.16
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/CHANGELOG.md +8 -0
- package/package.json +16 -13
- package/src/server/ConsoleFeature.js +10 -6
- package/src/server/consoleRuntime.js +4 -7
- package/src/server/consoleSettings/consoleSettingsActions.js +2 -2
- package/src/server/consoleSettings/consoleSettingsRepository.js +39 -45
- package/src/server/consoleSettings/consoleSettingsRoutes.js +4 -4
- package/src/server/types.js +2 -1
- package/src/shared/resources/consoleSettingsResource.js +14 -7
- package/src/shared/resources/consoleSettingsTableResource.js +17 -0
- package/test/consoleRouteResources.test.js +27 -64
- package/test/consoleSettingsRepository.mysql.test.js +176 -0
- package/test/featureRuntime.test.js +13 -3
- package/test/repositoryContracts.test.js +38 -12
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- `console.settings` reads and writes its record with only the caller's context and transaction (`passedOn(options)`) and its own settings: a caller's `format`, `lockVersion` or `returning` no longer reaches its `get` and `patch`.
|
|
6
|
+
- `@jskit-ai/database-runtime` and `@jskit-ai/resource-runtime` are peer dependencies, required and at the same versions, where they were dependencies: its settings are a table resource, and the app holds one copy of the table runtime that every package shares, as it does the kernel. npm installs a required peer as it installed the dependency, so an app installs the same packages and versions; a release whose versions disagree now fails the install with ERESOLVE instead of nesting a second copy.
|
|
7
|
+
- **Breaking (data-access redesign).** The console settings operations are an API contract, `consoleSettingsContract`, which replaces `consoleSettingsResource`, with the two its routes serve: view (GET) and patch (PATCH), each with the method of the route that serves it.
|
|
5
8
|
- The console resources' validation feedback names the catalog key `jskit.console.feedback.validationFailed` instead of plain English; JSKIT's forms render it with `text()`, so the English shown is unchanged. `apiValidation` is sent in the coded form with `validation_failed`.
|
|
9
|
+
- **Breaking (data-access redesign).** The console's settings record is its owner's capability, `console.settings` (the console settings repository: `getSingleton(options)` reads `{ id, ownerUserId, settings, createdAt, updatedAt }`), provided by `ConsoleFeature`; `console.core` no longer has `repositories`. Its type is `ConsoleSettings`.
|
|
10
|
+
|
|
11
|
+
### Upgrading
|
|
12
|
+
|
|
13
|
+
- Code that read `repositories.settings` of `console.core` requires `console.settings`.
|
|
6
14
|
|
|
7
15
|
### New message keys (1)
|
|
8
16
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/console-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -12,9 +12,8 @@
|
|
|
12
12
|
"./locales/en.json": "./locales/en.json"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@jskit-ai/
|
|
16
|
-
"@jskit-ai/
|
|
17
|
-
"@jskit-ai/users-core": "0.2.14",
|
|
15
|
+
"@jskit-ai/resource-core": "0.1.205",
|
|
16
|
+
"@jskit-ai/users-core": "0.2.16",
|
|
18
17
|
"json-rest-schema": "^1.0.17"
|
|
19
18
|
},
|
|
20
19
|
"description": "Console runtime: console settings schema, bootstrap flags, actions, and HTTP routes.",
|
|
@@ -22,14 +21,15 @@
|
|
|
22
21
|
"kind": "runtime",
|
|
23
22
|
"capabilities": {
|
|
24
23
|
"provides": [
|
|
25
|
-
"console.core"
|
|
24
|
+
"console.core",
|
|
25
|
+
"console.settings"
|
|
26
26
|
],
|
|
27
27
|
"requires": [
|
|
28
28
|
"auth.extensions",
|
|
29
29
|
"runtime.actions",
|
|
30
30
|
"runtime.bootstrap",
|
|
31
|
-
"runtime.
|
|
32
|
-
"runtime.
|
|
31
|
+
"runtime.http",
|
|
32
|
+
"runtime.resources"
|
|
33
33
|
]
|
|
34
34
|
},
|
|
35
35
|
"runtime": {
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"surfaces": [
|
|
60
60
|
{
|
|
61
61
|
"subpath": "./server/ConsoleFeature",
|
|
62
|
-
"summary": "Exports ConsoleFeature, which provides console.core
|
|
62
|
+
"summary": "Exports ConsoleFeature, which provides console.core (owner access, the console settings actions and routes, and the console bootstrap) and console.settings (the console's settings record, read with getSingleton)."
|
|
63
63
|
},
|
|
64
64
|
{
|
|
65
65
|
"subpath": "./shared/resources/consoleSettingsResource",
|
|
@@ -67,11 +67,12 @@
|
|
|
67
67
|
},
|
|
68
68
|
{
|
|
69
69
|
"subpath": "./server/types",
|
|
70
|
-
"summary": "
|
|
70
|
+
"summary": "Types of the console.core and console.settings capabilities, for features that require them."
|
|
71
71
|
}
|
|
72
72
|
],
|
|
73
73
|
"capabilities": [
|
|
74
|
-
"console.core"
|
|
74
|
+
"console.core",
|
|
75
|
+
"console.settings"
|
|
75
76
|
]
|
|
76
77
|
},
|
|
77
78
|
"server": {
|
|
@@ -96,8 +97,10 @@
|
|
|
96
97
|
}
|
|
97
98
|
},
|
|
98
99
|
"peerDependencies": {
|
|
99
|
-
"@jskit-ai/auth-core": "0.2.
|
|
100
|
-
"@jskit-ai/
|
|
101
|
-
"@jskit-ai/
|
|
100
|
+
"@jskit-ai/auth-core": "0.2.14",
|
|
101
|
+
"@jskit-ai/database-runtime": "0.1.263",
|
|
102
|
+
"@jskit-ai/http-runtime": "0.2.14",
|
|
103
|
+
"@jskit-ai/kernel": "0.2.14",
|
|
104
|
+
"@jskit-ai/resource-runtime": "0.2.12"
|
|
102
105
|
}
|
|
103
106
|
}
|
|
@@ -3,29 +3,33 @@ import { createConsoleAuthServiceDecorator } from "./consoleAuthServiceDecorator
|
|
|
3
3
|
import { createConsoleBootstrapContributor } from "./consoleBootstrapContributor.js";
|
|
4
4
|
import { registerConsoleSettingsRoutes } from "./consoleSettings/consoleSettingsRoutes.js";
|
|
5
5
|
import { createConsoleSettingsActions } from "./consoleSettings/consoleSettingsActions.js";
|
|
6
|
+
import { createRepository as createConsoleSettingsRepository } from "./consoleSettings/consoleSettingsRepository.js";
|
|
6
7
|
import { createConsoleRuntime } from "./consoleRuntime.js";
|
|
8
|
+
import { consoleSettingsTableResource } from "../shared/resources/consoleSettingsTableResource.js";
|
|
7
9
|
|
|
8
10
|
const ConsoleFeature = defineFeature({
|
|
9
11
|
id: "console.core",
|
|
10
12
|
domain: "console",
|
|
13
|
+
owns: [consoleSettingsTableResource],
|
|
11
14
|
requires: {
|
|
12
15
|
authExtensions: "auth.extensions",
|
|
13
16
|
bootstrap: "runtime.bootstrap",
|
|
14
|
-
database: "runtime.database",
|
|
15
17
|
http: "runtime.http"
|
|
16
18
|
},
|
|
17
19
|
provides: {
|
|
18
|
-
consoleCore: "console.core"
|
|
20
|
+
consoleCore: "console.core",
|
|
21
|
+
consoleSettings: "console.settings"
|
|
19
22
|
},
|
|
20
23
|
/**
|
|
21
24
|
* @param {object} capabilities
|
|
22
25
|
* @param {import("@jskit-ai/auth-core/server/types").AuthExtensions} capabilities.authExtensions
|
|
23
26
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeBootstrap} capabilities.bootstrap
|
|
24
|
-
* @param {import("@jskit-ai/database-runtime/server/types").RuntimeDatabase} capabilities.database
|
|
25
27
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeHttp} capabilities.http
|
|
28
|
+
* @param {{ consoleSettings: import("@jskit-ai/resource-runtime/server/types").ResourceHandle }} capabilities.resources
|
|
26
29
|
*/
|
|
27
|
-
setup({ authExtensions, bootstrap,
|
|
28
|
-
const
|
|
30
|
+
setup({ authExtensions, bootstrap, http, resources }) {
|
|
31
|
+
const consoleSettings = createConsoleSettingsRepository({ consoleSettings: resources.consoleSettings });
|
|
32
|
+
const consoleCore = createConsoleRuntime({ consoleSettingsRepository: consoleSettings });
|
|
29
33
|
|
|
30
34
|
authExtensions.registerServiceDecorator(
|
|
31
35
|
createConsoleAuthServiceDecorator({ consoleService: consoleCore.services.access })
|
|
@@ -39,7 +43,7 @@ const ConsoleFeature = defineFeature({
|
|
|
39
43
|
});
|
|
40
44
|
registerConsoleSettingsRoutes(http.router);
|
|
41
45
|
|
|
42
|
-
return { consoleCore };
|
|
46
|
+
return { consoleCore, consoleSettings };
|
|
43
47
|
},
|
|
44
48
|
actions({ consoleCore }) {
|
|
45
49
|
return createConsoleSettingsActions({
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { createService as createConsoleService } from "./consoleSettings/consoleService.js";
|
|
2
|
-
import { createRepository as createConsoleSettingsRepository } from "./consoleSettings/consoleSettingsRepository.js";
|
|
3
2
|
import { createService as createConsoleSettingsService } from "./consoleSettings/consoleSettingsService.js";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
|
-
* The `console.core` capability: the console
|
|
7
|
-
*
|
|
8
|
-
* @param {{
|
|
5
|
+
* The `console.core` capability: the console access service and the console
|
|
6
|
+
* settings service, over the `console.settings` record.
|
|
7
|
+
* @param {{ consoleSettingsRepository: import("./types.js").ConsoleSettings }} dependencies
|
|
9
8
|
*/
|
|
10
|
-
function createConsoleRuntime({
|
|
11
|
-
const consoleSettingsRepository = createConsoleSettingsRepository({ knex: database.knex });
|
|
9
|
+
function createConsoleRuntime({ consoleSettingsRepository }) {
|
|
12
10
|
const consoleService = createConsoleService({ consoleSettingsRepository });
|
|
13
11
|
const consoleSettingsService = createConsoleSettingsService({
|
|
14
12
|
consoleService,
|
|
@@ -16,7 +14,6 @@ function createConsoleRuntime({ database }) {
|
|
|
16
14
|
});
|
|
17
15
|
|
|
18
16
|
return Object.freeze({
|
|
19
|
-
repositories: Object.freeze({ settings: consoleSettingsRepository }),
|
|
20
17
|
services: Object.freeze({
|
|
21
18
|
access: consoleService,
|
|
22
19
|
settings: consoleSettingsService
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
emptyInputValidator
|
|
3
3
|
} from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
4
4
|
import { createEntityChangedActionEvent } from "@jskit-ai/kernel/server/actions";
|
|
5
|
-
import {
|
|
5
|
+
import { consoleSettingsContract } from "../../shared/resources/consoleSettingsResource.js";
|
|
6
6
|
|
|
7
7
|
/** @param {{ consoleSettingsService: import("../types.js").ConsoleSettingsService }} dependencies */
|
|
8
8
|
function createConsoleSettingsActions({ consoleSettingsService }) {
|
|
@@ -34,7 +34,7 @@ function createConsoleSettingsActions({ consoleSettingsService }) {
|
|
|
34
34
|
permission: {
|
|
35
35
|
require: "authenticated"
|
|
36
36
|
},
|
|
37
|
-
input:
|
|
37
|
+
input: consoleSettingsContract.operations.patch.body,
|
|
38
38
|
output: null,
|
|
39
39
|
extensions: {
|
|
40
40
|
assistant: { exclude: "Application-owned console settings require an explicit assistant field allowlist; arbitrary configuration is not model-safe." },
|
|
@@ -1,65 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createWithTransaction,
|
|
3
|
-
normalizeDbRecordId,
|
|
4
|
-
resolveRepoClient,
|
|
5
|
-
toInsertDateTime,
|
|
6
|
-
toIsoString
|
|
7
|
-
} from "@jskit-ai/database-runtime/shared";
|
|
1
|
+
import { normalizeDbRecordId, toIsoString } from "@jskit-ai/database-runtime/shared";
|
|
8
2
|
import { normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
+
import { passedOn } from "@jskit-ai/resource-runtime/server";
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
5
|
+
// The one console settings record's id.
|
|
6
|
+
const SINGLETON_ID = "1";
|
|
13
7
|
|
|
14
|
-
function
|
|
15
|
-
if (!
|
|
8
|
+
function mapSingletonRecord(record) {
|
|
9
|
+
if (!record) {
|
|
16
10
|
throw new Error("console_settings singleton row is missing.");
|
|
17
11
|
}
|
|
18
12
|
|
|
19
|
-
const ownerUserId = normalizeDbRecordId(
|
|
13
|
+
const ownerUserId = normalizeDbRecordId(record.ownerUserId, { fallback: null });
|
|
20
14
|
return {
|
|
21
|
-
id: normalizeDbRecordId(
|
|
15
|
+
id: normalizeDbRecordId(record.id, { fallback: SINGLETON_ID }),
|
|
22
16
|
ownerUserId,
|
|
23
17
|
settings: {},
|
|
24
|
-
createdAt: toIsoString(
|
|
25
|
-
updatedAt: toIsoString(
|
|
18
|
+
createdAt: toIsoString(record.createdAt),
|
|
19
|
+
updatedAt: toIsoString(record.updatedAt)
|
|
26
20
|
};
|
|
27
21
|
}
|
|
28
22
|
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
/**
|
|
24
|
+
* The `console.settings` capability: the one console_settings record, through the console feature's handle. Each
|
|
25
|
+
* call takes the call's options last and runs in their transaction, or on its own; its reads and writes take only
|
|
26
|
+
* the options' context and transaction, never a setting the caller named for another call.
|
|
27
|
+
* @param {object} dependencies
|
|
28
|
+
* @param {import("@jskit-ai/resource-runtime/server/types").ResourceHandle} dependencies.consoleSettings
|
|
29
|
+
*/
|
|
30
|
+
function createRepository({ consoleSettings }) {
|
|
31
|
+
/**
|
|
32
|
+
* Runs work in one transaction, or in the options' transaction, handing it the options its calls pass on. Failed
|
|
33
|
+
* work rejects with the work's own error.
|
|
34
|
+
* @template T
|
|
35
|
+
* @param {object} options
|
|
36
|
+
* @param {(txOptions: object) => Promise<T>} work
|
|
37
|
+
* @returns {Promise<T>}
|
|
38
|
+
*/
|
|
39
|
+
function withTransaction(options, work) {
|
|
40
|
+
return consoleSettings.withTransaction(options, work);
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
async function getSingleton(options = {}) {
|
|
38
|
-
|
|
39
|
-
return mapSingletonRow(await readSingleton(client));
|
|
44
|
+
return mapSingletonRecord(await consoleSettings.get(SINGLETON_ID, {}, { ...passedOn(options), missing: "skip" }));
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
async function ensureOwnerUserId(userId, options = {}) {
|
|
43
|
-
const client = resolveRepoClient(knex, options);
|
|
44
48
|
const candidateOwnerUserId = normalizeRecordId(userId, { fallback: null });
|
|
45
49
|
if (!candidateOwnerUserId) {
|
|
46
50
|
throw new TypeError("consoleSettingsRepository.ensureOwnerUserId requires a positive user id.");
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
const current =
|
|
53
|
+
const current = await getSingleton(options);
|
|
50
54
|
if (current.ownerUserId) {
|
|
51
55
|
return current.ownerUserId;
|
|
52
56
|
}
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
updated_at: nowDb()
|
|
60
|
-
});
|
|
58
|
+
// Only while the record has no owner: of two first owners at once, the first to write it wins.
|
|
59
|
+
await consoleSettings.patch(SINGLETON_ID, {
|
|
60
|
+
ownerUserId: candidateOwnerUserId,
|
|
61
|
+
updatedAt: new Date().toISOString()
|
|
62
|
+
}, { ...passedOn(options), onlyIf: { ownerUserId: null }, missing: "skip", returning: "none" });
|
|
61
63
|
|
|
62
|
-
const reloaded =
|
|
64
|
+
const reloaded = await getSingleton(options);
|
|
63
65
|
if (!reloaded.ownerUserId) {
|
|
64
66
|
throw new Error("console_settings owner_user_id could not be resolved.");
|
|
65
67
|
}
|
|
@@ -67,18 +69,10 @@ function createRepository({ knex }) {
|
|
|
67
69
|
return reloaded.ownerUserId;
|
|
68
70
|
}
|
|
69
71
|
|
|
70
|
-
// Touches the
|
|
72
|
+
// Touches the record's updatedAt; the settings have no columns.
|
|
71
73
|
async function updateSingleton(options = {}) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
updated_at: nowDb()
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
await client("console_settings")
|
|
78
|
-
.where({ id: 1 })
|
|
79
|
-
.update(dbPatch);
|
|
80
|
-
|
|
81
|
-
return getSingleton({ trx: client });
|
|
74
|
+
await getSingleton(options);
|
|
75
|
+
return mapSingletonRecord(await consoleSettings.patch(SINGLETON_ID, { updatedAt: new Date().toISOString() }, passedOn(options)));
|
|
82
76
|
}
|
|
83
77
|
|
|
84
78
|
return Object.freeze({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createJsonApiResourceRouteContract } from "@jskit-ai/http-runtime/shared/validators/jsonApiRouteTransport";
|
|
2
|
-
import {
|
|
2
|
+
import { consoleSettingsContract } from "../../shared/resources/consoleSettingsResource.js";
|
|
3
3
|
|
|
4
4
|
function resolveConsoleSettingsRecordId() {
|
|
5
5
|
return "console-settings";
|
|
@@ -20,7 +20,7 @@ function registerConsoleSettingsRoutes(router) {
|
|
|
20
20
|
...createJsonApiResourceRouteContract({
|
|
21
21
|
requestType: "console-settings",
|
|
22
22
|
responseType: "console-settings",
|
|
23
|
-
output:
|
|
23
|
+
output: consoleSettingsContract.operations.view.output,
|
|
24
24
|
outputKind: "record",
|
|
25
25
|
getRecordId: resolveConsoleSettingsRecordId
|
|
26
26
|
})
|
|
@@ -46,8 +46,8 @@ function registerConsoleSettingsRoutes(router) {
|
|
|
46
46
|
...createJsonApiResourceRouteContract({
|
|
47
47
|
requestType: "console-settings",
|
|
48
48
|
responseType: "console-settings",
|
|
49
|
-
body:
|
|
50
|
-
output:
|
|
49
|
+
body: consoleSettingsContract.operations.patch.body,
|
|
50
|
+
output: consoleSettingsContract.operations.view.output,
|
|
51
51
|
outputKind: "record",
|
|
52
52
|
getRecordId: resolveConsoleSettingsRecordId,
|
|
53
53
|
includeValidation400: true
|
package/src/server/types.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// The
|
|
1
|
+
// The types of the console capabilities, for features that require them.
|
|
2
2
|
|
|
3
3
|
/** @typedef {ReturnType<typeof import("./consoleRuntime.js").createConsoleRuntime>} ConsoleCore */
|
|
4
|
+
/** @typedef {ReturnType<typeof import("./consoleSettings/consoleSettingsRepository.js").createRepository>} ConsoleSettings The console's settings record, read with getSingleton: `console.settings`. */
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* The console access service: who owns the console.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createSchema } from "json-rest-schema";
|
|
2
|
-
import {
|
|
2
|
+
import { defineApiContract } from "@jskit-ai/resource-core/shared/apiContract";
|
|
3
3
|
import { OPERATION_MESSAGES } from "../operationMessages.js";
|
|
4
4
|
|
|
5
5
|
const consoleSettingsBodySchema = createSchema({});
|
|
@@ -11,14 +11,21 @@ const consoleSettingsOutputSchema = createSchema({
|
|
|
11
11
|
}
|
|
12
12
|
});
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
// The console settings over HTTP: read them (GET), and patch them (PATCH, the method of the route that serves it).
|
|
15
|
+
const consoleSettingsContract = defineApiContract({
|
|
15
16
|
namespace: "consoleSettings",
|
|
16
17
|
messages: OPERATION_MESSAGES,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
operations: {
|
|
19
|
+
view: {
|
|
20
|
+
method: "GET",
|
|
21
|
+
output: consoleSettingsOutputSchema
|
|
22
|
+
},
|
|
23
|
+
patch: {
|
|
24
|
+
method: "PATCH",
|
|
25
|
+
body: consoleSettingsBodySchema,
|
|
26
|
+
output: consoleSettingsOutputSchema
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
});
|
|
23
30
|
|
|
24
|
-
export {
|
|
31
|
+
export { consoleSettingsContract };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
|
|
2
|
+
|
|
3
|
+
// console_settings: the application's one console settings record (id 1, seeded by the migration), naming the user
|
|
4
|
+
// who owns the console. It belongs to the whole application, not to a workspace or user, so the table is public; the
|
|
5
|
+
// console feature owns it and only its repository reads or writes it.
|
|
6
|
+
const consoleSettingsTableResource = defineTableResource({
|
|
7
|
+
name: "consoleSettings",
|
|
8
|
+
tableName: "console_settings",
|
|
9
|
+
ownership: "public",
|
|
10
|
+
schema: {
|
|
11
|
+
ownerUserId: { type: "id", nullable: true },
|
|
12
|
+
createdAt: { type: "dateTime", storage: { writeSerializer: "datetime-utc" } },
|
|
13
|
+
updatedAt: { type: "dateTime", storage: { writeSerializer: "datetime-utc" } }
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export { consoleSettingsTableResource };
|
|
@@ -5,74 +5,37 @@ import { existsSync } from "node:fs";
|
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { deriveResourceRequiredMetadata } from "@jskit-ai/kernel/_testable";
|
|
7
7
|
import { resolveSchemaTransportSchemaDefinition } from "@jskit-ai/kernel/shared/validators";
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
assert.equal(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
assert.equal(typeof operation.
|
|
19
|
-
|
|
20
|
-
operation?.messages && typeof operation.messages === "object"
|
|
21
|
-
? operation.messages
|
|
22
|
-
: resource?.messages || resource?.operationMessages;
|
|
23
|
-
assert.equal(
|
|
24
|
-
typeof resolvedMessages,
|
|
25
|
-
"object",
|
|
26
|
-
`${label}.operations.${operationName} must resolve messages from operation.messages or resource.messages.`
|
|
27
|
-
);
|
|
28
|
-
assert.equal(
|
|
29
|
-
typeof resolveSchemaTransportSchemaDefinition(operation.output, {
|
|
30
|
-
context: `${label}.operations.${operationName}.output`,
|
|
31
|
-
defaultMode: "replace"
|
|
32
|
-
}),
|
|
33
|
-
"object",
|
|
34
|
-
`${label}.operations.${operationName} payload schema is required.`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
assert.equal(typeof resolveSchemaTransportSchemaDefinition(resource.operations.create.body, {
|
|
39
|
-
context: `${label}.operations.create.body`,
|
|
40
|
-
defaultMode: "create"
|
|
41
|
-
}), "object", `${label}.operations.create.body.schema is required.`);
|
|
42
|
-
assert.equal(typeof resolveSchemaTransportSchemaDefinition(resource.operations.replace.body, {
|
|
43
|
-
context: `${label}.operations.replace.body`,
|
|
44
|
-
defaultMode: "replace"
|
|
45
|
-
}), "object", `${label}.operations.replace.body.schema is required.`);
|
|
46
|
-
assert.equal(typeof resolveSchemaTransportSchemaDefinition(resource.operations.patch.body, {
|
|
47
|
-
context: `${label}.operations.patch.body`,
|
|
48
|
-
defaultMode: "patch"
|
|
49
|
-
}), "object", `${label}.operations.patch.body.schema is required.`);
|
|
50
|
-
|
|
51
|
-
const requiredMetadata = deriveResourceRequiredMetadata(resource);
|
|
52
|
-
assert.ok(Array.isArray(requiredMetadata.create), `${label}.derivedRequired.create must be an array.`);
|
|
53
|
-
assert.ok(Array.isArray(requiredMetadata.replace), `${label}.derivedRequired.replace must be an array.`);
|
|
54
|
-
assert.ok(Array.isArray(requiredMetadata.patch), `${label}.derivedRequired.patch must be an array.`);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
test("console settings resources expose canonical validators", () => {
|
|
58
|
-
assertResourceShape(consoleSettingsResource, "consoleSettings");
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("console settings operations expose canonical validators", () => {
|
|
62
|
-
for (const operationName of ["view", "list", "create", "replace", "patch"]) {
|
|
63
|
-
const operation = consoleSettingsResource.operations?.[operationName];
|
|
64
|
-
assert.equal(typeof operation?.method, "string", `${operationName}.method must exist.`);
|
|
65
|
-
assert.equal(typeof resolveSchemaTransportSchemaDefinition(operation?.output, {
|
|
8
|
+
import { registerConsoleSettingsRoutes } from "../src/server/consoleSettings/consoleSettingsRoutes.js";
|
|
9
|
+
import { consoleSettingsContract } from "../src/shared/resources/consoleSettingsResource.js";
|
|
10
|
+
|
|
11
|
+
test("the console settings API contract serves view and patch, with the methods of the routes that serve them", () => {
|
|
12
|
+
const contract = consoleSettingsContract;
|
|
13
|
+
assert.equal(contract.namespace, "consoleSettings");
|
|
14
|
+
assert.deepEqual(Object.keys(contract.operations), ["view", "patch"]);
|
|
15
|
+
assert.equal(contract.operations.view.method, "GET");
|
|
16
|
+
assert.equal(contract.operations.patch.method, "PATCH");
|
|
17
|
+
for (const [operationName, operation] of Object.entries(contract.operations)) {
|
|
18
|
+
assert.equal(typeof operation.messages, "object", `${operationName}.messages must exist.`);
|
|
19
|
+
assert.equal(typeof resolveSchemaTransportSchemaDefinition(operation.output, {
|
|
66
20
|
context: `${operationName}.output`,
|
|
67
21
|
defaultMode: "replace"
|
|
68
22
|
}), "object", `${operationName}.output.schema must exist.`);
|
|
69
|
-
if (operation?.body) {
|
|
70
|
-
assert.equal(typeof resolveSchemaTransportSchemaDefinition(operation.body, {
|
|
71
|
-
context: `${operationName}.body`,
|
|
72
|
-
defaultMode: operationName === "create" ? "create" : operationName === "replace" ? "replace" : "patch"
|
|
73
|
-
}), "object", `${operationName}.body.schema must exist.`);
|
|
74
|
-
}
|
|
75
23
|
}
|
|
24
|
+
assert.equal(typeof resolveSchemaTransportSchemaDefinition(contract.operations.patch.body, {
|
|
25
|
+
context: "patch.body",
|
|
26
|
+
defaultMode: "patch"
|
|
27
|
+
}), "object", "patch.body.schema must exist.");
|
|
28
|
+
assert.equal(contract.operations.patch.body.mode, "patch");
|
|
29
|
+
assert.deepEqual(deriveResourceRequiredMetadata(contract).patch, []);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("each console settings route uses its contract operation's method", () => {
|
|
33
|
+
const registered = [];
|
|
34
|
+
registerConsoleSettingsRoutes({ register: (method, url, options) => registered.push({ method, url, options }) });
|
|
35
|
+
assert.deepEqual(registered.map(({ method, url }) => `${method} ${url}`), [
|
|
36
|
+
`${consoleSettingsContract.operations.view.method} /api/console/settings`,
|
|
37
|
+
`${consoleSettingsContract.operations.patch.method} /api/console/settings`
|
|
38
|
+
]);
|
|
76
39
|
});
|
|
77
40
|
|
|
78
41
|
test("console-core does not use workspaceRoutes.js helper path", () => {
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import knexLib from "knex";
|
|
4
|
+
import { requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
|
|
5
|
+
import { createTestResourceRuntime } from "@jskit-ai/resource-runtime/test";
|
|
6
|
+
import migration from "../migrations/console_core_generic_initial.cjs";
|
|
7
|
+
import { createRepository } from "../src/server/consoleSettings/consoleSettingsRepository.js";
|
|
8
|
+
import { consoleSettingsTableResource } from "../src/shared/resources/consoleSettingsTableResource.js";
|
|
9
|
+
import { createConsoleRuntime } from "../src/server/consoleRuntime.js";
|
|
10
|
+
|
|
11
|
+
// The console settings record on MariaDB, through the repository (the console.settings capability) and the console
|
|
12
|
+
// runtime's services built on it, pinned before the repository moves onto a table resource: the singleton's shape, the
|
|
13
|
+
// first owner winning, its refusals and errors, and joining a caller's transaction.
|
|
14
|
+
|
|
15
|
+
// The runtime's own MySQL connection settings (database-runtime's resolveKnexConnectionFromEnvironment).
|
|
16
|
+
const RUNTIME_MYSQL = Object.freeze({ supportBigNumbers: true, bigNumberStrings: true, dateStrings: ["DATE"], timezone: "Z" });
|
|
17
|
+
const ISO_SECONDS = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.000Z$/u;
|
|
18
|
+
const LONG_AGO = "2001-02-03T04:05:06.000Z";
|
|
19
|
+
|
|
20
|
+
/** A migrated database of the test's own, beside TEST_DB_NAME, dropped after the test. */
|
|
21
|
+
async function ownDatabase(context, suffix) {
|
|
22
|
+
const name = `${requireTestDatabase(process.env)}_${suffix}`;
|
|
23
|
+
const server = { host: process.env.DB_HOST, port: Number(process.env.DB_PORT) || 3306, user: process.env.DB_USER, password: process.env.DB_PASSWORD };
|
|
24
|
+
const admin = knexLib({ client: "mysql2", connection: server, pool: { min: 0, max: 1 } });
|
|
25
|
+
await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
|
|
26
|
+
await admin.raw("CREATE DATABASE ??", [name]);
|
|
27
|
+
const knex = knexLib({ client: "mysql2", connection: { ...server, database: name, ...RUNTIME_MYSQL }, pool: { min: 0, max: 4 } });
|
|
28
|
+
context.after(async () => {
|
|
29
|
+
await knex.destroy();
|
|
30
|
+
await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
|
|
31
|
+
await admin.destroy();
|
|
32
|
+
});
|
|
33
|
+
await migration.up(knex);
|
|
34
|
+
// Seeded long ago, so the repository's own updates of updated_at show.
|
|
35
|
+
await knex("console_settings").where({ id: 1 }).update({ updated_at: "2001-02-03 04:05:06" });
|
|
36
|
+
return knex;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The repository on the console feature's handle of the record, over the test's database. */
|
|
40
|
+
async function repositoryOn(knex) {
|
|
41
|
+
const runtime = await createTestResourceRuntime({ knex });
|
|
42
|
+
const { consoleSettings } = await runtime.own("console.core", [consoleSettingsTableResource]);
|
|
43
|
+
return createRepository({ consoleSettings });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The current second, as the second-precision timestamps keep it. */
|
|
47
|
+
const thisSecond = () => Math.floor(Date.now() / 1000) * 1000;
|
|
48
|
+
|
|
49
|
+
test("the singleton answers its id, owner, empty settings and ISO timestamps", async (context) => {
|
|
50
|
+
requireTestDatabase(process.env);
|
|
51
|
+
const repository = await repositoryOn(await ownDatabase(context, "da_console_shape"));
|
|
52
|
+
assert.ok(Object.isFrozen(repository));
|
|
53
|
+
assert.deepEqual(Object.keys(repository), ["withTransaction", "getSingleton", "ensureOwnerUserId", "updateSingleton"]);
|
|
54
|
+
|
|
55
|
+
const singleton = await repository.getSingleton();
|
|
56
|
+
assert.deepEqual(Object.keys(singleton), ["id", "ownerUserId", "settings", "createdAt", "updatedAt"]);
|
|
57
|
+
assert.equal(singleton.id, "1");
|
|
58
|
+
assert.equal(singleton.ownerUserId, null);
|
|
59
|
+
assert.deepEqual(singleton.settings, {});
|
|
60
|
+
assert.match(singleton.createdAt, ISO_SECONDS);
|
|
61
|
+
assert.equal(singleton.updatedAt, LONG_AGO);
|
|
62
|
+
assert.notEqual((await repository.getSingleton()).settings, singleton.settings, "each read answers new settings");
|
|
63
|
+
|
|
64
|
+
const before = thisSecond();
|
|
65
|
+
const updated = await repository.updateSingleton();
|
|
66
|
+
assert.deepEqual(Object.keys(updated), ["id", "ownerUserId", "settings", "createdAt", "updatedAt"]);
|
|
67
|
+
assert.deepEqual({ ...updated, updatedAt: "" }, { ...singleton, updatedAt: "" }, "an update only touches updated_at");
|
|
68
|
+
assert.match(updated.updatedAt, ISO_SECONDS);
|
|
69
|
+
// A server that rounds fractional seconds, rather than cut them, may store the next second.
|
|
70
|
+
assert.ok(Date.parse(updated.updatedAt) >= before && Date.parse(updated.updatedAt) <= Date.now() + 1000, updated.updatedAt);
|
|
71
|
+
assert.deepEqual(await repository.getSingleton(), updated);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("the first owner wins, the same answer comes back after, and anything but a positive id is refused", async (context) => {
|
|
75
|
+
requireTestDatabase(process.env);
|
|
76
|
+
const repository = await repositoryOn(await ownDatabase(context, "da_console_owner"));
|
|
77
|
+
|
|
78
|
+
for (const userId of [0, -1, 1.5, "abc", "007", "", null, undefined, Number.MAX_SAFE_INTEGER + 1]) {
|
|
79
|
+
await assert.rejects(repository.ensureOwnerUserId(userId), {
|
|
80
|
+
name: "TypeError",
|
|
81
|
+
message: "consoleSettingsRepository.ensureOwnerUserId requires a positive user id."
|
|
82
|
+
}, String(userId));
|
|
83
|
+
}
|
|
84
|
+
assert.equal((await repository.getSingleton()).ownerUserId, null);
|
|
85
|
+
assert.equal((await repository.getSingleton()).updatedAt, LONG_AGO, "a refused id writes nothing");
|
|
86
|
+
|
|
87
|
+
const before = thisSecond();
|
|
88
|
+
assert.equal(await repository.ensureOwnerUserId(7), "7", "the id comes back as text");
|
|
89
|
+
const owned = await repository.getSingleton();
|
|
90
|
+
assert.equal(owned.ownerUserId, "7");
|
|
91
|
+
assert.ok(Date.parse(owned.updatedAt) >= before, "setting the owner touches updated_at");
|
|
92
|
+
|
|
93
|
+
assert.equal(await repository.ensureOwnerUserId(9), "7", "a later user gets the first owner back");
|
|
94
|
+
assert.equal(await repository.ensureOwnerUserId("7"), "7");
|
|
95
|
+
assert.equal(await repository.ensureOwnerUserId(" 7 "), "7");
|
|
96
|
+
assert.deepEqual(await repository.getSingleton(), owned, "and nothing changes");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("two first owners at once both get the same winner", async (context) => {
|
|
100
|
+
requireTestDatabase(process.env);
|
|
101
|
+
const repository = await repositoryOn(await ownDatabase(context, "da_console_race"));
|
|
102
|
+
const answers = await Promise.all([repository.ensureOwnerUserId(3), repository.ensureOwnerUserId(4)]);
|
|
103
|
+
assert.equal(answers[0], answers[1]);
|
|
104
|
+
assert.ok(["3", "4"].includes(answers[0]));
|
|
105
|
+
assert.equal((await repository.getSingleton()).ownerUserId, answers[0]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("the repository joins its caller's transaction: a rollback undoes the owner and the touch", async (context) => {
|
|
109
|
+
requireTestDatabase(process.env);
|
|
110
|
+
const repository = await repositoryOn(await ownDatabase(context, "da_console_trx"));
|
|
111
|
+
|
|
112
|
+
const failure = new Error("rolled back");
|
|
113
|
+
await assert.rejects(repository.withTransaction({}, async (txOptions) => {
|
|
114
|
+
assert.equal(await repository.ensureOwnerUserId(" 5 ", txOptions), "5");
|
|
115
|
+
assert.equal((await repository.getSingleton(txOptions)).ownerUserId, "5", "seen inside the transaction");
|
|
116
|
+
assert.equal((await repository.getSingleton()).ownerUserId, null, "not outside it before the commit");
|
|
117
|
+
assert.notEqual((await repository.updateSingleton(txOptions)).updatedAt, LONG_AGO);
|
|
118
|
+
throw failure;
|
|
119
|
+
}), (error) => error === failure);
|
|
120
|
+
const after = await repository.getSingleton();
|
|
121
|
+
assert.equal(after.ownerUserId, null);
|
|
122
|
+
assert.equal(after.updatedAt, LONG_AGO);
|
|
123
|
+
|
|
124
|
+
assert.equal(await repository.withTransaction({}, async (txOptions) => repository.ensureOwnerUserId(6, txOptions)), "6", "withTransaction answers the work's value");
|
|
125
|
+
assert.equal((await repository.getSingleton({ trx: null })).ownerUserId, "6", "kept once committed; a null trx reads outside one");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("without its row, every call fails with the same plain error", async (context) => {
|
|
129
|
+
requireTestDatabase(process.env);
|
|
130
|
+
const knex = await ownDatabase(context, "da_console_missing");
|
|
131
|
+
const repository = await repositoryOn(knex);
|
|
132
|
+
await knex("console_settings").where({ id: 1 }).del();
|
|
133
|
+
const missing = (error) => {
|
|
134
|
+
assert.equal(error.constructor, Error);
|
|
135
|
+
assert.equal(error.message, "console_settings singleton row is missing.");
|
|
136
|
+
assert.equal(error.code, undefined);
|
|
137
|
+
return true;
|
|
138
|
+
};
|
|
139
|
+
await assert.rejects(repository.getSingleton(), missing);
|
|
140
|
+
await assert.rejects(repository.ensureOwnerUserId(1), missing);
|
|
141
|
+
await assert.rejects(repository.updateSingleton(), missing);
|
|
142
|
+
assert.equal(await knex("console_settings").count({ rows: "*" }).first().then((row) => Number(row.rows)), 0, "nothing creates the row again");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("the console runtime's services make the first signed-in user the owner and refuse everyone else", async (context) => {
|
|
146
|
+
requireTestDatabase(process.env);
|
|
147
|
+
const repository = await repositoryOn(await ownDatabase(context, "da_console_runtime"));
|
|
148
|
+
const { services } = createConsoleRuntime({ consoleSettingsRepository: repository });
|
|
149
|
+
const refused = (status, code, message) => (error) => {
|
|
150
|
+
assert.equal(error.name, "AppError");
|
|
151
|
+
assert.equal(error.status, status);
|
|
152
|
+
assert.equal(error.statusCode, status);
|
|
153
|
+
assert.equal(error.code, code);
|
|
154
|
+
assert.equal(error.message, message);
|
|
155
|
+
return true;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
assert.equal(await services.access.isConsoleOwnerUserId(7), false, "no owner yet");
|
|
159
|
+
await assert.rejects(services.settings.getSettings({ context: {} }), refused(401, "console.owner.authenticationRequired", "Authentication required."));
|
|
160
|
+
assert.equal((await repository.getSingleton()).ownerUserId, null);
|
|
161
|
+
await assert.rejects(services.access.ensureInitialConsoleMember("someone"), refused(400, "console.owner.invalidUser", "Invalid console user."));
|
|
162
|
+
|
|
163
|
+
const before = thisSecond();
|
|
164
|
+
assert.deepEqual((await services.settings.getSettings({ context: { actor: { id: 7 } } })).value, { settings: {} });
|
|
165
|
+
assert.equal((await repository.getSingleton()).ownerUserId, "7", "the first signed-in user became the owner");
|
|
166
|
+
assert.equal(await services.access.isConsoleOwnerUserId("7"), true);
|
|
167
|
+
assert.equal(await services.access.isConsoleOwnerUserId(8), false);
|
|
168
|
+
assert.equal(await services.access.isConsoleOwnerUserId("not-an-id"), false);
|
|
169
|
+
|
|
170
|
+
await assert.rejects(services.settings.updateSettings({ context: { actor: { id: 8 } } }), refused(403, "console.owner.forbidden", "Forbidden."));
|
|
171
|
+
assert.equal((await repository.getSingleton()).ownerUserId, "7");
|
|
172
|
+
// The request's user counts when the context has no actor.
|
|
173
|
+
assert.deepEqual((await services.settings.updateSettings({ context: { requestMeta: { request: { user: { id: "7" } } } } })).value, { settings: {} });
|
|
174
|
+
assert.ok(Date.parse((await repository.getSingleton()).updatedAt) >= before, "an update touches the record");
|
|
175
|
+
assert.equal(await services.access.ensureInitialConsoleMember(9), "7");
|
|
176
|
+
});
|
|
@@ -13,15 +13,18 @@ test("ConsoleFeature assembles owner access, settings, routes, actions, and boot
|
|
|
13
13
|
const bootstrap = createBootstrapRuntime();
|
|
14
14
|
let actions = null;
|
|
15
15
|
let console = null;
|
|
16
|
+
let consoleSettings = null;
|
|
16
17
|
const probe = defineProvider({
|
|
17
18
|
id: "test.console.probe",
|
|
18
19
|
requires: {
|
|
19
20
|
actionCatalogue: "runtime.actions",
|
|
20
|
-
consoleCapability: "console.core"
|
|
21
|
+
consoleCapability: "console.core",
|
|
22
|
+
settingsCapability: "console.settings"
|
|
21
23
|
},
|
|
22
|
-
setup({ actionCatalogue, consoleCapability }) {
|
|
24
|
+
setup({ actionCatalogue, consoleCapability, settingsCapability }) {
|
|
23
25
|
actions = actionCatalogue;
|
|
24
26
|
console = consoleCapability;
|
|
27
|
+
consoleSettings = settingsCapability;
|
|
25
28
|
return {};
|
|
26
29
|
}
|
|
27
30
|
});
|
|
@@ -30,7 +33,12 @@ test("ConsoleFeature assembles owner access, settings, routes, actions, and boot
|
|
|
30
33
|
"runtime.logger": silentLogger,
|
|
31
34
|
"auth.extensions": authExtensions,
|
|
32
35
|
"runtime.bootstrap": bootstrap,
|
|
33
|
-
|
|
36
|
+
// The console feature's handle is only kept by its repository, so a resource runtime that hands out stubs will do.
|
|
37
|
+
"runtime.resources": {
|
|
38
|
+
own(featureId, owns) {
|
|
39
|
+
return Object.fromEntries(owns.map((entry) => [(entry.resource ?? entry).name, {}]));
|
|
40
|
+
}
|
|
41
|
+
},
|
|
34
42
|
"runtime.http": {
|
|
35
43
|
router: {
|
|
36
44
|
register(method, path) {
|
|
@@ -46,6 +54,8 @@ test("ConsoleFeature assembles owner access, settings, routes, actions, and boot
|
|
|
46
54
|
|
|
47
55
|
assert.equal(typeof console.services.access.requireConsoleOwner, "function");
|
|
48
56
|
assert.equal(typeof console.services.settings.updateSettings, "function");
|
|
57
|
+
assert.equal(typeof consoleSettings.getSingleton, "function");
|
|
58
|
+
assert.equal(Object.hasOwn(console, "repositories"), false);
|
|
49
59
|
assert.deepEqual(routes, [
|
|
50
60
|
"GET /api/console/settings",
|
|
51
61
|
"PATCH /api/console/settings"
|
|
@@ -2,27 +2,53 @@ import assert from "node:assert/strict";
|
|
|
2
2
|
import test from "node:test";
|
|
3
3
|
import { createRepository as createConsoleSettingsRepository } from "../src/server/consoleSettings/consoleSettingsRepository.js";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return work({ trxId: "trx-1" });
|
|
5
|
+
// A console settings handle whose transaction hands its work the options with the transaction.
|
|
6
|
+
function createHandleStub() {
|
|
7
|
+
return {
|
|
8
|
+
async withTransaction(options, work) {
|
|
9
|
+
return work({ ...options, trx: { trxId: "trx-1" } });
|
|
11
10
|
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return knex;
|
|
11
|
+
};
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
test("console-core repositories expose withTransaction", async () => {
|
|
18
|
-
const knex = createKnexStub();
|
|
19
15
|
const repositories = [
|
|
20
|
-
createConsoleSettingsRepository({
|
|
16
|
+
createConsoleSettingsRepository({ consoleSettings: createHandleStub() })
|
|
21
17
|
];
|
|
22
18
|
|
|
23
19
|
for (const repository of repositories) {
|
|
24
20
|
assert.equal(typeof repository.withTransaction, "function");
|
|
25
|
-
const result = await repository.withTransaction(async (
|
|
21
|
+
const result = await repository.withTransaction({}, async (txOptions) => ({ id: txOptions.trx.trxId }));
|
|
26
22
|
assert.deepEqual(result, { id: "trx-1" });
|
|
27
23
|
}
|
|
28
24
|
});
|
|
25
|
+
|
|
26
|
+
test("console.settings reads and writes with only the caller's context and transaction", async () => {
|
|
27
|
+
const calls = [];
|
|
28
|
+
const record = { id: "1", ownerUserId: null, createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-01T00:00:00.000Z" };
|
|
29
|
+
const consoleSettings = {
|
|
30
|
+
async get(id, params, options) {
|
|
31
|
+
calls.push(["get", options]);
|
|
32
|
+
return { ...record };
|
|
33
|
+
},
|
|
34
|
+
async patch(id, changes, options) {
|
|
35
|
+
calls.push(["patch", options]);
|
|
36
|
+
Object.assign(record, changes);
|
|
37
|
+
return { ...record };
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const repository = createConsoleSettingsRepository({ consoleSettings });
|
|
41
|
+
const trx = { trxId: "trx-1" };
|
|
42
|
+
const context = { actor: { id: "7" } };
|
|
43
|
+
// Settings a caller named for its own call (a format, a lock version, a returning) never reach these reads and writes.
|
|
44
|
+
const callerOptions = { context, trx, format: "jsonapi", lockVersion: 3, returning: "records" };
|
|
45
|
+
assert.equal(await repository.ensureOwnerUserId("7", callerOptions), "7");
|
|
46
|
+
await repository.updateSingleton(callerOptions);
|
|
47
|
+
assert.deepEqual(calls, [
|
|
48
|
+
["get", { context, trx, missing: "skip" }],
|
|
49
|
+
["patch", { context, trx, onlyIf: { ownerUserId: null }, missing: "skip", returning: "none" }],
|
|
50
|
+
["get", { context, trx, missing: "skip" }],
|
|
51
|
+
["get", { context, trx, missing: "skip" }],
|
|
52
|
+
["patch", { context, trx }]
|
|
53
|
+
]);
|
|
54
|
+
});
|