@whyour/qinglong 2.22.0 → 2.23.0

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.
Files changed (150) hide show
  1. package/back/protos/cron.proto +4 -0
  2. package/docs/development/node-cron-migration.md +49 -0
  3. package/docs/development/task-startup.md +78 -0
  4. package/docs/releases/2.22.0.md +8 -0
  5. package/docs/releases/2.23.0.md +33 -0
  6. package/docs/script-history.md +53 -0
  7. package/package.json +9 -8
  8. package/scripts/benchmark-script-history.cjs +39 -0
  9. package/scripts/publish-static.sh +34 -0
  10. package/scripts/sync-static.sh +20 -0
  11. package/scripts/wait-for-mirror.sh +214 -0
  12. package/shell/api.sh +56 -70
  13. package/shell/node_path_cache.sh +10 -2
  14. package/shell/otask.sh +7 -9
  15. package/shell/preload/sitecustomize.js +6 -2
  16. package/shell/preload/sitecustomize.py +4 -2
  17. package/shell/share.sh +1 -1
  18. package/shell/task-timeout.sh +63 -0
  19. package/shell/task.sh +8 -6
  20. package/shell/update.sh +40 -95
  21. package/shell/upgrade.cjs +8 -0
  22. package/static/build/api/config.js +2 -6
  23. package/static/build/api/env.js +15 -14
  24. package/static/build/api/open.js +2 -1
  25. package/static/build/api/script.js +155 -9
  26. package/static/build/api/subscription.js +3 -6
  27. package/static/build/api/system.js +35 -3
  28. package/static/build/config/grpcCerts.js +89 -40
  29. package/static/build/config/subscription.js +19 -11
  30. package/static/build/config/util.js +6 -4
  31. package/static/build/loaders/express.js +2 -0
  32. package/static/build/loaders/initData.js +20 -30
  33. package/static/build/loaders/initTask.js +11 -1
  34. package/static/build/middlewares/uniqueConstraintError.js +15 -0
  35. package/static/build/protos/cron.js +63 -2
  36. package/static/build/schedule/addCron.js +45 -63
  37. package/static/build/schedule/client.js +19 -1
  38. package/static/build/schedule/delCron.js +1 -1
  39. package/static/build/schedule/setConcurrency.js +27 -0
  40. package/static/build/services/config.js +2 -3
  41. package/static/build/services/cron.js +36 -10
  42. package/static/build/services/env.js +1 -1
  43. package/static/build/services/grpc.js +2 -1
  44. package/static/build/services/log.js +1 -1
  45. package/static/build/services/open.js +106 -31
  46. package/static/build/services/schedule.js +20 -10
  47. package/static/build/services/script.js +1 -1
  48. package/static/build/services/scriptHistory.js +30 -0
  49. package/static/build/services/sshKey.js +39 -14
  50. package/static/build/services/subscription.js +104 -53
  51. package/static/build/services/system.js +67 -32
  52. package/static/build/services/user.js +15 -10
  53. package/static/build/shared/cronCommand.js +17 -0
  54. package/static/build/shared/cronSchedule.js +101 -0
  55. package/static/build/shared/cronScheduler.js +73 -0
  56. package/static/build/shared/i18n.js +3 -2
  57. package/static/build/shared/logPath.js +15 -0
  58. package/static/build/shared/loginThrottle.js +45 -0
  59. package/static/build/shared/pLimit.js +19 -6
  60. package/static/build/shared/runCron.js +13 -4
  61. package/static/build/shared/scriptHistory.js +445 -0
  62. package/static/build/shared/shellQuote.js +11 -0
  63. package/static/build/shared/subscriptionMutationLock.js +34 -0
  64. package/static/build/shared/subscriptionPath.js +41 -0
  65. package/static/build/shared/subscriptionStorage.js +98 -0
  66. package/static/build/shared/utils.js +2 -1
  67. package/static/build/token.js +3 -14
  68. package/static/build/validation/schedule.js +3 -18
  69. package/static/dist/292.264757f0.async.js +1 -0
  70. package/static/dist/3512.95e41255.async.js +1 -0
  71. package/static/dist/index.html +2 -2
  72. package/static/dist/preload_helper.01fcd534.js +1 -0
  73. package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
  74. package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
  75. package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
  76. package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
  77. package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
  78. package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
  79. package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
  80. package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
  81. package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
  82. package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
  83. package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
  84. package/static/dist/{umi.fc019050.js → umi.3a24a9fa.js} +1 -1
  85. package/test/back/advisory-input-security.test.cjs +416 -0
  86. package/test/back/code-scanning-security.test.cjs +189 -0
  87. package/test/back/cron-macros.test.cjs +51 -0
  88. package/test/back/cron-schedule-compatibility.test.cjs +69 -0
  89. package/test/back/cron-scheduler-routing.test.cjs +59 -0
  90. package/test/back/draft-path-security.test.cjs +235 -0
  91. package/test/back/env-import-security.test.cjs +91 -0
  92. package/test/back/execution-lifecycle.test.cjs +1 -1
  93. package/test/back/file-access-scope.test.cjs +180 -0
  94. package/test/back/grpc-mtls-security.test.cjs +136 -0
  95. package/test/back/http-security.test.cjs +105 -5
  96. package/test/back/legacy-cron-recovery.test.cjs +76 -0
  97. package/test/back/linux-mirror-result.test.cjs +176 -0
  98. package/test/back/listen-security.test.cjs +1 -0
  99. package/test/back/mirror-status.test.cjs +257 -0
  100. package/test/back/node-cron-migration.test.cjs +65 -0
  101. package/test/back/node-path-cache.test.cjs +29 -0
  102. package/test/back/node-path-lock.test.cjs +6 -2
  103. package/test/back/preload-lazy-notify.test.cjs +168 -0
  104. package/test/back/queued-cron-invalidation.test.cjs +98 -0
  105. package/test/back/restart-instance-recovery.test.cjs +116 -0
  106. package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
  107. package/test/back/scheduler-concurrency.test.cjs +411 -0
  108. package/test/back/scheduler-reconciliation.test.cjs +58 -4
  109. package/test/back/script-file-access.test.cjs +210 -0
  110. package/test/back/script-history-api.test.cjs +551 -0
  111. package/test/back/script-history.test.cjs +383 -0
  112. package/test/back/shell-api-parsing.test.cjs +4 -4
  113. package/test/back/shell-translation-output.test.cjs +16 -0
  114. package/test/back/shell-upgrade.test.cjs +312 -0
  115. package/test/back/shellApiToken.test.cjs +151 -0
  116. package/test/back/ssh-config-home.test.cjs +113 -0
  117. package/test/back/static-mirror-trigger.test.cjs +441 -0
  118. package/test/back/static-mirror.test.cjs +179 -0
  119. package/test/back/stop-race.test.cjs +5 -1
  120. package/test/back/subscription-mutation-lock.test.cjs +83 -0
  121. package/test/back/subscription-review-regression.test.cjs +607 -0
  122. package/test/back/subscription-storage-regression.test.cjs +553 -0
  123. package/test/back/system-dependence-cache.test.cjs +1 -0
  124. package/test/back/system-export-history.test.cjs +119 -0
  125. package/test/back/systemTokenRenewal.test.cjs +497 -0
  126. package/test/back/task-shell-lifecycle.test.cjs +194 -0
  127. package/test/back/unique-constraint-error.test.cjs +72 -0
  128. package/test/back/upgrade-guards.test.cjs +285 -0
  129. package/test/back/upgrade-lifecycle.test.cjs +288 -0
  130. package/test/back/upgrade-transaction.test.cjs +133 -0
  131. package/test/back/user-security.test.cjs +102 -1
  132. package/test/back/wpush-grpc.test.cjs +1 -0
  133. package/test/fixtures/legacy-cron.json +1371 -0
  134. package/test/front/diff-file-routing.test.cjs +126 -0
  135. package/test/front/script-history-save.test.cjs +121 -0
  136. package/test/helpers/upgrade-fixture.cjs +30 -0
  137. package/version.yaml +14 -19
  138. package/static/dist/2096.6d98fd12.async.js +0 -1
  139. package/static/dist/4859.97d6653c.async.js +0 -1
  140. package/static/dist/6707.801b886b.async.js +0 -1
  141. package/static/dist/preload_helper.cf7de3fa.js +0 -1
  142. package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +0 -1
  143. package/static/dist/src__pages__diff__index.1714b941.async.js +0 -1
  144. package/static/dist/src__pages__script__editModal.22acdf7c.async.js +0 -1
  145. package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +0 -1
  146. package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
  147. package/static/dist/src__pages__script__index.a18b2692.async.js +0 -1
  148. package/static/dist/src__pages__script__saveModal.8965b74c.async.js +0 -1
  149. package/static/dist/src__pages__subscription__modal.38991e1e.async.js +0 -1
  150. /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
@@ -0,0 +1,257 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const os = require('node:os');
6
+ const { execFileSync, spawn, spawnSync } = require('node:child_process');
7
+
8
+ const script = path.resolve('scripts/wait-for-mirror.sh');
9
+ const git = (...args) =>
10
+ execFileSync('git', args, { encoding: 'utf8', stdio: 'pipe' }).trim();
11
+ const options = (env = {}) => ({
12
+ encoding: 'utf8',
13
+ env: {
14
+ ...process.env,
15
+ QL_MIRROR_WAIT_SECONDS: '3',
16
+ QL_MIRROR_POLL_INTERVAL: '1',
17
+ QL_MIRROR_QUERY_TIMEOUT: '1',
18
+ ...env,
19
+ },
20
+ });
21
+
22
+ function fixture(t) {
23
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-mirror-status-'));
24
+ t.after(() => fs.rmSync(tmp, { recursive: true, force: true }));
25
+ const source = path.join(tmp, 'source');
26
+ const mirror = path.join(tmp, 'mirror.git');
27
+ git('init', source);
28
+ git('init', '--bare', mirror);
29
+ git('-C', source, 'config', 'user.name', 'Fixture');
30
+ git('-C', source, 'config', 'user.email', 'fixture@example.invalid');
31
+ git('-C', source, 'checkout', '-b', 'develop');
32
+ fs.writeFileSync(path.join(source, 'snapshot'), 'first');
33
+ git('-C', source, 'add', '.');
34
+ git('-C', source, 'commit', '-m', 'first');
35
+ return { tmp, source, mirror };
36
+ }
37
+
38
+ test('mirror verification reads matching refs without changing either repository', (t) => {
39
+ const { source, mirror } = fixture(t);
40
+ git('-C', source, 'push', mirror, 'develop');
41
+ const before = git('ls-remote', '--refs', mirror);
42
+ const result = spawnSync(
43
+ 'bash',
44
+ [script, source, mirror, 'develop'],
45
+ options(),
46
+ );
47
+ assert.equal(result.status, 0, result.stderr + result.stdout);
48
+ assert.match(result.stdout, /Mirror matches the latest source refs/);
49
+ assert.equal(git('ls-remote', '--refs', mirror), before);
50
+ assert.equal(git('-C', source, 'status', '--porcelain'), '');
51
+ });
52
+
53
+ test('mirror polling follows a source that advances while waiting', async (t) => {
54
+ const { source, mirror } = fixture(t);
55
+ git('-C', source, 'push', mirror, 'develop');
56
+ fs.writeFileSync(path.join(source, 'snapshot'), 'second');
57
+ git('-C', source, 'commit', '-am', 'second');
58
+ const child = spawn(
59
+ 'bash',
60
+ [script, source, mirror, 'develop'],
61
+ options({
62
+ QL_MIRROR_WAIT_SECONDS: '6',
63
+ }),
64
+ );
65
+ let output = '';
66
+ let error = '';
67
+ let moved = false;
68
+ child.stderr.on('data', (data) => {
69
+ error += data;
70
+ });
71
+ child.stdout.on('data', (data) => {
72
+ output += data;
73
+ if (!moved && output.includes('Waiting for mirror')) {
74
+ moved = true;
75
+ fs.writeFileSync(path.join(source, 'snapshot'), 'third');
76
+ git('-C', source, 'commit', '-am', 'third');
77
+ git('-C', source, 'push', mirror, 'develop');
78
+ }
79
+ });
80
+ const status = await new Promise((resolve, reject) => {
81
+ child.on('error', reject);
82
+ child.on('close', resolve);
83
+ });
84
+ assert.equal(status, 0, error + output);
85
+ assert.ok(moved);
86
+ assert.match(output, new RegExp(git('-C', source, 'rev-parse', 'develop')));
87
+ });
88
+
89
+ test('empty or missing refs never count as a synchronized mirror', (t) => {
90
+ const { source, mirror } = fixture(t);
91
+ for (const ref of ['develop', 'missing']) {
92
+ const result = spawnSync(
93
+ 'bash',
94
+ [script, source, mirror, ref],
95
+ // Leave time to read both refs before checking the missing-ref diagnostic.
96
+ options(),
97
+ );
98
+ assert.equal(result.status, 1, result.stderr + result.stdout);
99
+ assert.match(result.stdout, /is missing refs\/heads\//);
100
+ assert.match(result.stdout, /::error::Mirror did not match/);
101
+ }
102
+ });
103
+
104
+ test('static releases require both the version branch and annotated tag object', (t) => {
105
+ const { source, mirror } = fixture(t);
106
+ git('-C', source, 'branch', 'v2.22.0');
107
+ git('-C', source, 'tag', '-a', 'v2.22.0', '-m', 'release');
108
+ git('-C', source, 'push', mirror, 'refs/tags/v2.22.0');
109
+ const args = [script, source, mirror, 'v2.22.0', 'tag'];
110
+ const env = {
111
+ QL_MIRROR_INCLUDE_VERSION_BRANCH: 'true',
112
+ QL_MIRROR_WAIT_SECONDS: '3',
113
+ };
114
+ const partial = spawnSync('bash', args, options(env));
115
+ assert.equal(partial.status, 1, partial.stderr + partial.stdout);
116
+ assert.match(partial.stdout, /mirror is missing refs\/heads\/v2.22.0/);
117
+ git('-C', source, 'push', mirror, 'refs/heads/v2.22.0');
118
+ const complete = spawnSync('bash', args, options(env));
119
+ assert.equal(complete.status, 0, complete.stderr + complete.stdout);
120
+ assert.match(
121
+ complete.stdout,
122
+ new RegExp(git('-C', source, 'rev-parse', 'refs/tags/v2.22.0')),
123
+ );
124
+ // A tag pointing to the same commit with different annotation is still stale.
125
+ git('-C', source, 'tag', '-fa', 'v2.22.0', '-m', 'updated annotation');
126
+ const stale = spawnSync('bash', args, options(env));
127
+ assert.equal(stale.status, 1, stale.stderr + stale.stdout);
128
+ });
129
+
130
+ test('network failures and hung ref queries stop within the configured deadline', (t) => {
131
+ const { tmp, source, mirror } = fixture(t);
132
+ const failure = spawnSync(
133
+ 'bash',
134
+ [script, source, path.join(tmp, 'absent'), 'develop'],
135
+ options({
136
+ QL_MIRROR_WAIT_SECONDS: '3',
137
+ }),
138
+ );
139
+ assert.equal(failure.status, 1, failure.stderr + failure.stdout);
140
+ assert.match(failure.stdout, /Could not read mirror refs/);
141
+
142
+ const bin = path.join(tmp, 'bin');
143
+ fs.mkdirSync(bin);
144
+ const realGit = execFileSync('which', ['git'], { encoding: 'utf8' }).trim();
145
+ fs.writeFileSync(
146
+ path.join(bin, 'git'),
147
+ '#!/bin/sh\nif [ "$1" = ls-remote ]; then sleep 30; fi\nexec "$QL_TEST_GIT" "$@"\n',
148
+ { mode: 0o755 },
149
+ );
150
+ const started = Date.now();
151
+ const hung = spawnSync('bash', [script, source, mirror, 'develop'], {
152
+ ...options({
153
+ PATH: `${bin}${path.delimiter}${process.env.PATH}`,
154
+ QL_TEST_GIT: realGit,
155
+ QL_MIRROR_WAIT_SECONDS: '2',
156
+ QL_MIRROR_QUERY_TIMEOUT: '20',
157
+ }),
158
+ timeout: 5000,
159
+ });
160
+ assert.equal(hung.status, 1, hung.stderr + hung.stdout);
161
+ assert.ok(Date.now() - started < 4500);
162
+ assert.match(hung.stdout, /within 2s/);
163
+ });
164
+
165
+ test('invalid mirror parameters fail before polling', (t) => {
166
+ const { source, mirror } = fixture(t);
167
+ for (const args of [
168
+ [],
169
+ [source, mirror, 'develop', 'other'],
170
+ [source, mirror, 'bad ref'],
171
+ ]) {
172
+ assert.notEqual(spawnSync('bash', [script, ...args], options()).status, 0);
173
+ }
174
+ for (const value of ['0', '-1', '1.5', '1+2', '9999999999999']) {
175
+ const result = spawnSync(
176
+ 'bash',
177
+ [script, source, mirror, 'develop'],
178
+ options({
179
+ QL_MIRROR_WAIT_SECONDS: value,
180
+ }),
181
+ );
182
+ assert.equal(result.status, 2, result.stderr + result.stdout);
183
+ }
184
+ });
185
+
186
+ test('Gitee workflows verify both mirrors and do not gate image publication', () => {
187
+ const workflow = require('js-yaml').load(
188
+ fs.readFileSync('.github/workflows/build-docker-image.yml', 'utf8'),
189
+ );
190
+ assert.equal(workflow.jobs.code_gitee, undefined);
191
+ assert.equal(workflow.jobs.static_gitee, undefined);
192
+ const job = workflow.jobs['verify-gitee-mirrors'];
193
+ assert.equal(job.needs, 'build-static');
194
+ assert.equal(job['timeout-minutes'], 14);
195
+ assert.deepEqual(job.permissions, { contents: 'write' });
196
+ assert.deepEqual(job.strategy.matrix.repository, [
197
+ 'qinglong',
198
+ 'qinglong-static',
199
+ ]);
200
+ const verify = job.steps.find((step) => step.run);
201
+ assert.equal(verify.env.MIRROR_REF, '${{ github.ref_name }}');
202
+ assert.equal(verify.env.MIRROR_REF_TYPE, '${{ github.ref_type }}');
203
+ assert.equal(
204
+ verify.env.QL_MIRROR_INCLUDE_VERSION_BRANCH,
205
+ "${{ matrix.repository == 'qinglong-static' }}",
206
+ );
207
+ assert.match(verify.run, /bash scripts\/wait-for-mirror.sh/);
208
+ assert.equal(verify.if, "${{ matrix.repository == 'qinglong' }}");
209
+ assert.equal(verify.env.GH_TOKEN, '${{ github.token }}');
210
+ assert.equal(verify.env.QL_MIRROR_NOTIFY_ON_START, 'false');
211
+ assert.equal(verify.env.QL_MIRROR_NOTIFY_REPO, 'whyour/qinglong');
212
+ assert.doesNotMatch(
213
+ JSON.stringify(verify),
214
+ /secrets\.|git push|sync-static|ssh-keyscan/,
215
+ );
216
+ const notify = job.steps.find((step) =>
217
+ /Notify and wait/.test(step.name || ''),
218
+ );
219
+ assert.equal(notify.if, "${{ matrix.repository == 'qinglong-static' }}");
220
+ assert.equal(notify.env.GH_TOKEN, '${{ secrets.API_TOKEN }}');
221
+ assert.match(notify.run, /bash scripts\/wait-for-mirror.sh/);
222
+ assert.equal(notify.env.QL_MIRROR_NOTIFY_REPO, 'whyour/qinglong-static');
223
+ assert.equal(notify.env.QL_MIRROR_INCLUDE_VERSION_BRANCH, 'true');
224
+ assert.doesNotMatch(
225
+ JSON.stringify(job),
226
+ /GITEE_TOKEN|git@gitee|sync-static|ssh-keyscan/,
227
+ );
228
+ const backfill = workflow.jobs['verify-existing-static-gitee'];
229
+ assert.match(backfill.if, /workflow_dispatch.*inputs.static_sync_only/);
230
+ assert.equal(backfill.needs, undefined);
231
+ const verifyExisting = backfill.steps.find((step) => step.run);
232
+ assert.equal(verifyExisting.env.MIRROR_REF, '${{ inputs.static_ref }}');
233
+ assert.equal(
234
+ verifyExisting.env.MIRROR_REF_TYPE,
235
+ '${{ inputs.static_ref_type }}',
236
+ );
237
+ assert.equal(verifyExisting.env.GH_TOKEN, '${{ secrets.API_TOKEN }}');
238
+ assert.match(verifyExisting.run, /wait-for-mirror.sh/);
239
+ assert.equal(
240
+ verifyExisting.env.QL_MIRROR_NOTIFY_REPO,
241
+ 'whyour/qinglong-static',
242
+ );
243
+ assert.equal(verifyExisting.env.QL_MIRROR_INCLUDE_VERSION_BRANCH, 'true');
244
+ assert.deepEqual(workflow.on.push.branches, ['master', 'develop']);
245
+ assert.doesNotMatch(
246
+ JSON.stringify(backfill),
247
+ /GITEE_TOKEN|git@gitee|sync-static|ssh-keyscan/,
248
+ );
249
+ for (const name of [
250
+ 'build-alpine',
251
+ 'build-debian',
252
+ 'build-alpine310',
253
+ 'build-debian310',
254
+ ]) {
255
+ assert.equal(workflow.jobs[name].needs, 'build-static');
256
+ }
257
+ });
@@ -0,0 +1,65 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const load = require('../helpers/load-security-module.cjs');
4
+ const { createCronJob } = require('../../back/shared/cronScheduler');
5
+ const logger = { info() {}, warn() {}, error() {} };
6
+
7
+ test('native calendar skips nonexistent spring time and duplicate fall hour', async (t) => {
8
+ const previous = process.env.TZ;
9
+ process.env.TZ = 'America/New_York';
10
+ t.after(() => { if (previous === undefined) delete process.env.TZ; else process.env.TZ = previous; });
11
+ t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: new Date('2026-03-08T05:00:00Z') });
12
+ const springRuns = [];
13
+ const spring = createCronJob('0 30 2 * * *', (date) => springRuns.push(date.toISOString()), { name: 'spring', logger });
14
+ t.after(() => spring.cancel());
15
+ t.mock.timers.tick(new Date('2026-03-09T06:30:00Z') - Date.now());
16
+ await new Promise(setImmediate);
17
+ assert.deepEqual(springRuns, ['2026-03-09T06:30:00.000Z']);
18
+ spring.cancel();
19
+ t.mock.timers.setTime(new Date('2026-11-01T05:30:00Z').getTime());
20
+ const fallRuns = [];
21
+ const fall = createCronJob('0 30 1 * * *', (date) => fallRuns.push(date.toISOString()), { name: 'fall', logger });
22
+ t.after(() => fall.cancel());
23
+ t.mock.timers.tick(new Date('2026-11-02T06:30:00Z') - Date.now());
24
+ await new Promise(setImmediate);
25
+ assert.deepEqual(fallRuns, ['2026-11-02T06:30:00.000Z']);
26
+ });
27
+
28
+ test('construction failure cleans staged jobs and preserves the running snapshot', async () => {
29
+ const cancelled = [];
30
+ const stacks = new Map([['old', [{ cancel: () => cancelled.push('old') }]]]);
31
+ let count = 0;
32
+ const { addCron } = load('back/schedule/addCron.ts', {
33
+ './data': { scheduleStacks: stacks },
34
+ '../shared/cronScheduler': { createCronJob: () => {
35
+ if (++count === 2) throw Error('construction failed');
36
+ return { start() { throw Error('must not start'); }, cancel: () => cancelled.push('staged') };
37
+ } },
38
+ '../shared/runCron': {}, '../loaders/logger': logger, '../shared/i18n': { tf: require('node:util').format },
39
+ });
40
+ await assert.rejects(new Promise((resolve, reject) => addCron({ request: {
41
+ replace: true, crons: [{ id: 'new', schedule: '* * * * *', extra_schedules: [{ schedule: '0 * * * *' }] }],
42
+ } }, (error) => error ? reject(error) : resolve())), /construction failed/);
43
+ assert.deepEqual([...stacks.keys()], ['old']);
44
+ assert.deepEqual(cancelled, ['staged']);
45
+ });
46
+
47
+ test('invalid subscription schedules are isolated and immediate runs without a schedule survive', async () => {
48
+ const warnings = [], runs = [];
49
+ const Service = load('back/services/schedule.ts', {
50
+ typedi: { Service: () => (x) => x, Inject: () => () => {} },
51
+ '../shared/pLimit': {},
52
+ }).default;
53
+ const service = new Service({ ...logger, warn: (...args) => warnings.push(args) });
54
+ service.runTask = async (...args) => { runs.push(args); };
55
+ const item = { id: 123, name: 'subscription', command: 'true', runOrigin: 'subscription' };
56
+ await service.createCronTask({ ...item, schedule: 'not a cron' });
57
+ assert.equal(warnings.length, 1);
58
+ assert.equal(service.scheduleStacks.size, 0);
59
+ await service.createCronTask(item, {}, true);
60
+ assert.equal(runs.length, 1);
61
+ await service.createCronTask({ ...item, schedule: '* * * * *' });
62
+ assert.equal(service.scheduleStacks.size, 1);
63
+ await service.cancelCronTask(item);
64
+ assert.equal(service.scheduleStacks.size, 0);
65
+ });
@@ -65,6 +65,35 @@ test('config, environment, cwd, and executable changes invalidate discovery', (t
65
65
  f.run();
66
66
  assert.equal(f.count(), before + 1);
67
67
  });
68
+ test('alternating Node/crond working directories retain independent cached roots', (t) => {
69
+ const f = fixture(t);
70
+ fs.mkdirSync(path.join(f.root, 'child'));
71
+ fs.writeFileSync(path.join(f.root, 'child', '.npmrc'), 'global-dir=/child\n');
72
+ for (let i = 0; i < 4; i++) {
73
+ assert.equal(f.run({ ANSWER: '/parent/modules' }).stdout.trim(), '/parent/modules');
74
+ assert.equal(
75
+ f.run({ ANSWER: '/child/modules' }, 'cd child; ' + f.command).stdout.trim(),
76
+ '/child/modules',
77
+ );
78
+ }
79
+ assert.equal(f.count(), 2, 'each working directory should discover only once');
80
+ assert.equal(fs.readdirSync(f.env.dir_tmp).filter((x) => x.endsWith('.cache')).length, 2);
81
+ });
82
+ test('refresh retires old per-environment records without removing locks or unrelated files', (t) => {
83
+ const f = fixture(t);
84
+ fs.mkdirSync(f.env.dir_tmp);
85
+ const prefix = `pnpm-root-${process.getuid()}`;
86
+ const names = [`${prefix}-1-2.cache`, `${prefix}.lock`, 'unrelated.cache'];
87
+ const old = new Date(Date.now() - 2 * 60 * 60 * 1000);
88
+ for (const name of names) {
89
+ const file = path.join(f.env.dir_tmp, name);
90
+ fs.writeFileSync(file, '');
91
+ fs.utimesSync(file, old, old);
92
+ }
93
+ assert.equal(f.run().status, 0);
94
+ assert.equal(fs.existsSync(path.join(f.env.dir_tmp, names[0])), false);
95
+ for (const name of names.slice(1)) assert.ok(fs.existsSync(path.join(f.env.dir_tmp, name)));
96
+ });
68
97
  test('expired or malformed records refresh and failed lookups are not cached', (t) => {
69
98
  const f = fixture(t);
70
99
  f.run();
@@ -51,7 +51,11 @@ function fixture(t) {
51
51
  fs.existsSync(env.CALLS)
52
52
  ? fs.readFileSync(env.CALLS, 'utf8').trim().split('\n').length
53
53
  : 0;
54
- const cache = path.join(env.dir_tmp, `pnpm-root-${process.getuid()}.cache`);
54
+ const key = run({}, '. "$HELPER"; ql_node_path_cache_key').stdout.trim();
55
+ const cache = path.join(
56
+ env.dir_tmp,
57
+ `pnpm-root-${process.getuid()}-${key.replace(' ', '-')}.cache`,
58
+ );
55
59
  return {
56
60
  root,
57
61
  bin,
@@ -61,7 +65,7 @@ function fixture(t) {
61
65
  asyncRun,
62
66
  count,
63
67
  cache,
64
- lock: cache + '.lock',
68
+ lock: path.join(env.dir_tmp, `pnpm-root-${process.getuid()}.lock`),
65
69
  };
66
70
  }
67
71
  function check(r, output = '/test/global/node_modules') {
@@ -0,0 +1,168 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const fs = require('node:fs');
4
+ const os = require('node:os');
5
+ const path = require('node:path');
6
+ const { spawnSync } = require('node:child_process');
7
+
8
+ function fixture(t, language) {
9
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-lazy-notify-'));
10
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
11
+ const extension = language === 'python' ? 'py' : 'js';
12
+ fs.copyFileSync(`shell/preload/sitecustomize.${extension}`, path.join(root, `sitecustomize.${extension}`));
13
+ fs.writeFileSync(path.join(root, `env.${extension}`), '');
14
+ fs.writeFileSync(path.join(root, 'before.sh'), 'export FROM_SHELL=before\n');
15
+ fs.writeFileSync(path.join(root, 'before.js'), 'process.env.FROM_LANGUAGE = "javascript";\n');
16
+ fs.writeFileSync(path.join(root, 'task_before.py'), 'import os\nos.environ["FROM_LANGUAGE"] = "python"\n');
17
+ fs.writeFileSync(path.join(root, 'client.js'), 'module.exports = { getEnvs: () => "client-ready" };\n');
18
+ fs.copyFileSync('shell/preload/client.py', path.join(root, 'client.py'));
19
+ fs.copyFileSync('shell/preload/esm-loader.mjs', path.join(root, 'esm-loader.mjs'));
20
+ const env = {
21
+ ...process.env,
22
+ PYTHONPATH: root,
23
+ PREV_PYTHONPATH: '',
24
+ NODE_OPTIONS: '',
25
+ PREV_NODE_OPTIONS: '',
26
+ QL_NODE_GLOBAL_PATH: '',
27
+ file_task_before: path.join(root, 'before.sh'),
28
+ file_task_before_js: path.join(root, 'before.js'),
29
+ dir_scripts: root,
30
+ task_before: 'export FROM_COMMAND=command',
31
+ envParam: 'ACCOUNTS',
32
+ numParam: '2-3',
33
+ ACCOUNTS: 'first&second&third',
34
+ };
35
+ const write = (name, text) => fs.writeFileSync(path.join(root, name), text);
36
+ const run = (body, extensionOverride = extension) => {
37
+ const entry = path.join(root, `entry.${extensionOverride}`);
38
+ write(path.basename(entry), body);
39
+ const python = language === 'python';
40
+ const result = spawnSync(python ? 'python3' : process.execPath,
41
+ python ? [entry] : ['--require', path.join(root, 'sitecustomize.js'), entry],
42
+ { cwd: root, env, encoding: 'utf8', timeout: 15000 });
43
+ assert.equal(result.status, 0, result.stderr + result.stdout);
44
+ return result.stdout;
45
+ };
46
+ return { root, env, write, run };
47
+ }
48
+
49
+ for (const extension of ['js', 'mjs']) {
50
+ test(`${extension} startup preserves hooks, environment selection and client without importing notifications`, (t) => {
51
+ const f = fixture(t, 'javascript');
52
+ f.write('__ql_notify__.js', 'throw Error("notification dependencies must not load at startup");');
53
+ f.run(`
54
+ const assert = ${extension === 'mjs' ? '(await import("node:assert/strict")).default' : 'require("node:assert/strict")'};
55
+ assert.equal(process.env.FROM_SHELL, 'before');
56
+ assert.equal(process.env.FROM_LANGUAGE, 'javascript');
57
+ assert.equal(process.env.FROM_COMMAND, 'command');
58
+ assert.equal(process.env.ACCOUNTS, 'second&third');
59
+ assert.equal(QLAPI.getEnvs(), 'client-ready');
60
+ assert.equal(typeof QLAPI.notify, 'function');
61
+ `, extension);
62
+ });
63
+ }
64
+
65
+ test('JavaScript first notification imports once and preserves arguments, this, promises and errors', (t) => {
66
+ const f = fixture(t, 'javascript');
67
+ f.write('__ql_notify__.js', `
68
+ global.notificationLoads = (global.notificationLoads || 0) + 1;
69
+ exports.sendNotify = function (...args) {
70
+ if (args[0] === 'fail') throw new Error('send failed');
71
+ global.notificationResult = Promise.resolve({ args, receiver: this });
72
+ return global.notificationResult;
73
+ };
74
+ `);
75
+ f.run(`
76
+ const assert = require('node:assert/strict');
77
+ assert.equal(global.notificationLoads, undefined);
78
+ (async () => {
79
+ const options = { channel: 'fixture' };
80
+ const promise = QLAPI.notify('title', 'content', options);
81
+ assert.equal(promise, global.notificationResult);
82
+ const result = await promise;
83
+ assert.deepEqual(result.args, ['title', 'content', options]);
84
+ assert.equal(result.receiver, QLAPI);
85
+ await QLAPI.notify('second');
86
+ assert.equal(global.notificationLoads, 1);
87
+ assert.throws(() => QLAPI.notify('fail'), /send failed/);
88
+ })().catch(error => { console.error(error); process.exitCode = 1; });
89
+ `);
90
+ });
91
+
92
+ test('JavaScript reports import failures on notify and permits a later retry', (t) => {
93
+ const f = fixture(t, 'javascript');
94
+ f.write('__ql_notify__.js', `
95
+ if (!process.env.NOTIFY_READY) throw Error('notification dependency unavailable');
96
+ exports.sendNotify = (...args) => args;
97
+ `);
98
+ f.run(`
99
+ const assert = require('node:assert/strict');
100
+ assert.equal(QLAPI.getEnvs(), 'client-ready');
101
+ assert.throws(() => QLAPI.notify('first'), /notification dependency unavailable/);
102
+ process.env.NOTIFY_READY = '1';
103
+ assert.deepEqual(QLAPI.notify('retry'), ['retry']);
104
+ `);
105
+ });
106
+
107
+ test('Python startup preserves hooks, environment selection and client without importing notifications', (t) => {
108
+ const f = fixture(t, 'python');
109
+ f.write('__ql_notify__.py', 'raise RuntimeError("notification dependencies must not load at startup")\n');
110
+ f.run(`
111
+ import os, sys
112
+ assert os.environ['FROM_SHELL'] == 'before'
113
+ assert os.environ['FROM_LANGUAGE'] == 'python'
114
+ assert os.environ['FROM_COMMAND'] == 'command'
115
+ assert os.environ['ACCOUNTS'] == 'second&third'
116
+ assert '__ql_notify__' not in sys.modules
117
+ assert callable(QLAPI.notify)
118
+ assert callable(QLAPI.getEnvs)
119
+ `);
120
+ });
121
+
122
+ test('Python first notification imports once and preserves args, kwargs, return values and errors', (t) => {
123
+ const f = fixture(t, 'python');
124
+ f.write('__ql_notify__.py', `
125
+ import builtins
126
+ builtins.notification_loads = getattr(builtins, 'notification_loads', 0) + 1
127
+ def send(*args, **kwargs):
128
+ if args[0] == 'fail':
129
+ raise ValueError('send failed')
130
+ return args, kwargs
131
+ `);
132
+ f.run(`
133
+ import builtins, sys
134
+ assert '__ql_notify__' not in sys.modules
135
+ assert QLAPI.notify('title', 'content', channel='fixture') == (('title', 'content'), {'channel': 'fixture'})
136
+ assert QLAPI.notify('second') == (('second',), {})
137
+ assert builtins.notification_loads == 1
138
+ try:
139
+ QLAPI.notify('fail')
140
+ except ValueError as error:
141
+ assert str(error) == 'send failed'
142
+ else:
143
+ raise AssertionError('notification exception was swallowed')
144
+ `);
145
+ });
146
+
147
+ test('Python reports import failures on notify and permits a later retry', (t) => {
148
+ const f = fixture(t, 'python');
149
+ f.write('__ql_notify__.py', `
150
+ import os
151
+ if not os.environ.get('NOTIFY_READY'):
152
+ raise RuntimeError('notification dependency unavailable')
153
+ def send(*args, **kwargs):
154
+ return args
155
+ `);
156
+ f.run(`
157
+ import os, sys
158
+ assert '__ql_notify__' not in sys.modules
159
+ try:
160
+ QLAPI.notify('first')
161
+ except RuntimeError as error:
162
+ assert str(error) == 'notification dependency unavailable'
163
+ else:
164
+ raise AssertionError('import exception was swallowed')
165
+ os.environ['NOTIFY_READY'] = '1'
166
+ assert QLAPI.notify('retry') == ('retry',)
167
+ `);
168
+ });
@@ -0,0 +1,98 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const load = require('../helpers/load-security-module.cjs');
4
+ const tick = () => new Promise(setImmediate);
5
+ function gate() { let release; const promise = new Promise(r => release = r); return { promise, release }; }
6
+
7
+ async function fixture() {
8
+ const logger = { info() {}, error() {}, warn() {} }, notices = [];
9
+ const limit = load('back/shared/pLimit.ts', {
10
+ '../data/system': { AuthDataType: { systemConfig: 'systemConfig' }, SystemModel: { sync: async () => {}, findOne: async () => ({ info: { cronConcurrency: 1 } }) } },
11
+ '../loaders/logger': logger,
12
+ '../services/notify': class {},
13
+ '../shared/i18n': { t: s => s, tf: s => s },
14
+ '../config': { grpcPort: 1 },
15
+ '../protos/api': { ApiClient: class { systemNotify(value, cb) { notices.push(value); cb(); } } },
16
+ '../config/grpcCerts': { getGrpcCerts: () => null },
17
+ }).default;
18
+ await tick();
19
+ const spawned = [], records = new Map(), jobs = new Map();
20
+ let readGate, killGate;
21
+ const { runCron } = load('back/shared/runCron.ts', {
22
+ 'cross-spawn': { spawn: command => { spawned.push(command); return {}; } },
23
+ './pLimit': limit,
24
+ '../loaders/logger': logger,
25
+ '../data/cron': { CrontabModel: { findOne: async ({ where }) => { const row = records.get(String(where.id)); if (readGate) await readGate.promise; return row; }, update: async () => {} }, CrontabStatus: { running: 0, queued: 3, idle: 1 } },
26
+ '../data/runningInstance': { RunningInstanceModel: { update: async () => {} }, InstanceStatus: { running: 0, stopped: 2 } },
27
+ '../config/util': { killTask: async () => { if (killGate) await killGate.promise; } },
28
+ './childProcess': { observeChildProcess: () => ({ completed: Promise.resolve({ code: 0 }) }), asError: e => e },
29
+ });
30
+ const { addCron } = load('back/schedule/addCron.ts', {
31
+ '../shared/cronScheduler': { createCronJob: (schedule, callback) => ({ start() {}, cancel() {}, fire: callback }) },
32
+ '../shared/cronSchedule': { isValidCronSchedule: x => x !== 'invalid' },
33
+ './data': { scheduleStacks: jobs }, '../shared/runCron': { runCron }, '../loaders/logger': logger,
34
+ '../shared/i18n': { tf: s => s },
35
+ });
36
+ const { delCron } = load('back/schedule/delCron.ts', { './data': { scheduleStacks: jobs }, '../loaders/logger': logger });
37
+ function add(id = '1', command = 'old', replace = false) {
38
+ records.set(id, { isDisabled: 0, allow_multiple_instances: 1 });
39
+ addCron({ request: { crons: [{ id, command, schedule: '* * * * * *' }], replace } }, err => assert.ifError(err));
40
+ return jobs.get(id)[0];
41
+ }
42
+ function remove(id = '1') { delCron({ request: { ids: [id] } }, err => assert.ifError(err)); }
43
+ async function block() { const g = gate(); const done = limit.runWithCronLimit({ id: 'blocker' }, async () => { await g.promise; }); await tick(); return { release: async () => { g.release(); await done; } }; }
44
+ return { limit, spawned, records, jobs, add, remove, block, notices, setReadGate: g => readGate = g, setKillGate: g => killGate = g };
45
+ }
46
+
47
+ for (const operation of ['disable', 'delete', 'update', 'replace', 'disable-enable']) {
48
+ test(`queued scheduled execution is invalidated by ${operation}`, async () => {
49
+ const f = await fixture(), old = f.add(), blocker = await f.block();
50
+ const queued = old.fire();
51
+ if (operation === 'disable' || operation === 'disable-enable') { f.records.get('1').isDisabled = 1; f.remove(); }
52
+ if (operation === 'delete') { f.records.delete('1'); f.remove(); }
53
+ if (operation === 'update') f.add('1', 'new');
54
+ if (operation === 'replace') f.add('1', 'new', true);
55
+ if (operation === 'disable-enable') f.add('1', 'old');
56
+ const next = f.jobs.get('1')?.[0].fire();
57
+ await blocker.release(); await queued; await next;
58
+ assert.deepEqual(f.spawned, ['update', 'replace'].includes(operation) ? ['new'] : operation === 'disable-enable' ? ['old'] : []);
59
+ assert.equal(f.limit.cronLimitPendingCount, 0);
60
+ assert.equal(f.limit.cronLimitActiveCount, 0);
61
+ });
62
+ }
63
+
64
+ test('stale callbacks cannot re-enqueue after cancellation', async () => {
65
+ const f = await fixture(), old = f.add(); f.remove(); await old.fire();
66
+ assert.deepEqual(f.spawned, []);
67
+ });
68
+
69
+ test('invalidation during an asynchronous database read prevents spawning', async () => {
70
+ const f = await fixture(), old = f.add(), pending = gate(); f.setReadGate(pending);
71
+ const run = old.fire(); await tick(); f.remove(); pending.release(); await run;
72
+ assert.deepEqual(f.spawned, []);
73
+ });
74
+
75
+ test('invalidation while replacing a running instance prevents the old spawn', async () => {
76
+ const f = await fixture(), old = f.add(), pending = gate();
77
+ f.records.set('1', { isDisabled: 0, allow_multiple_instances: 0, pid: 10, status: 0 }); f.setKillGate(pending);
78
+ const run = old.fire(); await tick(); f.remove(); pending.release(); await run;
79
+ assert.deepEqual(f.spawned, []);
80
+ });
81
+
82
+ test('database removal or disable is respected before scheduler RPC arrives', async () => {
83
+ for (const disabled of [true, false]) {
84
+ const f = await fixture(), old = f.add(), blocker = await f.block(); const run = old.fire();
85
+ if (disabled) f.records.get('1').isDisabled = 1; else f.records.delete('1');
86
+ await blocker.release(); await run; assert.deepEqual(f.spawned, []);
87
+ }
88
+ });
89
+
90
+ test('five stale runs do not suppress the new revision or erase its repeat accounting', async () => {
91
+ const f = await fixture(), old = f.add(), blocker = await f.block();
92
+ const pending = Array.from({ length: 5 }, () => old.fire());
93
+ const current = f.add('1', 'new'); pending.push(...Array.from({ length: 5 }, () => current.fire()));
94
+ assert.equal(f.notices.length, 0);
95
+ await blocker.release(); await Promise.all(pending);
96
+ assert.deepEqual(f.spawned, Array(5).fill('new'));
97
+ await current.fire(); assert.equal(f.spawned.length, 6);
98
+ });