Files
websitedev/proto/scripts/test-payload-local-api.ts
T
Do SikiandClaude Sonnet 5 e2b0c62628 test(cms): Payload config + Local API tests, fix stale Docker tests (MITHOME-96)
Adds the two test categories the ticket asked for, replacing the old
content-JSON-schema-only testing story now that Payload is the real
source of truth:

1. src/payload-config.test.ts — fast, DB-free unit tests over the
   Collection/Global config objects themselves. WHY not importing
   payload.config.ts directly: it pulls in the real `payload` and
   `@payloadcms/db-mongodb` packages as values (not just types), which
   are ESM-only and Jest's default transformIgnorePatterns skips
   node_modules entirely — confirmed by trying it
   (`SyntaxError: Cannot use import statement outside a module` from
   payload's own dist). Individual collection/global files only ever
   `import type` from 'payload' (erased at compile time), so they're
   safely importable in isolation. Two of the assertions are
   deliberate regression guards for real bugs found earlier this
   session (MITHOME-121): Media.access.read must stay public, and
   Partners.logo must stay optional.

2. scripts/test-payload-local-api.ts (npm run test:payload) — exercises
   every src/lib/payload-content.ts getter plus a full
   create/findByID/delete cycle against a real, already-migrated
   MongoDB. WHY a plain node script instead of a Jest integration
   config: same ESM problem as above, and Payload's dependency graph
   is too broad to safely add to transformIgnorePatterns — this
   follows the same working `node --import tsx` pattern already used
   by migrate-content-to-payload.ts. Not wired into
   pre-deploy-tests.sh (needs a live, pre-migrated MongoDB, same
   reasoning as the removed CMS integration tests); run manually or
   before a release.

3. Fixed the pre-existing (Docker-stack-gated, so silently never
   caught) staleness in integration.test.ts and e2e-docker.test.ts:
   unprefixed routes (/rolunk) → locale-prefixed (/hu/rolunk), and
   Mongo assertions against the old site_config/contact_submissions
   collections → the real globals/legal-pages/partners/
   contact-submissions collections Payload actually uses. Also fixed
   an unrelated stale error-message assertion for spam detection that
   never matched the route's real string.

Verified: full gate green (tsc, lint, 63 unit tests — 13 new — build),
and separately ran test:payload live against the dev MongoDB (8/8
passed, including the write/read/delete cycle actually hitting Mongo).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-12 16:37:57 +02:00

135 lines
4.8 KiB
TypeScript

/**
* MITHOME-96 — Payload Local API integrációs teszt.
*
* Ez a "Local API integrációs tesztek" rész a ticketből: valódi, futó
* MongoDB-vel ellenőrzi, hogy a src/lib/payload-content.ts adapter-réteg
* ténylegesen jó alakú adatot ad vissza a Globals/Collections-ökből — nem
* mock-olt Payload-dal, hanem éles Local API hívásokkal.
*
* WHY sima node script és nem Jest teszt: a Payload csomag (és több
* függősége, pl. @payloadcms/richtext-lexical) ESM-only dist-et ad ki —
* Jest (a next/jest SWC transformjával is) alapból nem transzformálja a
* node_modules-t, így `import { getPayload } from 'payload'` egy Jest
* tesztben `SyntaxError: Cannot use import statement outside a module`-lel
* bukik (kipróbálva). A `transformIgnorePatterns` kiterjesztése a teljes
* Payload-függőségi fára törékeny és karbantartás-igényes lenne — ehelyett
* ugyanazt a bevált mintát követjük, mint a migrate-content-to-payload.ts /
* export-content-snapshot.ts scriptek: `node --import tsx`, valódi Node
* ESM-mel, semmilyen Jest-transzform nem kell.
*
* Futtatás (proto/ mappából, futó MongoDB-vel, migrált tartalommal):
* MONGODB_URI="mongodb://admin:password123@localhost:27018/mozdit?authSource=admin" \
* npm run test:payload
*/
import assert from 'assert/strict'
import { getPayload } from 'payload'
import config from '../src/payload.config'
import {
getCommonContent,
getHomeContent,
getAboutContent,
getServicesContent,
getContactContent,
getLegalPage,
getPartners,
} from '../src/lib/payload-content'
let passed = 0
async function test(name: string, fn: () => Promise<void>) {
try {
await fn()
passed++
console.log(`✅ ${name}`)
} catch (error) {
console.error(`❌ ${name}`)
throw error
}
}
async function run() {
const payload = await getPayload({ config })
await test('getCommonContent visszaad hu és en tartalmat', async () => {
const hu = await getCommonContent('hu')
const en = await getCommonContent('en')
assert.ok(hu.buttons.webmail, 'hiányzó hu webmail gomb szöveg')
assert.ok(en.buttons.webmail, 'hiányzó en webmail gomb szöveg')
})
await test('getHomeContent unwrap-eli a trustBullets és services.items.features tömböket', async () => {
const home = await getHomeContent('hu')
assert.ok(Array.isArray(home.hero.trustBullets))
assert.ok(home.hero.trustBullets.every((v) => typeof v === 'string'))
for (const item of home.services.items) {
assert.ok(Array.isArray(item.features))
assert.ok(item.features.every((v) => typeof v === 'string'))
}
})
await test('getAboutContent unwrap-eli a story és team paragraphs tömböket', async () => {
const about = await getAboutContent('hu')
assert.ok(Array.isArray(about.story.paragraphs))
assert.ok(Array.isArray(about.team.paragraphs))
})
await test('getServicesContent unwrap-eli a specs.items tömböket', async () => {
const services = await getServicesContent('hu')
for (const service of services.details.services) {
assert.ok(Array.isArray(service.specs.items))
}
})
await test('getContactContent visszaadja a form mezőket', async () => {
const contact = await getContactContent('hu')
assert.ok(contact.form.fields.email.label)
})
await test('getLegalPage megtalálja mindkét jogi oldalt', async () => {
const privacy = await getLegalPage('adatvedelem', 'hu')
const terms = await getLegalPage('hasznalati-feltetelek', 'hu')
assert.equal(privacy?.slug, 'adatvedelem')
assert.equal(terms?.slug, 'hasznalati-feltetelek')
})
await test('getPartners csak logóval rendelkező partnereket ad vissza', async () => {
const partners = await getPartners()
for (const partner of partners) {
assert.ok(partner.logo.url)
assert.ok(partner.name)
}
})
await test('create/findByID/delete ciklus működik (contact-submissions)', async () => {
const created = await payload.create({
collection: 'contact-submissions',
data: {
name: 'Local API Test',
email: 'local-api-test@example.com',
subject: 'MITHOME-96 Local API teszt',
message: 'Ez a rekord a test-payload-local-api.ts futása során jön létre és törlődik.',
gdprConsent: true,
status: 'new',
},
})
assert.ok(created.id)
const found = await payload.findByID({ collection: 'contact-submissions', id: created.id })
assert.equal(found.email, 'local-api-test@example.com')
await payload.delete({ collection: 'contact-submissions', id: created.id })
await assert.rejects(
payload.findByID({ collection: 'contact-submissions', id: created.id })
)
})
console.log(`\n${passed}/${passed} Payload Local API teszt zöld.`)
process.exit(0)
}
run().catch((error) => {
console.error('\nPayload Local API teszt sikertelen:', error)
process.exit(1)
})