@jskit-ai/jskit-catalog 0.2.83 → 0.2.84
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/catalog/packages.json +267 -266
- package/package.json +1 -1
- package/scripts/browser-test-servers.mjs +5 -1
- package/scripts/nix-app.mjs +41 -14
- package/scripts/package-nix.mjs +4 -2
- package/scripts/test-command.mjs +3 -0
- package/scripts/test-environment.mjs +23 -0
package/package.json
CHANGED
|
@@ -17,6 +17,7 @@ import path from "node:path";
|
|
|
17
17
|
import process from "node:process";
|
|
18
18
|
import { pathToFileURL } from "node:url";
|
|
19
19
|
import { buildFrontend } from "./browser-test-build.mjs";
|
|
20
|
+
import { useTestAppEnvironment } from "./test-environment.mjs";
|
|
20
21
|
|
|
21
22
|
const SETTINGS_VARIABLE = "JSKIT_BROWSER_TESTS";
|
|
22
23
|
const LOG_DIRECTORY = path.join("test-results", "browser-test-servers");
|
|
@@ -123,6 +124,8 @@ const {
|
|
|
123
124
|
workers, host, basePort, server, build, prepare, env, health, workerEnvironments: workerEnvironmentsFile, signIn,
|
|
124
125
|
signedInStates
|
|
125
126
|
} = JSON.parse(process.env[SETTINGS_VARIABLE] || "{}");
|
|
127
|
+
// The servers, and the databases prepared for them, are APP_ENV=test.
|
|
128
|
+
useTestAppEnvironment(process.env, "jskit browser tests");
|
|
126
129
|
const baseEnvironment = { ...process.env };
|
|
127
130
|
delete baseEnvironment[SETTINGS_VARIABLE];
|
|
128
131
|
const baseUrls = Array.from({ length: workers }, (_, index) => `http://${host}:${basePort + index}`);
|
|
@@ -142,7 +145,8 @@ try {
|
|
|
142
145
|
...workerEnvironment,
|
|
143
146
|
HOST: host,
|
|
144
147
|
PORT: String(basePort + index),
|
|
145
|
-
TEST_PARALLEL_INDEX: String(index)
|
|
148
|
+
TEST_PARALLEL_INDEX: String(index),
|
|
149
|
+
APP_ENV: "test"
|
|
146
150
|
}));
|
|
147
151
|
await Promise.all(serverEnvironments.map((serverEnvironment, index) => {
|
|
148
152
|
const baseUrl = baseUrls[index];
|
package/scripts/nix-app.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// The programs of a JSKIT app that `jskit package nix` packaged. The package.nix it writes embeds this file whole, so
|
|
2
2
|
// it imports only Node's own modules. The package's bin/ runs it with the app's folder (--app) and a shell (--shell):
|
|
3
3
|
//
|
|
4
|
-
// <app> [--env FILE] [--migrate] serve: the app's start script, in place of this process
|
|
4
|
+
// <app> [--env FILE] [--migrate] serve: the app's start script, in place of this process; with --migrate, its
|
|
5
|
+
// db:migrate script first when it has one
|
|
5
6
|
// <app>-migrate [--env FILE] migrate: its db:migrate script, in place of this process
|
|
6
7
|
// <app>-check [--env FILE] check: exits 0 once GET /api/health answers 200, 1 after --wait seconds (60)
|
|
7
8
|
// <app>-prepare --settings JSON --env FILE [--user NAME]
|
|
8
|
-
// prepare: the settings, one JSON object, written as FILE for NAME alone
|
|
9
|
+
// prepare: the settings, one JSON object, written as FILE for NAME alone, and the
|
|
10
|
+
// folders they name for the app's files made for NAME
|
|
9
11
|
//
|
|
10
12
|
// A script runs as `npm run` would run it, with no npm: from the app's folder, by the shell, with node_modules/.bin
|
|
11
13
|
// and this Node first on PATH, NODE_ENV=production unless the environment or the env file says otherwise, and the
|
|
@@ -27,6 +29,10 @@ const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
|
|
27
29
|
const QUOTES = Object.freeze(["'", "`", "\""]);
|
|
28
30
|
const HEALTH_PATH = "/api/health";
|
|
29
31
|
const MODES = Object.freeze(["serve", "migrate", "check", "prepare", "npm-cache"]);
|
|
32
|
+
// The settings that name a folder a JSKIT app writes its files in (uploads, local and OIDC sign-in's files), each kept
|
|
33
|
+
// under the app's own read-only folder unless a setting names another. prepare makes each one given as an absolute
|
|
34
|
+
// path, for the user the app runs as.
|
|
35
|
+
const FILE_FOLDERS = Object.freeze(["JSKIT_STORAGE_FS_BASE_PATH", "AUTH_LOCAL_STORE_DIR", "AUTH_OIDC_STORE_DIR"]);
|
|
30
36
|
|
|
31
37
|
class Refusal extends Error {}
|
|
32
38
|
|
|
@@ -60,19 +66,29 @@ function scriptEnvironment({ app, envFile }) {
|
|
|
60
66
|
return env;
|
|
61
67
|
}
|
|
62
68
|
|
|
63
|
-
|
|
69
|
+
/** The app's script `name`; when it has none, null if `optional`, else a refusal. */
|
|
70
|
+
function scriptOf(app, name, { optional = false } = {}) {
|
|
64
71
|
const manifest = JSON.parse(readFileSync(path.join(app, "package.json"), "utf8"));
|
|
65
72
|
const script = manifest.scripts?.[name];
|
|
66
|
-
if (typeof script
|
|
67
|
-
return
|
|
73
|
+
if (typeof script === "string" && script.trim()) return script;
|
|
74
|
+
if (optional) return null;
|
|
75
|
+
throw new Refusal(`${manifest.name ?? app} has no ${name} script.`);
|
|
68
76
|
}
|
|
69
77
|
|
|
70
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Runs the app's script `name` in place of this process; with `first` (db:migrate), runs that script before it, when
|
|
80
|
+
* the app has one, and stops on its failure. An app without it has nothing to migrate, so it starts: one service
|
|
81
|
+
* command then fits every app.
|
|
82
|
+
*/
|
|
71
83
|
function replaceWithScript({ app, shell, envFile, name, first }) {
|
|
72
84
|
const env = scriptEnvironment({ app, envFile });
|
|
73
85
|
const script = scriptOf(app, name);
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
const firstScript = first ? scriptOf(app, first, { optional: true }) : null;
|
|
87
|
+
if (first && firstScript === null) {
|
|
88
|
+
const { name: appName } = JSON.parse(readFileSync(path.join(app, "package.json"), "utf8"));
|
|
89
|
+
process.stderr.write(`${appName ?? app} has no ${first} script, so there is nothing to migrate: starting it.\n`);
|
|
90
|
+
} else if (first) {
|
|
91
|
+
const ran = spawnSync(shell, ["-c", firstScript], { cwd: app, env: { ...env, npm_lifecycle_event: first }, stdio: "inherit" });
|
|
76
92
|
if (ran.error) throw ran.error;
|
|
77
93
|
if (ran.status !== 0) return ran.status ?? 1;
|
|
78
94
|
}
|
|
@@ -106,18 +122,29 @@ async function check({ envFile, wait }) {
|
|
|
106
122
|
return 1;
|
|
107
123
|
}
|
|
108
124
|
|
|
125
|
+
/** Gives `file` to `user` with the host's own chown, which finds the user through whichever account databases it keeps. */
|
|
126
|
+
function chownTo(user, file) {
|
|
127
|
+
const owned = spawnSync("chown", ["--", `${user}:`, file], { encoding: "utf8" });
|
|
128
|
+
if (owned.error || owned.status !== 0) throw new Refusal(`chown ${user}: ${owned.stderr || owned.error?.message}`.trim());
|
|
129
|
+
}
|
|
130
|
+
|
|
109
131
|
function prepare({ settings, envFile, user }) {
|
|
110
132
|
if (!settings || !envFile) throw new Refusal("prepare needs --settings FILE and --env FILE.");
|
|
111
|
-
const
|
|
133
|
+
const values = JSON.parse(readFileSync(settings, "utf8"));
|
|
134
|
+
const text = formatEnvFile(values);
|
|
135
|
+
// A host runs prepare as root before the app's services start, so the user they run as can write where the
|
|
136
|
+
// settings keep the app's files (a host's data folder is its own until then). A relative folder is the app's own.
|
|
137
|
+
for (const name of FILE_FOLDERS) {
|
|
138
|
+
const folder = values[name];
|
|
139
|
+
if (typeof folder !== "string" || !path.isAbsolute(folder)) continue;
|
|
140
|
+
mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
141
|
+
if (user) chownTo(user, folder);
|
|
142
|
+
}
|
|
112
143
|
mkdirSync(path.dirname(envFile), { recursive: true });
|
|
113
144
|
const temporary = `${envFile}.${process.pid}.tmp`;
|
|
114
145
|
try {
|
|
115
146
|
writeFileSync(temporary, text, { mode: 0o600 });
|
|
116
|
-
if (user)
|
|
117
|
-
// The host's own chown names the user, through whichever account databases the host keeps.
|
|
118
|
-
const owned = spawnSync("chown", ["--", `${user}:`, temporary], { encoding: "utf8" });
|
|
119
|
-
if (owned.error || owned.status !== 0) throw new Refusal(`chown ${user}: ${owned.stderr || owned.error?.message}`.trim());
|
|
120
|
-
}
|
|
147
|
+
if (user) chownTo(user, temporary);
|
|
121
148
|
renameSync(temporary, envFile);
|
|
122
149
|
} finally {
|
|
123
150
|
rmSync(temporary, { force: true });
|
package/scripts/package-nix.mjs
CHANGED
|
@@ -110,9 +110,11 @@ async function analyseApp(projectRoot) {
|
|
|
110
110
|
.map((entry) => `${entry.integrity} ${entry.resolved}`));
|
|
111
111
|
const name = nixPackageName(app.name ?? "");
|
|
112
112
|
const programs = [
|
|
113
|
-
{ name, does: "[--env FILE] [--migrate]: the start script, from the app's folder, NODE_ENV=production by default"
|
|
113
|
+
{ name, does: "[--env FILE] [--migrate]: the start script, from the app's folder, NODE_ENV=production by default; "
|
|
114
|
+
+ "with --migrate, db:migrate first when the app has one" },
|
|
114
115
|
{ name: `${name}-check`, does: "[--env FILE]: exits 0 once GET /api/health answers 200 on PORT (3000)" },
|
|
115
|
-
{ name: `${name}-prepare`, does: "--settings JSON --env FILE [--user NAME]: the settings as an env file for that user"
|
|
116
|
+
{ name: `${name}-prepare`, does: "--settings JSON --env FILE [--user NAME]: the settings as an env file for that user, "
|
|
117
|
+
+ "and the folders they name for the app's files made for it" },
|
|
116
118
|
...(typeof app.scripts?.["db:migrate"] === "string" ? [{ name: `${name}-migrate`, does: "[--env FILE]: the db:migrate script" }] : [])
|
|
117
119
|
];
|
|
118
120
|
return {
|
package/scripts/test-command.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import { pathToFileURL } from "node:url";
|
|
|
7
7
|
import { toPosixPath } from "./files.mjs";
|
|
8
8
|
import { BUDGET_VARIABLE, testConcurrency } from "./test-budget.mjs";
|
|
9
9
|
import { DATABASES_OPTION, testDatabaseRunFor } from "./test-databases.mjs";
|
|
10
|
+
import { useTestAppEnvironment } from "./test-environment.mjs";
|
|
10
11
|
import { STOPS_VARIABLE } from "./test-file-stop.mjs";
|
|
11
12
|
import { reportTestTimes, writeFileStops } from "./test-time-report.mjs";
|
|
12
13
|
|
|
@@ -197,6 +198,8 @@ function runNode(args, env) {
|
|
|
197
198
|
* @param {string[]} args
|
|
198
199
|
*/
|
|
199
200
|
async function runTests(args) {
|
|
201
|
+
// Every run, and its test databases, is APP_ENV=test.
|
|
202
|
+
useTestAppEnvironment(process.env, "jskit test");
|
|
200
203
|
const nodeArgs = args.filter((argument) => argument !== DATABASES_OPTION);
|
|
201
204
|
let plan = planTestRuns(nodeArgs);
|
|
202
205
|
const source = process.env[BUDGET_VARIABLE] ? BUDGET_VARIABLE : "half of this machine";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Tests run as APP_ENV=test: `jskit test` and the browser test servers set it, whatever the shell says, so a test
|
|
2
|
+
// never sends real email or payments and the test databases may be prepared. A shell whose APP_ENV is staging or
|
|
3
|
+
// production is a deployed server's, and no test runs there.
|
|
4
|
+
|
|
5
|
+
const DEPLOYED_ENVIRONMENTS = Object.freeze(["staging", "production"]);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sets `env.APP_ENV` to test, or throws, naming the fix, when it says staging or production.
|
|
9
|
+
* @param {Record<string, string | undefined>} env The process environment, changed in place.
|
|
10
|
+
* @param {string} command The command that runs the tests, for the refusal.
|
|
11
|
+
*/
|
|
12
|
+
function useTestAppEnvironment(env, command) {
|
|
13
|
+
const appEnv = String(env.APP_ENV ?? "").trim();
|
|
14
|
+
if (DEPLOYED_ENVIRONMENTS.includes(appEnv)) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`${command} runs tests with APP_ENV=test, and APP_ENV is ${appEnv} here: tests never run on a staging or ` +
|
|
17
|
+
"production server. Run them where APP_ENV is unset, development or test."
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
env.APP_ENV = "test";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { useTestAppEnvironment };
|