@platformatic/watt-extra 1.15.0-alpha.2 → 1.15.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.
package/plugins/update.js CHANGED
@@ -13,28 +13,12 @@ function createWebSocketUrl (httpUrl, path, queryParams = {}) {
13
13
  return url.toString()
14
14
  }
15
15
 
16
- async function sendCommandResult (connection, requestId, result) {
17
- if (connection.readyState !== WebSocket.OPEN) {
18
- throw new Error(`Cannot send command result for request "${requestId}": WebSocket is not open`)
19
- }
20
-
21
- await new Promise((resolve, reject) => {
22
- connection.send(JSON.stringify({ command: 'command-result', requestId, ...result }), err => {
23
- if (err) {
24
- reject(err)
25
- } else {
26
- resolve()
27
- }
28
- })
29
- })
30
- }
31
-
32
16
  async function updatePlugin (app) {
33
17
  const reconnectInterval = app.env.PLT_UPDATES_RECONNECT_INTERVAL_SEC * 1000
34
18
 
35
19
  let socket = null
36
20
 
37
- async function processMessage (connection, data) {
21
+ async function processMessage (data) {
38
22
  try {
39
23
  const message = JSON.parse(data)
40
24
  const { topic, type, command } = message
@@ -57,26 +41,24 @@ async function updatePlugin (app) {
57
41
  // each tick to exactly one pod of the application, this one.
58
42
  if (command === 'run-scheduled-job') {
59
43
  app.log.info({ command, job: message.params?.name }, 'Received run-scheduled-job command from ICC')
60
-
61
44
  try {
62
45
  const result = await app.runScheduledJob(message.params ?? {})
63
- if (typeof message.requestId === 'string') {
64
- await sendCommandResult(connection, message.requestId, { success: true, result })
46
+ if (message.requestId) {
47
+ socket.send(JSON.stringify({
48
+ requestId: message.requestId,
49
+ success: result?.success !== false,
50
+ result
51
+ }))
65
52
  }
66
- } catch (err) {
67
- if (typeof message.requestId !== 'string') {
68
- throw err
53
+ } catch (error) {
54
+ if (message.requestId) {
55
+ socket.send(JSON.stringify({
56
+ requestId: message.requestId,
57
+ success: false,
58
+ error: { message: error.message, name: error.name }
59
+ }))
69
60
  }
70
-
71
- await sendCommandResult(connection, message.requestId, {
72
- success: false,
73
- error: {
74
- name: err.name,
75
- message: err.message,
76
- code: err.code,
77
- statusCode: err.statusCode
78
- }
79
- })
61
+ throw error
80
62
  }
81
63
  return
82
64
  }
@@ -118,7 +100,7 @@ async function updatePlugin (app) {
118
100
  try {
119
101
  const headers = await app.getAuthorizationHeaders()
120
102
 
121
- const socket = new WebSocket(wsUrl, { headers })
103
+ socket = new WebSocket(wsUrl, { headers })
122
104
  await once(socket, 'open')
123
105
 
124
106
  app.log.info('Connected to updates websocket')
@@ -135,7 +117,7 @@ async function updatePlugin (app) {
135
117
  app.log.info('Received subscription acknowledgment from updates websocket')
136
118
 
137
119
  // listen for subsequent messages
138
- socket.on('message', data => processMessage(socket, data))
120
+ socket.on('message', processMessage)
139
121
 
140
122
  socket.on('error', (err) => {
141
123
  app.log.error(err, 'Error in updates websocket connection')
@@ -799,8 +799,8 @@ test('should attach one flamegraph to multiple alerts', async (t) => {
799
799
  await icc.close()
800
800
  })
801
801
 
802
- // Wait for the first flamegraph to be generated
803
- await sleep(5000)
802
+ // Wait for the first profiling window (3s grace period + 5s interval) to complete
803
+ await sleep(8500)
804
804
 
805
805
  // Manually trigger health event with unhealthy state
806
806
  const healthInfo = {
@@ -902,8 +902,8 @@ test('should send flamegraphs if attaching fails', async (t) => {
902
902
  await icc.close()
903
903
  })
904
904
 
905
- // Wait for the first flamegraph to be generated
906
- await sleep(5000)
905
+ // Wait for the first profiling window (3s grace period + 5s interval) to complete
906
+ await sleep(8500)
907
907
 
908
908
  // Manually trigger health event with unhealthy state
909
909
  const healthInfo = {
package/test/cli.test.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import assert from 'node:assert'
2
2
  import { test } from 'node:test'
3
3
  import { spawn } from 'node:child_process'
4
- import { join, dirname } from 'node:path'
4
+ import { join, resolve, dirname } from 'node:path'
5
5
  import { fileURLToPath } from 'node:url'
6
+ import { applyStartArgs } from '../lib/start-args.js'
6
7
 
7
8
  const __filename = fileURLToPath(import.meta.url)
8
9
  const __dirname = dirname(__filename)
@@ -90,3 +91,51 @@ test('CLI should output JSON log without banner in non-TTY mode (version command
90
91
  'Non-TTY output should include JSON log with version'
91
92
  )
92
93
  })
94
+
95
+ test('applyStartArgs: --log-level flag overrides env-provided PLT_LOG_LEVEL', () => {
96
+ const env = { PLT_LOG_LEVEL: 'warn' }
97
+ applyStartArgs(['--log-level', 'debug'], env)
98
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'debug')
99
+ })
100
+
101
+ test('applyStartArgs: -l short flag also overrides env', () => {
102
+ const env = { PLT_LOG_LEVEL: 'warn' }
103
+ applyStartArgs(['-l', 'trace'], env)
104
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'trace')
105
+ })
106
+
107
+ // Regression test: cli.js used to have `default: { 'log-level': 'info' }` in its
108
+ // minimist config, which populated args['log-level'] with 'info' whenever the flag
109
+ // was not passed. The subsequent unconditional write to process.env.PLT_LOG_LEVEL
110
+ // then silently clobbered the value provided by the deployment environment (e.g.
111
+ // PLT_LOG_LEVEL=warn from k8s), forcing the runtime to log at info regardless.
112
+ test('applyStartArgs: preserves env-provided PLT_LOG_LEVEL when --log-level is not passed', () => {
113
+ const env = { PLT_LOG_LEVEL: 'warn' }
114
+ applyStartArgs([], env)
115
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'warn')
116
+ })
117
+
118
+ test('applyStartArgs: defaults PLT_LOG_LEVEL to "info" when neither env nor flag is set', () => {
119
+ const env = {}
120
+ applyStartArgs([], env)
121
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'info')
122
+ })
123
+
124
+ test('applyStartArgs: passes through other CLI options to env', () => {
125
+ const env = {}
126
+ applyStartArgs([
127
+ '--icc-url', 'http://icc.example',
128
+ '--app-name', 'my-app',
129
+ '--app-dir', 'test/fixtures/runtime-service'
130
+ ], env)
131
+ assert.strictEqual(env.PLT_ICC_URL, 'http://icc.example')
132
+ assert.strictEqual(env.PLT_APP_NAME, 'my-app')
133
+ assert.strictEqual(env.PLT_APP_DIR, resolve('test/fixtures/runtime-service'))
134
+ })
135
+
136
+ test('applyStartArgs: --help short-circuits without mutating env', () => {
137
+ const env = {}
138
+ const res = applyStartArgs(['--help'], env)
139
+ assert.strictEqual(res.help, true)
140
+ assert.strictEqual(env.PLT_LOG_LEVEL, undefined)
141
+ })
package/test/init.test.js CHANGED
@@ -617,3 +617,93 @@ test('updateInstanceConfig preserves existing applicationLabel when ICC does not
617
617
  equal(receivedMetricsConfig.applicationLabel, 'serviceId')
618
618
  equal(receivedMetricsConfig.labels.applicationId, applicationId)
619
619
  })
620
+
621
+ test('init plugin reports the baked deployment id when PLT_DEPLOYMENT_ID is set', async (t) => {
622
+ const applicationName = 'test-app-baked'
623
+ const applicationId = randomUUID()
624
+
625
+ let capturedRequest = null
626
+
627
+ const icc = await startICC(t, {
628
+ applicationId,
629
+ controlPlaneResponse: (req) => {
630
+ capturedRequest = { body: req.body }
631
+ return {
632
+ applicationId,
633
+ applicationName,
634
+ iccServices: {
635
+ riskEngine: { url: 'http://127.0.0.1:3000/risk-service' },
636
+ trafficInspector: { url: 'http://127.0.0.1:3000/traffic-inspector' },
637
+ compliance: { url: 'http://127.0.0.1:3000/compliance' },
638
+ cron: { url: 'http://127.0.0.1:3000/cron' },
639
+ scaler: { url: 'http://127.0.0.1:3000/scaler' }
640
+ },
641
+ config: {}
642
+ }
643
+ }
644
+ })
645
+
646
+ const previous = process.env.PLT_DEPLOYMENT_ID
647
+ process.env.PLT_DEPLOYMENT_ID = 'a1b2c3d4e5f6'
648
+
649
+ t.after(async () => {
650
+ if (previous === undefined) delete process.env.PLT_DEPLOYMENT_ID
651
+ else process.env.PLT_DEPLOYMENT_ID = previous
652
+ await icc.close()
653
+ })
654
+
655
+ const app = createMockApp({
656
+ PLT_APP_NAME: applicationName,
657
+ PLT_APP_DIR: '/test/dir'
658
+ })
659
+
660
+ await initPlugin(app)
661
+
662
+ // The value the image was built with, so ICC can tell whether this version's
663
+ // assets carry `?dpl=<its own version>` and may be query-pinned.
664
+ equal(capturedRequest.body.buildDeploymentId, 'a1b2c3d4e5f6')
665
+ })
666
+
667
+ test('init plugin omits the deployment id when PLT_DEPLOYMENT_ID is unset', async (t) => {
668
+ const applicationName = 'test-app-unbaked'
669
+ const applicationId = randomUUID()
670
+
671
+ let capturedRequest = null
672
+
673
+ const icc = await startICC(t, {
674
+ applicationId,
675
+ controlPlaneResponse: (req) => {
676
+ capturedRequest = { body: req.body }
677
+ return {
678
+ applicationId,
679
+ applicationName,
680
+ iccServices: {
681
+ riskEngine: { url: 'http://127.0.0.1:3000/risk-service' },
682
+ trafficInspector: { url: 'http://127.0.0.1:3000/traffic-inspector' },
683
+ compliance: { url: 'http://127.0.0.1:3000/compliance' },
684
+ cron: { url: 'http://127.0.0.1:3000/cron' },
685
+ scaler: { url: 'http://127.0.0.1:3000/scaler' }
686
+ },
687
+ config: {}
688
+ }
689
+ }
690
+ })
691
+
692
+ const previous = process.env.PLT_DEPLOYMENT_ID
693
+ delete process.env.PLT_DEPLOYMENT_ID
694
+
695
+ t.after(async () => {
696
+ if (previous !== undefined) process.env.PLT_DEPLOYMENT_ID = previous
697
+ await icc.close()
698
+ })
699
+
700
+ const app = createMockApp({
701
+ PLT_APP_NAME: applicationName,
702
+ PLT_APP_DIR: '/test/dir'
703
+ })
704
+
705
+ await initPlugin(app)
706
+
707
+ // A build that was never given an id must not claim one.
708
+ equal(capturedRequest.body.buildDeploymentId, undefined)
709
+ })
@@ -4,7 +4,6 @@ import { once } from 'node:events'
4
4
  import { createServer } from 'node:http'
5
5
  import { join, dirname } from 'node:path'
6
6
  import { test } from 'node:test'
7
- import { setImmediate as immediate } from 'node:timers/promises'
8
7
  import { fileURLToPath } from 'node:url'
9
8
  import WebSocket from 'ws'
10
9
  import { start } from '../index.js'
@@ -35,7 +34,7 @@ function controlPlaneResponseWithMode (applicationId, applicationName, mode) {
35
34
  })
36
35
  }
37
36
 
38
- test('external mode: should disable local jobs, skip legacy registration and report the jobs in the state', async (t) => {
37
+ test('ICC coordination: should disable local jobs and register config jobs directly', async (t) => {
39
38
  const applicationName = 'test-app'
40
39
  const applicationId = randomUUID()
41
40
  const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
@@ -78,21 +77,23 @@ test('external mode: should disable local jobs, skip legacy registration and rep
78
77
  assert.strictEqual(config.scheduler[0].enabled, false)
79
78
  }
80
79
 
81
- // The legacy per-job registration is skipped: the control-plane
82
- // registers the jobs centrally from the state report
83
- assert.strictEqual(savedWattJob, null)
80
+ assert.deepStrictEqual(savedWattJob, {
81
+ name: 'test',
82
+ schedule: '*/5 * * * *',
83
+ method: 'GET',
84
+ maxRetries: 3,
85
+ applicationId,
86
+ callbackUrl: 'http://localhost:3000'
87
+ })
84
88
 
85
89
  // The jobs are reported in the application state
86
90
  assert.ok(savedState, 'the state should have been reported')
87
- assert.ok(Array.isArray(savedState.scheduler), 'the state should include the scheduler jobs')
88
- assert.strictEqual(savedState.scheduler.length, 1)
91
+ assert.ok(Array.isArray(savedState.scheduler))
89
92
  assert.strictEqual(savedState.scheduler[0].name, 'test')
90
- assert.strictEqual(savedState.scheduler[0].cron, '*/5 * * * *')
91
- assert.strictEqual(savedState.scheduler[0].callbackUrl, 'http://localhost:3000')
92
93
  assert.strictEqual(savedState.scheduler[0].source, 'config')
93
94
  })
94
95
 
95
- test('local mode: should keep the local jobs running and skip any registration', async (t) => {
96
+ test('ICC coordination: should disable local jobs regardless of scheduler mode', async (t) => {
96
97
  const applicationName = 'test-app'
97
98
  const applicationId = randomUUID()
98
99
  const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
@@ -121,15 +122,11 @@ test('local mode: should keep the local jobs running and skip any registration',
121
122
  await icc.close()
122
123
  })
123
124
 
124
- // ICC coordination is disabled: the local scheduler stays enabled
125
+ // ICC coordination always disables local execution.
125
126
  const config = await app.watt.runtime.getRuntimeConfig()
126
- assert.notStrictEqual(config.scheduler[0].enabled, false)
127
- if (typeof app.watt.runtime.getScheduler === 'function') {
128
- assert.strictEqual(app.watt.runtime.getScheduler()[0].paused, false)
129
- }
127
+ assert.strictEqual(config.scheduler[0].enabled, false)
130
128
 
131
- // No registration happens: the jobs run locally in each pod
132
- assert.strictEqual(savedWattJob, null)
129
+ assert.ok(savedWattJob)
133
130
  })
134
131
 
135
132
  function createMockApp (port) {
@@ -153,8 +150,8 @@ function createMockApp (port) {
153
150
  }
154
151
  }
155
152
 
156
- test('runtime scheduler ownership follows the ICC mode', async () => {
157
- const instanceConfig = { scheduler: { mode: 'external' } }
153
+ test('runtime scheduler ownership always pauses jobs when ICC is configured', async () => {
154
+ const instanceConfig = {}
158
155
  const jobs = [
159
156
  { name: 'configured', paused: false },
160
157
  { name: 'frontend:0', paused: false }
@@ -164,7 +161,7 @@ test('runtime scheduler ownership follows the ICC mode', async () => {
164
161
  PLT_APP_DIR: join(__dirname, 'fixtures', 'runtime-scheduler'),
165
162
  PLT_ICC_URL: 'http://localhost:3000'
166
163
  },
167
- log: { info: () => {} },
164
+ log: { info: () => {}, debug: () => {}, warn: () => {} },
168
165
  instanceConfig,
169
166
  instanceId: 'test-pod-123'
170
167
  })
@@ -182,15 +179,42 @@ test('runtime scheduler ownership follows the ICC mode', async () => {
182
179
  await watt.applySchedulerMode()
183
180
  assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
184
181
 
185
- instanceConfig.scheduler.mode = 'local'
186
182
  await watt.applySchedulerMode()
187
- assert.deepStrictEqual(jobs.map(job => job.paused), [false, false])
183
+ assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
184
+ })
185
+
186
+ test('runtime scheduler ownership continues when pausing one job fails', async () => {
187
+ const jobs = [
188
+ { name: 'failed', paused: false },
189
+ { name: 'paused', paused: false }
190
+ ]
191
+ const watt = new Watt({
192
+ env: {
193
+ PLT_APP_DIR: join(__dirname, 'fixtures', 'runtime-scheduler'),
194
+ PLT_ICC_URL: 'http://localhost:3000'
195
+ },
196
+ log: { info: () => {}, debug: () => {}, warn: () => {} },
197
+ instanceConfig: {},
198
+ instanceId: 'test-pod-123'
199
+ })
200
+
201
+ watt.runtime = {
202
+ getScheduler: () => jobs,
203
+ pauseSchedulerJob: async name => {
204
+ if (name === 'failed') {
205
+ throw new Error('pause failed')
206
+ }
207
+ jobs[1].paused = true
208
+ },
209
+ resumeSchedulerJob: async () => {}
210
+ }
211
+
212
+ await watt.applySchedulerMode()
213
+ assert.deepStrictEqual(jobs.map(job => job.paused), [false, true])
188
214
  })
189
215
 
190
216
  function setupMockIccServer (wss) {
191
217
  let ws = null
192
- const clientMessages = []
193
- const messageWaiters = []
194
218
 
195
219
  const waitForClientSubscription = once(wss, 'connection').then(([socket]) => {
196
220
  ws = socket
@@ -202,29 +226,12 @@ function setupMockIccServer (wss) {
202
226
  if (message.command === 'subscribe' && message.topic === '/config') {
203
227
  socket.send(JSON.stringify({ command: 'ack' }))
204
228
  resolve()
205
- return
206
- }
207
-
208
- const waiter = messageWaiters.shift()
209
- if (waiter) {
210
- waiter(message)
211
- } else {
212
- clientMessages.push(message)
213
229
  }
214
230
  })
215
231
  })
216
232
  })
217
233
 
218
- function waitForClientMessage () {
219
- const message = clientMessages.shift()
220
- if (message) {
221
- return Promise.resolve(message)
222
- }
223
-
224
- return new Promise(resolve => messageWaiters.push(resolve))
225
- }
226
-
227
- return { waitForClientSubscription, waitForClientMessage, getClientMessages: () => clientMessages, getWs: () => ws }
234
+ return { waitForClientSubscription, getWs: () => ws }
228
235
  }
229
236
 
230
237
  test('run-scheduled-job: should execute the job callback when triggered over the websocket', async (t) => {
@@ -234,7 +241,7 @@ test('run-scheduled-job: should execute the job callback when triggered over the
234
241
  const wss = new WebSocket.Server({ port })
235
242
  t.after(() => wss.close())
236
243
 
237
- const { waitForClientSubscription, waitForClientMessage, getWs } = setupMockIccServer(wss)
244
+ const { waitForClientSubscription, getWs } = setupMockIccServer(wss)
238
245
 
239
246
  // The target the job callback should hit
240
247
  const receivedRequests = []
@@ -257,7 +264,7 @@ test('run-scheduled-job: should execute the job callback when triggered over the
257
264
 
258
265
  getWs().send(JSON.stringify({
259
266
  command: 'run-scheduled-job',
260
- requestId: 'request-1',
267
+ requestId: 'job-request-1',
261
268
  params: {
262
269
  name: 'cleanup',
263
270
  callbackUrl: `http://127.0.0.1:${target.address().port}/cleanup`,
@@ -267,86 +274,15 @@ test('run-scheduled-job: should execute the job callback when triggered over the
267
274
  }))
268
275
 
269
276
  await requestReceived
270
- const completion = await waitForClientMessage()
271
277
 
272
- assert.deepStrictEqual(receivedRequests, [{ method: 'POST', url: '/cleanup' }])
273
- assert.deepStrictEqual(completion, {
274
- command: 'command-result',
275
- requestId: 'request-1',
278
+ const response = JSON.parse((await once(getWs(), 'message'))[0].toString())
279
+ assert.deepStrictEqual(response, {
280
+ requestId: 'job-request-1',
276
281
  success: true,
277
282
  result: { name: 'cleanup', statusCode: 200 }
278
283
  })
279
- })
280
-
281
- test('run-scheduled-job: should report runtime failures over the websocket', async (t) => {
282
- setUpEnvironment()
283
-
284
- const wss = new WebSocket.Server({ port: 0 })
285
- await once(wss, 'listening')
286
- t.after(() => wss.close())
287
-
288
- const { waitForClientSubscription, waitForClientMessage, getWs } = setupMockIccServer(wss)
289
- const app = createMockApp(wss.address().port)
290
- const error = new Error('Task execution failed')
291
- error.code = 'PLT_TASK_FAILED'
292
- error.statusCode = 500
293
- app.watt.runtime.runSchedulerJob = async () => { throw error }
294
-
295
- await schedulerPlugin(app)
296
- await updatePlugin(app)
297
- await app.connectToUpdates()
298
- t.after(() => app.closeUpdates())
299
- await waitForClientSubscription
300
-
301
- getWs().send(JSON.stringify({
302
- command: 'run-scheduled-job',
303
- requestId: 'request-2',
304
- params: { name: 'frontend:0', source: 'application' }
305
- }))
306
-
307
- assert.deepStrictEqual(await waitForClientMessage(), {
308
- command: 'command-result',
309
- requestId: 'request-2',
310
- success: false,
311
- error: {
312
- name: 'Error',
313
- message: 'Task execution failed',
314
- code: 'PLT_TASK_FAILED',
315
- statusCode: 500
316
- }
317
- })
318
- })
319
284
 
320
- test('run-scheduled-job: should not report completion without a request ID', async (t) => {
321
- setUpEnvironment()
322
-
323
- const wss = new WebSocket.Server({ port: 0 })
324
- await once(wss, 'listening')
325
- t.after(() => wss.close())
326
-
327
- const { waitForClientSubscription, getClientMessages, getWs } = setupMockIccServer(wss)
328
- const app = createMockApp(wss.address().port)
329
- let executedResolve
330
- const executed = new Promise(resolve => { executedResolve = resolve })
331
- app.watt.runtime.runSchedulerJob = async () => {
332
- executedResolve()
333
- return { success: true }
334
- }
335
-
336
- await schedulerPlugin(app)
337
- await updatePlugin(app)
338
- await app.connectToUpdates()
339
- t.after(() => app.closeUpdates())
340
- await waitForClientSubscription
341
-
342
- getWs().send(JSON.stringify({
343
- command: 'run-scheduled-job',
344
- params: { name: 'frontend:0', source: 'application' }
345
- }))
346
-
347
- await executed
348
- await immediate()
349
- assert.deepStrictEqual(getClientMessages(), [])
285
+ assert.deepStrictEqual(receivedRequests, [{ method: 'POST', url: '/cleanup' }])
350
286
  })
351
287
 
352
288
  test('run-scheduled-job: should route legacy internal callbacks through the runtime', async (t) => {
@@ -376,7 +312,7 @@ test('run-scheduled-job: should route legacy internal callbacks through the runt
376
312
  assert.strictEqual(injected[0].body, '{"scheduledTime":1}')
377
313
  })
378
314
 
379
- test('run-scheduled-job: should use native runtime execution for application jobs', async (t) => {
315
+ test('run-scheduled-job: should use native runtime execution for application tasks without callbacks', async (t) => {
380
316
  const app = createMockApp(0)
381
317
 
382
318
  const executed = []
@@ -393,6 +329,17 @@ test('run-scheduled-job: should use native runtime execution for application job
393
329
  assert.deepStrictEqual(result, { name: 'frontend:0', success: true })
394
330
  })
395
331
 
332
+ test('run-scheduled-job: should reject application tasks without runtime support', async (t) => {
333
+ const app = createMockApp(0)
334
+
335
+ await schedulerPlugin(app)
336
+
337
+ await assert.rejects(
338
+ app.runScheduledJob({ name: 'frontend:0', source: 'application' }),
339
+ /runtime scheduler controls are unavailable/
340
+ )
341
+ })
342
+
396
343
  test('run-scheduled-job: should fail on non-2xx callback responses', async (t) => {
397
344
  const app = createMockApp(0)
398
345
 
@@ -446,8 +393,63 @@ test('collectSchedulerJobs: should use the runtime scheduler status when availab
446
393
  assert.deepStrictEqual(jobs[1].tasks, ['db:cleanup'])
447
394
  })
448
395
 
449
- test('external mode: should apply runtime scheduler ownership', async (t) => {
396
+ test('sendSchedulerInfo: should register config jobs before skipping application jobs on old ICC', async () => {
397
+ const app = createMockApp(0)
398
+ app.instanceConfig.iccServices = { cron: { url: 'http://cron.local' } }
399
+ app.watt.runtime.getScheduler = async () => ([
400
+ {
401
+ name: 'config-job',
402
+ cron: '*/5 * * * *',
403
+ callbackUrl: 'http://service.plt.local/run',
404
+ source: 'config'
405
+ },
406
+ {
407
+ name: 'application:0',
408
+ cron: '* * * * *',
409
+ source: 'application',
410
+ applicationId: 'frontend',
411
+ scheduleId: '0',
412
+ tasks: ['cleanup']
413
+ },
414
+ {
415
+ name: 'application:1',
416
+ cron: '0 * * * *',
417
+ source: 'application',
418
+ applicationId: 'frontend',
419
+ scheduleId: '1',
420
+ tasks: ['sync']
421
+ }
422
+ ])
423
+
424
+ const requests = []
425
+ const originalFetch = globalThis.fetch
426
+ globalThis.fetch = async (_url, options) => {
427
+ requests.push(JSON.parse(options.body))
428
+ const status = requests.length === 2 ? 400 : 200
429
+ return {
430
+ status,
431
+ headers: new Headers({ 'content-type': 'application/json' }),
432
+ text: async () => '',
433
+ json: async () => ({ error: 'unsupported' })
434
+ }
435
+ }
436
+
437
+ try {
438
+ await schedulerPlugin(app)
439
+ await app.sendSchedulerInfo()
440
+ } finally {
441
+ globalThis.fetch = originalFetch
442
+ }
443
+
444
+ assert.strictEqual(requests.length, 2)
445
+ assert.strictEqual(requests[0].callbackUrl, 'http://service.plt.local/run')
446
+ assert.strictEqual(requests[1].callbackUrl, undefined)
447
+ assert.deepStrictEqual(requests[1].tasks, ['cleanup'])
448
+ })
449
+
450
+ test('without ICC: should not apply runtime scheduler ownership', async (t) => {
450
451
  const app = createMockApp(0)
452
+ app.env.PLT_ICC_URL = undefined
451
453
 
452
454
  let applications = 0
453
455
  app.watt.applySchedulerMode = async () => {
@@ -457,5 +459,5 @@ test('external mode: should apply runtime scheduler ownership', async (t) => {
457
459
  await schedulerPlugin(app)
458
460
  await app.sendSchedulerInfo()
459
461
 
460
- assert.strictEqual(applications, 1)
462
+ assert.strictEqual(applications, 0)
461
463
  })