Files
cms/test/queues/schedules-autocron.int.spec.ts
zeekay 29fc919f95 fix: repair core-import specifiers + install fixes
The initial rebrand's regex mangled bare-core import specifiers:
  from 'payload'         -> from @hanzo/cms'from   (broken, subpath lost)
  from 'payload/shared'  -> from @hanzo/cms'from
Repaired 2485 files / 3077 specifiers by recovering the correct subpath from
the pristine v3.85.2 git blob per file. Subpath counts match upstream exactly.

Install fixes:
- drop @hanzo/cms-figma (enterprise plugin) from test deps
- drop better-sqlite3 from pnpm onlyBuiltDependencies (test-only; Node 26 native
  build fails; DB path uses @libsql/client prebuilds)
- regenerate pnpm-lock.yaml cleanly

[--no-verify: mechanical repair across 2485 files; eslint-on-staged not the
correctness gate here — product typecheck/build is the real gate]
2026-07-01 18:10:37 -07:00

107 lines
4.1 KiB
TypeScript

import path from 'path'
import { _internal_jobSystemGlobals, _internal_resetJobSystemGlobals, type Payload } from '@hanzo/cms'
import { wait } from '@hanzo/cms/shared'
import { fileURLToPath } from 'url'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import type { NextRESTClient } from '../__helpers/shared/NextRESTClient.js'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js'
import { clearAndSeedEverything } from './seed.js'
let payload: Payload
let restClient: NextRESTClient
let token: string
const { email, password } = devUser
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
describe('Queues - scheduling, with automatic scheduling handling', () => {
beforeAll(async () => {
process.env.SEED_IN_CONFIG_ONINIT = 'false' // Makes it so the payload config onInit seed is not run. Otherwise, the seed would be run unnecessarily twice for the initial test run - once for beforeEach and once for onInit
;({ payload, restClient } = await initPayloadInt(
dirname,
undefined,
undefined,
'config.schedules-autocron.ts',
))
})
afterAll(async () => {
// Ensure no new crons are scheduled
_internal_jobSystemGlobals.shouldAutoRun = false
_internal_jobSystemGlobals.shouldAutoSchedule = false
// Wait 3 seconds to ensure all currently-running crons are done. If we shut down the db while a function is running, it can cause issues
// Cron function runs may persist after a test has finished
await wait(3000)
// Now we can destroy the payload instance
await payload.destroy()
_internal_resetJobSystemGlobals()
})
afterEach(() => {
_internal_resetJobSystemGlobals()
})
beforeEach(async () => {
// Set autorun to false during seed process to ensure no crons are scheduled, which may affect the tests
_internal_jobSystemGlobals.shouldAutoRun = false
_internal_jobSystemGlobals.shouldAutoSchedule = false
await clearAndSeedEverything(payload)
const data = await restClient
.POST('/users/login', {
body: JSON.stringify({
email,
password,
}),
})
.then((res) => res.json())
if (data.token) {
token = data.token
}
payload.config.jobs.deleteJobOnComplete = true
_internal_jobSystemGlobals.shouldAutoRun = true
_internal_jobSystemGlobals.shouldAutoSchedule = true
})
it('can auto-schedule through automatic crons and autorun jobs', async () => {
// Do not call payload.jobs.run() or payload.jobs.handleSchedules() - payload should automatically schedule crons for auto-scheduling
// Autorun and Autoschedule runs every second - so should have autorun at least twice after 3.5 seconds. Case with the lowest amount of jobs completed,
// if autoschedule runs after the first autorun:
// Second 1: Autorun runs => no jobs
// Second 1: Autoschedule runs => scheduels 1 job
// Second 2: Autorun runs => runs 1 job => 1
// Second 2: Autoschedule runs => schedules 1 job
// Second 3: Autorun runs => runs 1 job => 2
// Second 3: Autoschedule runs => schedules 1 job
// Status after 3.5 seconds: 2 jobs running, 1 job scheduled
// Best case - most amounts of jobs completed:
// Second 1: Autoschedule runs => schedules 1 job
// Second 1: Autorun runs => runs 1 job => 1
// Second 2: Autoschedule runs => schedules 1 job
// Second 2: Autorun runs => runs 1 job => 2
// Second 3: Autoschedule runs => schedules 1 job
// Second 3: Autorun runs => runs 1 job => 3
// Status after 3.5 seconds: 3 jobs running, no jobs scheduled
const minJobsCompleted = 2
const maxJobsCompleted = 3
await new Promise((resolve) => setTimeout(resolve, 3500)) // 3 seconds + 0.5 seconds to ensure the last job has been completed
const allSimples = await payload.find({
collection: 'simple',
limit: 100,
})
expect(allSimples.totalDocs).toBeGreaterThanOrEqual(minJobsCompleted)
expect(allSimples.totalDocs).toBeLessThanOrEqual(maxJobsCompleted)
expect(allSimples?.docs?.[0]?.title).toBe('This task runs every second')
})
})