@jskit-ai/rewarded-core 0.2.12 → 0.2.13
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/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/rewarded-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"test": "
|
|
6
|
+
"test": "jskit test"
|
|
7
7
|
},
|
|
8
8
|
"exports": {
|
|
9
9
|
"./shared": "./src/shared/index.js",
|
|
@@ -125,17 +125,17 @@
|
|
|
125
125
|
}
|
|
126
126
|
},
|
|
127
127
|
"dependencies": {
|
|
128
|
-
"@jskit-ai/crud-core": "0.2.
|
|
129
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
130
|
-
"@jskit-ai/json-rest-api-core": "0.2.
|
|
131
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
132
|
-
"@jskit-ai/users-core": "0.2.
|
|
133
|
-
"@jskit-ai/workspaces-core": "0.2.
|
|
128
|
+
"@jskit-ai/crud-core": "0.2.12",
|
|
129
|
+
"@jskit-ai/database-runtime": "0.1.260",
|
|
130
|
+
"@jskit-ai/json-rest-api-core": "0.2.12",
|
|
131
|
+
"@jskit-ai/resource-crud-core": "0.1.202",
|
|
132
|
+
"@jskit-ai/users-core": "0.2.13",
|
|
133
|
+
"@jskit-ai/workspaces-core": "0.2.13",
|
|
134
134
|
"json-rest-schema": "^1.0.17"
|
|
135
135
|
},
|
|
136
136
|
"peerDependencies": {
|
|
137
|
-
"@jskit-ai/auth-core": "0.2.
|
|
138
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
139
|
-
"@jskit-ai/kernel": "0.2.
|
|
137
|
+
"@jskit-ai/auth-core": "0.2.11",
|
|
138
|
+
"@jskit-ai/http-runtime": "0.2.11",
|
|
139
|
+
"@jskit-ai/kernel": "0.2.11"
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -1,35 +1,17 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import knexLib from "knex";
|
|
4
3
|
import providerConfigsMigration from "../migrations/rewarded_provider_configs_initial.cjs";
|
|
5
4
|
import rulesMigration from "../migrations/rewarded_rules_initial.cjs";
|
|
6
5
|
import unlockReceiptsMigration from "../migrations/rewarded_unlock_receipts_initial.cjs";
|
|
7
6
|
import watchSessionsMigration from "../migrations/rewarded_watch_sessions_initial.cjs";
|
|
8
7
|
import foreignKeysMigration from "../migrations/constraints/rewarded_foreign_keys.cjs";
|
|
8
|
+
import { openOwnDatabase } from "./mysqlDatabase.js";
|
|
9
9
|
|
|
10
10
|
// Knex's filename order within rewarded-core's migrations/ directory.
|
|
11
11
|
const TABLE_MIGRATIONS = [providerConfigsMigration, rulesMigration, unlockReceiptsMigration, watchSessionsMigration];
|
|
12
12
|
const TABLES = ["rewarded_provider_configs", "rewarded_rules", "rewarded_unlock_receipts", "rewarded_watch_sessions"];
|
|
13
13
|
const databaseUrl = process.env.REWARDED_TEST_DATABASE_URL;
|
|
14
14
|
|
|
15
|
-
function databaseOptions() {
|
|
16
|
-
const url = new URL(databaseUrl);
|
|
17
|
-
assert.equal(url.protocol, "mysql:", "Rewarded shipped migrations require MySQL.");
|
|
18
|
-
assert.match(url.pathname, /^\/jskit_rewarded_test_[a-z0-9_]+$/u, "Use an owned disposable rewarded test database.");
|
|
19
|
-
return {
|
|
20
|
-
client: "mysql2",
|
|
21
|
-
connection: {
|
|
22
|
-
host: url.hostname,
|
|
23
|
-
port: Number(url.port) || 3306,
|
|
24
|
-
user: decodeURIComponent(url.username),
|
|
25
|
-
password: decodeURIComponent(url.password),
|
|
26
|
-
database: url.pathname.slice(1),
|
|
27
|
-
...(url.searchParams.has("socketPath") ? { socketPath: url.searchParams.get("socketPath") } : {})
|
|
28
|
-
},
|
|
29
|
-
pool: { min: 1, max: 1 }
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
15
|
async function foreignKeys(knex) {
|
|
34
16
|
return knex("information_schema.REFERENTIAL_CONSTRAINTS")
|
|
35
17
|
.whereRaw("CONSTRAINT_SCHEMA = DATABASE()")
|
|
@@ -57,7 +39,7 @@ const ALL_KEYS = [
|
|
|
57
39
|
test("a fresh MySQL database migrates rewarded-core before users and workspaces exist, then adds its keys", {
|
|
58
40
|
skip: databaseUrl ? false : "REWARDED_TEST_DATABASE_URL (MySQL) is required: the shipped migrations use MySQL defaults."
|
|
59
41
|
}, async (t) => {
|
|
60
|
-
const knex =
|
|
42
|
+
const { knex, dropDatabase } = await openOwnDatabase(databaseUrl, "keys");
|
|
61
43
|
let ownsTables = false;
|
|
62
44
|
t.after(async () => {
|
|
63
45
|
try {
|
|
@@ -69,6 +51,7 @@ test("a fresh MySQL database migrates rewarded-core before users and workspaces
|
|
|
69
51
|
}
|
|
70
52
|
} finally {
|
|
71
53
|
await knex.destroy();
|
|
54
|
+
await dropDatabase();
|
|
72
55
|
}
|
|
73
56
|
});
|
|
74
57
|
for (const table of [...TABLES, "users", "workspaces"]) {
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import knexLib from "knex";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A MySQL database of the test file's own beside REWARDED_TEST_DATABASE_URL's, named after it with the file's
|
|
6
|
+
* suffix, so the package's database files can run in parallel. dropDatabase() removes it after the test.
|
|
7
|
+
* @param {string} databaseUrl
|
|
8
|
+
* @param {string} suffix
|
|
9
|
+
* @param {Record<string, unknown>} [connectionOptions]
|
|
10
|
+
*/
|
|
11
|
+
async function openOwnDatabase(databaseUrl, suffix, connectionOptions = {}) {
|
|
12
|
+
const url = new URL(databaseUrl);
|
|
13
|
+
assert.equal(url.protocol, "mysql:", "Rewarded shipped migrations require MySQL.");
|
|
14
|
+
assert.match(url.pathname, /^\/jskit_rewarded_test_[a-z0-9_]+$/u, "Use an owned disposable rewarded test database.");
|
|
15
|
+
const server = {
|
|
16
|
+
host: url.hostname,
|
|
17
|
+
port: Number(url.port) || 3306,
|
|
18
|
+
user: decodeURIComponent(url.username),
|
|
19
|
+
password: decodeURIComponent(url.password),
|
|
20
|
+
...(url.searchParams.has("socketPath") ? { socketPath: url.searchParams.get("socketPath") } : {}),
|
|
21
|
+
...connectionOptions
|
|
22
|
+
};
|
|
23
|
+
const database = `${url.pathname.slice(1)}_${suffix}`;
|
|
24
|
+
const admin = knexLib({ client: "mysql2", connection: { ...server, database: url.pathname.slice(1) }, pool: { min: 1, max: 1 } });
|
|
25
|
+
await admin.raw("DROP DATABASE IF EXISTS ??", [database]);
|
|
26
|
+
await admin.raw("CREATE DATABASE ??", [database]);
|
|
27
|
+
const knex = knexLib({ client: "mysql2", connection: { ...server, database }, pool: { min: 1, max: 1 } });
|
|
28
|
+
async function dropDatabase() {
|
|
29
|
+
try {
|
|
30
|
+
await admin.raw("DROP DATABASE IF EXISTS ??", [database]);
|
|
31
|
+
} finally {
|
|
32
|
+
await admin.destroy();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return { knex, dropDatabase };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { openOwnDatabase };
|
|
@@ -18,6 +18,7 @@ import { resource as rulesResource } from "../src/shared/rewardedRuleResource.js
|
|
|
18
18
|
import { resource as watchSessionsResource } from "../src/shared/rewardedWatchSessionResource.js";
|
|
19
19
|
import { resource as unlockReceiptsResource } from "../src/shared/rewardedUnlockReceiptResource.js";
|
|
20
20
|
import { createService } from "../src/server/service.js";
|
|
21
|
+
import { openOwnDatabase } from "./mysqlDatabase.js";
|
|
21
22
|
|
|
22
23
|
const RESOURCES = [
|
|
23
24
|
["rewardedProviderConfigs", providerConfigsResource, providerConfigsMigration],
|
|
@@ -27,32 +28,15 @@ const RESOURCES = [
|
|
|
27
28
|
];
|
|
28
29
|
const databaseUrl = process.env.REWARDED_TEST_DATABASE_URL;
|
|
29
30
|
|
|
30
|
-
function
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
pool: { min: 1, max: 1 }
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const url = new URL(databaseUrl);
|
|
41
|
-
assert.equal(url.protocol, "mysql:", "Rewarded shipped migrations require MySQL.");
|
|
42
|
-
assert.match(url.pathname, /^\/jskit_rewarded_test_[a-z0-9_]+$/u, "Use an owned disposable rewarded test database.");
|
|
43
|
-
return {
|
|
44
|
-
client: "mysql2",
|
|
45
|
-
connection: {
|
|
46
|
-
host: url.hostname,
|
|
47
|
-
port: Number(url.port) || 3306,
|
|
48
|
-
user: decodeURIComponent(url.username),
|
|
49
|
-
password: decodeURIComponent(url.password),
|
|
50
|
-
database: url.pathname.slice(1),
|
|
51
|
-
...(url.searchParams.has("socketPath") ? { socketPath: url.searchParams.get("socketPath") } : {}),
|
|
52
|
-
timezone: "Z"
|
|
53
|
-
},
|
|
31
|
+
async function openDatabase() {
|
|
32
|
+
if (databaseUrl) return openOwnDatabase(databaseUrl, "service", { timezone: "Z" });
|
|
33
|
+
const knex = knexLib({
|
|
34
|
+
client: "better-sqlite3",
|
|
35
|
+
connection: { filename: ":memory:" },
|
|
36
|
+
useNullAsDefault: true,
|
|
54
37
|
pool: { min: 1, max: 1 }
|
|
55
|
-
};
|
|
38
|
+
});
|
|
39
|
+
return { knex, dropDatabase: async () => {} };
|
|
56
40
|
}
|
|
57
41
|
|
|
58
42
|
function actorContext(workspaceId = "1", userId = "7", testCase = "") {
|
|
@@ -64,7 +48,7 @@ function actorContext(workspaceId = "1", userId = "7", testCase = "") {
|
|
|
64
48
|
}
|
|
65
49
|
|
|
66
50
|
test(`rewarded grants use real resources and atomic transactions (${databaseUrl ? "MySQL shipped migrations" : "SQLite resource schema"})`, async (t) => {
|
|
67
|
-
const knex =
|
|
51
|
+
const { knex, dropDatabase } = await openDatabase();
|
|
68
52
|
let ownsTables = false;
|
|
69
53
|
t.after(async () => {
|
|
70
54
|
try {
|
|
@@ -77,6 +61,7 @@ test(`rewarded grants use real resources and atomic transactions (${databaseUrl
|
|
|
77
61
|
}
|
|
78
62
|
} finally {
|
|
79
63
|
await knex.destroy();
|
|
64
|
+
await dropDatabase();
|
|
80
65
|
}
|
|
81
66
|
});
|
|
82
67
|
const tables = [...RESOURCES.map(([, resource]) => resource.tableName), "users", "workspaces"];
|